python - OpenCV boundingRect 输出

标签 python opencv

我是 OpenCV 和 Python 的新手,我制作了一个程序来查找面积大于 500 的轮廓并将它们保存到新图像中我按照互联网上的建议使用了 boundingRect,它运行并完成了工作但我得到了图像输出的问题。似乎感兴趣区域附近的噪音也被保存了下来。正如您在下图中看到的,在 ROI 旁边有一些微小的形状。输出对其他图像有好处,只是我想消除这样的噪音。有没有办法消除输出中的这类噪声?

这是我编写的程序的输出: output or the new image

这是输入图像:

enter image description here

最佳答案

用轮廓隐藏

此解决方案使用 cv2.drawContours() 简单地在噪声上绘制黑色轮廓。我通过几次膨胀迭代运行黑白样本图像,按区域过滤轮廓,然后在噪声上绘制黑色轮廓线。我使用阈值功能是因为在最初看似简单的黑白图像中发现了很多微小的噪声。

输入:

enter image description here

代码:

import cv2

thresh_value = 10
img = cv2.imread("cells_BW.jpg")
img = cv2.medianBlur(img, 5)
dilation = cv2.dilate(img,(3,3),iterations = 3)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(T, thresh) = cv2.threshold(img_gray, thresh_value, 255, cv2.THRESH_BINARY)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = [i for i in contours if cv2.contourArea(i) < 5000]
cv2.drawContours(img, contours, -1, (0,0,0), 10, lineType=8)

cv2.imwrite("cells_BW_CLEAN.jpg", img)

输出:

enter image description here

关于python - OpenCV boundingRect 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53127776/

相关文章:

python - OpenCV/Python : read specific frame using VideoCapture

algorithm - haar级联正例图像大小调整

c - 带视频的opencv圆检测

python - 迭代具有不同可能字符的字符串

Python:通过元素计数重建列表

python - 为什么这些列表方法(追加、排序、扩展、删除、清除、反转)返回 None 而不是结果列表?

python - 用表格打印多索引数据框

python - python中跨多个进程的同步

c++ - 关于如何使用英特尔的集成性能原语评估 openCV 的建议?

python - 如何通过单击python标签中的按钮开始录制