python - 从图像中删除最大轮廓

标签 python opencv image-processing

我有这样一张图片

enter image description here

我正在尝试检测并删除此图像中的箭头,以便最终得到一个只有文本的图像。

我尝试了下面的方法,但它不起作用

image_src = cv2.imread("roi.png")
gray = cv2.cvtColor(image_src, cv2.COLOR_BGR2GRAY)
canny=cv2.Canny(gray,50,200,3)
ret, gray = cv2.threshold(canny, 10, 255, 0)
contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
largest_area = sorted(contours, key=cv2.contourArea)[-1]
mask = np.ones(image_src.shape[:2], dtype="uint8") * 255
cv2.drawContours(mask, [largest_area], -1, 0, -1)
image = cv2.bitwise_and(image_src, image_src, mask=mask)

上面的代码似乎给我返回了带有箭头的相同图像。

如何删除箭头?

最佳答案

以下将删除最大的轮廓:

import numpy as np
import cv2

image_src = cv2.imread("roi.png")

gray = cv2.cvtColor(image_src, cv2.COLOR_BGR2GRAY)
ret, gray = cv2.threshold(gray, 250, 255,0)

image, contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros(image_src.shape, np.uint8)
largest_areas = sorted(contours, key=cv2.contourArea)
cv2.drawContours(mask, [largest_areas[-2]], 0, (255,255,255,255), -1)
removed = cv2.add(image_src, mask)

cv2.imwrite("removed.png", removed)

请注意,在这种情况下最大的轮廓将是整个图像,因此它实际上是第二大轮廓。

关于python - 从图像中删除最大轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968339/

相关文章:

python - LSTM RNN 同时预测多个时间步长和多个特征

java - OpenCV - Canny 边缘检测无法正常工作

Python OpenCV 添加的文本质量不佳

image - 对图像进行机器学习时减少特征数量的方法

opencv - 灰度图像到彩色

python - Azure 语音到文本 0x38 (SPXERR_AUDIO_SYS_LIBRARY_NOT_FOUND)

python - 将数值映射到字符串

python - 为什么 Python set 交集比 Rust HashSet 交集快?

python - 检查两个轮廓是否相交?

image - 识别音乐符号的程序