# How to return position of one image inside of another [duplicate]

This question already has an answer here:

With ImageAlign result is another image, that is composition of another two. But I'm looking for function that will find position, x-y coordinates of smaller image inside bigger one. Is there such function? I'm not looking for position of some feature, that is on smaller and bigger image, but for exact coordinate of upper left corner of smaller image inside bigger.

## marked as duplicate by Mr.Wizard♦Jun 7 '16 at 4:22

• I have marked this question as a duplicate. A link is inserted above your post. Please review the question and its answers. If you feel that your question is different please edit it, making reference to older question and clarifying how your needs differ. – Mr.Wizard Jun 7 '16 at 4:23

The most straightforward approach is ImageCorrelate. I'll show you an example. nikie wrote an excellent answer explaining this method here.

large = ExampleData[{"TestImage", "Boat"}]


smaller = ColorConvert[Import["http://i.stack.imgur.com/NAHqc.png"], "Grayscale"]


corr = ImageCorrelate[large, smaller, EuclideanDistance];


The minimum – the blackest area – is the best match between the smaller image and the larger image.

min = PixelValuePositions[corr, "Min"] // First;
HighlightImage[
large, Rectangle[
min - ImageDimensions[smaller]/2,
min + ImageDimensions[smaller]/2
]]


• may I ask what version Mma you are using. The documentation states an image can be used as a kernel (as you have done). I run your code and get "is not a valid kernel specification" for smaller. – ubpdqn Jun 6 '16 at 23:29
• @ubpdqn I can replicate your problems, thank you for pointing it out. It appears something happens when I upload the image and then download it again, so everyone will have to crop their own subimage for demonstration. – C. E. Jun 7 '16 at 2:21
• thank you for clarifiying. The image information of uploaded (the context sensitive menu Mma provides) is different for the the two images, so I think the conversion process renders the smaller an unsuitable kernel. This is a minor point and I had already +1 your answer. I like that what almost seems to be a black dot served as the anchor for the highlight in the highlighted image. :) – ubpdqn Jun 7 '16 at 2:25
• @ubpdqn Now figured out why, had to convert it to grayscale. Thanks again for pointing out that there was a problem. – C. E. Jun 7 '16 at 2:46
• The PixelValuePositions call can be simplified to PixelValuePositions[corr, "Min"]. – Alexey Popkov Jun 7 '16 at 4:15