4

Please can some one explain how to identify square shape of contours which are not exactly separated each other. For example I need to identify the number of squares in below image and the x,y coordinates of their edges. I try to go through this question but it didn't work for me.

enter image description here

So please can some one explain this using simple code example.

This is the image that I can generated can you please explain how to identify above squares in this image.

enter image description here

So please be kind enough to explain this.

Community
  • 1
  • 1
NadLnk
  • 279
  • 2
  • 9

1 Answers1

5

You have to use fact, that red component of each square equals 255, and do the threshold. Here's what I've done:

  1. Do a red color segmentation: enter image description here

  2. Do dilatation (to remove holes): enter image description here

  3. (Optional) Do a check if each contour is a square.

Code:

Mat src = imread("input.png"), red;
extractChannel(src, red, 2);

threshold(red, red, 254, 255, THRESH_BINARY);

Mat element = getStructuringElement(MORPH_RECT, Size( 2, 2 ), Point( 1, 1 ));
dilate(red, red, element);
ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
  • Hay Thank you very much for your quick reply. Can you please tell me if those squares are having same color like other lines how could I extract those squares ? Please be kind enough to explain that. – NadLnk Aug 12 '12 at 04:53
  • I don't understand your question. – ArtemStorozhuk Aug 12 '12 at 14:30
  • I mean if whole the image have only black colored lines how could I identify those squares ? – NadLnk Aug 12 '12 at 15:21
  • Lines are not squares. What are you talking about? – ArtemStorozhuk Aug 12 '12 at 16:16
  • I have updated the question by uploading sample image to it. In that kind of image how can I identify those squares ? – NadLnk Aug 13 '12 at 12:53
  • @NadLnk if you have accepted my answer, than your question is already answered. Ask a new question and I will help you. – ArtemStorozhuk Aug 13 '12 at 17:36
  • @Astor I had ask it as separate question could you please give some sort of solution for that as well.http://stackoverflow.com/questions/12088176/identify-contours-in-a-image-which-are-having-same-color-using-opencv-or-javacv Thanks – NadLnk Aug 23 '12 at 09:01