user3786219
user3786219

Reputation: 217

Spot segmentation on grayscale images

I am new to image processing and have the following question: how to detect black spots and areas on the images below?

enter image description here

enter image description here

The problem I am facing is that the black areas do not have sharp well-defined edges and image segmentation algorithms I have looked up so far assume smooth edges.

EDIT: Below is the image I would like to see which is also a response to Ian Chu who suggested a thresholding method, I use the user's answer to clarify my question enter image description here

The brown areas are obtained by Ian Chu using thresholding. I would like also to mark small black spots around those areas. I marked them by orange ( sorry for the sloppy drawings). I hope that my question is now a little bit clearer.

Upvotes: 2

Views: 245

Answers (1)

Ian Chu
Ian Chu

Reputation: 3143

You can use Otsu's to automatically choose the threshold, but you lose the ability to specifically choose what you want to mask.

enter image description here

enter image description here

import cv2
import numpy as np

# mark holes
def mark(img):
    # crop margins
    margin = 5;
    img = img[margin:-margin,margin:-margin];
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY);

    # threshold
    gray = cv2.GaussianBlur(gray, (5,5), 0);
    _, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU);
    mask = cv2.bitwise_not(mask);

    # redraw
    img[mask == 255] = (0,0,100);
    return img;

# load image
image1 = cv2.imread("spots1.png");
image2 = cv2.imread("spots2.png");

# mark image
image1 = mark(image1);
image2 = mark(image2);

# show
cv2.imshow("one", image1);
cv2.imshow("two", image2);
cv2.waitKey(0);

Upvotes: 5

Related Questions