Images dimensions error in python
Asked Answered
A

4

6

Trying to match two images to find out the scores between them.But it shows some dimension error.Unable to fix the issue.My code is given below:

from skimage.measure import compare_ssim
#import argparse
#import imutils
import cv2

img1="1.png"
img2="2.png"


# load the two input images
imageA = cv2.imread(img1)
imageB = cv2.imread(img2)

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


# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))

This give n an error:

raise ValueError('Input images must have the same dimensions.')

ValueError: Input images must have the same dimensions.

How to fix this issue?

Attwood answered 23/2, 2018 at 9:58 Comment(1)
What are imageA.shape and imageB.shape?Ceiba
I
13

Amending Saurav Panda's answer:

You can reshape one of the images to the size of other image like this:

imageB=cv2.resize(imageB,imageA.shape)

note that

(H, W) = imageA.shape
# to resize and set the new width and height 
imageB = cv2.resize(imageB, (W, H))

the cv2.resize function inputs expects (W,H). This is the reverse order of the output of cv2.shape (H,W), so you need to catch that, or you'll get the same error when comparing non-square images.

Inhalator answered 14/9, 2018 at 23:29 Comment(0)
A
3

You can do this in many ways:

Like in the first method, you can assign a fixed dimension which would be less than the actual dimensions of the image and resize both images to this same size. Like, resize all images to (150,150), etc.

In second method you can reshape one of the images to the size of other images. Try this code:

imageB=cv2.resize(imageB,imageA.shape)

This will work for you, but in case the difference in dimensions of two image is very large, sometimes you may lose some data. You can compare for both x and y dimensions and find the smallest one.Then resize both images to this smallest dimension of x and y.

Airing answered 24/2, 2018 at 5:21 Comment(1)
yes, that is the problem. if other image is resized, some data lossesEncephalomyelitis
F
2

The error

'Input images must have the same dimensions.'

Tells you that the function you called expects input images of the same dimensions and that you did not do this.

You obviously fix that by providing input images that have the same dimensions or by not calling that function if the images have different dimensions and if you cannot change that for whatever reason.

Compare imageA.shape and imageB.shape after loading the images from file.

For simple debugging:

print imageA.shape
print imageB.shape
Fougere answered 23/2, 2018 at 10:40 Comment(2)
the result i got is : (162, 256, 3) and (162, 250, 3)..I want to resize these two imagesAttwood
@ShaonPaul well then decide what to do. remove excess columns, add missing columns or maybe both? you can also rescale both or one image to get same dimensions.Fougere
A
0

You can use tensorflow. See this link and you can modify your data accordingly

Alderney answered 25/1, 2023 at 8:38 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewAltostratus

© 2022 - 2024 — McMap. All rights reserved.