conversion of image file into binary txt file

23 views (last 30 days)
megha
megha on 18 Dec 2013
Answered: Image Analyst on 18 Dec 2013
I have converted the image file into binary txt file using this: I = imread ('app-log-icon.png') Y = rgb2gray(I) level = graythresh(Y) BW = im2bw(Y,level) Z = imresize(Y,[40 40]) dlmwrite('C:\Users\Prasann\Desktop\new.txt',BW,' ') But i dont want any space or Comma between two elements so how should i proceed with this problem as i have to call this binary txt file in my verilog coding thank u

Answers (2)

Thomas
Thomas on 18 Dec 2013
Hi megha
You can do the following
Urbinary = [0 1 0 0 1 0 1 0; % it says hi by the way!
0 1 0 0 1 0 1 0;
0 1 1 1 1 0 1 0;
0 1 0 0 1 0 1 0;
0 1 0 0 1 0 1 0]
[rows, cols] = size(Urbinary);
bins = cell(rows*cols,1);
bin_vect = reshape(Urbinary, rows*cols,1);
for i = 1:size(bin_vect)
bins{i} = num2str(bin_vect(i)); % convert to strings and put in a cell
end
bin_shape = reshape(bins,rows,cols);
char_bin = reshape(horzcat(bin_shape{:}),rows,cols); concatenate and reshape back to original
% write to desired location
[filename, pathname] = uiputfile('*.txt', 'Locate directory for to write .txt file to');
outpath = strcat(pathname,filename);
fid = fopen(outpath,'w');
for i=1: size(char_bin,1)
fprintf(fid,'%s\r\n',char_bin(i,:));
end
fclose(fid);
Hope this helps
Thomas

Image Analyst
Image Analyst on 18 Dec 2013
Can't you just do
fprintf(fid, '%d ', Z); % Write 0 or 1 a followed by a space.
? It should write out the whole array that way in a single call to fprintf().

Tags

Community Treasure Hunt

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

Start Hunting!