4

I am stuck with a problem that is how to recognize some patterns in image.

the image is the image of paper which is pure white and the patterns are in four corners are in black. I want to recognize the black patterns on the image?

I surf a lot on the net and found that the opencv as a answer. but there is nothing provided that describe how to use opencv in order to achieve the required feature.

Please help me with some coding point of view or provide some link which I should follow or any name of any open source library which I should use to achieve this feature. The image for pattern is below:- enter image description here

The image consist of pure white background and four black patterns in the corner.I need to recognize these black patterns in the all four corners all then process the image.One corner shown in oval to highlight it.

Any suggestions will be highly appreciated.

Thanks in advance!

Gypsa
  • 11,150
  • 6
  • 42
  • 82

2 Answers2

3

I really don't understand your problem - if you say:

The image is the image of paper which is pure white and the patterns are in four corners are in black.

Then what's the problem to mask only these four contours from image? After doing mask with 4 squares with length 40 pixels I got this:

enter image description here

To remove small areas you can use morphological operations. I got this:

enter image description here

And just draw them (optional) on input image. Here's result:

enter image description here

To implement this algorithm I use OpenCV library. I'm 100% sure that it works on IOS - OpenCV team finally published IOS version. So if you say:

I tried running the OpenCV-iOS link but the project does not run, it is showing errors.

Then we can't help you with that because we are not telepathists to see your problem. Just small suggestion - try to google your problem. I'm 99% sure that it should help.

And lest I forget - here's c++ code:

Mat src = imread("input.png"), tmp;

//convert image to 1bit
cvtColor(src, tmp, CV_BGR2GRAY);
threshold(tmp, tmp, 200, 255, THRESH_OTSU);

//do masking
#define DELTA 40
for (size_t i=0; i<tmp.rows; i++)
{
    for (size_t j=0; j<tmp.cols; j++)
    {
        if(!((i < DELTA && j < DELTA)
         || (i < DELTA && j > tmp.cols - DELTA)
         || (i > tmp.rows - DELTA && j < DELTA)
         || (i > tmp.rows - DELTA && j > tmp.cols - DELTA)))
        {
            //set color to black
            tmp.at<uchar>(i, j) = 255;
        }
    }
}

bitwise_not(tmp,tmp);

//erosion and dilatation:
Mat element = getStructuringElement(MORPH_RECT, Size(2, 2), Point(1, 1));

erode(tmp, tmp, element);
dilate(tmp, tmp, element);

//(Optional) find contours and draw them:
vector<Vec4i> hierarchy;
vector<vector<Point2i> > contours;

findContours(tmp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

for (size_t i=0; i<contours.size(); i++)
{
    drawContours(src, contours, i, Scalar(0, 0, 255), 1);
}
ArtemStorozhuk
  • 8,542
  • 4
  • 30
  • 52
  • thanks for your answer, I am working on this answer to convert the code in objectiive c and use it, I need to ask you explanation of two lines:-Mat src = imread("input.png"), tmp; what does this line means and what is tmp here and convert image to 1bit means converting image to gray scale or some thing else.Please reply so that I can proceed my work. – Gypsa Aug 16 '12 at 04:50
  • hey my question is that I want to detect whether the four corners which are shown in the image posted are present on the image taken from camera or not, if present then I want to take only that part that lies inside four corners. I do not understand how will I achieve this from the answer you posted. – Gypsa Aug 16 '12 at 05:15
  • @Gypsa You don't need to convert it. OpenCV has release for IOS - just use it! As I already mentioned in my post - google question before asking - here's [Mat explanation](http://opencv.willowgarage.com/documentation/cpp/basic_structures.html) and here's [imread explanation](http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html#cv-imread). `Tmp` means *temporary* matrix. If you want to detect paper on camera than look on [this link](http://stackoverflow.com/questions/8667818/opencv-c-obj-c-detecting-a-sheet-of-paper-square-detection). – ArtemStorozhuk Aug 16 '12 at 05:54
  • I succeeded in making dilate method draw contours is not working but I want to ask that from this how can I recognize if four corners are present in image or not.Can you help me on this part. – Gypsa Aug 16 '12 at 06:57
  • If findContours returns not four than image doesn't contains these contours. This is very easy method. Here's complicated - you can check each contour of each corner-square with real shape. Look at `matchShapes`, `cross-correlation` and `Euclidean distance` methods in WEB. Also look at `matchTemplate`. – ArtemStorozhuk Aug 16 '12 at 09:24
  • can you please elaborate your comment. I am not getting what to do after the findcontours method. – Gypsa Aug 16 '12 at 12:32
  • Google, my friend, google it! [matchShapes](http://opencv.willowgarage.com/documentation/python/structural_analysis_and_shape_descriptors.html#matchshapes), [matchTemplate](http://opencv.itseez.com/doc/tutorials/imgproc/histograms/template_matching/template_matching.html). – ArtemStorozhuk Aug 16 '12 at 14:19
  • I have awarded bounty to your answer but it really does not helped me.Thanks anyways. – Gypsa Aug 21 '12 at 04:33
  • @Gypsa Thanks. Actally I gave you all needed info to solve your problem. – ArtemStorozhuk Aug 21 '12 at 09:23
1

Maybe this question is helpful for you, especially the link to the Tennis Ball Recognizing Tutorial seems to be pretty much what you are looking for.

Regarding how to use OpenCV on iOS you might want to take a look at OpenCV-iOS and Computer Vision with iOS.

Community
  • 1
  • 1
zlajo
  • 2,073
  • 18
  • 24
  • I tried running the OpenCV-iOS link but the project does not run, it is showing errors. – Gypsa Aug 13 '12 at 06:56