Writing array to binary file in a loop using fwrite

3 views (last 30 days)
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 Comment
Hiten
Hiten on 13 Jan 2021
UPDATE**
Solved by transposing the arrays during fwrite and fread as they write/read the arrays column-wise.

Sign in to comment.

Answers (0)

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!