繁体   English   中英

如何一次打印列表中的一个元素而不结转前一个元素?

[英]How to print one element in a list at a time and not carry forward the previous one?

在此处输入图像描述

正如您在图像中看到的那样,我正在尝试实现预期的行为,但不知何故无法实现。 我相信错误来自我编写的 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()

每次循环创建一个新的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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM