【问题标题】:Getting coordinates of the edges of the box inside the image in python在python中获取图像内框边缘的坐标
【发布时间】:2022-09-23 05:17:37
【问题描述】:

我正在尝试获取附加图像中框边缘的坐标[x, y]

这是我用来获取边缘坐标的图像:

我发现很难获得。任何人,请帮助我获得坐标。

image= Image.open(r\"C:/Users/LikithP/OneDrive - Ennoventure Inc/Documents/Projects/Gold_Bar/finding_corner_points/mask_images/enc-1.jpg\")

numpy_data=np.array(image)
img = numpy_data[:,:,0]
_, th = cv2.threshold(img, img.mean(), 255, cv2.THRESH_BINARY_INV)
th = cv2.morphologyEx(th, cv2.MORPH_CLOSE, np.ones((3,3)))

x1, y1 = 0, 0
y2, x2 = th.shape[:2]

while np.all(th[:,x1]==255):
    x1 = x1+1
while np.all(th[:,x2-1]==255):
    x2 = x2-1
while np.all(th[y1,:]==255):
    y1 = y1+1
while np.all(th[y2-1,:]==255):
    y2 = y2-1
cv2.imwrite(\"image.jpg\",image[y1:y2-1,x1:x2-1])

这给出了错误TypeError: \'JpegImageFile\' object is not subscriptable

  • 看起来像一个错字。最后一行,在 cv2.imwrite() 中,您索引对象而不是应该是 img 而不是图像的实际图像。

标签: python numpy


【解决方案1】:

尝试引用 img 而不是 image。您最初尝试索引Image 对象,而不是img 中的实际图像数据:

cv2.imwrite("image.jpg", img[y1:y2-1,x1:x2-1])

【讨论】:

  • 谢谢 -Jason Chia,但它会提供完整的输入图像,我只想要图像的黑色部分,并且在矩形形式的边缘没有白色像素。
猜你喜欢
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 2013-06-29
相关资源
最近更新 更多