【发布时间】:2020-11-07 19:36:29
【问题描述】:
想象一下,我有以下带有擦除区域的图像,现在我想知道该区域的边框像素,如何在 Python 中实现? The image with one area being erased
【问题讨论】:
标签: python image image-processing computer-vision
想象一下,我有以下带有擦除区域的图像,现在我想知道该区域的边框像素,如何在 Python 中实现? The image with one area being erased
【问题讨论】:
标签: python image image-processing computer-vision
emmm我就直接把代码不跑了,以后你自己试试吧。
import numpy as np
import cv2 as cv
im = cv.imread('you_input_image.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
# assume the while area 255.255.255 are what you put manually and you want it removed.
ret, thresh = cv.threshold(imgray, 254, 255, 0)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# 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)
cnt=contours[max_index]
print cnt
# cnt contains all the point in this largest contour
【讨论】:
您可以使用 opencv 来执行此操作。主要使用的函数是cv2.findContours
在下面用红色画出边框。
import cv2
import numpy as np
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
im = plt.imread('uKTss.jpg')
gray = rgb2gray(im)
contours = cv2.findContours(gray.astype(np.uint8),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)[-2]
for contour in contours:
cv2.drawContours(im, contour, -1, (255, 0, 0), 1)
plt.imshow(im)
plt.show()
【讨论】: