Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract width and height of contour in javacv?

I am developing project on component identification using javacv package (Opencv ). I used a method to return set of rectangles on the image as "CvSeq" What I need to know is how to do following things

  • How can I get each rectangle from methods output (from CvSeq)?
  • How to access the lengths and width of the rectangle ?

This is the method that returns the rectangles

public static CvSeq findSquares( final IplImage src,  CvMemStorage storage)
{

CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);

IplImage pyr = null, timg = null, gray = null, tgray;
timg = cvCloneImage(src);

CvSize sz = cvSize(src.width() & -2, src.height() & -2);
tgray = cvCreateImage(sz, src.depth(), 1);
gray = cvCreateImage(sz, src.depth(), 1);
pyr = cvCreateImage(cvSize(sz.width()/2, sz.height()/2), src.depth(), src.nChannels());

// down-scale and upscale the image to filter out the noise
cvPyrDown(timg, pyr, CV_GAUSSIAN_5x5);
cvPyrUp(pyr, timg, CV_GAUSSIAN_5x5);
cvSaveImage("ha.jpg",   timg);
CvSeq contours = new CvContour();
// request closing of the application when the image window is closed
// show image on window
// find squares in every color plane of the image
for( int c = 0; c < 3; c++ )
{
    IplImage channels[] = {cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1)};
    channels[c] = cvCreateImage(sz, 8, 1);
    if(src.nChannels() > 1){
        cvSplit(timg, channels[0], channels[1], channels[2], null);
    }else{
        tgray = cvCloneImage(timg);
    }
    tgray = channels[c]; // try several threshold levels
    for( int l = 0; l < N; l++ )
    {
    //             hack: use Canny instead of zero threshold level.
    //             Canny helps to catch squares with gradient shading
                   if( l == 0 )
                {
    //                apply Canny. Take the upper threshold from slider
    //                and set the lower to 0 (which forces edges merging)
                      cvCanny(tgray, gray, 0, thresh, 5);
   //                 dilate canny output to remove potential
   //                // holes between edge segments
                      cvDilate(gray, gray, null, 1);
                 }
                 else
                 {
    //                apply threshold if l!=0:
                      cvThreshold(tgray, gray, (l+1)*255/N, 255, CV_THRESH_BINARY);
                 }
    //            find contours and store them all as a list
                cvFindContours(gray, storage, contours, sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

                CvSeq approx;

  //            test each contour
                while (contours != null && !contours.isNull()) {
                       if (contours.elem_size() > 0) {
                            approx = cvApproxPoly(contours, Loader.sizeof(CvContour.class),storage, CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
                    if( approx.total() == 4
                            &&
                            Math.abs(cvContourArea(approx, CV_WHOLE_SEQ, 0)) > 1000 &&
                        cvCheckContourConvexity(approx) != 0
                        ){
                        double maxCosine = 0;
                        //
                        for( int j = 2; j < 5; j++ )
                        {
           //         find the maximum cosine of the angle between joint edges
                      double cosine = Math.abs(angle(new CvPoint(cvGetSeqElem(approx, j%4)), new CvPoint(cvGetSeqElem(approx, j-2)), new CvPoint(cvGetSeqElem(approx, j-1))));
                       maxCosine = Math.max(maxCosine, cosine);
                         }
                         if( maxCosine < 0.2 ){
                             cvSeqPush(squares, approx);
                         }
                    }
                }
                contours = contours.h_next();
            }
        contours = new CvContour();
    }
}
return squares;
}

This is the sample original image that I used

enter image description here

And this is the image that I got after drawing lines around the matching rectangles

enter image description here

Actually in above images I'm tying to remove those large rectangles and just need to identify other rectangles so I need some code example to understand how to archive above goals. Please be kind enough to share your experience with me. Thanks !

like image 618
SL_User Avatar asked Jun 21 '12 01:06

SL_User


2 Answers

OpenCV finds contours of the white objects in black background. In your case it is reverse, objects are black. And in that way, even image border is also an object. So to avoid that, just reverse image such that background is black.

Below I have demonstrated it (using OpenCV-Python):

import numpy as np
import cv2

im = cv2.imread('sofsqr.png')
img = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(img,127,255,1)

Remember, instead of using seperate function for inverting, I used it in threshold. Just convert the threshold type to BINARY_INV (ie '1').

Now you have an image as below :

enter image description here

Now we find the contours. Then for each contour, we approximate it and check if it is a rectangle by looking at the length of approximated contour, which should be four for a rectangle.

If drawn, you get like this:

enter image description here

And at the same time, we also find the bounding rect of each contour. The bounding rect has a shape like this : [initial point x, initial point y, width of rect, height of rect]

So you get the width and height.

Below is the code:

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    approx = cv2.approxPolyDP(cnt,cv2.arcLength(cnt,True)*0.02,True)
    if len(approx)==4:
        cv2.drawContours(im,[approx],0,(0,0,255),2)
        x,y,w,h = cv2.boundingRect(cnt)

EDIT :

After some comments, I understood that, the real aim of the question is to avoid large rectangles and select only smaller ones.

It can be done using bounding rect values we obtained. ie, Select only those rectangles whose length is less than a threshold value, or breadth or area. As an example, in this image, I took area should be less than 10000.(A rough estimate). If it is less than 10000, it should be selected and we denote it in red color, otherwise, false candidate, represented in blue color ( just for visualization).

for cnt in contours:
    approx = cv2.approxPolyDP(cnt,cv2.arcLength(cnt,True)*0.02,True)
    if len(approx)==4:
        x,y,w,h = cv2.boundingRect(approx)
        if w*h < 10000:
            cv2.drawContours(im,[approx],0,(0,0,255),-1)
        else:
            cv2.drawContours(im,[approx],0,(255,0,0),-1)

Below is the output i got :

enter image description here

How to get that threshold value? :

It completely depends on you and your application. Or you can find it by trial and error methods. ( i did so).

Hope that solves your problem. All functions are standard opencv functions. So i think you won't find any problem to convert to JavaCV.

like image 61
Abid Rahman K Avatar answered Nov 09 '22 16:11

Abid Rahman K


Just noticed that there is a bug in the code provided in the question:

IplImage channels[] = {cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1)};
channels[c] = cvCreateImage(sz, 8, 1);
if(src.nChannels() > 1){
    cvSplit(timg, channels[0], channels[1], channels[2], null);
}else{
    tgray = cvCloneImage(timg);
}
tgray = channels[c];

This means if there is a single channel, tgray will be an empty image. It should read:

IplImage channels[] = {cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1), cvCreateImage(sz, 8, 1)};
channels[c] = cvCreateImage(sz, 8, 1);
if(src.nChannels() > 1){
    cvSplit(timg, channels[0], channels[1], channels[2], null);
    tgray = channels[c];
}else{
    tgray = cvCloneImage(timg);
}
like image 2
rcomblen Avatar answered Nov 09 '22 17:11

rcomblen