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.
Asked
Active
Viewed 1,209 times
6
Alexey Popkov
- 61,809
- 7
- 149
- 368
Dragutin
- 920
- 5
- 14
-
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 07 '16 at 04:23
1 Answers
8
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
]]

-
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 06 '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 07 '16 at 02:21
-
1thank 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
smalleran 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 07 '16 at 02: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 07 '16 at 02:46
-
The
PixelValuePositionscall can be simplified toPixelValuePositions[corr, "Min"]. – Alexey Popkov Jun 07 '16 at 04:15 -