I have a image with me. I would like to obtain and mark the position having minimum intensity in that image using MATLAB programming. I would also like to get the value of minimum intensity
-
7I have the code with me, but for some *reason*, I am not able to share it. – Divakar Mar 03 '15 at 17:48
-
So do you need any more help? If not, accepting an answer signifies that you no longer need help. http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – rayryeng Mar 08 '15 at 05:31
2 Answers
To correctly identify where in the image the minimum intensity is, combine a call with min
and find
. As such, if A
is your image, do the following:
min_val = min(A(:));
[row,col] = find(A == min_val), 1);
min_val
will contain the minimum intensity in your image. What is important is unrolling the matrix A
to a single column vector by A(:)
. This way, you are ensuring that min
operates along all of the pixel values at the same time. In the above code, row
and col
will contain the row and column locations of where minimum intensity was found. The intricacy with this is that if you have more than one pixel that shares the same minimum intensity, find
will return all locations that share this same intensity. If you just want one of them, append 1
as the second parameter to find
.
If you want to mark where in the image the minimum intensity is, that's simply done by indexing:
A(row,col) = 255;
This will set the location of where the minimum intensity is located to be white or 255.
C.Colden's code is also a good attempt, but it will not work if the input is not a vector (i.e. a matrix or array that has more than two dimensions) as min
will operate along the first non-singleton dimension. This is why you need to unroll the image into a single vector as in my code above.
If we input a matrix into min
, in your case, min
will find the minimum along each column. If you want, you would have to call min
twice to ascertain the row location by applying min
on the column found by the first min
call but I find the above code more readable.
However, for academic purposes, this is what you would do if you wanted to only use min
:
[min_col, rows] = min(A); %// Find the minimum among all columns as well as which row for each column gave the minimum
[min_val, col] = min(min_col); %// Now, for each of the minimum values, figure out which column gave us the global minimum as well as the minimum itself
row = rows(col); %// Now find the row location of this global minimum
As you can see, I would rather stick with the first attempt with find
and min
.... it's just more readable.

- 102,964
- 22
- 184
- 193
You can load your image using imread
(http://ch.mathworks.com/help/matlab/ref/imread.html). Afterwards you can treat the image like a matrix. So if "A" is your image, you can use max
(http://ch.mathworks.com/help/matlab/ref/max.html) or min
to find the values using Min_Value = min(A(:))
. Afterwards you can search for the location of the Min_Value in your matrix using [row,column] = find(A == Min_Value)
Example:
A = [3,2,1;4,5,20;7,8,9];
Min_Value = min(A(:));
[row,column]=find(A == Min_Value);

- 627
- 1
- 8
- 28
-
1I think that would give you the row index of the min of each column, not the index of the global min. – eigenchris Mar 03 '15 at 18:47
-
-
-
1In this case `I` will contain the column-major location of where the minimum was found. If you want the row and column location, consider making a call with `ind2sub` to do that for you: `[row,col] = ind2sub(size(A), I);` – rayryeng Mar 03 '15 at 22:05
-
Good point. However I think the "easiest/simplest" solution for the question is to first find the `min` and then search for it in the matrix. Anyway thanks for the good `ìnd2sub` example ;) – C.Colden Mar 03 '15 at 22:10
-
1Ah that's great. Can you provide how you'd search for where the minimum value is? You only have half of the story! – rayryeng Mar 04 '15 at 00:01