Gulzar
Gulzar

Reputation: 27946

How to get rid of points in bw image and be left with connected pixels?

I have the following image (image is fully black, don't mind the border)

enter image description here

I want to be left only with the (1 pixel-wide) line, and be rid of all the points (some of which are actually a couple of pixels).

I know this is rather easy, but i am out of ideas for now :(

What I have tried:

a morphology operation for opening (erode + dilate) - left me with either nothing or the same

blob detection - doing this would maybe work after dilation, but it crashes with no error (windows exception), and I have no time to debug opencv.


would love any idea that works.

Upvotes: 0

Views: 442

Answers (3)

Karol Żak
Karol Żak

Reputation: 2406

[UPDATE] - Didn't notice it needs to be 1x1 pixels line

Like you said - I ignored the border

import cv2
import numpy as np
import matplotlib.pyplot as plt


img = cv2.imread("dumpster/eLIRj.png")
cv2.imwrite("dumpster/masking/1.png", img)

# convert to grayscale (single channel)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite("dumpster/masking/2.png", gray)

# otsu thresholding
otsu = cv2.threshold(
    gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
cv2.imwrite("dumpster/masking/3.png", otsu)

# erode with horizontal and vertical lines (2x1 pixels)
kernel = np.ones((2,1), dtype=np.uint8) 
erosion = cv2.erode(otsu, kernel, iterations = 1)
kernel = np.ones((1,2), dtype=np.uint8) 
erosion = erosion + cv2.erode(otsu, kernel, iterations = 1)
cv2.imwrite("dumpster/masking/4.png", erosion)

# dilate whats left with 3x3 kernel
kernel = np.ones((3,3), dtype=np.uint8)
dilated = cv2.dilate(erosion, kernel, iterations=1)
cv2.imwrite("dumpster/masking/5.png", dilated)

# create binary mask and apply it on thresholded, grayscale image
mask = dilated / 255
final = otsu * mask
cv2.imwrite("dumpster/masking/6.png", final)

Original image:
enter image description here

Dilated:
enter image description here

Final:
enter image description here

Upvotes: 1

antoine
antoine

Reputation: 1873

I would go with a custom erode function that count the non black pixel in a 3x3 window, if there is one, the pixel value is untouched, if there is no, the pixel will become black.

In C++ it looks like (EDIT : I've added a blur based method which is much more faster):

#include <opencv2/opencv.hpp>
#include <chrono>

int main(void)
{
  cv::Mat input = cv::imread("salt.png", cv::IMREAD_GRAYSCALE);
  imshow("input", input);

  cv::Mat thresh;
  // threshold the input just to be sure to get only white or black pixels.
  cv::threshold(input, thresh, 128, 255, cv::THRESH_BINARY);

  cv::Mat mask = cv::Mat::ones(3,3,CV_8UC1);
  mask.at<unsigned char>(1,1)=0;
  cv::imshow("mask", mask);
  cv::Mat output1 = thresh;
  cv::Mat output2 = thresh;

  { // first method : iterate over all pixels (very slow but didactic)
    auto start = std::chrono::high_resolution_clock::now();
    for(int r=1; r<thresh.rows-1; r++)
    {
      for(int c=1; c<thresh.cols-1; c++)
      {
        cv::Rect roi(c-1,r-1,3,3);
        cv::Mat window = thresh(roi);
        cv::Scalar val = cv::sum(window.mul(mask));
        if(val[0]==0)
          output1.at<unsigned char>(r,c) = 0;
      }
    }
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end-start);
    std::cout << "iterating method: " << duration.count() << std::endl; // 116965µs

    cv::imshow("output", output1);
  }

  { // optimized method : use blur and threshold
    cv::Mat blur, output2=thresh, thresh2;
    auto start = std::chrono::high_resolution_clock::now();

    cv::blur(thresh, blur, cv::Size(3,3));

    cv::threshold(blur, thresh2, 60, 255, cv::THRESH_BINARY);
    output2.mul(thresh2);
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end-start);
    std::cout << "blur method: " << duration.count() << std::endl; // 378µs

    cv::imshow("blur", output2);
  }

  auto diff = (output1 - output2) * 255;
  std::cout << "diff norm: " << cv::norm(diff) << std::endl; // 0

  while(1)
  {
    if(27 ==cv::waitKey(0))
      break;
  }
  return 0;
}

Output :

Processed Image

Upvotes: 1

Gulzar
Gulzar

Reputation: 27946

I ended up doing the following, just to get done with it. I am sure something smarter could have been done:

    thread_defect_mask_clean = thread_defect_mask_noisy.copy()
    ret, connected_components_labels = cv2.connectedComponents(thread_defect_mask_noisy.astype('uint8'), connectivity=8)
    for label in range(1, ret):
        label_count = np.count_nonzero(label == connected_components_labels)
        if label_count < self._min_thread_defect_size:
            thread_defect_mask_clean[label == connected_components_labels] = 0

This counts the size of each connected component, and enforces a threshold (5 here)

Upvotes: 1

Related Questions