MATLAB Answers

how to create image from text file data.?

Asked by Lalit Patil on 22 Nov 2012

This is the text file i have generated.. I want to create image from it.. It will be binary image..



fileID = fopen('patil142.txt');

C = textscan(fileID, '%f32 %f32');






I am able to read it only but don't know how to create image..?


Lalit Patil on 22 Nov 2012

I want 1:480 rows and 1:720 columns.. every row and column changes at 1 pixel as given in text file.. 0.5 pixels will be taken as round(0.5) = 1

Walter Roberson
on 22 Nov 2012

The text file you linked to has x running 1 to 480, but y in the range 333 to 340, and has no 0's in it. If you are hitting 0 then either there is a problem in the reading or you are reading different data than you linked to.

Lalit Patil on 22 Nov 2012

sorry, i forget to upload new text file that contains 0's at somewhere..

scatter(x,y) shows plot and i want image. and the second column contains pixel intensities, so, 0 means nothing will be showed in image..

only positive integer values will be shown in image..

1 Answer

Answer by Image Analyst
on 22 Nov 2012
 Accepted answer

Try this:

maxRows = max(y);
maxCols = max(x);
binaryImage = false(maxRows, maxCols);
for k = 1 : length(x)
  row = int32(y);
  col = int32(x);
  binaryImage(row, col) = true;


Walter Roberson
on 22 Nov 2012

What do you want to have happen where there are zeros? Put something in the left margin?

binaryImage = logical(accumarray( fix([x(:), 1+y(:)]), 1, [480 720]) );

or skip that row?

nz = y ~= 0;
x = x(nz);
y = y(nz);
binaryImage = logical(accumarray( ceil([x(:), y(:)]), 1, [480 720]) );
Lalit Patil on 22 Nov 2012

Yes this is the thing which i want.. Now the problem get solved...

Walter Roberson
on 22 Nov 2012

Which version? The one that skipped those rows?

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!