Reputation: 3
I have almost 900 .jpg images (obtained by splitting a video in frames, each image is 116ko), and I want to compare each of my frames to the first one (= frame0) using the "Mean Squared Error".
It is quite easy to load two images and compare them, but it is much more difficult (for me at least) to write a loop that loads and compares my images. My aim is just to have a list with all the "Mean Squared Error" (MSE) values in it.
Issues:
I don't need to have all the images loaded or opened at the same time, but I don't know how to load an image just during the processing time.
My images are in the same folder as my .py
file.
When I run my code, it returns:
Traceback (most recent call last): File "C:\Users\Susie\Documents\programmes_python\python\images_video\comparaison-images2.py", line 46, in compare_images(frame0, frame0) File "C:\Users\Susie\Documents\programmes_python\python\images_video\comparaison-images2.py", line 24, in compare_images s = ssim(imageA, imageB) File "C:\Python27\lib\site-packages\skimage\measure_structural_similarity.py", line 151, in compare_ssim "win_size exceeds image extent. If the input is a multichannel " ValueError: win_size exceeds image extent. If the input is a multichannel (color) image, set multichannel=True.
# import the necessary packages
from skimage.measure import compare_ssim as ssim
import numpy as np
import cv2
from os import listdir
from os.path import isfile, join
def mse(imageA, imageB):
# the 'Mean Squared Error' between the two images is the
# sum of the squared difference between the two images
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
# return the MSE, the lower the error, the more "similar"
# the two images are
return err
def compare_images(imageA, imageB):
# compute the mean squared error and structural similarity
# index for the images
m = mse(imageA, imageB)
s = ssim(imageA, imageB)
mse_list.append(m)
ssim_list.append(s)
print (mse_list)
print (ssim_list)
# load the images and convert the images to grayscale
frame0 = cv2.imread("frame0.jpg")
mypath='C:\Users\Susie\Documents\programmes_python\python\images_video'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = np.empty(len(onlyfiles), dtype=object)
for n in range(1, len(onlyfiles)):
images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
images[n] = cv2.cvtColor(images[n], cv2.COLOR_BGR2GRAY)
# compare the images
mse_list = []
ssim_list = []
compare_images(frame0, frame0)
for n in range(1, len(onlyfiles)):
compare_images(frame0, images[n])
Upvotes: 0
Views: 2198
Reputation: 571
Since you're using grayscale images, you need to specify this by adding the 0 parameter as follows:
frame0 = cv2.imread("frame0.jpg", 0)
Check OpenCV documentation for more detail: https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html
Upvotes: 0
Reputation: 21223
I would like to mention a couple of programming mistakes:
1. Attribute error :
Ensure that the .py
file and the collection of images are present within the same folder. If they are not, mention the path exclusively for eg. mypath='C:/Users/Susie/Documents/programmes_python/python/images_video
2. Value error :
Comparing a multi-channel image with a single channel image throws this error. Make sure the images are of the same size and have same number of channels.
Making mistakes is a step to learning !!!
Upvotes: 1