Extract each pixel of an image
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello All,
A quick question and I believe it will be fairly easy as well but being new I am stuck in some fundamentals and this forum is helping me to move forward.
Problem: I wrote a code which is stacking 1024 images of 640x512 resolution in a 3D matrix. And wrote few loops which is extracting the appropriate image from 1024 if needed and 2. Cutting the stack of images in row and column in next two loops as needed.
Question: Now I would like to read each pixel of the image one by one such that (:,1,1) will correspond to the first row and give me the values for that. But I want to create a for loop which is capable of reading each pixel of each image. How can I create a loop for that? I have attached the code I am working with.
Please advice. Thanks
edited by Guillaume to add the code to the post:
clc; clear all;
i=49;
for j=1:1:1024;
x=imread([ num2str(i+j) '.bmp']);
z1(j ,: ,:)=x;
end
z=im2double(z1);
p=input('input the frame number\n');
for k=1:1:512
for l=1:1:640;
a(k,l)=z(p,k,l);
end
end
subplot(1,3,1);
imshow(a);
m=input('frame number between 1 to 640\n');
for v=1:1:j;
for w=1:1:512;
b(v,w)=z(v,w,m);
end
end
subplot(1,3,2);
imshow(b);
n=input('frame number between 1 to 512\n');
for g=1:1:j;
for h=1:1:640;
c(g,h)=z(g,n,h);
end
end
subplot(1,3,3);
imshow(c);
bh=hilbert(b);
ch=hilbert(c);
bx=b.*b;
bi=bh-b;
by=bi.*bi;
by=bh.*bh;
bz=sqrt(bx+by);
figure;
imshow(bz);
cx=c.*c;
cy=ch.*ch;
cz=sqrt(cx+cy);
figure;
imshow(cz)
댓글 수: 2
Rik
2017년 9월 22일
Why are your reading it to a matrix where the first dimension is the image? Convention is to have 3D matrices be (row,col,page) if there is no reason to deviate from that. Also, are you certain your images are loaded as grey scale images? Otherwise they will have a third dimension (RGB), even if the image is a grey scale image.
Why do want a loop that can read all pixels? All pixels are in your matrix, so what is your problem.
A last note: you are using im2double on the entire 3D array. You should only apply this function to separate images, so inside the loop. Think on what you want to do in the end, and then think if you really need the conversion to double.
Guillaume
2017년 9월 22일
편집: Guillaume
2017년 9월 22일
@Rik, I personally don't see any issue with having image index as the first or last dimension, imagestack(imageindex, row, col) works just as well as imagestack(row, col, imageindex). As long as it's clearly defined which dimension is what.
Convention is to have 3D matrices be (row,col,page) Except that mathworks went and threw away that convention with their image processing toolbox and went with (x, y) for many of their function which correspond to (col, row). The problems with conventions there are two many.
Assuming all the images are the same format (uint8 most likely) there is absolutely no issue converting the whole stack to double in one go with im2double. Note that all im2double does is divide the values by 255 (for uint8 inputs).
@Rahul, My first piece of advice would be to rename all your variables using full words that explain what they represent. As it is, it is very difficult for somebody to follow your code because they constantly have to remember what was in z1, j, p, etc.
For example, instead of z1 use imagestack, instead of j use imagecount, instead of p use framenumber. Instantly, the code is much easier to follow.
Second piece of advice, don't hardcode sizes of arrays. Matlab knows how big the arrays are, just ask it. That way when you'll want to reuse your code with 1280x1024 images you have nothing to do. It'll work straight away:
for k = 1:size(z1, 2) %row would be a better variable name than k
for l = 1:size(z1, 3) %col would be a better variable name than l
But of course, the loops are not needed
a = permute(z1(p, :, :), [2 3 1]);
And if you were stacking your images in the 3rd dimension instead of the 1st as advised by Rik, then it's simply:
a = z1(:, :, p);
As for your question, I don't understand it. Maybe give an example of what you want.
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!