List of usage examples for org.opencv.imgproc Imgproc boundingRect
public static Rect boundingRect(MatOfPoint points)
From source file:ImageReade.java
public static void detectLetter(Mat img) { ArrayList<Rect> boundRect = new ArrayList<>(); Mat img_gray, img_sobel, img_threshold, element; img_gray = new Mat(); img_sobel = new Mat(); img_threshold = new Mat(); element = new Mat(); Imgproc.cvtColor(img, img_gray, Imgproc.COLOR_BGRA2GRAY); imshow("Rec img_gray", img_gray); Imgproc.Sobel(img_gray, img_sobel, CvType.CV_8U, 1, 0, 3, 1, 0, Imgproc.BORDER_DEFAULT); imshow("Rec img_sobel", img_sobel); Imgproc.threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); imshow("Rec img_threshold", img_threshold); element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(16, 6)); Imgproc.morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); imshow("Rec img_threshold second", img_threshold); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); //Imgproc.findContours(img_threshold, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE); Imgproc.findContours(img_threshold, contours, new Mat(), 0, 1); for (int i = 0; i < contours.size(); i++) { System.out.println(Imgproc.contourArea(contours.get(i))); // if (Imgproc.contourArea(contours.get(i)) > 100) { // //Imgproc.approxPolyDP( contours.get(i), contours_poly[i], 3, true ); // Rect rect = Imgproc.boundingRect(contours.get(i)); // System.out.println(rect.height); // if (rect.width > rect.height) { // //System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width); // Core.rectangle(img, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255)); // } // // // } if (Imgproc.contourArea(contours.get(i)) > 100) { MatOfPoint2f mMOP2f1 = new MatOfPoint2f(); MatOfPoint2f mMOP2f2 = new MatOfPoint2f(); contours.get(i).convertTo(mMOP2f1, CvType.CV_32FC2); Imgproc.approxPolyDP(mMOP2f1, mMOP2f2, 3, true); mMOP2f2.convertTo(contours.get(i), CvType.CV_32S); Rect rect = Imgproc.boundingRect(contours.get(i)); if (rect.width > rect.height) { Core.rectangle(img, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)); }/*from ww w. jav a2 s.c o m*/ } } imshow("Rec Detected", img); }
From source file:PlateSegment.java
public List<Rect> contourToBoundingBoxes(List<MatOfPoint> contours) { Rect boundingRectangle;//from w w w . j av a2 s. c o m List<Rect> boundingBoxes = new ArrayList<Rect>(); for (MatOfPoint contour : contours) { //Create a bounding box... :) boundingRectangle = Imgproc.boundingRect(contour); boundingBoxes.add(boundingRectangle); } return boundingBoxes; }
From source file:ThirdTry.java
public static void detectLetter(Mat img, Mat m2) { ArrayList<Rect> boundRect = new ArrayList<>(); Mat img_gray, img_sobel, img_threshold, element; img_gray = new Mat(); img_sobel = new Mat(); img_threshold = new Mat(); element = new Mat(); Imgproc.cvtColor(img, img_gray, Imgproc.COLOR_BGRA2GRAY); //imshow("Rec img_gray", img_gray); Imgproc.Sobel(img_gray, img_sobel, CvType.CV_8UC1, 1, 0, 3, 1, 0, Imgproc.BORDER_DEFAULT); //imshow("Rec img_sobel", img_sobel); Imgproc.threshold(m2, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY); //imshow("Rec img_threshold", img_threshold); element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 2)); Imgproc.morphologyEx(m2, img_threshold, CV_MOP_CLOSE, element); imshow("Rec img_threshold second", img_threshold); element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12, 12)); Imgproc.morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); //imshow("Rec img_threshold second", img_threshold); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); //Imgproc.findContours(img_threshold, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE); Imgproc.findContours(img_threshold, contours, new Mat(), 0, 1); for (int i = 0; i < contours.size(); i++) { System.out.println(Imgproc.contourArea(contours.get(i))); // if (Imgproc.contourArea(contours.get(i)) > 100) { // //Imgproc.approxPolyDP( contours.get(i), contours_poly[i], 3, true ); // Rect rect = Imgproc.boundingRect(contours.get(i)); // System.out.println(rect.height); // if (rect.width > rect.height) { // //System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width); // Core.rectangle(img, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255)); // } // // // } if (Imgproc.contourArea(contours.get(i)) > 100) { MatOfPoint2f mMOP2f1 = new MatOfPoint2f(); MatOfPoint2f mMOP2f2 = new MatOfPoint2f(); contours.get(i).convertTo(mMOP2f1, CvType.CV_32FC2); Imgproc.approxPolyDP(mMOP2f1, mMOP2f2, 3, true); mMOP2f2.convertTo(contours.get(i), CvType.CV_32S); Rect rect = Imgproc.boundingRect(contours.get(i)); if (rect.width > rect.height) { Core.rectangle(img, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)); }//from w w w.j a v a 2 s . c o m } } //imshow("Rec Detected", img); }
From source file:OCV_BoundingRect.java
License:Open Source License
@Override public void run(ImageProcessor ip) { byte[] byteArray = (byte[]) ip.getPixels(); int w = ip.getWidth(); int h = ip.getHeight(); int num_slice = ip.getSliceNumber(); ArrayList<Point> lstPt = new ArrayList<Point>(); MatOfPoint pts = new MatOfPoint(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (byteArray[x + w * y] != 0) { lstPt.add(new Point(x, y)); }//ww w. java 2 s . c om } } if (lstPt.isEmpty()) { return; } pts.fromList(lstPt); Rect rect = Imgproc.boundingRect(pts); showData(rect, num_slice); }
From source file:classes.BlobsFinder.java
public void findBlobContours() { Mat grayImage = new Mat(); Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY); ImageUtils.saveImage(grayImage, outImageName + "_grayImage.png", request); Mat gaussianImage = new Mat(); Imgproc.GaussianBlur(grayImage, gaussianImage, new Size(0, 0), 3); Core.addWeighted(grayImage, 1.5, gaussianImage, -1, 0, gaussianImage); ImageUtils.saveImage(gaussianImage, outImageName + "_gaussianGrayImage.png", request); Mat binaryImage = new Mat(); Imgproc.adaptiveThreshold(gaussianImage, binaryImage, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 15, 4); ImageUtils.saveImage(binaryImage, outImageName + "_binaryImage.png", request); Mat erodedImage = new Mat(); binaryImage.copyTo(erodedImage);/*from w w w. j av a2 s .co m*/ Mat structuringElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)); Point anchor = new Point(-1, -1); Imgproc.morphologyEx(erodedImage, erodedImage, Imgproc.MORPH_CLOSE, structuringElement, anchor, 1); ImageUtils.saveImage(erodedImage, outImageName + "_erodedImage.png", request); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(erodedImage, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); Mat originalContoursImage = new Mat(image.size(), CvType.CV_8UC1, new Scalar(0)); Scalar contourColor = new Scalar(255); int thickness = -1; // Thicknes should be lower than zero in order to drawn the filled contours Imgproc.drawContours(originalContoursImage, contours, -1, contourColor, thickness); // Drawing all the contours found ImageUtils.saveImage(originalContoursImage, outImageName + "_originalContoursImage.png", request); Mat erodedContoursImage = new Mat(); Imgproc.erode(originalContoursImage, erodedContoursImage, structuringElement, anchor, 1); ImageUtils.saveImage(erodedContoursImage, outImageName + "_erodedContoursImage.png", request); ArrayList<MatOfPoint> finalContours = new ArrayList<MatOfPoint>(); Mat finalContourImage = new Mat(image.size(), CvType.CV_8UC1, new Scalar(0)); Imgproc.findContours(erodedContoursImage, finalContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); for (int i = 0; i < finalContours.size(); i++) { MatOfPoint currentContour = finalContours.get(i); double area = Imgproc.contourArea(currentContour); if (area > MIN_AREA) { validContours.add(currentContour); String fabricPath = generateFabricPathString(currentContour); contourPaths.add(fabricPath); Rect boundingRect = Imgproc.boundingRect(currentContour); topLeftCorners.add(boundingRect.tl()); contoursAreas.add(area); } } // Drawing ALL the valid contours Imgproc.drawContours(finalContourImage, validContours, -1, contourColor, thickness); ImageUtils.saveImage(finalContourImage, outImageName + "_finalContourImage.png", request); }
From source file:classes.FloodFiller.java
private void fillFrom(Point seed, int lo, int up, Scalar backgroundColor, Scalar contourFillingColor) { Mat object = ObjectGenerator.extract(image, seed.x, seed.y, 10, 10); this.meanColor = Core.mean(object); Rect ccomp = new Rect(); Mat mask = Mat.zeros(image.rows() + 2, image.cols() + 2, CvType.CV_8UC1); int connectivity = 4; int newMaskVal = 255; int ffillMode = 1; int flags = connectivity + (newMaskVal << 8) + (ffillMode == 1 ? Imgproc.FLOODFILL_FIXED_RANGE : 0); Scalar newVal = new Scalar(0.299, 0.587, 0.114); Imgproc.threshold(mask, mask, 1, 128, Imgproc.THRESH_BINARY); filledArea = Imgproc.floodFill(image.clone(), mask, seed, newVal, ccomp, new Scalar(lo, lo, lo), new Scalar(up, up, up), flags); // Highgui.imwrite("mask.png", mask); ImageUtils.saveImage(mask, "mask.png", request); morphologicalImage = new Mat(image.size(), CvType.CV_8UC3); Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1)); ArrayList<Mat> mask3 = new ArrayList<Mat>(); mask3.add(mask);// w w w . ja v a 2s . c om mask3.add(mask); mask3.add(mask); Core.merge(mask3, mask); // Applying morphological filters Imgproc.erode(mask, morphologicalImage, element); Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_CLOSE, element, new Point(-1, -1), 9); Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_OPEN, element, new Point(-1, -1), 2); Imgproc.resize(morphologicalImage, morphologicalImage, image.size()); // Highgui.imwrite("morphologicalImage.png", morphologicalImage); ImageUtils.saveImage(morphologicalImage, "morphologicalImage.png", request); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Core.split(mask, mask3); Mat binarymorphologicalImage = mask3.get(0); Imgproc.findContours(binarymorphologicalImage.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); contoursImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor); int thickness = -1; // Thicknes should be lower than zero in order to drawn the filled contours Imgproc.drawContours(contoursImage, contours, -1, contourFillingColor, thickness); // Drawing all the contours found // Highgui.imwrite("allContoursImage.png", contoursImage); ImageUtils.saveImage(contoursImage, "allContoursImage.png", request); if (contours.size() > 1) { int minContourWith = 20; int minContourHeight = 20; int maxContourWith = 6400 / 2; int maxContourHeight = 4800 / 2; contours = filterContours(contours, minContourWith, minContourHeight, maxContourWith, maxContourHeight); } if (contours.size() > 0) { MatOfPoint biggestContour = contours.get(0); // getting the biggest contour contourArea = Imgproc.contourArea(biggestContour); if (contours.size() > 1) { biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one } Point[] points = biggestContour.toArray(); path = "M " + (int) points[0].x + " " + (int) points[0].y + " "; for (int i = 1; i < points.length; ++i) { Point v = points[i]; path += "L " + (int) v.x + " " + (int) v.y + " "; } path += "Z"; biggestContourImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor); Imgproc.drawContours(biggestContourImage, contours, 0, contourFillingColor, thickness); // Highgui.imwrite("biggestContourImage.png", biggestContourImage); ImageUtils.saveImage(biggestContourImage, "biggestContourImage.png", request); Mat maskForColorExtraction = biggestContourImage.clone(); if (isWhite(backgroundColor)) { Imgproc.dilate(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3); } else { Imgproc.erode(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3); } // Highgui.imwrite("maskForColorExtraction.png", maskForColorExtraction); ImageUtils.saveImage(maskForColorExtraction, "maskForColorExtraction.png", request); Mat extractedColor = new Mat(); if (isBlack(backgroundColor) && isWhite(contourFillingColor)) { Core.bitwise_and(maskForColorExtraction, image, extractedColor); } else { Core.bitwise_or(maskForColorExtraction, image, extractedColor); } // Highgui.imwrite("extractedColor.png", extractedColor); ImageUtils.saveImage(extractedColor, "extractedColor.png", request); computedSearchWindow = Imgproc.boundingRect(biggestContour); topLeftCorner = computedSearchWindow.tl(); Rect croppingRect = new Rect(computedSearchWindow.x, computedSearchWindow.y, computedSearchWindow.width - 1, computedSearchWindow.height - 1); Mat imageForTextRecognition = new Mat(extractedColor.clone(), croppingRect); // Highgui.imwrite(outImageName, imageForTextRecognition); ImageUtils.saveImage(imageForTextRecognition, outImageName, request); // // // Mat data = new Mat(imageForTextRecognition.size(), CvType.CV_8UC3, backgroundColor); // imageForTextRecognition.copyTo(data); // data.convertTo(data, CvType.CV_8UC3); // // // The meanColor variable represents the color in the GBR space, the following line transforms this to the RGB color space, which // // is assumed in the prepareImage method of the TextRecognitionPreparer class // Scalar userColor = new Scalar(meanColor.val[2], meanColor.val[1], meanColor.val[0]); // // ArrayList<String> recognizableImageNames = TextRecognitionPreparer.generateRecognizableImagesNames(data, backgroundColor, userColor); // for (String imageName : recognizableImageNames) { // // try { // // First recognition step // String recognizedText = TextRecognizer.recognize(imageName, true).trim(); // if (recognizedText != null && !recognizedText.isEmpty()) { // recognizedStrings.add(recognizedText); // } // // Second recognition step // recognizedText = TextRecognizer.recognize(imageName, false).trim(); // if (recognizedText != null && !recognizedText.isEmpty()) { // recognizedStrings.add(recognizedText); // } // // } catch (Exception e) { // } // } // //// ArrayList<BufferedImage> recognizableBufferedImages = TextRecognitionPreparer.generateRecognizableBufferedImages(data, backgroundColor, userColor); //// for (BufferedImage bufferedImage : recognizableBufferedImages) { //// try { //// // First recognition step //// String recognizedText = TextRecognizer.recognize(bufferedImage, true).trim(); //// if (recognizedText != null && !recognizedText.isEmpty()) { //// recognizedStrings.add(recognizedText); //// } //// // Second recognition step //// recognizedText = TextRecognizer.recognize(bufferedImage, false).trim(); //// if (recognizedText != null && !recognizedText.isEmpty()) { //// recognizedStrings.add(recognizedText); //// } //// //// } catch (Exception e) { //// } //// } // // // // compute all moments Moments mom = Imgproc.moments(biggestContour); massCenter = new Point(mom.get_m10() / mom.get_m00(), mom.get_m01() / mom.get_m00()); // draw black dot Core.circle(contoursImage, massCenter, 4, contourFillingColor, 8); } }
From source file:classes.FloodFiller.java
private static ArrayList<MatOfPoint> filterContours(List<MatOfPoint> contours, int minContourWith, int minContourHeight, int maxContourWith, int maxContourHeight) { ArrayList<MatOfPoint> results = new ArrayList<MatOfPoint>(); for (MatOfPoint currentContour : contours) { Rect boundingBox = Imgproc.boundingRect(currentContour); if (boundingBox.width > minContourWith && boundingBox.height > minContourHeight) { if (boundingBox.width < maxContourWith && boundingBox.height < maxContourHeight) { results.add(currentContour); }//w ww .j a v a 2 s .c o m } } return results; }
From source file:classes.ObjectFinder.java
private void computeSearchWindow() { List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); // a vector of contours // retrieve the external contours // all pixels of each contours Imgproc.findContours(this.morphologicalImage.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);//from ww w . j a v a2 s .c o m // Draw black contours on a white image this.contoursImage = new Mat(morphologicalImage.size(), CvType.CV_8U, new Scalar(255)); if (contours.size() > 1) { int minContourWith = 20; int minContourHeight = 20; int maxContourWith = 6400 / 2; int maxContourHeight = 4800 / 2; contours = filterContours(contours, minContourWith, minContourHeight, maxContourWith, maxContourHeight); } if (contours.size() > 1) { Collections.sort(contours, new ContourComparator()); // Sorttig the contours to take ONLY the bigger one } computedSearchWindow = new Rect(); massCenter = new Point(-1, -1); if (contours.size() > 0) { this.firstContour = contours.get(0); Mat contournedImage = this.firstContour; // draw all contours in black with a thickness of 2 Scalar color = new Scalar(0); int thickness = 2; Imgproc.drawContours(contoursImage, contours, 0, color, thickness); // // testing the bounding box computedSearchWindow = Imgproc.boundingRect(this.firstContour); topLeftCorner = computedSearchWindow.tl(); // compute all moments Moments mom = Imgproc.moments(contournedImage); massCenter = new Point(mom.get_m10() / mom.get_m00(), mom.get_m01() / mom.get_m00()); // draw black dot Core.circle(contoursImage, massCenter, 4, color, 8); } }
From source file:classes.ObjectFinder.java
private ArrayList<MatOfPoint> filterContours(List<MatOfPoint> contours, int minContourWith, int minContourHeight, int maxContourWith, int maxContourHeight) { ArrayList<MatOfPoint> results = new ArrayList<MatOfPoint>(); for (MatOfPoint currentContour : contours) { Rect boundingBox = Imgproc.boundingRect(currentContour); if (boundingBox.width > minContourWith && boundingBox.height > minContourHeight) { if (boundingBox.width < maxContourWith && boundingBox.height < maxContourHeight) { results.add(currentContour); }/*from w w w . j av a 2s . c o m*/ } } return results; }
From source file:cmib_4_4.Countour.java
public static void main(String args[]) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat image = Highgui.imread("input1.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat image1 = Highgui.imread("input1.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat image4 = Highgui.imread("input1.jpg"); Imgproc.threshold(image1, image1, 0, 255, THRESH_OTSU); Imgproc.Canny(image1, image1, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU); Mat image2 = Mat.zeros(image.rows() + 2, image.cols() + 2, CV_8U); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(image1, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); for (int i = 0; i < contours.size(); i++) { if (Imgproc.contourArea(contours.get(i)) > 100) { Rect rect = Imgproc.boundingRect(contours.get(i)); Imgproc.floodFill(image1, image2, new Point(150, 150), new Scalar(255)); Rect rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height); Mat image_roi_rgb = new Mat(image4, rectCrop); Highgui.imwrite("crop2.jpg", image_roi_rgb); if (rect.height > 28) { Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)); }/*w w w . j a v a 2 s. c o m*/ } } Highgui.imwrite("falciparum2.jpg", image); }