How compare pixels from two images to find a match?

36 views (last 30 days)
I am having two images where second image is the warped image of previous one. Using random generator , I am selecting 8x8 pixels to compare both images and find the match(minimization function).
I am getting an error in fminunc
Error using fminunc (line 171)
FMINUNC requires the following inputs to be of data type double: 'X0'.
Error in mainsample (line 14)
[s fval] = fminunc(@myfun , subblock)
How do I go about it?
My code for generating random pixels are:
clear all;
close all;
I = rgb2gray(imread('testimage.jpg'));
original = rgb2gray(imread('testimage.jpg'));
original = double(original);
for( j = 0 : 1:7)
r = randi(225-7);
c = randi(225-7);
subblock = I(r:r+7, c:c+7);
imshow(subblock);figure;
t = [1 1];
[s fval] = fminunc(@myfun , subblock)
end
function f = myfun(x)
scale = 0.7;
J = imresize(x, scale);
theta = 30;
x0 = imrotate(J,theta);
for( i = 1:1:225)
f = (x^2 - x0(i)^2);
end

Answers (4)

Stalin Samuel
Stalin Samuel on 21 Oct 2014
  2 Comments
Emmanuel
Emmanuel on 21 Oct 2014
Hey! Thank you for your reply! I am getting an error with the above mentioned function, which I'm unable to debug

Sign in to comment.


Image Analyst
Image Analyst on 20 Nov 2016
Your algorithm of checking randomly selected patches for similarity seems destined to fail. You'd be better off using normxcorr2() to find where one patch occurs in the other image, like in my attached demo.
To compare images in general, use psnr() or immse() or ssim().
  12 Comments
Arati Gawade
Arati Gawade on 28 Apr 2022
Ok... actually I want to print the output by using if else statement and based on value of template matching as a condition. Is this right approach ?
Image Analyst
Image Analyst on 28 Apr 2022
You could do that, though for an image, if there are lots of places where the template is close in appearance to the main figure, you're going to have many, many things printed, perhaps thousands. But if you want that, then ok.

Sign in to comment.


Augustine Ekweariri
Augustine Ekweariri on 22 Nov 2016
You can check the difference using Euclidian distance.
test_img = imread('image1.bmp');
input_im = imread('image2.bmp');
minimum =1000;
[row, col] = size(test_img(:,:,1));
distance = zeros(row, col);
for xAxis = 1 : row
for yAxis = 1 : col
distance(xAxis, yAxis) = sqrt((input_im(xAxis, 1) - test_img(yAxis, 1)).^ 2 + (input_im(xAxis, 2) - test_img(yAxis, 2)).^ 2);
end
end
if distance < minimum
minimum = distance;
end
  1 Comment
Image Analyst
Image Analyst on 22 Nov 2016
First of all, you made the common beginner mistake of swapping x and y with row and column. The first index of the image arrays is not the x coordinate. It's the y coordinate and it's deceptive to call the y coordinate of input_img "xAxis". If the image is not square and the number of rows is more than the number of columns, your code will throw an error. Like I said, it's a common mistake, so be careful about that.
Secondly the computation doesn't make sense. You're taking the "distance" between the delta of the first columns and the delta of the second columns. Not only doesn't that make sense, but it ignores all columns from 3 onwards.
Third, using minimum, which goes from a scalar to a 2-D array, is unneeded and never even used.
And finally the whole loop thing could be vectorized into a single line of code.

Sign in to comment.


Walter Roberson
Walter Roberson on 20 Nov 2016
Change
for( i = 1:1:225)
f = (x^2 - x0(i)^2);
end
to
f = 0;
for( i = 1:1:225)
f = f + (x(i).^2 - x0(i).^2);
end
Or, better yet, do not loop and instead
f = sum(x(:).^2 - x0(:).^2);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!