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.
-
$\begingroup$ 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. $\endgroup$– Mr.WizardJun 7, 2016 at 4:23
1 Answer
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];
ImageAdjust[corr]
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
]]
-
$\begingroup$ 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
. $\endgroup$– ubpdqnJun 6, 2016 at 23:29 -
$\begingroup$ @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. $\endgroup$– C. E.Jun 7, 2016 at 2:21
-
1$\begingroup$ 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. :) $\endgroup$– ubpdqnJun 7, 2016 at 2:25 -
$\begingroup$ @ubpdqn Now figured out why, had to convert it to grayscale. Thanks again for pointing out that there was a problem. $\endgroup$– C. E.Jun 7, 2016 at 2:46
-
$\begingroup$ The
PixelValuePositions
call can be simplified toPixelValuePositions[corr, "Min"]
. $\endgroup$ Jun 7, 2016 at 4:15