首页 > 解决方案 > 去除点和线等图像噪声

问题描述

我是 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)

这是原始图像:

在此处输入图像描述

阈值后:

在此处输入图像描述

标签: pythonopencv

解决方案


如果您强调的 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 检测和绘制轮廓?


推荐阅读