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 CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by