What is the fastest way to locate an image inside a larger image?
Using ImageCorrelate
, you can do something like
bbox[img_, crop_] := {#, # + Reverse@ImageDimensions[crop] - 1} &@
Position[ImageData@
Binarize[
ImageCorrelate[img, crop, SquaredEuclideanDistance,
Padding -> None], .001], 0][[1]]
Example
img = ExampleData[{"TestImage", "Mandrill"}]
crop = ImageTake[img, {40, 80}, {141, 200}]
bbox[img, crop]
{{40, 141}, {80, 200}}
Edit
In response to the OP's question,here is an explanation of how bbox
works.
ImageCorrelate
in bbox
creates a new image by calculating the Euclidean distance between crop
and each of the sub images of img
with the same dimensions as crop
. The option Padding -> None
is to make sure that the pixel at {i,j}
in the result of ImageCorrelate
corresponds to the sub image whose upper left corner is at position {i,j}
in the original image.
The Euclidean distance between two images is zero only if they are the same, so to find the position of crop
in img
we just need to extract the position of the pixels with value {0.,0.,0.}
from the image data of ImageCorrelate
which is what Position[ImageData@Binarize[....]], 0]
does.
This will give us the position of the upper left corner of crop
. The lower right corner is then the upper-left corner plus the dimensions of crop
(note that since ImageDimensions
returns {number of cols, number of rows}
, and the bounding box is given as {{rowMin, colMin}, {roxMaw, colMax}}
we need to reverse ImageDimensions
)
Edit 2
As Szabolcs pointed out, the cut off in Binarize
is somewhat arbitrary, and might cause false positives. As an alternative you could do something like this instead, which would find the best fit in the image:
bbox[img_, crop_] := {#, # + Reverse@ImageDimensions[crop] - 1} &@
(Position[#, Min[#]][[1]] &@
ImageData[ColorConvert[
ImageCorrelate[img, crop, SquaredEuclideanDistance,
Padding -> None], "Graylevel"]])
If your final assertion is true:
Because b contains an exact copy of c, this can be thought of as a problem of finding a submatrix in a larger matrix.
You will find your answer here:
A fast implementation in Mathematica for Position2D
Since your images are apparently not exact, you can find the position of the upper left corner of the sample with correlation:
b = Import["http://portfusion.sourceforge.net/i/m1/B.png"];
c = Import["http://portfusion.sourceforge.net/i/m1/C.png"];
full = ImageData[ColorConvert[b, "Grayscale"]];
sample = ImageData[ColorConvert[c, "Grayscale"]];
cor = ListCorrelate[1/(sample /. 0. -> 1.0*^-6), full];
Position[cor, Min@cor]
{{94, 88}}
The correlation looks like this:
cor // Rescale // Image
For (not necessarily) more complicated cases, you may want to have a look at the geometric transformation functions in Mathematica.
img = Import["~/temp/geomtrafo.png"]; (* The image *)
crop = ImageCrop[img, {100, 100}]; (* Crop small portion *)
FindGeometricTransform[img, crop, Transformation -> "Similarity"][[2]]
Notice the two numbers in the TransformationFunction
? These are the lower left coordinates of the cropped image in the large one. I used the parameter Transformation -> "Similarity"
here, which considers rotations, translations, and scalings; there are more basic and more complicated ones available, refer to the help function of FindGeometricTransform
.
I'm afraid I don't know how to extract these numbers properly from there, but that shouldn't be an issue.