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 get the outline display of card number in OpenCV4Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to get the outline display of the card number in OpenCV4Android, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

We added a photo event to the ImgView in OpenCVCaremaActivity.

First, add a variable of boolean, so that the real-time dynamic display at the time of the button does not need to be processed.

Button event flow

We will process the acquired Mat image, and we have written all the processing methods in OpenCvUtil. We will talk about the GetAreaMat method in a moment.

Then convert the processed Mat into the image of bitmap.

Then convert the image of bitmap into an array of byte and pass it to our home page interface.

Close this page.

Finally, the onActivityResulty method of the main page converts the array of byte back to bmp and displays it on the page.

Mat image processing method

When our camera gets the card number, the photos taken are processed, and the redundant parts are removed as far as possible to get a complete card information so that we can recognize the images in the later order.

The operation principle of obtaining card image

Gradient calculation of the original image

Then the gradient image is binarized.

Then the discovery and filtering of the contour

Return to the image

The GetAreaMat method in OpenCvUtils is posted directly according to the processing method written in our above principle flow. The Matgradient method in it is a method of image gradation. If you take a closer look at it, you will see the whole process:

Public class OpenCvUtils {

/ / get the required area

Public static Mat GetAreaMat (Mat src) {

Mat dst = new Mat ()

Mat area = new Mat ()

Src.copyTo (dst)

/ / first, the resources are graded.

Matgradient (dst)

/ / change to grayscale image first

Imgproc.cvtColor (dst, dst, Imgproc.COLOR_BGR2GRAY)

/ / binary image processing

Imgproc.threshold (dst, dst, 40,255, Imgproc.THRESH_BINARY)

/ / A pair of images are opened (corroded first and then expanded)

Mat strElement = Imgproc.getStructuringElement (Imgproc.MORPH_RECT, new Size (3,3))

Imgproc.morphologyEx (dst, dst, Imgproc.MORPH_OPEN, strElement)

/ / find the outline

List contours = new ArrayList ()

Mat hireachy = new Mat ()

/ * *

* Imgproc.RETR_EXTERNAL stands for finding the largest outline of the outer layer.

, /

Imgproc.findContours (dst, contours, hireachy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point (0,0))

/ * *

* get width and height

* used to divide 2 in order to account for half of the total image and to analyze the area you want to find

, /

Int hw = dst.cols () / 2

Rect roi = new Rect ()

For (int iTuno witi hw) {

Roi.x = rect.x

Roi.y = rect.y

Roi.width = rect.width

Roi.height = rect.height

Break

}

}

/ / 0 indicates that the area was not found

If (roi.width = = 0 | | roi.height = = 0) {

Src.copyTo (area)

} else {

Src.submat (roi) .copyto (area)

}

Dst.release ()

Return area

}

/ / gradient the Mat object

Private static void Matgradient (Mat src) {

/ / calculate the gradient first

Mat grad_x = new Mat ()

Mat grad_y = new Mat ()

Mat abs_grad_x = new Mat ()

Mat abs_grad_y = new Mat ()

/ / gradient the X direction first

Imgproc.Scharr (src, grad_x, CvType.CV_32F, 1,0)

/ / gradient the Y direction again

Imgproc.Scharr (src, grad_y, CvType.CV_32F, 0,1)

/ / absolute value

/ * *

* because the grad_x and grad_y obtained may be negative, we require a value between 0255and 255mm.

* so it is necessary to generate the absolute value of the original gradient.

, /

Core.convertScaleAbs (grad_x, abs_grad_x)

Core.convertScaleAbs (grad_y, abs_grad_y)

Grad_x.release (); / / remember to release and save resources in time after processing.

Grad_y.release (); / / remember to release and save resources in time after processing.

/ / gradient image

/ * *

* remerge the image according to the last two absolute values, and the two 0.5 parameters are the specific gravity of the image.

, /

Core.addWeighted (abs_grad_x, 0.5, abs_grad_y, 0.5,0, src)

Abs_grad_x.release (); / / remember to release and save resources in time after processing.

Abs_grad_y.release (); / / remember to release and save resources in time after processing.

}

}

The above content is how to get the card number outline display in OpenCV4Android. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report