JavaOpencv implements Question Answering card scanning for bank card number interception and javaopencv Question Answering card
Opencv needs to be used in the project. After learning about it, we made two mini programs about java Opencv question card scanning and bank card number interception.
We will not discuss the installation and download of Opencv, but mainly post code. I hope you can give me more advice.
Question Sheet
Import org. opencv. core. *; import org. opencv. imgcodecs. imgcodecs; import org. opencv. imgproc. imgproc; import java. util. *; import static org. opencv. core. cvType. CV_8U; import static org. opencv. imgproc. imgproc. MORPH_RECT;/*** @ author lsw * @ email lsw_demail@163.com */public class OpenCv {static {System. loadLibrary (Core. NATIVE_LIBRARY_NAME);} public static void main (String [] args) {String sheet = "D: // A4.jpg "; // A4 binary expanded image path String results = "E: // result.jpg"; String msg = rowsAndCols (sheet, results); System. out. println (msg);} public static void Canny (String oriImg, String dstImg, int threshold) {// load the image Mat img = Imgcodecs. imread (oriImg); Mat srcImage2 = new Mat (); Mat srcImage3 = new Mat (); Mat srcImage4 = new Mat (); Mat srcImage5 = new Mat (); // convert the image into a grayscale image Imgproc. cvtColor (img, srcImage2, Imgproc. COLOR_RGB2GRAY); // binarization Imgproc. adaptiveThreshold (srcImage2, srcImage3, 255, Imgproc. ADAPTIVE_THRESH_MEAN_C, Imgproc. THRESH_BINARY_INV, 255, 1); // determine the size of the corrosion and expansion cores Mat element = Imgproc. getStructuringElement (MORPH_RECT, new Size (1, 6); // corrosion operation Imgproc. erode (srcImage3, srcImage4, element); // expansion operation Imgproc. dilate (srcImage4, srcImage5, element); // Imgcodecs. imwrite ("E:/picpool/card/enresults.jpg", srcImage4); // determine the ROI region of each answer card. Mat imag_placement = srcImage4.submat (new Rect (200,106 5, 1930,221 0 )); // identify all the outlines of the Vector <MatOfPoint> chapter1 = new Vector <> (); Imgproc. findContours (imag_ch1, chapter1, new Mat (), 2, 3); Mat result = new Mat (imag_ch1.size (), CV_8U, new Scalar (255); Imgproc. drawContours (result, chapter1,-1, new Scalar (0), 2); Imgcodecs. imwrite ("E: // result.jpg", result); // a new rectangle set is used to hold the outline List <RectComp> RectCompList = new ArrayList <> (); for (int I = 0; I <chapter1.size (); I ++) {Rect rm = Imgproc. boundingRect (chapter1.get (I); RectComp ti = new RectComp (rm); // load the contour within the range of 50-80 into the rectangular set if (ti. rm. width> 60 & ti. rm. width <85) {RectCompList. add (ti) ;}// A new map is used to store the answer (A \ B \ C \ D) TreeMap <Integer, string> listenAnswer = new TreeMap <> (); // sort listenAnswer by X axis. RectCompList. sort (o1, o2)-> {if (o1.rm. x> o2.rm. x) {return 1;} if (o1.rm. x = o2.rm. x) {return 0;} if (o1.rm. x <o2.rm. x) {return-1;} return-1;});/* If the precision is high, you can use the pixel Calculation for (RectComp rc: RectCompList) {int x = RectCompList. get (t ). getRm (). x-16; int y = RectCompList. get (t ). getRm (). y-94; // If the split on the x axis is calculated for more than five questions, there will be a large split int xSplit = x/85/5; // because the first question x = 21 in the computer starts from 0, the reality is from 1, so + 1 int xTitleNum = x/85 + 1; // due to the accuracy problem, the x-axis will gradually decrease to the previous answer. if there are no more than two answers, no problem. if there is a problem about the x-axis 40, if (x % 85> 20) {System. out. println ("Degree of x axis decrease" + x % 85); xTitleNum ++;} xTitleNum = xTitleNum-xSplit; System. out. println (xTitleNum);} * // determines the selected answer (A \ B \ C \ D) for (RectComp rc: RectCompList) {for (int h = 0; h <7; h ++) {if (rc. rm. contains (new Point (rc. rm. x + 20,115 + (320 * h) {for (int w = 0; w <4; w ++) {if (rc. rm. contains (new Point (55 + (500 * w), rc. rm. y) {listenAnswer. put (1 + (20 * h) + (5 * w), "A");} else if (rc. rm. contains (new Point (135 + (500 * w), rc. rm. y) {listenAnswer. put (2 + (20 * h) + (5 * w), "A");} else if (rc. rm. contains (new Point (215 + (500 * w), rc. rm. y) {listenAnswer. put (3 + (20 * h) + (5 * w), "A");} else if (rc. rm. contains (new Point (300 + (500 * w), rc. rm. y) {listenAnswer. put (4 + (20 * h) + (5 * w), "A");} else if (rc. rm. contains (new Point (380 + (500 * w), rc. rm. y) {listenAnswer. put (5 + (20 * h) + (5 * w), "A") ;}} else if (rc. rm. contains (new Point (rc. rm. x + 20,165 + (320 * h) {for (int w = 0; w <4; w ++) {if (rc. rm. contains (new Point (55 + (500 * w), rc. rm. y) {listenAnswer. put (1 + (20 * h) + (5 * w), "B");} else if (rc. rm. contains (new Point (135 + (500 * w), rc. rm. y) {listenAnswer. put (2 + (20 * h) + (5 * w), "B");} else if (rc. rm. contains (new Point (215 + (500 * w), rc. rm. y) {listenAnswer. put (3 + (20 * h) + (5 * w), "B");} else if (rc. rm. contains (new Point (300 + (500 * w), rc. rm. y) {listenAnswer. put (4 + (20 * h) + (5 * w), "B");} else if (rc. rm. contains (new Point (380 + (500 * w), rc. rm. y) {listenAnswer. put (5 + (20 * h) + (5 * w), "B") ;}} else if (rc. rm. contains (new Point (rc. rm. x + 20,220 + (320 * h) {for (int w = 0; w <4; w ++) {if (rc. rm. contains (new Point (55 + (500 * w), rc. rm. y) {listenAnswer. put (1 + (20 * h) + (5 * w), "C");} else if (rc. rm. contains (new Point (135 + (500 * w), rc. rm. y) {listenAnswer. put (2 + (20 * h) + (5 * w), "C");} else if (rc. rm. contains (new Point (215 + (500 * w), rc. rm. y) {listenAnswer. put (3 + (20 * h) + (5 * w), "C");} else if (rc. rm. contains (new Point (300 + (500 * w), rc. rm. y) {listenAnswer. put (4 + (20 * h) + (5 * w), "C");} else if (rc. rm. contains (new Point (380 + (500 * w), rc. rm. y) {listenAnswer. put (5 + (20 * h) + (5 * w), "C") ;}} else if (rc. rm. contains (new Point (rc. rm. x + 20,275 + (320 * h) {for (int w = 0; w <4; w ++) {if (rc. rm. contains (new Point (55 + (500 * w), rc. rm. y) {listenAnswer. put (1 + (20 * h) + (5 * w), "D");} else if (rc. rm. contains (new Point (135 + (500 * w), rc. rm. y) {listenAnswer. put (2 + (20 * h) + (5 * w), "D");} else if (rc. rm. contains (new Point (215 + (500 * w), rc. rm. y) {listenAnswer. put (3 + (20 * h) + (5 * w), "D");} else if (rc. rm. contains (new Point (300 + (500 * w), rc. rm. y) {listenAnswer. put (4 + (20 * h) + (5 * w), "D");} else if (rc. rm. contains (new Point (380 + (500 * w), rc. rm. y) {listenAnswer. put (5 + (20 * h) + (5 * w), "D") ;}}} Iterator iter = listenAnswer. entrySet (). iterator (); while (iter. hasNext () {Map. entry entry = (Map. entry) iter. next (); Object key = entry. getKey (); Object val = entry. getValue (); System. out. println ("Question" + key + ", score:" + val) ;}} public static String rowsAndCols (String oriImg, String dstImg) {String msg = ""; canny (oriImg, dstImg, 50); Mat mat = Imgcodecs. imread (dstImg); msg + = "\ n rows:" + mat. rows (); msg + = "\ n columns:" + mat. cols (); msg + = "\ nheight:" + mat. height (); msg + = "\ nwidth:" + mat. width (); msg + = "\ nelemSide:" + mat. elemSize (); // CvType sequence seq = null; return msg ;}}
The core code is as follows: there is another class, and I will not post it easily. You can go to my github and find the core code to understand the general idea.
Github address: https://github.com/shiwenlin/opencv
The bank card is also very simple. In the end, the bank card image is also captured in the github project. We can also use other plug-ins to convert it into numbers,
-
Top
-
1
-
Step on
-
0
View comments