0

I'm working on a firing simulator project. I'm using IP camera to capture a screen with the bullet holes. The problem I'm facing is getting only new bullet holes. I tried SSIM method to achieve that:

before = cv2.imread('before.png')
after = cv2.imread('after.png')

# Convert images to grayscale
before_gray = cv2.cvtColor(before, cv2.COLOR_BGR2GRAY)
after_gray = cv2.cvtColor(after, cv2.COLOR_BGR2GRAY)

# Compute SSIM between two images
(score, diff) = compare_ssim(before_gray, after_gray, full=True)
# The diff image contains the actual image differences between the two images
# and is represented as a floating point data type in the range [0,1] 
# so we must convert the array to 8-bit unsigned integers in the range
# [0,255] before we can use it with OpenCV
diff = (diff * 255).astype("uint8")

# Threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]

# The largest contour should be the new detected difference
if len(contour_sizes) > 0:
    largest_contour = max(contour_sizes, key=lambda x: x[0])[1]
    x,y,w,h = cv2.boundingRect(largest_contour)
    cv2.rectangle(before, (x, y), (x + w, y + h), (36,255,12), 2)
    cv2.rectangle(after, (x, y), (x + w, y + h), (36,255,12), 2)

cv2.imshow('before', before)
cv2.imshow('after', after)
cv2.imshow('diff',diff)
cv2.waitKey(0)

And I get this result. My before and after images were same (with little motion because of air on screen) but SSIM method is always detecting change in same images.

My first image is:
enter image description here

Second image is:
enter image description here

And the result I got after processing is enter image description here

Are there any other methods of getting new difference? I tried applying Canny method on image but it's not working as expected.

Georgy
  • 12,464
  • 7
  • 65
  • 73
WatchMyApps Lab
  • 113
  • 1
  • 7
  • difference image if the camera is very static and lighting conditions don't vary. – Micka May 03 '19 at 11:57
  • I want to neg-late the lighting, and want to get only new hole, can you tell me any other method except ssim? – WatchMyApps Lab May 03 '19 at 12:37
  • 1
    SSIM is a measure of difference that tries to mimic how we humans perceive image quality. It is totally not suited for this purpose you use it for. You need to register the images first, then do a simple pixel comparison. – Cris Luengo May 03 '19 at 12:51
  • can you extract all the holes with simple thresholding? – Micka May 03 '19 at 12:54
  • @Micka Yes i can extract all hole using canny method but not working as expected because it returns different values like sometime 20, 21, 19 like this so this is not good for finding only new contour – WatchMyApps Lab May 03 '19 at 13:42
  • @Micka can I use HoughCircles method on this, i don't think there is perfect circles on images of bullet. – WatchMyApps Lab May 03 '19 at 13:47

0 Answers0