Remove Shadows Between Objects

asked 2018-10-15 07:51:56 -0600

danieltak gravatar image

updated 2020-10-08 10:10:54 -0600

When making a program to count seeds, the contrast is poor on the image: And a Backlight isn't possible.

Seeds

Tried the following WaterShed Algorithms:

Without success, due to the poor contrast and shadow between the Seeds.

To improve remove the shadow between the seeds, GrabCut was tried.

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

img = cv2.imread("C:\\image\\img.bmp")

mask = np.zeros(img.shape[:2],np.uint8)

bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

rect = (0,0,1023,767)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)

mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
cv2.imshow('Imagem', img)
cv2.waitKey(0)
cv2.imwrite('C:\\Users\\Desktop\\test\\result.bmp', img)
plt.imshow(img),plt.colorbar(),plt.show()

And the following result:

Result

EDIT:

With the code:

import numpy as np
import cv2
from matplotlib import pyplot as plt
from scipy.ndimage import label
import imutils


def show_image(name, img, vf = False):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    if vf:
        plt.imshow(img),plt.colorbar(),plt.show()

img = cv2.imread("C:\\image\\seeds.bmp")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# show_image('Imagem Inicial', img, True)

# https://stackoverflow.com/questions/44752240/how-to-remove-shadow-from-scanned-images-using-opencv
dilated_img = cv2.dilate(img, np.ones((13,13), np.uint8))
# show_image('Dilated', dilated_img)

bg_img = cv2.medianBlur(dilated_img, 7)
# show_image('Median Blur', bg_img)

diff_img = 255 - cv2.absdiff(img, bg_img)
# show_image('Diff', diff_img)

_, img_bin = cv2.threshold(diff_img, 128, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
show_image('Bin1', img_bin)
cv2.imwrite('C:\\image\\bin.bmp', img_bin)

The following image was processed:

Binary

However, the Watershed Algo or Blob Count is still not working.

Question

It wasn't possible to remove the shadows between the seeds, to improve the performance of the watershed algorithm. Is there a better way to remove the sadhows between objects?

edit retag flag offensive close merge delete