繁体   English   中英

opencv清晰边界的提取图像

[英]opencv sharply bounded extracted image

我能够提取两个图像之间的差异(原始图像的边缘不清晰)。 我正在尝试将图像转换为具有尖锐四边形边缘的图像(中间步骤转换为最终图像)。 我在cv2和numpy中使用python 2.7。 我现在的主要问题是,我不知道如何对其进行编程以生成最终图像(因此,如何剪切不清晰的边缘并生成具有锐利边缘的最终图像)。

请,如何进行这种图像处理?

原始图片 在此处输入图片说明

中间步骤 在此处输入图片说明

最终影像 在此处输入图片说明

附加图片: 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

首先我们找到平滑的轮廓,然后使用approxPolyDP其拉直为四边形:

import cv2
import numpy as np

img = cv2.imread(r'c:\TEMP\so57564249.jpg')

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold and shrink mask
_, thresh = cv2.threshold(img_gray, 10, 255, cv2.THRESH_BINARY)
thresh = cv2.erode(thresh, np.ones((10,10), np.uint8), iterations=1)

# find countour
contours,_ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]

# straighten contour to quadrilateral
epsilon = 0.001*cv2.arcLength(cnt,True)
while True:
   epsilon += epsilon
   approx = cv2.approxPolyDP(cnt,epsilon,True)
   if len(approx) <= 4:
       break

# output
thresh[:,:] = 0
thresh = cv2.fillPoly(thresh, [approx], 255)
img = cv2.bitwise_and(img, img, mask=thresh)
cv2.polylines(img, [approx], True, (0,255,0), 2)

在此处输入图片说明

选择内核大小10来消除图像的模糊边缘几乎是任意的。 您可能需要调整它。 要显示其效果,您可以添加

cv2.polylines(img, [cnt], True, (0,0,255))

在最后一行之后绘制初始贴图图像的平滑轮廓。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM