How to know maximal x and y cordinate in image

51 visualizzazioni (ultimi 30 giorni)
Sipher17
Sipher17 il 19 Giu 2019
Commentato: Walter Roberson il 20 Giu 2019
Hi I have an image,I want to find x and y coordinate of the maximum value in pixel , how can i do that? Thanks a lot before
  7 Commenti
Image Analyst
Image Analyst il 20 Giu 2019
Sipher, I thought I gave a pretty thorough answer below. Did you ever actually scroll down to the Answers section below to look at any answers, or have you just been looking up here at comments?
Walter Roberson
Walter Roberson il 20 Giu 2019
What do you want to do if the location with maximum green contribution is not the same as the location with maximum red contribution. For example, if there is one pixel that is (255,0,0) and a different pixel that is (0,255,0) then which of the two is the "maximum" ?
I would remind you again that the pixels are hardware display items, and that the values stored in your arrays that you are sending to the display routines might be quite different. For example if you have an array which is alternating red and green pixel columns, and you send it to be displayed to a window that has only half as many pixels wide as the array has columns, then the display hardware would have to average the adjacent red and green array elements into single pixels, which would give you a yellow display. You would find that that yellow was the "maximum" pixel by taking a screen capture and examining the screen capture, but the yellow that you pick out that way has only an indirect relationship to the array elements you asked to be displayed.

Accedi per commentare.

Risposte (2)

drummer
drummer il 19 Giu 2019
Hi Sipher.
The example below is for a simple 3x3 matrix. But you can change to your image instead (as an image is a matrix).
The fprintf part just shows the ouput of the coordinates in the command window.
clc, clear all;
image = [2 3 1; 5 4 8; 10 11 20];
[x, y] = size(image);
max_value = max(image(:))
for i=1:x
for j=1:y
if(image(i,j)== max_value)
i;
j;
end
end
end
fprintf('The maximum value in the matrix is %d\n', max_value);
fprintf('The x cordinate is %d', i);
fprintf(' and the y-coordinate is %d\n', j);
The maximum value is 20, located at image(3,3).
Cheers
  1 Commento
Adam
Adam il 19 Giu 2019
You would need a break in your if statement in that example, else it would not print the correct coordinates if the maximum value where anywhere other than the last element. Also don't call a variable image as it over-rides the very important function of the same name.
[~,idx] = max( img(:) );
[y,x] = ind2sub( [size( img)], idx );
should work fine.

Accedi per commentare.


Image Analyst
Image Analyst il 19 Giu 2019
To get the maximum pixel coordinate for an image, that's simply the number of rows and columns.
[rows, columns, numberOfColorChannels] = size(yourImage);
The min is just simply 1.
To see the coordinates and image values as you mouse around, use impixelinfo:
hp = impixelinfo;
You can set the position property of hp to move it to a location on the figure you like, otherwise it's at the lower left.
To spatially calibrate your image in real world units instead of pixels you can use xdata and ydata properties in imshow().
grayImage = imread('moon.tif');
imshow(grayImage, 'XData', 1:2777, 'YData', 1:4166);
axis('on', 'image');
% Display coordinates and image intensity/color as you move the cursor over the image:
hp = impixelinfo;
hp.Units = 'Normalized';
hp.Position = [.01, .95, 0.4, 0.03];
You can also see my attach spatial calibration demo where you can measure things in pixels and then multiply measurements by a spatial calibration factor that you create by drawing a distance in your image and telling it how many real world units long it is.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by