Writing array to binary file in a loop using fwrite

조회 수: 3 (최근 30일)
Hiten
Hiten 2021년 1월 13일
댓글: Hiten 2021년 1월 13일
Hi,
I am trying to write the indices of an image in a binary file uising fwrite. The indices of the entire image are written as a list of [row, col, image_number]. The fwrite and fread functions work for 1 image but for multiple images, the data seems to be distorted.
The input image and the output plot is attached. Output is supposed to be a stack of the same image but somehow they are rotated.
Thanks,
input image -
output plot -
Here is the code:
rounding_div_xy = 10;
img_in = imread('liver2d.png');
if size(img_in,3)>1
img_in = img_in(:,:,1);
end
img_in = imbinarize(img_in);
fid_pcloud = fopen('pcloud_xy.bin','w');
[rys,cxs,vs] = find(img_in>0); % -> checked that rys and cxs are 'double'
pcloud_xy_ctr = 0;
for z = 0:0.2:2
pcloud_xy_solid = [0,0,0];
for q = 1:length(rys)
pcloud_xy_ctr = pcloud_xy_ctr+1;
pcloud_xy_solid(q,:) = [cxs(q)/rounding_div_xy,rys(q)/rounding_div_xy,z];
end
fwrite(fid_pcloud,pcloud_xy_solid,'double');
end
fclose(fid_pcloud);
% reading the file ---------------------------------
fid_pcloud = fopen('pcloud_xy.bin','r');
aa = fread(fid_pcloud,[pcloud_xy_ctr,3],'double');
fclose(fid_pcloud);
pcshow(aa);
  댓글 수: 1
Hiten
Hiten 2021년 1월 13일
UPDATE**
Solved by transposing the arrays during fwrite and fread as they write/read the arrays column-wise.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by