Home>

I want to detect the contour of the issue and calculate the center of gravity.
Please write your question in detail here.
After masking the image, when I tried to detect the contour and calculate the center of gravity, the following error occurred.

areas = np.array (list (map (cv2.contourArea, contours)))
cv2.error: OpenCV (4.4.0) C: \ Users \ appveyor \ AppData \ Local \ Temp \ 1 \ pip-req-build-j8nxabm_ \ opencv \ modules \ imgproc
V_32F || depth == CV_32S) in function'cv :: contourArea'
Corresponding source code
import cv2
import numpy as np
import csv
threshold = 0
lower_S = np.array ([0,0,20])
upper_S = np.array ([180,83,255])
lower_V = np.array ([0,0,94])
upper_V = np.array ([180,255,255])
picture = "026537.jpg"
img = cv2.imread (picture)
# hsv Convert to color space
hsv = cv2.cvtColor (img, cv2.COLOR_BGR2HSV)
#mask processing
img_mask1 = cv2.inRange (hsv, lower_S, upper_S)
img_mask2 = cv2.inRange (hsv, lower_V, upper_V)
img_mask = cv2.bitwise_or (img_mask1, img_mask2)
img_masked = cv2.bitwise_and (img, img, mask = img_mask)
#grayscale conversion
dst = cv2.cvtColor (img_masked, cv2.COLOR_BGR2GRAY)
# Contour detection
image, contours = cv2.findContours (dst, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#Area calculation
areas = np.array (list (map (cv2.contourArea, contours)))
#Calculation of center of gravity
max_idx = np.argmax (areas)
max_area = areas [max_idx]
result = cv2.moments (contours [max_idx])
x = int (result ["m10"]/result ["m00"])
y = int (result ["m01"]/result ["m00"])
coordination = ([])
#CSV file creation
with open ('Cordinations.csv','w') as f:
    writer = csv.writer (f)
    writer.writerow ([x], [y])
    f.close ()
Supplementary information (FW/tool version, etc.)

Language: Python
Working environment: Visual Studio
OpenCV (4.4)