
正如您在影像中看到的那樣,我正在嘗試實作預期的行為,但不知何故無法實作。我相信錯誤來自我撰寫的 for 回圈。
我嘗試使用break,但這不是我想要的,因為它在繪制第一個輪廓時會中斷回圈。
供您參考,輪廓在 3D 串列中。
第一個輪廓: [[[125 300]] [[124 301]] [[124 302]] [[124 303]] [[124 304]] [[125 304]] [[126 304]] [[127 304 ]] [[128 304]] [[129 304]] [[130 304]] [[131 304]] [[132 304]] [[133 304]] [[132 303]] [[131 302]] [[130 301]] [[129 301]] [[128 300]] [[127 300]] [[126 300]]]
它與python佇列有關嗎?
有人對此有想法嗎?
import cv2
import math
from matplotlib import pyplot as plt
def lines():
# reading image in BGR format
img = cv2.imread("C://Users/ranz/Desktop/1.bmp")
res = cv2.resize(img, (570,610), cv2.INTER_LINEAR)
cnt_point = []
#grayscale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#threshold for black pixel
_, thresh = cv2.threshold(gry,0,255,cv2.THRESH_BINARY_INV)
#find all contours
cnt,_ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
#draw blank mask
blank_mask = np.zeros((thresh.shape[0], thresh.shape[2], 3), np.uint8)
for i in range(len(cnt)):
cv2.drawContours(blank_mask, cnt[i], -1, (0, 255, 0), 1)
print(cnt[i])
cv2.imshow('test',blank_mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
lines()
uj5u.com熱心網友回復:
blank_mask每次回圈都創建一個新的,而不是在與上一次迭代相同的掩碼上繪制。
for i in range(len(cnt)):
blank_mask = np.zeros((thresh.shape[0], thresh.shape[2], 3), np.uint8)
cv2.drawContours(blank_mask, cnt[i], -1, (0, 255, 0), 1)
print(cnt[i])
cv2.imshow('test',blank_mask)
cv2.waitKey(0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/497355.html
上一篇:為什么它永遠保持加載?
