convert image into 3XN matrix

조회 수: 1 (최근 30일)
bes
bes 2012년 9월 14일
I have a dem data(an image of 6000X5200). I need to convert the data and create a 3XN matrix with row number, coloum number and Z value (pixel value of the image represent the Z value).
orgim = imread('dem'); % read DEM data
nentry = 0;
for ii = 1:size(orgim,2)
for jj = 1:size(orgim,1)
nentry = nentry +1;
mydata(nentry, :) = [ii, jj, orgim(jj,ii)];
clear X Y Z;
end
end
This works but takes time. Is there any optimum way to convert the multi-dimentional 6000X 5200 matrix as 3XN matrix, without loop
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2012년 9월 14일
[x,y] = ndgrid(1:size(orgim,1),1:size(orgim,2));
mydata = [y(:),x(:),orgim(:)];
Jan
Jan 2012년 9월 14일
"clear X Y Z"?! This line can obviously omitted.

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 9월 14일
편집: Andrei Bobrov 2012년 9월 14일
[x,y] = ndgrid(1:size(orgim,1),1:size(orgim,2));
mydata = [y(:),x(:),orgim(:)];
or
mydata = [fliplr(fullfact(size(orgim))), orgim(:)];

추가 답변 (2개)

Jan
Jan 2012년 9월 14일
Although Andrei's solution is smarter, I suggest the pre-allocation as general programming schema:
orgim = imread('dem'); % read DEM data
mydata = zeros(numel(orgim), 3)); % Pre-allocate!!!
nentry = 0;
for ii = 1:size(orgim,2)
for jj = 1:size(orgim,1)
nentry = nentry +1;
mydata(nentry, :) = [ii, jj, orgim(jj,ii)];
end
end
This can be noticably faster already.

bes
bes 2012년 10월 3일
Thanks all. Your comments helped me a lot.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by