Why image doesnt show correctly while reading a binary file?

10 views (last 30 days)
fady sameh
fady sameh on 24 Dec 2021
Edited: DGM on 25 Dec 2021
im trying to take an image saved in a binary formate and change it to a 2D matrix and print it as its orginal image but what ig get is a gray image with black strips , and i check that 2D array is holding atctual pixel numbers of the image.
here is the code for reading binary file:
function [M, M1]=read_raw(filename1, filename2)
%Storing size of image
% if filename1(1)=='c'
% row =352; col=288;
% row1 = 352; col1=288;
% else
% row = 720; col=480;
% row1 = 720; col1=480;
% end
row=160;
col=90;
%Reading the two files inputted in raw format in a 2D matrix
%For First File
if true
X = fopen(filename1, 'r');
I = fread(X, row*col, 'uint8=>uint8');
Z = reshape(I, row, col);
Z = Z';
Z
figure(1);
subplot(211);
imshow(Z);
title(filename1);
M = Z;
end
%For Second File
if true
X1 = fopen(filename2, 'r');
I1 = fread(X1, row*col, 'uint8=>uint8');
Z1 = reshape(I1, row, col);
Z1 = Z1';
figure(1);
subplot(212);
imshow(Z1);
title(filename2);
M1 = Z1;
end
end
output:
  5 Comments

Sign in to comment.

Accepted Answer

DGM
DGM on 24 Dec 2021
Edited: DGM on 25 Dec 2021
The files are just literal text files with a .bin extension. The easy way to deal with that is just rename the file. If you want to avoid renaming the files, check out Stephen's comment below.
row = 160;
col = 90;
filename1 = 'Rsaved1.txt';
filename2 = 'Rsaved2.txt';
I = readmatrix(filename1);
Z1 = reshape(uint8(I), row, col).';
I = readmatrix(filename2);
Z2 = reshape(uint8(I), row, col).';
imshow(Z1)
figure
imshow(Z2)
The clue was that the file contained way more bytes than would be necessary for a uint8 image of that geometry. A check with a hex editor helps a lot in figuring out what exactly a .bin file is.
The file represents the uint8 pixel values in decimal, but since there is no fractional part, the file is filled with a bunch of extraneous zeros.
EDIT
See comment below
row = 160;
col = 30;
filename1 = 'Rsaved1.txt';
filename2 = 'Rsaved2.txt';
I = readmatrix(filename1);
Z1 = permute(reshape(uint8(I), row, col, 3),[2 1 3]);
I = readmatrix(filename2);
Z2 = permute(reshape(uint8(I), row, col, 3),[2 1 3]);
figure
imshow(Z1)
figure
imshow(Z2)
  7 Comments
DGM
DGM on 25 Dec 2021
It's grayscale because that's what you indicated it would be with the geometry. The code you posted indicating how the file was generated dictates that the file has no color information. You only wrote the red channel to the file.
Rframe = ones(4,4,3).*permute([1 2 3],[1 3 2])
Rframe =
Rframe(:,:,1) = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Rframe(:,:,2) = 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 Rframe(:,:,3) = 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
numel(Rframe)
ans = 48
fileID=fopen('test.txt','w');
for r=1:size(Rframe,1) % rows
for c=1:size(Rframe,2) % columns
fprintf(fileID,"%f\n",Rframe(r,c)); % only the first channel
end
fprintf(fileID,"\n");
end
fclose(fileID);
A = readmatrix('test.txt');
numel(A)
ans = 16
[min(A) max(A)]
ans = 1×2
1 1
Dim 3 of the array is never addressed. You'd probably have a third loop.
If it were pagewise RGB, then you could use the edit above. It's as simple as changing the specified geometry of the output array. The call to permute() is the same as elementwise transposition, but the .' operator only works on 2D arrays.
That said, I would question why any of this is necessary. Why convert an image to a text file and then back? Why write integers using %f instead of %d? Is this to feed to another program that takes formatted text images? I'd have to ask, because if that's so, then that would dictate how the color information needs to be arranged in the file. It may typically be pixelwise (RGBRGBRGB) or pagewise (RRRGGGBBB), though color order may vary.

Sign in to comment.

More Answers (2)

yanqi liu
yanqi liu on 24 Dec 2021
close all;
clear all; clc;
a = load('Rsaved1.bin');
a = reshape(a, 160, 90);
b = load('Rsaved2.bin');
b = reshape(b, 160, 90);
figure; imshow(a', [])
figure; imshow(b', [])

Image Analyst
Image Analyst on 25 Dec 2021
Try this:
col = 30;
fileName = 'Rsaved1.txt';
I = readmatrix(fileName);
% r = I(1:3:end);
% g = I(2:3:end);
% b = I(3:3:end);
% Assume all red first, then all the green, and finally all the blue.
r = I(1:4800);
g = I(4801:2*4800);
b = I(2*4800+1:end)
r = reshape(uint8(r), [], col).';
g = reshape(uint8(g), [], col).';
b = reshape(uint8(b), [], col).';
rgbImage = cat(3, r, g, b);
imshow(rgbImage, 'InitialMagnification', 300);
axis('on', 'image')

Community Treasure Hunt

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

Start Hunting!