0

I have this image:

Image

Here I have an image on a green background and an area marked with a red line within it. I want to calculate the area of the marked portion with respect to the Image.

I am cropping the image to remove the green background and calculating the area of the cropped Image. From here I don't know how to proceed.

I have noticed that Contour can be used for this but the problem is how do I draw the contour in this case.

I guess if I can create the contour and fill the marked area with some color, I can subtract it from the whole(cropped) image and get both the areas.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
neon
  • 1
  • 2

1 Answers1

1

In your link, they use the method threshold with a colour in parameter. Basically it takes your source image and sets as white all pixels greater than this value, or black otherwise (This means that your source image needs to be a greyscale image). This threshold is what enables you to "fill the marked area" in order to make a contour detection possible.

However, I think you should try to use the method inRange on your cropped picture. It is pretty much the same as threshold, but instead of having one threshold, you have a minimum and a maximum boundary. If your pixel is in the range of colours given by your boundaries, then it will be set as white. If it isn't, then it will be set as black. I don't know if this will work, but if you try to isolate the "most green" colours in your range, then you might get your big white area on the top right.

Then you apply the method findContours on your binarized image. It will give you all the contours it found, so if you have small white dots on other places in your image it doesn't matter, you'll only have to select the biggest contour found by the method.

Be careful, if the range of inRange isn't appropriate, the big white zone you should find on top right might contain some noise, and it could mess with the detection of contours. To avoid that, you could blur your image and do some stuff like erosion/dilation. This way you might get a better detection.


EDIT

I'll add some code here, but it can't be used as is. As I said, I have no knowledge in Python so all I can do here is provide you the OpenCV methods with the parameters to provide.

Let's make also a review of the steps:

  1. Binarize your image with inRange. You need to find appropriate values for your minimum and maximum boundaries. What you want to do here is isolate the green colours since it is mostly what composes the area inside your contour. I can't really suggest you something better than trial and error to find the best thresholds. Let's start with those min and max values : (0, 125, 0) and (255, 250, 255)

inRange(source_image, Scalar(0, 125, 0), Scalar(255, 250, 255), binarized_image)

  1. Check your result with imshow

imshow("bin", binarized_image)

  1. If you binarization is ok (you can detect the area you want quite well), apply findContours. I'm sorry I don't understand the syntax used in your tutorial nor in the documentation, but here are the parameters:

    • binarized_mat: your binarized image
    • contours: an array of arrays of Point which will contain all the contours detected. Each contour is stored as an array of points.
    • mode: you can choose whatever you want, but I'd suggest RETR_EXTERNAL in your case.
  2. Get the array with the biggest size, since it might be the contour with the highest number of points (the largest one then).

  3. Calculate the area inside

Hope this helps!

  • Thanks for the help. Appreciate it a lot. I've just begun, so some codes would have been great. – neon Dec 08 '16 at 14:06
  • I have absolutely no experience in Python so I won't be able to help you there :/. All I can do is help you with the computer vision / OpenCV part. – Sonia Seddiki Dec 09 '16 at 09:08
  • @neon I edited my answer with some code, but I'm sorry I can't provide you much more – Sonia Seddiki Dec 09 '16 at 10:57
  • @neon there are plenty of tutorials and books on opencv. most of it is available for free on the internet. – Piglet Dec 09 '16 at 11:11