Tarcisiofl
Tarcisiofl

Reputation: 143

How to remove shadows from foreground after segmentation?

I am developing an algorithm in Python, which is supposed to identify the area of a leaf that contains spots to report the severity of a disease.

Therefore to be able to achieve the goal, I need to segment the image on foreground (leaf) and background.

During my research, I found out about LeafSnap (State of the Art) and follow the paper to segment the leaf on the image using OpenCV Expectation Maximization, which is trained using S and V form HSV color space; however, it still returns some false positives due to reflection or shadow.

Segmentation Issues

So, I'm trying to figure out a way to avoid or decrease the incidence of false positives. Any hint on it?

Original Images

Upvotes: 2

Views: 4999

Answers (1)

nathancy
nathancy

Reputation: 46610

Here's an approach using color segmentation with cv2.inRange() to remove the shadows. The idea is to convert the image into HSV format and define a lower and upper color range. Next we perform contour filtering to extract the largest contour, draw this onto a new blank mask, and perform a bitwise-and operation to get our result.


Using these screenshotted segmentation images as input (left), here's the result (right)

Code

import numpy as np
import cv2

image = cv2.imread('1.png')
blank_mask = np.zeros(image.shape, dtype=np.uint8)
original = image.copy()
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([18, 42, 69])
upper = np.array([179, 255, 255])
mask = cv2.inRange(hsv, lower, upper)

cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
for c in cnts:
    cv2.drawContours(blank_mask,[c], -1, (255,255,255), -1)
    break

result = cv2.bitwise_and(original,blank_mask)

cv2.imshow('result', result)
cv2.waitKey()

Color segmentation HSV code to determine the lower/upper color threshold range

import cv2
import sys
import numpy as np

def nothing(x):
    pass

# Create a window
cv2.namedWindow('image')

# create trackbars for color change
cv2.createTrackbar('HMin','image',0,179,nothing) # Hue is from 0-179 for Opencv
cv2.createTrackbar('SMin','image',0,255,nothing)
cv2.createTrackbar('VMin','image',0,255,nothing)
cv2.createTrackbar('HMax','image',0,179,nothing)
cv2.createTrackbar('SMax','image',0,255,nothing)
cv2.createTrackbar('VMax','image',0,255,nothing)

# Set default value for MAX HSV trackbars.
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)

# Initialize to check if HSV min/max value changes
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

img = cv2.imread('1.png')
output = img
waitTime = 33

while(1):

    # get current positions of all trackbars
    hMin = cv2.getTrackbarPos('HMin','image')
    sMin = cv2.getTrackbarPos('SMin','image')
    vMin = cv2.getTrackbarPos('VMin','image')

    hMax = cv2.getTrackbarPos('HMax','image')
    sMax = cv2.getTrackbarPos('SMax','image')
    vMax = cv2.getTrackbarPos('VMax','image')

    # Set minimum and max HSV values to display
    lower = np.array([hMin, sMin, vMin])
    upper = np.array([hMax, sMax, vMax])

    # Create HSV Image and threshold into a range.
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    output = cv2.bitwise_and(img,img, mask= mask)

    # Print if there is a change in HSV value
    if( (phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
        print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
        phMin = hMin
        psMin = sMin
        pvMin = vMin
        phMax = hMax
        psMax = sMax
        pvMax = vMax

    # Display output image
    cv2.imshow('image',output)

    # Wait longer to prevent freeze for videos.
    if cv2.waitKey(waitTime) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

Upvotes: 6

Related Questions