How compare pixels from two images to find a match?
22 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(4 个)
Image Analyst
2016-11-20
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 个评论
Arati Gawade
2022-4-28
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
2022-4-28
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.
Augustine Ekweariri
2016-11-22
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 个评论
Image Analyst
2016-11-22
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.
Walter Roberson
2016-11-20
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!