gpt4 book ai didi

python - 在已知 XY 坐标时绘制像素和轮廓

转载 作者:行者123 更新时间:2023-12-02 17:24:34 25 4
gpt4 key购买 nike

我在 python 中使用 Open CV,我从轮廓和点多边形测试操作中提取了一些点。 (我基本上使用 pointpolygon 测试来检查某些像素是否在轮廓内)。
现在我得到了分数,但我想直观地确认我所拥有的点是否实际上在轮廓内。怎么办?
理想情况下,我想先绘制轮廓,然后绘制提取的像素。
我可以使用 drawcontours 函数绘制轮廓,但是为了绘制这些点,我知道的唯一方法是 plt.scatter,它更多地使用 matplotlib 而不是 opencv。

我处理这个问题的方式可能存在根本性的错误,因为感觉应该有一种更简单的方法来做到这一点,而我却错过了。这是到目前为止我所拥有的代码片段以供引用。

编辑 :根据 Mark Setchell 的建议,添加了一个代码片段,我在其中使用 image[x,y] 将颜色设置为特定像素。但是, (x,y) 在我的情况下是笛卡尔坐标,如果我将它作为图像的像素坐标给出,它就不起作用。

import numpy as np
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('ti.png',1)
plt.imshow(img)

ret, thresh = cv2.threshold(img, 250, 300, cv2.THRESH_BINARY_INV)

greySrc = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
laplacianSrc= cv2.Laplacian(greySrc, cv2.CV_8U, greySrc, ksize = 5)
contours, hierarchy=cv2.findContours(laplacianSrc, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)


indices = np.where(img!= [0])
coordinates = zip(indices[0], indices[1])
pts=list(coordinates)

in_or_out=[cv2.pointPolygonTest(contours[0],point,measureDist=False) for point in pts]
pts_ins_ctr=[x for x, y in zip(pts, in_or_out) if y == 1]
xi=[pts_ins_ctr[i][0] for i in range(len(pts_ins_ctr))]
yi=[pts_ins_ctr[i][1] for i in range(len(pts_ins_ctr))]

img[np.array(pts_ins_ctr)]=np.array([255,0,0],dtype=np.uint8)
plt.imshow(img)

输入图片
enter image description here

输出图像
enter image description here

最佳答案

这个解决了。谢谢@MarkSetchell

for pts in pts_ins_ctr:
img[tuple(reversed(pts))]=[255,0,0]


有两件事情奏效
  • 在循环中分配颜色,而不是尝试一次分配所有颜色。
  • im[y,x]=pix_color (如标记评论)
  • 关于python - 在已知 XY 坐标时绘制像素和轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60181940/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com