Defect Detection using OpenCV

asked 2018-04-26 00:45:54 -0500

Dear Members,

I am trying to detect defect in image by comparing defected image with original one. I have achieved it so far using canny algorithm.

Now i have to fill color to defected area after applying canny algorithm to it.

Kindly let me know for the same.

Thanks in advance.

edit retag flag offensive close merge delete



please show us, what you tried, code, images ...

berak gravatar imageberak ( 2018-04-26 00:59:51 -0500 )edit

Hi Berak,

 Thanks for reply,

I am attaching code:

im = cv2.imread("C:/Original_Image.jpg", 0)
im1 = cv2.imread("C:/Defect_Image.jpg", 0)

diff = cv2.absdiff(im, im1)
#cv2.imwrite("C:\this.jpg", diff)
ret, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
contours, hierarchy, we = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = cv2.cvtColor(im1, cv2.COLOR_GRAY2BGR)

    hierarchy = hierarchy[0]
    hierarchy = []

for contour, hier in zip(contours, hierarchy):
    (x, y, w, h) = cv2.boundingRect(contour)
    cv2.rectangle(mask, (x, y), (x+w, y+h), (255, 0, 0), 3)

cv2.imwrite("C:\mask.jpg", mask)
cv2.imwrite("C:\thresh.jpg", thresh)
Ranganath gravatar imageRanganath ( 2018-04-26 04:39:48 -0500 )edit

im: original image without defect im1: with defect (A scratch on image)

Ranganath gravatar imageRanganath ( 2018-04-26 04:43:30 -0500 )edit

problem what i am facing is i am not able to get rectangle shape on defected areas.

Ranganath gravatar imageRanganath ( 2018-04-26 04:45:26 -0500 )edit

Images ... my guess is your contours are not closed hence incorrect bounding rects

StevenPuttemans gravatar imageStevenPuttemans ( 2018-04-27 07:05:03 -0500 )edit

Hi Steven,

  How to work on that?
Ranganath gravatar imageRanganath ( 2018-04-30 01:02:43 -0500 )edit