Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

I have executed a code and it seem to give errors. Is there any invalid statements? The image is of [r x c] = 256 X 256 and I tried to store each co-ordinate {(1,1),(1,​2),...,(25​6,256)} corresponding to each pixel value in an array 'C'.

1 view (last 30 days)
I = imread('Cameraman.tif');
[r,c] = size(I);
k = 1;
loop to store the pixel co-ordinates
for i = 1:r;
for j = 1:c;
C(k,:) = [i,j];
k = k + 1;
end
end
Expected should be: When I type C(10,10), it should return the pixel value corresponding the resp co-ordinate.
  1 Comment
Stephen23
Stephen23 on 22 Mar 2016
Edited: Stephen23 on 22 Mar 2016
You already imported the image as an array I, so why not just access it directly?, e.g.:
pixval = I(10,10,:)
Good programmers try to keep their code as simple as possible, because this makes it faster, more robust, and easier to test, easier to undestand, easier to write, easier to fix...
Why bother indirectly accessing the pixels when you can index into the image directly?
Your proposal "When I type C(10,10), it should return the pixel value corresponding the resp co-ordinate" can be achieved in one step using num2cell, but this is a total waste of time.

Answers (0)

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!