E
E
ElemAnybody2019-09-25 11:15:26
Python
ElemAnybody, 2019-09-25 11:15:26

Python. Determining the area of ​​a figure found using OpenCV?

A document is given (let's say that it is printed, and not drawn as I have here) 5d8b1e9ee1924510025272.jpeg
The code is written in such a way that it finds the contour of the object's border, but I cannot take the area or points.
Here is the result of what I managed to do. 5d8b1f247d3f4599751454.png
I need to calculate the area of ​​\u200b\u200bthis figure ... The first thing that came to my mind was the Peak formula, but in order to do it, you need to build a cell with a size of 1 cm on an A4 sheet (accuracy is not as important as the result) . Further, using the formula S = L + (B / 2) -1, it will be possible to calculate the area of ​​\u200b\u200bthe figure, where L are points inside the figure, and B are points on the contour of the figure.
As I understand it, you can somehow calculate the points inside the object through the cv.floodfill function, but I could not master it. Who can write this code for me? I already have no idea, I just started to study OpenCV (3.7), but there are very few such instructions, I only find a theory that does not relate to my task in any way

#мой код
import numpy as np
import cv2 as cv
import imutils
import cv2

# параметры цветового фильтра
hsv_min = np.array((0, 54, 5), np.uint8)
hsv_max = np.array((187, 255, 253), np.uint8)
img = cv.imread("C:\\45\\000.jpg")
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV )
# меняем цветовую модель с BGR на HSV
thresh = cv.inRange(hsv, hsv_min, hsv_max )
# применяем цветовой фильтр
# ищем контуры и складируем их в переменную contours
contours, hierarchy = cv.findContours(thresh.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
#попытка нарисовать хотябы линию на картинке (не работает)
c = max(contours, key = cv.contourArea)
M = cv2.moments(c)
if M["m00"] != 0:
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    print (cX,cY)
else:
    cX, cY = 0,0
    print (cX,cY)

# отображаем контуры поверх изображения
cv.drawContours(img, contours, -1, (255, 0, 0), 2, cv.LINE_AA, hierarchy, 0)
cv.imshow('contours', img)
cv.drawContours(img, contours, -1, (255, 0, 0), 2, cv.LINE_AA, hierarchy, 2)
# выводим итоговое изображение в окно
cv.imshow('All_con', img)
cv.imshow('thresh', thresh)
cv.waitKey()
cv.destroyAllWindows()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
o5a, 2019-09-25
@ElemAnybody

It's already calculated, it's M["m00"].

D
Developer, 2019-09-25
@samodum

There are many ways.
If the polygon is vectorized, i.e., the coordinates of the ends of all its edges are known, then the polygon is triangulated into a set of triangles (it is first divided into a set of convex polygons) and its area is calculated for each.
If it is a raster, then yes, it is necessary to calculate the area of ​​the filled figure. Here the main problem is in determining the starting point, which will definitely lie inside the polygon.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question