伙计们,我有一些代码可以找到并绘制一个形状(Drop Shape)周围的轮廓,现在我需要找到该轮廓中每一行的宽度
我在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
答案 0 :(得分:0)
尝试查看每一行。查找第一个非零值和最后一个非零值。这些将是水滴的左侧和右侧。
# 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)