Reading pixels of an image

Hello, How does MATLAB read the pixels in an image? Is it row wise or column wise? Suppose I have a 5x6 image and I wanted to read 21st pixel, what value is returned? Is it the value at (4,3) or (1,5)?

Réponses (3)

jonas
jonas le 24 Oct 2018
Modifié(e) : jonas le 24 Oct 2018

1 vote

Indexing works by going through the matrix column by column, so that would be (1,5) in your example. Don't be afraid to test things in MATLAB.
A =
1 2
3 4
>> A(3)
ans =
2
You can also test it by writing out the values of all indices with this syntax
>> A(:)
ans =
1
3
2
4
As you can see, the two columns are stacked on top of eachother.

5 commentaires

Deep P
Deep P le 24 Oct 2018
Thank You. That's right, but if I am reading any image into a variable and want to know a particular pixel value, can I still use the same synatx? Like A(x), where x is the pixel number.
Deep P
Deep P le 24 Oct 2018
syntax*
Image Analyst
Image Analyst le 24 Oct 2018
I guess you haven't seen my answer below yet.
Deep P
Deep P le 24 Oct 2018
Yes I did, what if it is a RGB image? Image with 3 dimensions, how are pixel values read?
Image Analyst
Image Analyst le 24 Oct 2018
See comment on my answer below.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 24 Oct 2018

1 vote

It's the 21st element which you can see by looking at m(21). If you use (row, column) indexing element 21 is the same as (1, 5). See this little illustration:
m = reshape(1:30, 5, 6) % Sample data
% Show the value of the 21st element using linear indexing:
m(21)
% Show the value of the (4, 3) element using (row, column) indexing:
m(4, 3)
% Show the value of the (1, 5) element using (row, column) indexing:
m(1, 5)
What you'll see:
m =
1 6 11 16 21 26
2 7 12 17 22 27
3 8 13 18 23 28
4 9 14 19 24 29
5 10 15 20 25 30
ans =
21
ans =
14
ans =
21

4 commentaires

Deep P
Deep P le 24 Oct 2018
Thank You
Image Analyst
Image Analyst le 24 Oct 2018
Indexes cycle fastest from left to right. For a color image, the left most index (rows) goes first/fastest, then you move over a column and go down columns. This is all for the red channel (3rd index = 1). Then it repeats all that for the green channel (index 3 = 2), and finally for the blue channel (3rd index = 3).
Deep P
Deep P le 29 Oct 2018
Hello, Thanks for explaining. Is there any way to do indexing row wise in MATLAB?
Image Analyst
Image Analyst le 30 Oct 2018
If you change only the first index, you will change the row while remaining in the same column.

Connectez-vous pour commenter.

Abdul Rehman
Abdul Rehman le 24 Oct 2018
Modifié(e) : Abdul Rehman le 24 Oct 2018

0 votes

Basically MATLAB Read image in form of Matrix,
A=[22 33 55 66 77 ; 33 55 77 88 99 ; 4 2 66 8 9];
like this, when you try in matlab Random 5*6 Matrix and perform operation.it's return the pixel value at that location in Matrix. For grayscale image it's range is (0-255).
if true
A=[22 33 44 1 2 4 ; 2 3 55 77 99 2 ;2 44 5 77 84 2; 33 4 55 2 1 4 ; 33 55 66 77 88 32];
A(4,3);
A(1,5);
end
Answer is:
Hopefully you get it.

Catégories

En savoir plus sur Read, Write, and Modify Image dans Centre d'aide et File Exchange

Question posée :

le 24 Oct 2018

Commenté :

le 30 Oct 2018

Community Treasure Hunt

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

Start Hunting!

Translated by