【发布时间】:2020-12-06 23:47:49
【问题描述】:
def find_border(self):
print("Start capturing the border")
ret, thresh = cv2.threshold(self.__grayScaledImage, 250, 255, 0)
contours = cv2.findContours(thresh.astype(np.uint8), cv2.RETR_TREE,
cv2.CHAIN_APPROX_NONE)[-2]
# There might be multiple are with 255. then you need to find the index of the largest contour
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
border = contours[max_index]
border = border.reshape(-1, border.shape[2])
for i, j in border:
if i >=0 and i < self.__image.shape[0] and j >= 0 and j < self.__image.shape[1]:
self.__image[i, j] = [255, 0, 0]
print("Finish capturing the border")
# cv2.drawContours(self.__image, border, -1, (255, 0, 0), 1)
plt.imshow(self.__image)
plt.show()
border = border.reshape(-1, border.shape[2])
return border
我有上面的代码 sn-ps 来获取图像的边框像素,但是当我尝试两种方法来可视化边框时:使用cv2.drawContours 或在像素上标记红点作为边框像素。
drawContours 给了我一个合理的输出,但是点标记方法生成了一个旋转的轮廓。
后来我检查了边框像素,发现它们实际上是旋转后反映的点。
我这里有
self.__grayScaledImage = cv2.cvtColor(self.__image, cv2.COLOR_RGB2GRAY)
【问题讨论】:
标签: image-processing graphics computer-vision