1

I have the following original image: Original_image

I want to contour the dark brownfield and calculate its area.

Full code so far:

image=cv2.imread(file)

blurred_frame = cv2.GaussianBlur(image, (5, 5), 0)
hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)

brown_lo=np.array([10,0,0])
brown_hi=np.array([20,150,150])

mask=cv2.inRange(hsv,brown_lo,brown_hi)

contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

for contour in contours:
        cv2.drawContours(image, contour, -1, (0, 255, 0), 3)

show(image)
show(mask)

But I do not get the filled field.

enter image description here

I have received a much better image after the comment. How do I calculate the area of the field now?

Gabriele
  • 737
  • 2
  • 8
  • 20
  • 1
    If you want to detect a colour, it is not an ideal strategy to immediately discard the colour and go to greyscale. Have a look here... https://stackoverflow.com/a/50215020/2836621 – Mark Setchell Apr 23 '20 at 09:46
  • Try summing the pixels in the mask... that will tell you how many brown pixels you have. **Numpy** `np.sum()` is good for summing... – Mark Setchell Apr 23 '20 at 09:58
  • I need the whole area surrounded by red now. Summing pixels would only give me the outline, right? – Gabriele Apr 23 '20 at 10:01
  • 1
    Oh, I see. You could use a *"floodfill"* to fill the area and then count. And you can look at **OpenCV** `findContours()` or **scikit-image** `measure` https://scikit-image.org/docs/dev/api/skimage.measure.html – Mark Setchell Apr 23 '20 at 10:08

0 Answers0