Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Edge Detection and outline drawing the maximum Edge of Image by Android OpenCv4

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to achieve edge detection and outline drawing the largest edge of an image by Android OpenCv4". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Canny edge detection

There are five main steps in edge detection based on Canny algorithm, which are Gaussian filtering, pixel gradient calculation, non-maximum pixel gradient suppression, lag threshold processing and isolated weak edge suppression. Whether Canny performs well in the presence of noise depends on the previous noise reduction process, and Gaussian processing can be done manually to improve the recognition rate.

/ * * the input image of image must be a single-channel or three-channel image of CV_8U. Edges output image, a single-channel image of the same size as the input image, and the data type is CV_8U. The first lag threshold for threshold1. The second lag threshold of threshold2. The diameter of apertureSize Sobel operator. The flag of L2gradient's method for calculating the amplitude of image gradient. Default is false**/public static void Canny (Mat image, Mat edges, double threshold1, double threshold2, int apertureSize, boolean L2gradient)

Use

/ * canny algorithm, edge detection * / public static Mat canny (Bitmap bitmap) {Mat mSource = new Mat (); Utils.bitmapToMat (bitmap, mSource); Mat grayMat = new Mat (); Imgproc.cvtColor (mSource,grayMat,Imgproc.COLOR_BGR2GRAY); / / convert to grayscale image Mat mat = mSource.clone (); Imgproc.Canny (mSource, mat, 75,200) Return mat;} gets the maximum rectangle of the image / * returns the largest rectangle after edge detection, and returns the mat matrix after * * @ param cannyMat * Canny * @ return * / public Rect findMaxRect (Mat cannyMat) {Mat tmp = mSource.clone (); List contours = new ArrayList (); Mat hierarchy = new Mat () / / find Imgproc.findContours (cannyMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); int index = 0; double perimeter = 0; / / find the largest matching contour for (int I = 0; I

< contours.size(); i++) { // 最大面积// double area = Imgproc.contourArea(contours.get(i)); //最大周长 MatOfPoint2f source = new MatOfPoint2f(); source.fromList(contours.get(i).toList()); double length = Imgproc.arcLength(source,true); if(length>

Perimeter) {perimeter = length; index = I;}} / * * Parameter 1: image, the image of the outline to be drawn. * * Parameter 2: contours, the collection of outlines to be drawn. * * Parameter 3: contourIdx. The index of the outline to be drawn in contours. A negative number means that all contours are drawn. * * Parameter 4: color, the color of the outline is drawn. * * Parameter 5: thickness, the line thickness of the outline. If it is a negative number, draw the interior of the outline. * * Parameter 6: lineType, line type. FILLED LINE_4 4 connected LINE_8 8 connected LINE_AA anti-aliasing * / Imgproc.drawContours (tmp, contours, index, new Scalar (0.0,0.0, 255.0), 9, Imgproc.LINE_AA) Rect rect = Imgproc.boundingRect (contours.get (index)); / / Imgproc.rectangle (tmp, rect, new Scalar (0.0,0.0, 255.0), 4, Imgproc.LINE_8); showImg (tmp); return rect } / * display image * @ param mat * / private void showImg (Mat mat) {Bitmap bitmap = Bitmap.createBitmap (mat.width (), mat.height (), Bitmap.Config.ARGB_8888); Utils.matToBitmap (mat, bitmap); mIvSrc.setImageBitmap (bitmap); mat.release () } "Android OpenCv4 how to achieve edge detection and outline to draw the largest edge of the image" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report