python - 如何获取图像中的边框像素或删除区域

标签 python image image-processing computer-vision

假设我有下面的图像,其中有一个被删除的区域,现在我想知道这个区域的边框像素,我如何在Python中实现它? The image with one area being erased

最佳答案

Emm,我就直接把代码放下来,不运行,你稍后可以自己尝试一下。

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

关于python - 如何获取图像中的边框像素或删除区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64724189/

相关文章:

python - python google app engine中的断开链接图像

python - 避免在每个页面上运行 GeoIP

c# - 自动对焦例程检测非常小的模糊差异

html - 设置 img 边框以推送列表中的下一个图像

python - 如何使用带有图像的边界框进行多标签图像训练?

python - 如何请求 HTML 中多重选择的所有元素?

python - python/django 中巨型 heredoc 的上下文替代

python - 为什么我的代码无法访问 ENTER 或 STRAIGHT 文件夹或 .png 文件

Haskell 图像处理库?

python - 将 ffprobe 与 Python 一起使用时无法识别文件类型