How write binary image pixel values to a text file such that each row contains only one pixel value of 8bit?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
DGM
2022-1-12
编辑:DGM
2022-1-12
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 个评论
DGM
2022-1-13
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!