0

I am trying to solve a problem where I have to compare one image with a list of images for the similarity.

for fn in image_path_list:
difference = cv2.subtract(image1, fn)

result = not np.any(difference) #if difference is all zeros it will return False

if result is True:
    print("The images are the same")
else:
    cv2.imwrite("result.jpg", difference)
    print("the images are different")

That leads to:

TypeError: src2 is not a numpy array, neither a scalar

What wrong am I doing? (I have to compare image1 with all the images in image_path_list)

GhostCat
  • 137,827
  • 25
  • 176
  • 248
chetan parmar
  • 73
  • 1
  • 7
  • 1
    Welcome to Stack Overflow! Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. But I think your question is still not answerable. **You** should [edit] your question now, to add missing details (see [mcve] ). Feel free to drop me a comment in case you have further questions or feedback for me. – GhostCat Sep 17 '18 at 07:39
  • As in: please read [no pictures of exceptions](http://idownvotedbecau.se/imageofanexception/) / [no pictures of code](http://idownvotedbecau.se/imageofcode). Then use the [edit] link to replace screen shots of text with nicely formatted/indented text within your question. – GhostCat Sep 17 '18 at 07:39

1 Answers1

0
difference = cv2.subtract(image1, fn)

Here fn is the PATH to the image on disk, not an image. You should read it from the disk.

try this:

image2 = cv2.imread(fn)
difference = cv2.subtract(image1, image2)
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
  • I did as you said Ishara but I got another error " error: OpenCV(3.4.1) C:\bld\opencv_1520732670222\work\opencv-3.4.1\modules\core\src\arithm.cpp:659: error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function cv::arithm_op" – chetan parmar Sep 17 '18 at 06:15
  • the images you compare must be same in size and number of channels. Otherwise how can they be compared. – Ishara Madhawa Sep 17 '18 at 06:24
  • read the [documentation](https://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#void%20subtract(InputArray%20src1,%20InputArray%20src2,%20OutputArray%20dst,%20InputArray%20mask,%20int%20dtype)) for `subtract` function. It says: `subtract` calculates Difference between two arrays, when both input arrays have the same size and the same number of channels – Ishara Madhawa Sep 17 '18 at 06:32
  • I made the following changes still the same error, plz tell me how can i fix it? I have tried resizing the images. image1 = cv2.resize(image1,(360,480)) for fn in image_path_list: image2 = cv2.imread(fn) im = cv2.resize(image2,(360,480)) difference = cv2.subtract(image1, image2) result = not np.any(difference) #if difference is all zeros it will return False if result is True: print("same") else: cv2.imwrite("result.jpg", difference) print("differ") – chetan parmar Sep 17 '18 at 07:12