Writing array to binary file in a loop using fwrite

3 visualizaciones (últimos 30 días)
Hiten
Hiten el 13 de En. de 2021
Comentada: Hiten el 13 de En. de 2021
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 comentario
Hiten
Hiten el 13 de En. de 2021
UPDATE**
Solved by transposing the arrays during fwrite and fread as they write/read the arrays column-wise.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Read, Write, and Modify Image en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by