2

I am new to opencv using python and trying to get the shape of a contour in an image. Considering only regular shapes like square, rectangle, circle and triangle is there any way to get the contour shape using only numpy and cv2 libraries?

Also i want to find the colour inside a contour. How can I do it?

For finding area of a contour there is an inbuilt function: cv2.contourArea(cnt). Are there inbuilt functions for "contour shape" and "color inside contour" also? Please help!

Note : The images I am considering contains multiple regular shapes.

Ankit Prasad
  • 31
  • 1
  • 5
  • I got the part of identifying the shapes in an image. Can anyone help me identifying the color inside a contour. note: color inside all contours are single colored and not multicolored i.e. in one contour it is only one among red,blue,green,yellow – Ankit Prasad Nov 19 '16 at 10:58
  • Regarding "contours inside contours", you can have `findContours` return a structure representing the hieararchy of contours found. OpenCV has a [tutorial](http://docs.opencv.org/trunk/d9/d8b/tutorial_py_contours_hierarchy.html) on how to use this. – Dan Mašek Nov 19 '16 at 14:54
  • @Dan You misunderstood color as contour. I actually wanted to know how to detect color inside a contour. – Ankit Prasad Nov 19 '16 at 17:59
  • Oh, right you are, my bad :) A simple idea - for each contour, create a mask image by drawing the contour polygon filled with white. Use this mask to extract only the corresponding area from the source image. In the resulting image, iterate over non-zero pixels, collecting the unique colors. | BTW, show us some sample input images you're trying to work with. – Dan Mašek Nov 19 '16 at 18:05
  • @DanMašek I am sorry, but the sample images are kind of confidential. I can tell u the description. Its a square image (300X300px) divided into 9 equal square segments. Each square segment has a colored shape inside it. Colors used for shapes are red,green,blue and yellow. Shapes used are square, rectangle, triangle and circle (shapes are not always of the same area). I need to find the color and shape of the figures inside each square segment. – Ankit Prasad Nov 20 '16 at 06:58
  • take a look at [this question](http://stackoverflow.com/q/34969505/5294258) – sturkmen Nov 22 '16 at 07:03
  • thank you @sturkmen but I achieved what was required. I used masking and finding the average hue value of enclosed pixels in a particular contour. And then compared the hue value with standard hue range for red green blue and yellow (whichever color is needed). – Ankit Prasad Nov 23 '16 at 14:43

2 Answers2

2

This method might be longer, but right now it is on the top of my head. For finding contour shape, use findcontours function, it will give vector of points as output(boundary points of contours). Now find the center of contour, using moments.

for finding contour use this function-

 cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) 

image is the canny output image.

calculate center from moments, refer to this link http://docs.opencv.org/trunk/dd/d49/tutorial_py_contour_features.html

calculate distance of each point stored in contours from the center Now classify shaped by comparing distance of points from center

1)circle - all contours points will be roughly at equal distance from center.

2)square, rectangle- find farthest 4 points from center, These points will be vertices and will have approximately same distance. Now differentiate square from rectangle using edge length

3) traingles - this can be tricky, for different types of triangle, so you can just use else condition here, since you have only 4 shapes

For finding colour, use the vertices for square, rectangle and triangle to create a mask.

Since you have single color only, you make a small patch around center and get the avg value of RGB pixels there. Assume you have center at (100,100) and its a circle with radius 20 pixel. create patch of size say 10 X 10, with center at (100,100) and find average value to R,G and B values in this patch.

for red R ~ 255 G ~0 and B~0

for green R ~ 0 G ~255 and B~0

for blue R ~0 G ~0 and B~255

Note: opencv stores value as BGR, not RGB

Garvita Tiwari
  • 584
  • 2
  • 12
1

For finding the shape of a particular contour we can draw a bounded rectangle around the contour. Now we can compare the area of contour with the area of bounded rectangle. If area of contour is equal to half the area of bounded rectangle the shape is a triangle. If the area of contour is less that area of bounded rectangle but is greater than half the area of bounded rectangle then its a circle.

Note: This method is limited to regular triangle and circle. this doesnt apply to polygons like hexagon,heptagon etc.

Ankit Prasad
  • 31
  • 1
  • 5
  • That is a really clever use of a bounding box. Have not thought about using it this way! Thumbsup! – alecxe Dec 10 '19 at 14:43