Writing array to binary file in a loop using fwrite

4 次查看(过去 30 天)
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