извлекать ширину строка за строкой контура в opencv-python - PullRequest
0 голосов
/ 03 июня 2019

Эй, ребята, у меня есть код, чтобы найти и нарисовать контур вокруг формы (Drop Shape), теперь мне нужно найти ширину каждой строки в этом контуре

enter image description here

я использовал нарезку в массиве numpy, но это не может помочь мне

ret, self.frame = cap.read()

#do prosecc on  raw image frame
gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
blur=cv2.GaussianBlur(gray, (7, 7), 0)
flag, thresh = cv2.threshold(blur,128,255 , cv2.THRESH_BINARY)
edged=cv2.Canny(thresh,50,100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)

#find contours in edged capture, then grab the largest one
contours,hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

#find biggest countour based on area and draw contour
c=max(contours,key=cv2.contourArea)
cv2.drawContours(self.frame, [c], 0, (0,255,0), 1)

#here i need a loop for find width of every row of my contour

1 Ответ

0 голосов
/ 03 июня 2019

Попробуйте посмотреть на каждый ряд.Найдите первое ненулевое значение и последние ненулевые значения.Это будут левая и правая стороны капли воды.

# Draw the contours on a seperate image
contours_only = np.zeros_like(img)
cv2.drawContours(contours_only, [c], 0, (0,255,0), 1)

gray = cv2.cvtColor(contours_only, cv2.COLOR_BGR2GRAY)
start, end = [], []
# Iterate through each row in the image
for row_num in range(img.shape[0]-1):
    # Slice a row from the image
    row = gray[row_num: row_num + 1, :]
    # Find the left side
    left_px = np.argmax(row)
    # Find the right side
    row = np.flip(row)
    right_px = img.shape[1] - np.argmax(row)
    # Draw some of the rows
    if row_num%15 == 0 and left_px != 0 and right_px != 0 :
        cv2.line(img, (left_px, row_num), (right_px, row_num), (255,255,0), 2)

left and right side

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...