# Problem with tracing a marker in an image

My problem seems simple: I try to follow a marker across a number of images. All work fine, but when I update the marker from image to image, I observe a weird oscillatory behavior. I isolated the problem and it appears also in the case below, where I try to find the marker several times in the same image.

image = Import["https://i.sstatic.net/IEVL0.jpg"];
size = 40 (*marker size*)
row = 256 (*marker center row*)
col = 1013(*marker center column*)
Do[
{
rowMin = row - size,
rowMax = row + size,
colMin = col - size,
colMax = col + size,
marker = ImageTake[image, {rowMin, rowMax}, {colMin, colMax}],(*marker images*)
corr = ImageCorrelate[image, marker, SquaredEuclideanDistance], (*correlate image and marker*)
min = PixelValuePositions[corr, "Min"] // First ,
Print[min],
row = min[[2]],
col = min[[1]],
Print[marker]
}, {i, 1, 5}]


I would expect to see the marker image five times. Instead it appears every second time and in between another section of the image appears. Any thought why this is happens, and what I can do against it, would be very much appreciated.

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Feb 14, 2021 at 23:43

The reason that the marker image changes is because of the different coordinate origins used by ImageTake and PixelValuePositions. A quick fix is to add ImageDimensions after Import, and change row = min[[2]] to adjust for the different origins.

{w, h} = ImageDimensions[image]; (*needed to adjust image origin*)


The origin problem is easily fixed by changing this line in the Do statement:

row = min[[2]],


to:

row = h - min[[2]] + 1,


Image origins used by ImageTake and PixelValuePositions

The origin used by ImageTake is the upper left corner. Increasing row values count the vertical position from top to bottom, and increasing columns count the horizontal position from left to right.

The values returned by PixelValuePositions use an origin at the lower left corner. Increasing $$x$$ values count the horizontal position from left to right, and increasing $$y$$ values count the vertical position from bottom to top, offset by 0.5.

When you use PixelValuePositions $$x$$ and $$y$$ values (lower left origin) with ImageTake (upper left origin), you need to adjust the vertical row-value for ImageTake.

• thanks a lot for this piece of information. Work great now!! Feb 17, 2021 at 7:22
• @Christian - You're welcome. Please accept this answer to show that it solved your problem. Feb 17, 2021 at 16:54