-1

I wrote the script, but the script does not work. I don't know what bug it has. I tested the screenCapture function. It works. My code is supposed do pixel by pixel comparison and only compare the red color at this point.

function [X,Y] = findImageXY(ImageName)
X = 0;
Y = 0;


%Load the small image
SmallImage = imread(ImageName); 

%Screen Capture the right screen
ScreenImage1=ScreenCapture([1600,1,1600,900],'ScreenImage.bmp');
ScreenImage = imread('ScreenImage.bmp');


for ii = 1:size(ScreenImage,1) -  size(SmallImage,1) - 1
    for jj = 1:size(ScreenImage,2) - size(SmallImage,2) - 1
        X = 0;
        for i = 1:size(SmallImage,1) 
            for j = 1:size(SmallImage,2)
               if ScreenImage(ii+i,jj+j,1)~= SmallImage(i,j,1)
                   X = inf;
                   Y = inf;
                   break
               end
            end
            if isinf(X) == 1
                break
            end
        end
        if X == 0
            X = jj + ceil(size(SmallImage,2)/2);
            Y = ii + ceil(size(SmallImage,1)/2);
            break
        end
    end
    if X~=0 && isinf(X)~=1
        break
    end
end
beaker
  • 16,331
  • 3
  • 32
  • 49
Marco
  • 985
  • 7
  • 22
  • 50
  • Hi, you can use the function `normxcorr2` to get the indexes where the small image is most similar to the big one in the indexes offset. I think that the values in the two images might not be identical, then you can't use exact comparison but you should theshold the difference . But I think it's best to use `normxcorr2`. – dafnahaktana Aug 26 '17 at 09:22
  • Also, In case of a screen capture images, it's depends on what on your screen, if your screen have a lot of smooth areas, for example if your screen show a page on stackoverflow site then it has lot of white smooth areas and so you have many areas that are identical and it's a problem to find the right match . – dafnahaktana Aug 26 '17 at 09:30
  • 1
    Kind of overkill but you can use typical image registration methods for this. Here is one that would fit your problem. https://www.mathworks.com/help/images/ref/imregister.html – Durkee Aug 26 '17 at 15:02

1 Answers1

-1

Look at the example in the normxcorr2 documentation:

https://www.mathworks.com/help/images/ref/normxcorr2.html

Worth thinking about: If what you are trying to do is template matching on two similar, but not numerically identical images, you are going to want to use a technique like normalized cross correlation or phase correlation that is robust to things like illumination differences and noise. Just noticed that @dafnahaktana pointed this same thing out in the comments.

c = normxcorr2(SmallImage(:,:,1),ScreenImage(:,:,1));
[ypeak, xpeak] = find(c==max(c(:)));
YtopLeft = ypeak-size(SmallImage,1)+1;
XtopLeft = xpeak-size(SmallImage,2)+1;

Your code assumes that there exists an exact match between the sub image and the larger image, which is not very common in most real world template matching problems, where there is frequently noise, illumination differences, etc.

Alex Taylor
  • 1,402
  • 1
  • 9
  • 15