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

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.

• 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. Jun 7, 2016 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["https://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. Jun 6, 2016 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. Jun 7, 2016 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. :) Jun 7, 2016 at 2:25
• @ubpdqn Now figured out why, had to convert it to grayscale. Thanks again for pointing out that there was a problem. Jun 7, 2016 at 2:46
• The PixelValuePositions call can be simplified to PixelValuePositions[corr, "Min"]. Jun 7, 2016 at 4:15