MATLAB Answers


How to create binary image file from text file with coordinates of points?

Asked by Jordanka
on 21 Nov 2012

Hello, I wrote a script creating number of files, each one containing x,y,z coordinates of four different points (practically a 3x4 matrix ). How can I now transform these files in binary image files? I would like then to stack each set of 24 binary images into single 24bit image in a way such that instead of being identified with one digit (0 or 1) each pixel should be identified with 24 (101111....), each digit corresponding to the value of the relevant pixel of every single binary image. Thank you!


I practically start from arrays, writing them into files. Here is the code I have written...

T_vect = 0:pi/s:pi-pi/s;

for i = 1:s

    T = T_vect(i);
        if ((T >= 0) && (T <= t)) || ((T >= pi - t) && (T <= pi-pi/s)) 
            X = [(-l/2), (l/2)];
            Y = [((-l/2)*tan(T)), ((l/2)*tan(T))];
            M = [X', Y', z; X', Y', z1];
            dlmwrite (strcat('MyM',num2str(i),'.dat'), M, '\t');
            X1 = [((w/2)*cot(T)), ((-w/2)*cot(T))];
            Y1 = [(w/2), (-w/2)];
            N = [X1', Y1', z; X1', Y1', z1];
            dlmwrite (strcat('MyM',num2str(i),'.dat'), N, '\t');


I don't like that someone will work the code out for me but just to indicate a function that might do it. Then I will read in the help and try to write the script myself. Many thanks

Are z and z1 constant?

You write two values for each (x,y) pair. What value should appear in the binary image at that (x,y) location?

And the values, z and z1, never change - they're the same for every row. What are those values and why are they constant?


No products are associated with this question.

2 Answers

Answer by Image Analyst
on 21 Nov 2012

I didn't quite follow what you're generating and don't know the range of your numbers or which one is supposed to be the red, green, and blue, or if your 24 bit image is an RGB image or a 24 bit grayscale image, or how you're defining "binary". In image processing, binary means a 0 or 1 logical/boolean image. If so, then that's not a 24 bit RGB image with 8 bits (0-255) for the red value, 8 bits for the green value, and 8 bits for the blue value. Maybe you mean binary, like all computer variables are binary, of course. I have no idea. So overall, it's not clear what you want. But in general you can instantiate a color image like this:

rgbImage = zeros(300, 400, 3, 'uint8');

and then assign values like this:

rgbImage(row, column, 1) = redValue;
rgbImage(row, column, 2) = greenValue;
rgbImage(row, column, 3) = blueValue;

Make sure the values are in the range 0-255 if you want to display the image with imshow() or image(). Scale them and cast to uint8 if you have to.

  1 Comment

Thank you, Image Analyst, for your response. I will try your suggestion. Sorry for not being sufficiently clear.

Answer by bym
on 21 Nov 2012

I don't know if this will help, but I thought I would try it. Basically it takes a 3 dimensional logical matrix (n x 24) and converts it to a n x 3 RGB matrix. I'm sure there are better implementations; just can't think of them now.

a = rand(20,20,24);
b = a < .15;   %create logical matrix
cb = char(b+48);
rgb = zeros(20,20,3);
for m = 1:3
    for c = 1:20
        for r = 1:20
            t = squeeze(cb(r,c,m*(1:8)))';
            rgb(r,c,m) = bin2dec(t);
imshow (rgb,'InitialMagnification','Fit')

  1 Comment

Thank you very much, Proecsm! I will report on the result as soon as I try it.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!