python - 去除点和线等图像噪声
问题描述
我是 OpenCV 和 Python 的新手,在消除输入图像中的噪声时遇到了问题。我只想提取 WBC 的细胞核,所以我使用加法来突出显示细胞核并使用阈值处理来去除图像中的 RBC。我成功去除了红细胞,但血小板没有去除,边缘出现了一些线条。我还尝试使用膨胀、腐蚀、打开和关闭来对图像进行降噪,但核被破坏了。
这是我的代码:
img = cv2.imread('1.bmp')
img_2 = cv2.imread('1.bmp')
input_img = cv2.addWeighted(img, 0.55, img_2, 0.6, 0)
retval, threshold = cv2.threshold(input_img, 158, 255, cv2.THRESH_BINARY)
threshold = cv2.cvtColor(threshold, cv2.COLOR_BGR2GRAY)
retval2, threshold2 = cv2.threshold(threshold, 0, 255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
blur2 = cv2.medianBlur(threshold2,5)
这是原始图像:
阈值后:
解决方案
如果您强调的 WBC 的核始终是阈值之前最大的轮廓,我建议使用findContours
单独存储它并删除较小的斑点,如下所示:
vector<vector<Point>>contours; //Vector for storing contour
vector<Vec4i> hierarchy;
//Find the contours in the image
findContours(input_img, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
for (int i = 0; i< contours.size(); i++) // iterate through each contour.
{
double a = contourArea(contours[i], false); // Find the area of contour
if (a>largest_area){
largest_area = a;
//Store the index of largest contour
largest_contour_index = i;
// Find the bounding rectangle for biggest contour
bounding_rect = boundingRect(contours[i]);
}
}
Scalar color(255, 255, 255);
// Draw the largest contour using the previously stored index.
Mat dst;
drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy);
我的代码是 C++,但您可以找到 python 示例:如何在 Python 中使用 OpenCV 检测和绘制轮廓?
推荐阅读
- swift - 使用内部闭包时可能存在内存泄漏
- python - 在 flask_restplus RequestParser 中添加多个 json 字段
- javascript - js映射连接值和字符串(意外的令牌错误)
- ios - 在核心数据中,如何在保存时将项目保存到各自的类别父级?
- angular - Mocking a Directive to Test a Component - Angular 8 with jasmine and Karma
- php - 如何在 php 中使用 if(isset($_GET['type']==1){} 编写返回页面
- c# - 将项目引用的 DLL 和 PDB 文件包含到 nuget 包中
- javascript - OrderBy + foreach + dataRef 在 Firestore/Firebase 中不起作用
- linux - 使用 bash 脚本创建短和长 sqlplus 连接 X 时间
- authentication - 在主机名上接受 raspberryPI 上的 ssh 登录,但在本地 IP 上不接受