Search code examples
pythonopencvimage-processingimage-segmentation

How do you segment the darker spots from the blurry gray corners?


I'm trying to segment the dark grayish spots from the blurry gray areas on the corner, I did binary thresholding and morphological operations and it works great from the blobs in the middle, but the corners I'm having a bit of trouble.

black and white blob image

# Binary Thresholding
ret,threshImg = cv2.threshold(denoiseImg, 220, 255,cv2.THRESH_BINARY)
threshImg = cv2.bitwise_not(threshImg)

# Morphological Operation
# Initialization of kernel size
kernel2 = np.ones((2,2), np.uint8)
kernel5 = np.ones((5,5), np.uint8)

# Morphological Dilation
dilationImg = cv2.dilate(threshImg, kernel2, iterations = 1) #

# Morphological Closing
closingImg = cv2.morphologyEx(dilationImg, cv2.MORPH_CLOSE, kernel5)  # uses closing to fill gaps in the foreground
closingImg = cv2.bitwise_not(closingImg)

This is the result. segmented blob


Solution

  • You can do division normalization in Python/OpenCV to mitigate some of that issue. You basically blur the image and then divide the image by the blurred version.

    Input:

    enter image description here

    import cv2
    import numpy as np
    
    # read the image
    img = cv2.imread('dark_spots.jpg')
    
    # convert to gray
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    # blur
    smooth = cv2.GaussianBlur(gray, None, sigmaX=10, sigmaY=10)
    
    # divide gray by morphology image
    division = cv2.divide(gray, smooth, scale=255)
    
    # save results
    cv2.imwrite('dark_spots_division.jpg',division)
    
    # show results
    cv2.imshow('smooth', smooth)  
    cv2.imshow('division', division)  
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Results:

    enter image description here