Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

how to create image from text file data.?

Asked by Lalit Patil

Lalit Patil

on 22 Nov 2012
Accepted Answer by Image Analyst

Image Analyst

http://speedy.sh/yCYux/patil142.txt

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

clc;

clear;

fileID = fopen('patil142.txt');

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

fclose(fileID);

x=cell2mat(C(:,1));

y=cell2mat(C(:,2));

x=x';

y=y';

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

4 Comments

Lalit Patil

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

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

Lalit Patil

on 22 Nov 2012

http://speedy.sh/cdEqJ/patil113.txt

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..

Lalit Patil

Lalit Patil

1 Answer

Answer by Image Analyst

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;
end

14 Comments

Walter Roberson

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

Lalit Patil

on 22 Nov 2012

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

Walter Roberson

Walter Roberson

on 22 Nov 2012

Which version? The one that skipped those rows?

Image Analyst

Image Analyst

Contact us