java - Detecting rectangles using OpenCV -
so i'm new opencv , past 2-3 days i've searched lot how use opencv in java , android studio perform perspective correction , detect biggest rectangle in bitmaps , based on searches have done work result bitmap not correct.i'm sure i've done lot of things wrong great if me.
thanks in advance help.
public void onpicturetaken(byte[] data, camera camera) { bitmap myimage = bitmapfactory.decodebytearray(data, 0, data.length); mat matimage = new mat(myimage.getheight(),myimage.getwidth(), cvtype.cv_8uc3); bitmap mybitmap32 = myimage.copy(bitmap.config.argb_8888, true); utils.bitmaptomat(mybitmap32, matimage); correctperspective(matimage); }
public static void correctperspective(mat imgsource) { // convert image black , white (8 bit) imgproc.canny(imgsource.clone(), imgsource, 50, 50); // apply gaussian blur smoothen lines of dots imgproc.gaussianblur(imgsource, imgsource, new org.opencv.core.size(5, 5), 5); // find contours list<matofpoint> contours = new arraylist<matofpoint>(); imgproc.findcontours(imgsource, contours, new mat(), imgproc.retr_list, imgproc.chain_approx_simple); double maxarea = -1; matofpoint temp_contour = contours.get(0); // index 0 starting // point matofpoint2f approxcurve = new matofpoint2f(); (int idx = 0; idx < contours.size(); idx++) { temp_contour = contours.get(idx); double contourarea = imgproc.contourarea(temp_contour); // compare contour previous largest contour found if (contourarea > maxarea) { // check if contour 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; approxcurve = approxcurve_temp; } } } imgproc.cvtcolor(imgsource, imgsource, imgproc.color_bayerbg2rgb); double[] temp_double; temp_double = approxcurve.get(0, 0); point p1 = new point(temp_double[0], temp_double[1]); temp_double = approxcurve.get(1, 0); point p2 = new point(temp_double[0], temp_double[1]); temp_double = approxcurve.get(2, 0); point p3 = new point(temp_double[0], temp_double[1]); temp_double = approxcurve.get(3, 0); point p4 = new point(temp_double[0], temp_double[1]); list<point> source = new arraylist<point>(); source.add(p1); source.add(p2); source.add(p3); source.add(p4); mat startm = converters.vector_point2f_to_mat(source); mat result = warp(imgsource, startm); //saving bitmap bitmap resultbitmap = bitmap.createbitmap(result.cols(), result.rows(),bitmap.config.argb_8888);; mat tmp = new mat (result.cols(), result.rows(), cvtype.cv_8u, new scalar(4)); imgproc.cvtcolor(result, tmp, imgproc.color_rgb2bgra); utils.mattobitmap(tmp, resultbitmap); }
public static mat warp(mat inputmat, mat startm) { int resultwidth = 1200; int resultheight = 680; point ocvpout4 = new point(0, 0); point ocvpout1 = new point(0, resultheight); point ocvpout2 = new point(resultwidth, resultheight); point ocvpout3 = new point(resultwidth, 0); if (inputmat.height() > inputmat.width()) { ocvpout3 = new point(0, 0); ocvpout4 = new point(0, resultheight); ocvpout1 = new point(resultwidth, resultheight); ocvpout2 = new point(resultwidth, 0); } mat outputmat = new mat(resultwidth, resultheight, cvtype.cv_8uc4); list<point> dest = new arraylist<point>(); dest.add(ocvpout1); dest.add(ocvpout2); dest.add(ocvpout3); dest.add(ocvpout4); mat endm = converters.vector_point2f_to_mat(dest); mat perspectivetransform = imgproc.getperspectivetransform(startm, endm); imgproc.warpperspective(inputmat, outputmat, perspectivetransform, new size(resultwidth, resultheight), imgproc.inter_cubic); return outputmat; }
Comments
Post a Comment