Python libraries failed for detailed image comparison between two shifted images captured using webcam

 ̄綄美尐妖づ 提交于 2020-01-02 21:50:11

问题


I want to have detailed image comparison which are captured using webcam.

I tried OpenCV and other Python libraries for image comparison which work good when I do any digital change in image that is changes done on image using PC (using Paint).

But when I do any change in image physically using pen or any other object and capture the image with a webcam, then the same library is unable to detected the change done on the image.

Factors which leads for such issue:

  1. camera (I am using Logitech c310)
  2. External noise (I am capturing image under LED tube light)
  3. While changing the design it may shift a bit, which is displayed as difference as well.

My code :

from skimage.measure import compare_ssim
import argparse
import imutils
import cv2
import numpy as np

# load the two input images
imageA = cv2.imread('./t_0.png')
cv2.imwrite("./test/org.jpg", imageA)
# imageA = cv2.medianBlur(imageA,29)
imageB = cv2.imread('./t_1.png')
cv2.imwrite("./test/test.jpg", imageB)
# imageB = cv2.medianBlur(imageB,29)

# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

##########################################################################################################

difference = cv2.subtract(grayA,grayB)    
result = not np.any(difference)
if result is True:
    print ("Pictures are the same")
else:
    cv2.imwrite("./test/open_cv_subtract.jpg", difference )
    print ("Pictures are different, the difference is stored.")

##########################################################################################################

diff = cv2.absdiff(grayA, grayB)
cv2.imwrite("./test/absdiff.png", diff)

##########################################################################################################

grayB=cv2.resize(grayB,(grayA.shape[1],grayA.shape[0]))
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))

#########################################################################################################

thresh = cv2.threshold(diff, 25, 255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
#s = imutils.grab_contours(cnts)
count = 0
# loop over the contours
for c in cnts:
    # images differ
    count=count+1
    (x, y, w, h) = cv2.boundingRect(c)
    cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
    cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)

##########################################################################################################

print (count)
cv2.imwrite("./test/original.jpg", imageA)
# cv2.imshow("Modified", imageB)
cv2.imwrite("./test/test_image.jpg", imageB)
cv2.imwrite("./test/compare_ssim.jpg", diff)
cv2.imwrite("./test/thresh.jpg", thresh)
cv2.waitKey(0)
  • I don’t want to use cv2.medianBlur as this will lesser the image quality.

  • there is no need to resize or crop image as all images captured using webcam will be of same size.

  • Environment will always remain same for image capture only the design will change with minor changes (such as small dots over design).

Image 1:

Image 2:

Resulted Image :

It was able to find 1000s of differences:

absdiff image (opencv) :

expected output :

It is not able to detect detailed required difference.

Can any one help me with Python code or library for detail image which can detect the changes done on image physically to comparison two images mention as above.

There are many question which was matching my requirements but none of then compare the image which are captured using a webcam with the required result.

  1. How to archive a constant environment for image capture?
  2. How to have detail image comparison (small pin point dot)?
  3. which camera I need to use?
  4. What type if light I need to use to create a bright and constant environment?
  5. Is there any other way, any other programming language or library to do this?

Do help me for find the solution using Python.

来源:https://stackoverflow.com/questions/54590050/python-libraries-failed-for-detailed-image-comparison-between-two-shifted-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!