How write binary image pixel values to a text file such that each row contains only one pixel value of 8bit?

6 ビュー (過去 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);

採用された回答

DGM
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
00111110 00111111 01000001 00111111 00111111
% 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,:).')
00111110 00111111 01000001 00111111 00111111
% write to the file
Id2=fopen('img3.txt','wt');
fprintf(Id2,[repmat('%c',[1 8]) '\n'],B.');
fclose(Id2);
  3 件のコメント
DGM
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 ExchangeConvert Image Type についてさらに検索

製品


リリース

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by