Distinguish defective grains from an image of grains

Viewed 162

I am trying to classify the grains into defective and non-defective using image processing tools like OpenCV in Python. One of the images looks as shown in the figure.

Food Grains

I first segmented out the grains separately by ranging using HSV space and applied some edge detections algorithms like canny, adaptive threshold but I can find a particular way to resolve this because I recently started exploring and I'm still exploring the power of image processing.

Defective:

Defective_1 canny_1 Defective_2 enter image description here enter image description here enter image description here

Non-Defective:

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

I have two queries:

1) What metrics to use to distinguish the grains, if I use the edge_detected pictures as shown above.

2) Since the edge detection using canny is failed in the third image of Non-Defective ones.Is there any other features that I could rely on.

1 Answers

By example:

import cv2
import numpy as np
img=cv2.imread('YfClv.jpg')
#convert to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#color definition
blue_lower = np.array([100,118,33])
blue_upper = np.array([119,255,160])
#blue color mask (sort of thresholding, actually segmentation)
mask = cv2.inRange(hsv, blue_lower, blue_upper)
se=cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (20, 20))
mask=cv2.dilate(mask, se)
mask=cv2.bitwise_not(mask)
edges = cv2.Canny(img,300,100) #find edges
out=cv2.bitwise_and(edges, mask) #xor for testing
#cv2.imshow('test', out)
cv2.imwrite('bad_seeds.png', out)

Result, inside edges: enter image description here

Related