Total Pageviews


October 2, 2016

Converting Image format in a Binary Text Stream

by 4hathacker  |  in Image Processing at  9:48 AM

Images are found in several different formats like .png, .jpg, .jpeg, .gif, etc. These formats are the standards defined for storing the pixel information. This information may be compressed using a compression algorithm or may be uncompressed using another algorithms. But what if we want to play with the bits directly. 

For example, in one of my college projects when I was dealing with hardware implementation for image processing algorithm, it was necessary to have information of pixels in bits to perform digital processing like "AND", "OR", etc. This information,in the form of binary 0s and 1s, was sent to the logic circuit designed . 

In this article, we are going to discuss how to convert an image format and save it in a binary stream in a text file. The algorithm involved uses a number of different functions to read an image, grayscale conversion, thresholding, binary conversion and many more.

In the first few steps, read the image and save it in a variable. Then, convert the image in grayscale and do some thresholding. And then use, im2bw() for binary conversion. Now, there is a need of resizing the original image according to the requirement. I have given the resizing rows and columns as 40. Using a function dlmwrite(), the binary information is to be written in ASCII format to separate matrix elements.

clc; clear all; close all;
I = imread ('peppers.png');             % to read image and store image matrix in variable I
Y = rgb2gray(I);                             % to convert rgb image to grayscale and store it in Y
level = graythresh(Y);                    % for global threshold level
Binary = im2bw(Y,level);              % image conversion to binary
Z = imresize(Y,[40 40]);                % resize image with no. of rows and cols
dlmwrite('E:\new1.txt',Binary,' ');  % writes matrix Binary into an ASCII format file, using delimiter                                                          % to separate matrix elements

Until this, our work is half done. The elements are to be read column-wise with the content of each stream of binary in a separate cell. The information yet obtained is to be saved in a binary vector array i.e., bin_vect having appropriate size which then further converted to string.

[rows, cols] = size(Binary);          % to find size of Binary
 bins = cell(rows*cols,1);             % creates cell array of empty matrices of given size
 bin_vect = reshape(Binary, rows*cols ,1); 
                                                      % gives rows*cols size matrix whose 
                                                      % elements are taken columnwise from Binary
 for i = 1:size(bin_vect) 
     bins{i} = num2str(bin_vect(i));  % convert to strings and put in a cell

Finally, reshape the image information and using some file handling to save the information completes the task.

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)


This whole task is to be done in a single program to achieve a binary image stream of 0s and 1s which then could be used for image operations like dilation, erosion, edge detection,etc. using a logical circuit implementation. The resulting text file opened in notepad looks like the image given above.


Like Our Facebook Page

Nitin Sharma's DEV Profile
Proudly Designed by 4hathacker.