Ask Your Question
0

Defect Detection using OpenCV

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

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

Comments

2

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

berak gravatar imageberak ( 2018-04-26 00:59:51 -0600 )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)

try:
    hierarchy = hierarchy[0]
except:
    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 -0600 )edit

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

Ranganath gravatar imageRanganath ( 2018-04-26 04:43:30 -0600 )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 -0600 )edit

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

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

Hi Steven,

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

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-05-15 05:55:00 -0600

updated 2019-05-17 22:27:33 -0600

supra56 gravatar image

Hi Ranganath try the following code:

import cv2
im = cv2.imread("image.jpg", 0)
im1 = cv2.imread("image1.jpg", 0)

diff = cv2.absdiff(im, im1)
ret, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = cv2.cvtColor(im1, cv2.COLOR_GRAY2BGR)

try:
    hierarchy = hierarchy[0]
except:
    hierarchy = []

for contour in contours[:-1]:
    (x, y, w, h) = cv2.boundingRect(contour)
    area = cv2.contourArea(contour)
    if area>100:
        cv2.rectangle(im1, (x, y), (x+w, y+h), (255, 0, 0), 3)

cv2.imshow("mask", im1)
cv2.imshow("thresh", thresh)
cv2.waitKey(0)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-04-26 00:45:54 -0600

Seen: 4,485 times

Last updated: May 17 '19