如何一次在列表中打印一个元素,而不是继续前进?
如您在图像中所看到的,我正在尝试实现预期的行为,但以某种方式无法做到。我相信我编码的循环来自循环。
我尝试了休息,但这不是我想要的,因为它一旦绘制了第一个轮廓,它就会打破循环。
对于您的信息,轮廓位于3D列表中。
第一轮廓:[[[[125 300]] [[124 301]] [[124 302]] [[124 303]] [[124 304]] [[125 304]] [[125 304]] [[126 304] [[127 304] [[127 304] ]] [[128 304]] [[129 304]] [[130 304]] [[131 304]] [[132 304]] [[133 304]] [[132 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()
As you can see in the image, I'm trying to achieve the expected behavior but somehow cannot make it. I believe the error is come from for loop that I coded.
I tried with break, but it's not what I want since it break out the loop once it draws the first contour.
For you info, the contour is in 3D list.
First contour: [[[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]]]
Does it relate to python queue ?
Anyone has idea on this ?
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次通过循环创建一个新的
black_mask
,而不是在与先前迭代相同的掩码上绘制。Create a new
blank_mask
each time through the loop, rather than drawing on the same mask as the previous iteration.