Why image doesnt show correctly while reading a binary file?
Show older comments
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
yanqi liu
on 24 Dec 2021
yes,sir,may be upload some file to debug
Walter Roberson
on 24 Dec 2021
Especially if you can also show us an idea of what the intended image would look like.
fady sameh
on 24 Dec 2021
Walter Roberson
on 24 Dec 2021
Without the bin file and without the original image to guide us, there is not a lot we can do.
You should zip the .bin files and attach the .zip
fady sameh
on 24 Dec 2021
Accepted Answer
More Answers (2)
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
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')

Categories
Find more on Purple in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


