List of usage examples for org.opencv.core MatOfPoint2f total
public long total()
From source file:karthiknr.TextID.ProcessAsyncActivity.java
License:Apache License
public Mat findWarpedMat(Mat imgSource) { //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;//from ww w . j ava2 s. com maxAreaIdx = idx; approxCurve = approxCurve_temp; largest_contour = temp_contour; } } } Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB); //Mat sourceImage =Imgcodecs.imread(Environment.getExternalStorageDirectory().getAbsolutePath()+"/TextID/"+"/oocr.png"); double[] temp_double; temp_double = approxCurve.get(0, 0); Point p1 = new Point(temp_double[0], 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]); //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]); // Core.circle(imgSource,p4,100,new Scalar(0,0,255)); 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); return startM; }
From source file:mineshcvit.opendocscanner.CropImage.java
License:Apache License
private void makeDefault() { // minesh: finding the largest rect in the given image //Mat grayImage= Imgcodecs.imread(IMAGE_PATH, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE); ////////////////////// /////////////w ww .j a va 2 s.com Mat imgSource = new Mat(); Utils.bitmapToMat(mBitmap, imgSource); // Utils.bitmapToMat(bmp32, imgMAT); Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY); //Mat imgSource = Imgcodecs.imread(mImagePath,Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE); Log.w("myApp", "image path from isnde makedefault() is " + mImagePath); int matwidth = imgSource.width(); int matheight = imgSource.height(); Log.w("myApp", "mat image width, from makedefault() is " + matwidth); Log.w("myApp", "mat image height from, makedefault() is " + matheight); Mat imageBin = new Mat(); double threshold = Imgproc.threshold(imgSource, imageBin, 0, 255, Imgproc.THRESH_OTSU); Log.w("myApp", "otsu threshold is " + threshold); //for canny higher threshold is chosen as otsus threshold and lower threshold is half of the otsu threshold value Imgproc.Canny(imgSource.clone(), imgSource, threshold * 0.5, threshold); // Imgcodecs.imwrite(mImagePath, imgSource); // int canny_height=imgSource.height(); // int canny_width=imgSource.width(); // Log.w("myApp", "canny image height is "+canny_height); Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(3, 3), 3); // find the contours List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); //MatVector contours = new MatVector(); Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); double maxArea = -1; MatOfPoint temp_contour = contours.get(0); // the largest is at the // index 0 for starting // point MatOfPoint2f approxCurve = new MatOfPoint2f(); 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; approxCurve = approxCurve_temp; } } } double[] temp_double; temp_double = approxCurve.get(0, 0); Point p1 = new Point(temp_double[0], 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]); // 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]); // Core.circle(imgSource,p4,100,new Scalar(0,0,255)); ArrayList<Point> source = new ArrayList<Point>(); ArrayList<Point> topPoints = new ArrayList<Point>(); ArrayList<Point> bottomPoints = new ArrayList<Point>(); ArrayList<Point> sortedPoints = new ArrayList<Point>(); source.add(p1); source.add(p2); source.add(p3); source.add(p4); Collections.sort(source, new Comparator<Point>() { public int compare(Point o1, Point o2) { return Double.compare(o1.y, o2.y); } }); topPoints.add(source.get(0)); topPoints.add(source.get(1)); Collections.sort(topPoints, new Comparator<Point>() { public int compare(Point o1, Point o2) { return Double.compare(o1.x, o2.x); } }); bottomPoints.add(source.get(2)); bottomPoints.add(source.get(3)); Collections.sort(bottomPoints, new Comparator<Point>() { public int compare(Point o1, Point o2) { return Double.compare(o1.x, o2.x); } }); sortedPoints.add(topPoints.get(0));//top left sortedPoints.add(bottomPoints.get(0));//bottom left sortedPoints.add(bottomPoints.get(1));//bottom right sortedPoints.add(topPoints.get(1));//top right /* c++ code to sort the points void sortCorners(std::vector<cv::Point2f>& corners, cv::Point2f center) { std::vector<cv::Point2f> top, bot; for (int i = 0; i < corners.size(); i++) { if (corners[i].y < center.y) top.push_back(corners[i]); else bot.push_back(corners[i]); } cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0]; cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1]; cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0]; cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1]; corners.clear(); corners.push_back(tl); corners.push_back(tr); corners.push_back(br); corners.push_back(bl); } ... // Get mass center cv::Point2f center(0,0); for (int i = 0; i < corners.size(); i++) center += corners[i]; center *= (1. / corners.size()); sortCorners(corners, center); */ //p1 t0 p4 are in the anti clock wise order starting from top left // double s=source.get(0).x; ///////////////// ///////////////// int width = mBitmap.getWidth(); int height = mBitmap.getHeight(); Log.w("myApp", "bitmap width is " + width); Log.w("myApp", "bitmap height is " + height); 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 x = (width - cropWidth) / 2; int y = (height - cropHeight) / 2; RectF cropRect = new RectF(x, y, x + cropWidth, y + cropHeight); */ /// To test the points order /* Point p1 = new Point(1.0*x,1.0*y ); Point p2 = new Point(1.0*x+150.0,1.0*y+1.0*cropHeight); Point p3 = new Point(1.0*x+1.0*cropWidth,1.0*y+1.0*cropHeight); Point p4 = new Point(1.0*x+1.0*cropWidth,1.0*y); ArrayList<Point> source = new ArrayList<Point>(); source.add(p1); source.add(p2); source.add(p3); source.add(p4); */ //////////////////////////// Log.w("myApp", "from inside makedeafult inside cropimage calss, default crop rect values are set and now highlight view will be initiated "); HighlightView hv = new HighlightView(mImageView, imageRect, sortedPoints); Log.w("myApp", "higlight view initiated; done"); mImageView.add(hv); Log.w("myApp", "add hv is done; done"); mImageView.invalidate(); mCrop = hv; Log.w("myApp", "mcrop=hv donee"); mCrop.setFocus(true); ; }