Ask Your Question
0

Draw difference in countours using reference images

asked 2019-07-12 00:50:55 -0600

varul gravatar image

Hi, I am trying to draw contours from the difference which I have got by comparing 2 reference images but somehow I am not able to draw that difference using draw contours Any suggestion will be very helpful

Code:

import cv2
import numpy as np
import imutils

f = cv2.imread("2.jpg")
s = cv2.imread("1.jpg")

difference = cv2.absdiff(s, f)

grey = cv2.cvtColor(difference,cv2.COLOR_BGR2GRAY)
c,h = cv2.findContours(grey, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

for contour in c:
    area = cv2.contourArea(contour)

    if area > 10:
        print(area)
        c = cv2.drawContours(difference, contour, -1, (0, 255, 0), 3) 
cv2.imwrite("Frame.jpg", c)
#cv2.imshow("difference", difference)
cv2.waitKey(1)
cv2.destroyAllWindows()

using below reference image

1st image

image description

2nd image

image description

edit retag flag offensive close merge delete

Comments

1

you cannot draw colored pixels into a grayscale image

berak gravatar imageberak ( 2019-07-12 01:12:48 -0600 )edit

okay but is there any possibility to draw the contour on these images by any means

varul gravatar imagevarul ( 2019-07-12 01:35:02 -0600 )edit
1

please also remove the c= part in c = cv2.drawContours, since you are overwriting your contours array.

don't reuse variable names easily, typical noob error.

berak gravatar imageberak ( 2019-07-12 01:45:11 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-07-12 01:46:06 -0600

berak gravatar image

use a seperate bgr "drawing" image to visualize the colored contours:

draw_img = cv2.cvtColor(grey, cv2.COLOR_GRAY2BGR)
...
    cv2.drawContours(draw_img, contour, -1, (0, 255, 0), 3) 
...
cv2.imwrite("Frame.jpg", draw_img)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-07-12 00:50:55 -0600

Seen: 102 times

Last updated: Jul 12 '19