如何一次在列表中打印一个元素,而不是继续前进?

发布于 2025-02-11 05:46:43 字数 1483 浏览 2 评论 0原文

如您在图像中所看到的,我正在尝试实现预期的行为,但以某种方式无法做到。我相信我编码的循环来自循环。

我尝试了休息,但这不是我想要的,因为它一旦绘制了第一个轮廓,它就会打破循环。

对于您的信息,轮廓位于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()

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2025-02-18 05:46:43

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

Create a new blank_mask each time through the loop, rather than drawing on the same mask as the previous iteration.

    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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文