필터 지우기
필터 지우기

How compare pixels from two images to find a match?

조회 수: 25 (최근 30일)
Emmanuel
Emmanuel 2014년 10월 21일
댓글: Image Analyst 2022년 4월 28일
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

답변 (4개)

Stalin Samuel
Stalin Samuel 2014년 10월 21일
  댓글 수: 2
Emmanuel
Emmanuel 2014년 10월 21일
Hey! Thank you for your reply! I am getting an error with the above mentioned function, which I'm unable to debug
Augustine Ekweariri
Augustine Ekweariri 2016년 11월 19일
You did not share the solution

댓글을 달려면 로그인하십시오.


Image Analyst
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
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
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
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
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
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);

Community Treasure Hunt

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

Start Hunting!

Translated by