Ask Your Question
0

findcontours in java not giving desired results

asked 2013-04-21 14:02:57 -0600

Pradeep gravatar image

Hello All,

Today is my first day at OpenCV. I recently got fascinated with OCR and found OCV to be best and can do more than usual it. So after setting up my development environment I started to code. OCV version 2.4.5 JDK 1.7 Eclipse

I got the code the test code working. Somehow my contours are not getting matched. Not sure if I am missing something

import org.opencv.imgproc.*;

import org.opencv.core.; import org.opencv.highgui.; import java.util.*;

public class Test{

public static void main(String args[]){
    // Load the library

    System.loadLibrary("opencv_java245");

    // Consider the image for processing
    Mat image = Highgui.imread("C:/Users/patiprad/Desktop/IwQY6.png", Imgproc.COLOR_BGR2GRAY);
    Mat imageHSV = new Mat(image.size(), Core.DEPTH_MASK_8U);
    Mat imageBlurr = new Mat(image.size(), Core.DEPTH_MASK_8U);
    Mat imageA = new Mat(image.size(), Core.DEPTH_MASK_ALL);
    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);

    Highgui.imwrite("C:/Users/patiprad/Desktop/test1.png",imageBlurr);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();    
    Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
    //Imgproc.drawContours(imageBlurr, contours, 1, new Scalar(0,0,255));
    for(int i=0; i< contours.size();i++){
        System.out.println(Imgproc.contourArea(contours.get(i)));
        if (Imgproc.contourArea(contours.get(i)) > 50 ){
            Rect rect = Imgproc.boundingRect(contours.get(i));
            System.out.println(rect.height);
            if (rect.height > 28){
            //System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width);
            Core.rectangle(image, new Point(rect.x,rect.height), new Point(rect.y,rect.width),new Scalar(0,0,255));
            }
        }
    }
    Highgui.imwrite("C:/Users/patiprad/Desktop/test2.png",image);
}

}

Certainly am missing some basics here. Please find the result of the code.

Result.png Input.png

edit retag flag offensive close merge delete

Comments

1

the code is basically to replicate what Abid has got going here in Python http://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python

Pradeep gravatar imagePradeep ( 2013-04-21 14:09:21 -0600 )edit

I've run the above code, but I do not understand that after having been part of the cup, how to get them out. For example, after separation sequence 9 | 8 | 2 | 1 ... and now how to derive this number sequence to the console

Nguyễn Văn Nam gravatar imageNguyễn Văn Nam ( 2015-09-22 20:47:38 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2013-04-22 07:39:12 -0600

Guanta gravatar image

Isn't the following line wrong?

Core.rectangle(image, new Point(rect.x,rect.height), new Point(rect.y,rect.width),new Scalar(0,0,255));

I think it should be:

Core.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
edit flag offensive delete link more

Comments

Thank you for looking at it.It helped Sorry for missing it.

Pradeep gravatar imagePradeep ( 2013-04-23 23:31:19 -0600 )edit

this actually worked tq...

tejash gravatar imagetejash ( 2017-01-04 05:44:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-04-21 14:02:57 -0600

Seen: 24,310 times

Last updated: Sep 22 '15