Ask Your Question

Revision history [back]

find contour opencv java

Hello, I want my program to input an image (attachment) image description

and output a string like :

  • Question 1: box 2 checked
  • Question 2: box 1 checked
  • Question 3: box 3 checked
  • Question 4: box 2 checked

Etc ... (as many question as I have per image)

I can simply locate empty boxes with this program:

import java.util.ArrayList; import java.util.List; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc;public class test { public static void main(String[] args) { // Load the library of openCv System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Consider the image for processing Mat image = Imgcodecs.imread("C:/attachement.jpg", Imgproc.COLOR_BGR2GRAY); Mat imageHSV = new Mat(image.size(), CvType.CV_8UC4); Mat imageBlurr = new Mat(image.size(),CvType.CV_8UC4); Mat imageA = new Mat(image.size(), CvType.CV_32F); Imgproc.cvtColor(image, imageHSV, Imgproc.COLOR_BGR2GRAY); Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5,5), 0); Imgproc.adaptiveThreshold(imageBlurr, imageA, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,7, 5); List<matofpoint> contours = new ArrayList<matofpoint>();
Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE); for(int i=0; i< contours.size();i++) { if (Imgproc.contourArea(contours.get(i)) > 50 ) { Rect rect = Imgproc.boundingRect(contours.get(i)); if ((rect.height > 35 && rect.height < 60) && (rect.width > 35 && rect.width < 60)) { Imgproc.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255)); } } } Imgcodecs.imwrite("C:/Users/Benreghai/Downloads/tof/output.png",image); } }

Here is the output of my program: (image attached) output

Thank you for your help !

find contour opencv java

Hello, I want my program to input an image (attachment) image description

and output a string like :

  • Question 1: box 2 checked
  • Question 2: box 1 checked
  • Question 3: box 3 checked
  • Question 4: box 2 checked

Etc ... (as many question as I have per image)

I can simply locate empty boxes with this program:

import java.util.ArrayList;
 import java.util.List;
 import org.opencv.core.Core;
 import org.opencv.core.CvType;
 import org.opencv.core.Mat;
 import org.opencv.core.MatOfPoint;
 import org.opencv.core.Point;
 import org.opencv.core.Rect;
 import org.opencv.core.Scalar;
 import org.opencv.core.Size;
 import org.opencv.imgcodecs.Imgcodecs;
 import org.opencv.imgproc.Imgproc;public class test {
 public static void main(String[] args) {
     // Load the library of openCv
     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     // Consider the image for processing
     Mat image = Imgcodecs.imread("C:/attachement.jpg", Imgproc.COLOR_BGR2GRAY);
     Mat imageHSV = new Mat(image.size(), CvType.CV_8UC4);
     Mat imageBlurr = new Mat(image.size(),CvType.CV_8UC4);
     Mat imageA = new Mat(image.size(), CvType.CV_32F);
     Imgproc.cvtColor(image, imageHSV, Imgproc.COLOR_BGR2GRAY);
     Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5,5), 0);
     Imgproc.adaptiveThreshold(imageBlurr, imageA, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,7, 5);
          List<matofpoint> List<MatOfPoint> contours = new ArrayList<matofpoint>(); 
ArrayList<MatOfPoint>(); Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE); for(int i=0; i< contours.size();i++) { if (Imgproc.contourArea(contours.get(i)) > 50 ) { Rect rect = Imgproc.boundingRect(contours.get(i)); if ((rect.height > 35 && rect.height < 60) && (rect.width > 35 && rect.width < 60)) { Imgproc.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255)); } } } Imgcodecs.imwrite("C:/Users/Benreghai/Downloads/tof/output.png",image); }

}

Here is the output of my program: (image attached) output

Thank you for your help !

find contour opencv java

Hello, I want my program to input an image (attachment) image description

and output a string like :

  • Question 1: box 2 checked
  • Question 2: box 1 checked
  • Question 3: box 3 checked
  • Question 4: box 2 checked

Etc ... (as many question as I have per image)

I can simply locate empty boxes with this program:

import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;public class test {
public static void main(String[] args) {
    // Load the library of openCv
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    // Consider the image for processing
    Mat image = Imgcodecs.imread("C:/attachement.jpg", Imgproc.COLOR_BGR2GRAY);
    Mat imageHSV = new Mat(image.size(), CvType.CV_8UC4);
    Mat imageBlurr = new Mat(image.size(),CvType.CV_8UC4);
    Mat imageA = new Mat(image.size(), CvType.CV_32F);
    Imgproc.cvtColor(image, imageHSV, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5,5), 0);
    Imgproc.adaptiveThreshold(imageBlurr, imageA, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,7, 5);
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();    
    Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
    for(int i=0; i< contours.size();i++)
    {
        if (Imgproc.contourArea(contours.get(i)) > 50 )
        {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            if ((rect.height > 35 && rect.height < 60) && (rect.width > 35 && rect.width < 60))
            {
                Imgproc.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
            }
        }
    }
    Imgcodecs.imwrite("C:/Users/Benreghai/Downloads/tof/output.png",image);
}

}

Here is the output of my program: (image attached) output

Thank you for your help !

find contour opencv java

Hello, I want my program to input an image (attachment) image description

and output a string like :

  • Question 1: box 2 checked
  • Question 2: box 1 checked
  • Question 3: box 3 checked
  • Question 4: box 2 checked

Etc ... (as many question as I have per image)

I can simply locate empty boxes with this program:

import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;public class test {
public static void main(String[] args) {
    // Load the library of openCv
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    // Consider the image for processing
    Mat image = Imgcodecs.imread("C:/attachement.jpg", Imgproc.COLOR_BGR2GRAY);
    Mat imageHSV = new Mat(image.size(), CvType.CV_8UC4);
    Mat imageBlurr = new Mat(image.size(),CvType.CV_8UC4);
    Mat imageA = new Mat(image.size(), CvType.CV_32F);
    Imgproc.cvtColor(image, imageHSV, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5,5), 0);
    Imgproc.adaptiveThreshold(imageBlurr, imageA, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,7, 5);
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();    
    Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
    for(int i=0; i< contours.size();i++)
    {
        if (Imgproc.contourArea(contours.get(i)) > 50 )
        {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            if ((rect.height > 35 && rect.height < 60) && (rect.width > 35 && rect.width < 60))
            {
                Imgproc.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
            }
        }
    }
    Imgcodecs.imwrite("C:/Users/Benreghai/Downloads/tof/output.png",image);
}

}

Here is the output of my program: (image attached) output

Thank you for your help !