How write binary image pixel values to a text file such that each row contains only one pixel value of 8bit?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
vidya nand
am 11 Jan. 2022
Kommentiert: vidya nand
am 14 Jan. 2022
I am trying to read txt file using $readmemb in verilog , for this I need binary pixel values for one pixel in each row.
for e.g.
11010100
01011001
00101011
..... and so on
When I try writing to a txt file in matlab it fills all the display .How to do it?
clear all;
close all;
I_in=imread('C:\Users\ASUS\Documents\MATLAB\work\Mona_Lisa.jpg'); // I_in is a 256x256x3 image .
B = dec2bin(I_in); // B is a 196608 x 8 char matrix
fprintf(B);
Id2=fopen('img3.txt','wt');
fprintf(Id2,B);
fclose(Id2);
0 Kommentare
Akzeptierte Antwort
DGM
am 12 Jan. 2022
Bearbeitet: DGM
am 12 Jan. 2022
I'm no wizard with fprintf(), but this is one way to deal with it.
I_in=imread('peppers.png');
B = cellstr(dec2bin(I_in,8)); % make sure that the words are 8 char wide
fprintf('%s\n',B{1:5}) % display a sample of what's written to the file
% write to the file
Id2=fopen('img3.txt','wt');
fprintf(Id2,'%s\n',B{:});
fclose(Id2);
Alternatively, you could avoid the use of the cell array (might be faster)
I_in=imread('peppers.png');
B = dec2bin(I_in,8);
fprintf([repmat('%c',[1 8]) '\n'],B(1:5,:).')
% write to the file
Id2=fopen('img3.txt','wt');
fprintf(Id2,[repmat('%c',[1 8]) '\n'],B.');
fclose(Id2);
3 Kommentare
DGM
am 13 Jan. 2022
fname = 'img3.txt';
nbits = 8;
I_in = imread('peppers.png');
s = size(I_in);
B = dec2bin(I_in,nbits);
% write to the file
Id2 = fopen(fname,'wt');
fprintf(Id2,[repmat('%c',[1 nbits]) '\n'],B.');
fclose(Id2);
% read the file
Id2 = fopen(fname,'r');
A = fscanf(Id2,'%s');
fclose(Id2);
A = reshape(A,nbits,[]).';
I_out = uint8(bin2dec(A));
I_out = reshape(I_out,s);
imshow(I_out)
Note that it's necessary to know the size of the image in order to reconstruct it. Without this information, guesses can be made by finding the factors of the vector length and making some reasoned assumptions about practical aspect ratio and the channel arrangements.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Convert Image Type finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!