How to access left bottom pixel in image using (0,0)?

26 views (last 30 days)
As the origin in image matrix is left top corner. I want to change it origin to left bottom for further processing. When i write (0,0) in matlab, it return me the pixel value of left bottom corner. Anyone can help me?

Answers (4)

J. Alex Lee
J. Alex Lee on 18 Jan 2020
How about if you flip the image?
% if the image is in I
I = flipud(I)
  2 Comments
Muhammad khan
Muhammad khan on 19 Jan 2020
This command flip the entire data of the image. I don't want to flip the image. I just want to flip the y indexes to access the left bottom by using (0,0). Please explain it.
J. Alex Lee
J. Alex Lee on 19 Jan 2020
I guess I struggle to understand. Flipping the indices literally means flipping the image itself.
If you are concerned only about disply of the image using something like imshow(), then you can counteract flipping the image
% if the image is in I
I = flipud(I)
imshow(I)
set(gca,'YDir','normal')
I'll post some alternatives based one guessing your intent.

Sign in to comment.


J. Alex Lee
J. Alex Lee on 19 Jan 2020
Since you seem to want to "index" by (0,0), perhaps what you are looking for is assigning real (x,y) coordinates to the image and then identifying pixel's index based on their location in in the (x,y) plane you have imposed. For example, if you want the upper right corner to have coordinates (1,1), you can define a meshgrid accordingly, but you will again have to flip the y coordinates upside down
% again if I is the original image
x = linspace(0,1,size(I,2)); % size in dim 2 because for images, x coordinate corresponds to columns
y = linspace(0,1,size(I,1)); % size in dim 1 because for images, y coordinate corresponds to rows
y = flip(y);
% contourf(X,Y,I)
imshow(I,'XData',x,'YData',y)
set(gca,'YDir','normal')
See that you can put something near the lower left corner
hold on;
plot(0.1,0.1,'or')
But now to access the image indices, you have to first search for the closest pixel to the coordinates you want, then find its index in the arrays X and Y
XoI = 0; % x coordinate of interest
YoI = 0; % y coordinate of interest
[~,xidx] = min(abs(x-XoI))
[~,yidx] = min(abs(y-YoI))
Or something like that

J. Alex Lee
J. Alex Lee on 19 Jan 2020
If you just want to translate the unflipped image's row index to be translated into a different index as if you were indexing the flipped image correctly, you can subtract the index from the number of rows and add 1
Say youw anted the 5th pixel "up" from the "bottom" of the as-is-image, which is 100 pixles tall. That corresponds to the 96th pixel "down".
yIdxInMyMind = 5
yIdxOfActualImage = size(I,1) - yIdxInMyMind + 1

Image Analyst
Image Analyst on 19 Jan 2020
Edited: Image Analyst on 19 Jan 2020
Just make a function and use the function name instead of the variable name. To make it even simpler you can avoid passing in the image variable name by using a global variable or having the function be nested in your calling function.
function grayLevel = MyImage(x, y)
global myImage; % Let this function see the image variable without having to pass it in via the input argument list.
[rows, columns, numberOfColorChannels] = size(myImage); % Get dimensions of image.
column = x + 1; % Convert to 1-based index.
row = rows - y; % Convert to reverse 1-based index.
grayLevel = myImage(row, column, :);
Remember to declare myImage (or whatever you named your image variable) global in your main, calling routine. Only functions that have the variable declared global can see the variable - other functions cannot. Now before other global-haters chime in about how you should never use global variables, I suggest they look at the code first to see that it's perfectly safe in this case since the global variable never gets changed inside the function.
So let's say you had an image called rgbImage and wanted the RGB value in the lower left corner, instead of using rgbImage(rows, 1) you could use
global rgbImage; % Just needed once in the calling routine, not every time you call MyImage().
rgbValue = MyImage(0, 0);
Of course it also works with gray scale images.
HOWEVER, I strongly suggest you get used to calling the location using 1-based rows and columns, or else you might get careless in some other program, where you did not use my hack above, and try to do
pixelValue = grayImage(x, y); % WRONG way to do it!!!
This is one of the most common beginner mistakes - using (x,y) to reference images (or any 2-D matrix) instead of grayImage(y, x) which is the proper way of doing grayImage(row, column) if you were to use x,y nomenclature instead of row,column nomenclature.
  2 Comments
J. Alex Lee
J. Alex Lee on 19 Jan 2020
1-based versus 0-based and the (row,col) vs (x,y) being transposed are both reasons why I would conceptually separate the "indices of the array representation of the image" and the "coordinates of the image" by explicit semantics in the code. The idea of creating the function as an interface for coordinate-based access is nice, and to mitigate confusion one might name the function and avoid the global variable
function val = ImageValueFromCoordinates(I,x,y)
[rows, columns, numberOfColorChannels] = size(I); % Get dimensions of image.
column = x + 1; % Convert to 1-based index.
row = rows - y; % Convert to reverse 1-based index.
val = I(row, column, :);
end
What do you think?
Image Analyst
Image Analyst on 19 Jan 2020
That's fine - you can rename the variables to whatever you want, and pass in the image also, if you want. Though I'd use something more descriptive than I, since I (upper case) often looks identical to, or a lot like (depending on the font), 1 (one) or l (lower case L). Plus using too many single letter variables can make your code look like a confusing alphabet soup of a program.

Sign in to comment.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!