Java Code Examples for org.opencv.imgproc.Imgproc#Canny
The following examples show how to use
org.opencv.imgproc.Imgproc#Canny .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java From MOAAP with MIT License | 8 votes |
void Contours() { Mat grayMat = new Mat(); Mat cannyEdges = new Mat(); Mat hierarchy = new Mat(); List<MatOfPoint> contourList = new ArrayList<MatOfPoint>(); //A list to store all the contours //Converting the image to grayscale Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY); Imgproc.Canny(originalMat, cannyEdges, 10, 100); //finding contours Imgproc.findContours(cannyEdges, contourList, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); //Drawing contours on a new image Mat contours = new Mat(); contours.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC3); Random r = new Random(); for (int i = 0; i < contourList.size(); i++) { Imgproc.drawContours(contours, contourList, i, new Scalar(r.nextInt(255), r.nextInt(255), r.nextInt(255)), -1); } //Converting Mat back to Bitmap Utils.matToBitmap(contours, currentBitmap); imageView.setImageBitmap(currentBitmap); }
Example 2
Source File: Masking.java From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 | 7 votes |
public PreProcessor preprocessImage(PreProcessor preProcessor) { List<Mat> images = preProcessor.getImages(); List<Mat> processed = new ArrayList<Mat>(); for (Mat img : images){ preProcessor.normalize0255(img); /*************************************************************************************** * Title: Automatic calculation of low and high thresholds for the Canny operation in opencv * Author: VP * Date: 16.04.2013 * Code version: - * Availability: http://stackoverflow.com * ***************************************************************************************/ double otsu_thresh_val = Imgproc.threshold(img, img, 0, 255, Imgproc.THRESH_OTSU); Imgproc.Canny(img, img, otsu_thresh_val * 0.5, otsu_thresh_val); processed.add(img); } preProcessor.setImages(processed); return preProcessor; }
Example 3
Source File: ProcessHelper.java From OpenCV-android with Apache License 2.0 | 6 votes |
/** * 边缘检测 * * @param origin 原始bitmap * @param callback 回调 */ public void canny(Bitmap origin, ProcessCallback callback) { if (origin == null) { return; } try { Bitmap bitmap = Bitmap.createBitmap(origin.getWidth(), origin.getHeight(), Bitmap.Config.RGB_565); Utils.bitmapToMat(origin, rgbMat); Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY); Mat edges = new Mat(); // 阈值极限 Imgproc.Canny(grayMat, edges, 50, 300); Utils.matToBitmap(edges, bitmap); callback.onSuccess(bitmap); } catch (Exception e) { callback.onFailed(e.getMessage()); } }
Example 4
Source File: Finder.java From SikuliNG with MIT License | 6 votes |
public static Mat detectEdges(Mat mSource) { Mat mSourceGray = Element.getNewMat(); Mat mDetectedEdges = Element.getNewMat(); int edgeThresh = 1; int lowThreshold = 100; int ratio = 3; int kernelSize = 5; int blurFilterSize = 3; if (mSource.channels() == 1) { mSourceGray = mSource; } else { Imgproc.cvtColor(mSource, mSourceGray, toGray); } Imgproc.blur(mSourceGray, mDetectedEdges, new Size(blurFilterSize, blurFilterSize)); Imgproc.Canny(mDetectedEdges, mDetectedEdges, lowThreshold, lowThreshold * ratio, kernelSize, false); return mDetectedEdges; }
Example 5
Source File: MainActivity.java From open-quartz with Apache License 2.0 | 6 votes |
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba(); mGray = inputFrame.gray(); switch (MainActivity.viewMode) { case MainActivity.VIEW_MODE_RGBA: return mRgba; case MainActivity.VIEW_MODE_HIST: return mRgba; case MainActivity.VIEW_MODE_CANNY: Imgproc.Canny(mGray, mIntermediateMat, 80, 100); Imgproc.cvtColor(mIntermediateMat, mGray, Imgproc.COLOR_GRAY2BGRA, 4); return mGray; case MainActivity.VIEW_MODE_SOBEL: Imgproc.Sobel(mGray, mGray, CvType.CV_8U, 1, 1); // Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0); Imgproc.cvtColor(mGray, mGray, Imgproc.COLOR_GRAY2BGRA, 4); return mGray; case MainActivity.VIEW_MODE_PIXELIZE: Imgproc.resize(mGray, mIntermediateMat, mSize0, 0.1, 0.1, Imgproc.INTER_NEAREST); Imgproc.resize(mIntermediateMat, mRgba, mRgba.size(), 0.0, 0.0, Imgproc.INTER_NEAREST); return mRgba; case MainActivity.VIEW_MODE_GRAY: return mGray; case MainActivity.VIEW_MODE_FEATURES: FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr()); return mRgba; default: return mRgba; } }
Example 6
Source File: Canny.java From opencv-fun with GNU Affero General Public License v3.0 | 5 votes |
public static void loop (Mat img, ImgWindow wnd) { // generate gray scale and blur Mat gray = new Mat(); Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY); // Imgproc.blur(gray, gray, new Size(3, 3)); // detect the edges Mat edges = new Mat(); int lowThreshold = 60; int ratio = 3; Imgproc.Canny(img, edges, lowThreshold, lowThreshold * ratio); wnd.setImage(edges); }
Example 7
Source File: HoughLines.java From opencv-fun with GNU Affero General Public License v3.0 | 5 votes |
public static void main (String[] args) { CVLoader.load(); // load the image Mat img = Highgui.imread("data/topdown-6.jpg"); // generate gray scale and blur Mat gray = new Mat(); Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY); Imgproc.blur(gray, gray, new Size(3, 3)); // detect the edges Mat edges = new Mat(); int lowThreshold = 50; int ratio = 3; Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio); Mat lines = new Mat(); Imgproc.HoughLinesP(edges, lines, 1, Math.PI / 180, 50, 50, 10); for(int i = 0; i < lines.cols(); i++) { double[] val = lines.get(0, i); Core.line(img, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255), 2); } ImgWindow.newWindow(edges); ImgWindow.newWindow(gray); ImgWindow.newWindow(img); }
Example 8
Source File: Tutorial2Activity.java From OpenCV-AndroidSamples with MIT License | 5 votes |
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { final int viewMode = mViewMode; switch (viewMode) { case VIEW_MODE_GRAY: // input frame has gray scale format Imgproc.cvtColor(inputFrame.gray(), mRgba, Imgproc.COLOR_GRAY2RGBA, 4); break; case VIEW_MODE_RGBA: // input frame has RBGA format mRgba = inputFrame.rgba(); break; case VIEW_MODE_CANNY: // input frame has gray scale format mRgba = inputFrame.rgba(); Imgproc.Canny(inputFrame.gray(), mIntermediateMat, 80, 100); Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4); break; case VIEW_MODE_FEATURES: // input frame has RGBA format mRgba = inputFrame.rgba(); mGray = inputFrame.gray(); FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr()); break; } return mRgba; }
Example 9
Source File: MainActivity.java From SimpleDocumentScanner-Android with MIT License | 5 votes |
/** * Detect the edges in the given Mat * @param src A valid Mat object * @return A Mat processed to find edges */ private Mat edgeDetection(Mat src) { Mat edges = new Mat(); Imgproc.cvtColor(src, edges, Imgproc.COLOR_BGR2GRAY); Imgproc.GaussianBlur(edges, edges, new Size(5, 5), 0); Imgproc.Canny(edges, edges, 75, 200); return edges; }
Example 10
Source File: MainActivity.java From MOAAP with MIT License | 5 votes |
void HoughCircles() { Mat grayMat = new Mat(); Mat cannyEdges = new Mat(); Mat circles = new Mat(); //Converting the image to grayscale Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY); Imgproc.Canny(grayMat, cannyEdges, 10, 100); Imgproc.HoughCircles(cannyEdges, circles, Imgproc.CV_HOUGH_GRADIENT, 1, cannyEdges.rows() / 15);//, grayMat.rows() / 8); Mat houghCircles = new Mat(); houghCircles.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1); //Drawing lines on the image for (int i = 0; i < circles.cols(); i++) { double[] parameters = circles.get(0, i); double x, y; int r; x = parameters[0]; y = parameters[1]; r = (int) parameters[2]; Point center = new Point(x, y); //Drawing circles on an image Imgproc.circle(houghCircles, center, r, new Scalar(255, 0, 0), 1); } //Converting Mat back to Bitmap Utils.matToBitmap(houghCircles, currentBitmap); imageView.setImageBitmap(currentBitmap); }
Example 11
Source File: MainActivity.java From MOAAP with MIT License | 5 votes |
void HoughLines() { Mat grayMat = new Mat(); Mat cannyEdges = new Mat(); Mat lines = new Mat(); //Converting the image to grayscale Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY); Imgproc.Canny(grayMat, cannyEdges, 10, 100); Imgproc.HoughLinesP(cannyEdges, lines, 1, Math.PI / 180, 50, 20, 20); Mat houghLines = new Mat(); houghLines.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1); //Drawing lines on the image for (int i = 0; i < lines.cols(); i++) { double[] points = lines.get(0, i); double x1, y1, x2, y2; x1 = points[0]; y1 = points[1]; x2 = points[2]; y2 = points[3]; Point pt1 = new Point(x1, y1); Point pt2 = new Point(x2, y2); //Drawing lines on an image Imgproc.line(houghLines, pt1, pt2, new Scalar(255, 0, 0), 1); } //Converting Mat back to Bitmap Utils.matToBitmap(houghLines, currentBitmap); imageView.setImageBitmap(currentBitmap); }
Example 12
Source File: MainActivity.java From MOAAP with MIT License | 5 votes |
void Canny() { Mat grayMat = new Mat(); Mat cannyEdges = new Mat(); //Converting the image to grayscale Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY); Imgproc.Canny(grayMat, cannyEdges, 10, 100); //Converting Mat back to Bitmap Utils.matToBitmap(cannyEdges, currentBitmap); imageView.setImageBitmap(currentBitmap); }
Example 13
Source File: HighGuiUtil.java From javautils with Apache License 2.0 | 5 votes |
/** * 边缘检测的原理:检测出图像中所有灰度值变化较大的点,而且这些点连起来构成若干线条,这些线条就称之为图像的边缘。 * @param oriImg * @param dstImg * @param threshold */ public static void canny(String oriImg, String dstImg, int threshold) { Mat img = Highgui.imread(oriImg); Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY); /**Canny(Mat image, Mat edges, double threshold1, double threshold2, int apertureSize, boolean L2gradient) * 第一个参数,InputArray类型的image,输入图像,即源图像,填Mat类的对象即可,且需为单通道8位图像。 * 第二个参数,OutputArray类型的edges,输出的边缘图,需要和源图片有一样的尺寸和类型。 * 第三个参数,double类型的threshold1,第一个滞后性阈值。 * 第四个参数,double类型的threshold2,第二个滞后性阈值。 * 第五个参数,int类型的apertureSize,表示应用Sobel算子的孔径大小,其有默认值3。 * 第六个参数,bool类型的L2gradient,一个计算图像梯度幅值的标识,有默认值false。 */ Imgproc.Canny(img, img, threshold, threshold * 3, 3, true); Highgui.imwrite(dstImg, img); }
Example 14
Source File: Proc.java From android-object-distance with Apache License 2.0 | 4 votes |
public static double findMarkerWidth(String imgPath){ Mat frame = Highgui.imread(imgPath); Mat gscale = new Mat(); Mat blur = new Mat(); Mat edged = new Mat(); // convert the image to grayscale, blur it, and detect edges if(frame.channels()>1) Imgproc.cvtColor(frame, gscale, Imgproc.COLOR_BGR2GRAY); else gscale = frame; Imgproc.GaussianBlur(gscale, blur, new Size(5, 5), 0); Imgproc.Canny(blur, edged, 35, 125); // find the contours in the edged image and keep the largest one; // we'll assume that this is our piece of paper in the image List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(edged.width(), edged.height(), CvType.CV_8UC1); Imgproc.findContours(edged.clone(), contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); int max_idx = 0; // if any contour exist... if (hierarchy.size().height > 0 && hierarchy.size().width > 0) { double max_area = 0; double area; // find the contour with largest area for (int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0]) { area = Imgproc.contourArea(contours.get(idx)); if(area > max_area){ max_area = area; max_idx = idx; } Imgproc.drawContours(frame, contours, idx, new Scalar(0, 0, 255)); } //Riz: Save File //Imgproc.drawContours(frame, contours, max_idx, new Scalar(250, 0, 0)); byte[] bytes = new byte[ frame.rows() * frame.cols() * frame.channels() ]; File file = new File(CameraActivity.activity.getExternalFilesDir(null), "pic_contour"+ Integer.toString(pic_count) + ".jpg"); pic_count++; Boolean bool = null; String filename = file.toString(); bool = Highgui.imwrite(filename, frame); if (bool == true) Log.d(LOG_TAG, "SUCCESS writing image to external storage"); else Log.d(LOG_TAG, "Fail writing image to external storage"); Log.i(LOG_TAG, "Max Area: " + Double.toString(max_area)); } else{ Log.e(LOG_TAG, "No Contour Found!"); } MatOfPoint2f newPoint = new MatOfPoint2f(contours.get(max_idx).toArray()); return Imgproc.arcLength(newPoint, true); }
Example 15
Source File: PrimitiveDetection.java From FTCVision with MIT License | 4 votes |
/** * Locate rectangles in an image * * @param grayImage Grayscale image * @return Rectangle locations */ public RectangleLocationResult locateRectangles(Mat grayImage) { Mat gray = grayImage.clone(); //Filter out some noise by halving then doubling size Filter.downsample(gray, 2); Filter.upsample(gray, 2); //Mat is short for Matrix, and here is used to store an image. //it is n-dimensional, but as an image, is two-dimensional Mat cacheHierarchy = new Mat(); Mat grayTemp = new Mat(); List<Rectangle> rectangles = new ArrayList<>(); List<Contour> contours = new ArrayList<>(); //This finds the edges using a Canny Edge Detector //It is sent the grayscale Image, a temp Mat, the lower detection threshold for an edge, //the higher detection threshold, the Aperture (blurring) of the image - higher is better //for long, smooth edges, and whether a more accurate version (but time-expensive) version //should be used (true = more accurate) //Note: the edges are stored in "grayTemp", which is an image where everything //is black except for gray-scale lines delineating the edges. Imgproc.Canny(gray, grayTemp, 0, THRESHOLD_CANNY, APERTURE_CANNY, true); //make the white lines twice as big, while leaving the image size constant Filter.dilate(gray, 2); List<MatOfPoint> contoursTemp = new ArrayList<>(); //Find contours - the parameters here are very important to compression and retention //grayTemp is the image from which the contours are found, //contoursTemp is where the resultant contours are stored (note: color is not retained), //cacheHierarchy is the parent-child relationship between the contours (e.g. a contour //inside of another is its child), //Imgproc.CV_RETR_LIST disables the hierarchical relationships being returned, //Imgproc.CHAIN_APPROX_SIMPLE means that the contour is compressed from a massive chain of //paired coordinates to just the endpoints of each segment (e.g. an up-right rectangular //contour is encoded with 4 points.) Imgproc.findContours(grayTemp, contoursTemp, cacheHierarchy, Imgproc.CV_RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); //MatOfPoint2f means that is a MatofPoint (Matrix of Points) represented by floats instead of ints MatOfPoint2f approx = new MatOfPoint2f(); //For each contour, test whether the contour is a rectangle //List<Contour> contours = new ArrayList<>() for (MatOfPoint co : contoursTemp) { //converting the MatOfPoint to MatOfPoint2f MatOfPoint2f matOfPoint2f = new MatOfPoint2f(co.toArray()); //converting the matrix to a Contour Contour c = new Contour(co); //Attempt to fit the contour to the best polygon //input: matOfPoint2f, which is the contour found earlier //output: approx, which is the MatOfPoint2f that holds the new polygon that has less vertices //basically, it smooths out the edges using the third parameter as its approximation accuracy //final parameter determines whether the new approximation must be closed (true=closed) Imgproc.approxPolyDP(matOfPoint2f, approx, c.arcLength(true) * EPLISON_APPROX_TOLERANCE_FACTOR, true); //converting the MatOfPoint2f to a contour Contour approxContour = new Contour(approx); //Make sure the contour is big enough, CLOSED (convex), and has exactly 4 points if (approx.toArray().length == 4 && Math.abs(approxContour.area()) > 1000 && approxContour.isClosed()) { //TODO contours and rectangles array may not match up, but why would they? contours.add(approxContour); //Check each angle to be approximately 90 degrees //Done by comparing the three points constituting the angle of each corner double maxCosine = 0; for (int j = 2; j < 5; j++) { double cosine = Math.abs(MathUtil.angle(approx.toArray()[j % 4], approx.toArray()[j - 2], approx.toArray()[j - 1])); maxCosine = Math.max(maxCosine, cosine); } if (maxCosine < MAX_COSINE_VALUE) { //Convert the points to a rectangle instance rectangles.add(new Rectangle(approx.toArray())); } } } return new RectangleLocationResult(contours, rectangles); }
Example 16
Source File: CannyEdgesProcessor.java From Camdroid with Apache License 2.0 | 4 votes |
protected void execute() { out = gray(); Imgproc.blur(out, out, new Size(3, 3)); Imgproc.Canny(out, out, min, max); }
Example 17
Source File: CropImage.java From reader with MIT License | 4 votes |
private void makeDefault() { HighlightView hv = new HighlightView(mImageView); int width = mBitmap.getWidth(); int height = mBitmap.getHeight(); Rect imageRect = new Rect(0, 0, width, height); // make the default size about 4/5 of the width or height // int cropWidth = Math.min(width, height) * 4 / 5; // int cropHeight = cropWidth; int cropWidth = width; int cropHeight = height; if (mAspectX != 0 && mAspectY != 0) { if (mAspectX > mAspectY) { // �����辩缉��� cropHeight = cropWidth * mAspectY ;// mAspectX; } else { cropWidth = cropHeight * mAspectX ;// mAspectY; } } int x = (width - cropWidth) / 2; int y = (height - cropHeight) / 2; Mat imgSource = new Mat(); Utils.bitmapToMat(mBitmap, imgSource); //convert the image to black and white Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY); //convert the image to black and white does (8 bit) Imgproc.Canny(imgSource, imgSource, 50, 50); //apply gaussian blur to smoothen lines of dots Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(5, 5), 5); //find the contours List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); double maxArea = -1; int maxAreaIdx = -1; Log.d("size",Integer.toString(contours.size())); MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point MatOfPoint2f approxCurve = new MatOfPoint2f(); MatOfPoint largest_contour = contours.get(0); //largest_contour.ge List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>(); //Imgproc.drawContours(imgSource,contours, -1, new Scalar(0, 255, 0), 1); for (int idx = 0; idx < contours.size(); idx++) { temp_contour = contours.get(idx); double contourarea = Imgproc.contourArea(temp_contour); //compare this contour to the previous largest contour found if (contourarea > maxArea) { //check if this contour is a square MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() ); int contourSize = (int)temp_contour.total(); MatOfPoint2f approxCurve_temp = new MatOfPoint2f(); Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize*0.05, true); if (approxCurve_temp.total() == 4) { maxArea = contourarea; maxAreaIdx = idx; approxCurve=approxCurve_temp; largest_contour = temp_contour; } } } Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB); double[] temp_double; float x1, y1, x2, y2; temp_double = approxCurve.get(0,0); Point p1 = new Point(temp_double[0], temp_double[1]); x1 = (float)temp_double[0]; y1 = (float)temp_double[1]; //Core.circle(imgSource,p1,55,new Scalar(0,0,255)); //Imgproc.warpAffine(sourceImage, dummy, rotImage,sourceImage.size()); temp_double = approxCurve.get(1,0); Point p2 = new Point(temp_double[0], temp_double[1]); // Core.circle(imgSource,p2,150,new Scalar(255,255,255)); temp_double = approxCurve.get(2,0); Point p3 = new Point(temp_double[0], temp_double[1]); x2 = (float)temp_double[0]; y2 = (float)temp_double[1]; //Core.circle(imgSource,p3,200,new Scalar(255,0,0)); temp_double = approxCurve.get(3,0); Point p4 = new Point(temp_double[0], temp_double[1]); RectF cropRect = new RectF(x, y, x + cropWidth, y + cropHeight); //RectF cropRect = new RectF(x1, y1, x2, y2); // �����辩缉��� hv.setup(mImageMatrix, imageRect, cropRect, mCircleCrop,false /*mAspectX != 0 && mAspectY != 0*/); mImageView.add(hv); }
Example 18
Source File: ProcessHelper.java From OpenCV-android with Apache License 2.0 | 4 votes |
/** * 直线检测 * * @param origin 原始bitmap * @param callback 回调 */ public void hough(Bitmap origin, ProcessCallback callback) { if (origin == null) { return; } try { Bitmap bitmap = Bitmap.createBitmap(origin.getWidth(), origin.getHeight(), Bitmap.Config.RGB_565); Utils.bitmapToMat(origin, rgbMat); Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY); Mat edges = new Mat(); Mat src = new Mat(origin.getHeight(), origin.getWidth(), CvType.CV_8UC4); Mat lines = new Mat(); // 拷贝 Mat origination = new Mat(src.size(), CvType.CV_8UC1); src.copyTo(origination); // 通过Canny得到边缘图 Imgproc.cvtColor(origination, grayMat, Imgproc.COLOR_BGR2GRAY); Imgproc.Canny(grayMat, edges, 50, 200); // 获取直线图 Imgproc.HoughLinesP(edges, lines, 1, Math.PI / 180, 10, 0, 10); Mat houghLines = new Mat(); houghLines.create(edges.rows(), edges.cols(), CvType.CV_8UC1); // 绘制直线 for (int i = 0; i < lines.rows(); i++) { double[] points = lines.get(i, 0); if (null != points) { double x1, y1, x2, y2; x1 = points[0]; y1 = points[1]; x2 = points[2]; y2 = points[3]; Point pt1 = new Point(x1, y1); Point pt2 = new Point(x2, y2); Imgproc.line(houghLines, pt1, pt2, new Scalar(55, 100, 195), 3); } } Utils.matToBitmap(houghLines, bitmap); callback.onSuccess(bitmap); } catch (Exception e) { callback.onFailed(e.getMessage()); } }
Example 19
Source File: NativeClass.java From AndroidDocumentScanner with MIT License | 4 votes |
public List<MatOfPoint2f> getPoints(Mat src) { // Blur the image to filter out the noise. Mat blurred = new Mat(); Imgproc.medianBlur(src, blurred, 9); // Set up images to use. Mat gray0 = new Mat(blurred.size(), CvType.CV_8U); Mat gray = new Mat(); // For Core.mixChannels. List<MatOfPoint> contours = new ArrayList<>(); List<MatOfPoint2f> rectangles = new ArrayList<>(); List<Mat> sources = new ArrayList<>(); sources.add(blurred); List<Mat> destinations = new ArrayList<>(); destinations.add(gray0); // To filter rectangles by their areas. int srcArea = src.rows() * src.cols(); // Find squares in every color plane of the image. for (int c = 0; c < 3; c++) { int[] ch = {c, 0}; MatOfInt fromTo = new MatOfInt(ch); Core.mixChannels(sources, destinations, fromTo); // Try several threshold levels. for (int l = 0; l < THRESHOLD_LEVEL; l++) { if (l == 0) { // HACK: Use Canny instead of zero threshold level. // Canny helps to catch squares with gradient shading. // NOTE: No kernel size parameters on Java API. Imgproc.Canny(gray0, gray, 10, 20); // Dilate Canny output to remove potential holes between edge segments. Imgproc.dilate(gray, gray, Mat.ones(new Size(3, 3), 0)); } else { int threshold = (l + 1) * 255 / THRESHOLD_LEVEL; Imgproc.threshold(gray0, gray, threshold, 255, Imgproc.THRESH_BINARY); } // Find contours and store them all as a list. Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); for (MatOfPoint contour : contours) { MatOfPoint2f contourFloat = MathUtils.toMatOfPointFloat(contour); double arcLen = Imgproc.arcLength(contourFloat, true) * 0.02; // Approximate polygonal curves. MatOfPoint2f approx = new MatOfPoint2f(); Imgproc.approxPolyDP(contourFloat, approx, arcLen, true); if (isRectangle(approx, srcArea)) { rectangles.add(approx); } } } } return rectangles; }
Example 20
Source File: GeneralUtils.java From super-cloudops with Apache License 2.0 | 2 votes |
/** * canny算法,边缘检测 * * @param src * @return */ public static Mat canny(Mat src) { Mat mat = src.clone(); Imgproc.Canny(src, mat, 60, 200); return mat; }