# Image matching and transformation

I hope this picture can help me to be clearer. The triangles represent some values of an image (EDITED: positions). The same for the circles, but from a different image. Some values matches, some others not, as you can see (I know, I am not good at drawing!). I would like to ask you if there is a way to find the values that match (i.e. transform triangles/circles in values again) and list the corresponding values from one of the two images.

Thank you

• "The triangles represent some values of an image" - do you mean positions? or pixel values at the indicated positions? – MelaGo Sep 29 '19 at 19:24
• Yes, I mean positions – still_learning Sep 29 '19 at 19:28

This is fairly straightforward if the features are of similar size and non-overlapping. For illustration, I made this image with red and green spots. The co-localized spots are almost, but not exactly, coincident. (Something resembling red and green channels from a DNA microarray, maybe.)

Here are the red and green channels:

{r, g} = ColorSeparate[img][[1 ;; 2]]


Spots can be automatically detected with MorphologicalComponents. (If there is background signal or noise amongst the real features, some more work would need to be done to select the authentic features.)

spots = MorphologicalComponents /@ {r, g};


ComponentMeasurements can then be used to get the centroid of each spot.

centroids = ComponentMeasurements[#, "Centroid"][[All, 2]] & /@ spots;


Check that spot detection is working:

Graphics[{Red, PointSize[Large], Point[centroids[[1]]], Green,
Point[centroids[[2]]]}]


Next, we can decide on some tolerance within which spots will be called co-localized. For example, I used the mean radius of the spots.

tolerance =
Mean[Flatten[ComponentMeasurements[#, "EquivalentDiskRadius"] & /@ spots][[All, 2]]];


The coordinates of the green spots that co-localize with red spots are then

coincidentspots =
Select[Table[
{gcentroid, EuclideanDistance[Nearest[centroids[[1]], gcentroid][[1]], gcentroid]},
{gcentroid, centroids[[2]]}],
#[[2]] < tolerance &][[All, 1]]

(* {{176.5, 467.5}, {403.5, 463.5}, {63.4845, 459.015}, {166., 381.5}, {414.485, 370.015}, {73., 281.}, {166., 276.}, {292., 198.5}, {78., 82.5}, {292., 82.5}, {427., 73.5}} *)


For illustration:

Show[r, Graphics[{Green, PointSize[Large], Point[coincidentspots]}]]