OpenCV如何平滑轮廓,减少噪音 [英] OpenCV how to smooth contour, reducing noise

查看:591
本文介绍了OpenCV如何平滑轮廓,减少噪音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提取了图像的轮廓,您可以在此处看到:

I extracted the contours of an image, that you can see here:

但是,它有些杂音. 如何消除噪音?我做了一次特写以弄清楚我的意思

However, it has some noise. How can I smooth the noise? I did a close up to make clearer what I want to meant

我使用过的原始图片:

Original image that I've used:

代码:

rMaskgray = cv2.imread('redmask.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
(thresh, binRed) = cv2.threshold(rMaskgray, 50, 255, cv2.THRESH_BINARY)

Rcontours, hier_r = cv2.findContours(binRed,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
r_areas = [cv2.contourArea(c) for c in Rcontours]
max_rarea = np.max(r_areas)
CntExternalMask = np.ones(binRed.shape[:2], dtype="uint8") * 255

for c in Rcontours:
    if(( cv2.contourArea(c) > max_rarea * 0.70) and (cv2.contourArea(c)< max_rarea)):
        cv2.drawContours(CntExternalMask,[c],-1,0,1)

cv2.imwrite('contour1.jpg', CntExternalMask)

推荐答案

尝试升级到OpenCV 3.1.0.在对新版本进行了一些代码修改后,如下所示,我在OpenCV 3.1.0版中进行了尝试,但没有看到您所描述的任何效果.

Try an upgraded to OpenCV 3.1.0. After some code adaptations for the new version as shown below, I tried it out with OpenCV version 3.1.0 and did not see any of the effects you are describing.

import cv2
import numpy as np

print cv2.__version__

rMaskgray = cv2.imread('5evOn.jpg', 0)
(thresh, binRed) = cv2.threshold(rMaskgray, 50, 255, cv2.THRESH_BINARY)

_, Rcontours, hier_r = cv2.findContours(binRed,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
r_areas = [cv2.contourArea(c) for c in Rcontours]
max_rarea = np.max(r_areas)
CntExternalMask = np.ones(binRed.shape[:2], dtype="uint8") * 255

for c in Rcontours:
    if(( cv2.contourArea(c) > max_rarea * 0.70) and (cv2.contourArea(c)< max_rarea)):
        cv2.drawContours(CntExternalMask,[c],-1,0,1)

cv2.imwrite('contour1.jpg', CntExternalMask)

这篇关于OpenCV如何平滑轮廓,减少噪音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆