find contour opencv java [closed]

asked 2016-12-31 10:10:35 -0600

updated 2020-10-08 02:33:10 -0600

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 !

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by sturkmen
close date 2020-10-08 02:34:16.149146

Comments

So... What's the question? It looks like you're just about done. You know which boxes are empty, and by extension, which one is occupied. Just separate them vertically so you know how many questions there are.

Tetragramm gravatar imageTetragramm ( 2016-12-31 12:33:36 -0600 )edit

At the image level yes. But I want the program not release me an output image but rather strings of characters like:

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

Thanks for help !!

benreghaimehdi gravatar imagebenreghaimehdi ( 2016-12-31 12:38:24 -0600 )edit