MATLAB Answers

convert image pixels to binary file

190 ビュー (過去 30 日間)
vafa knm
vafa knm 2019 年 8 月 14 日
コメント済み: Walter Roberson 2019 年 8 月 15 日
Hi! I have an image size 768*1024 and I want to convert all pixel values to a binary file. I write this:
" image = imread ( 'F:\out.png');
im_gry = rgb2gray(image); "
so I have these pixel values:
a.PNG
Now how I can convert this values to a binary file?!

  2 件のコメント

Guillaume
Guillaume 2019 年 8 月 14 日
You need to explain what you mean by binary file. As you can see from the answers, it can be interpreted many ways. After all, the 'F:\out.png' you started with is already a binary file.
vafa knm
vafa knm 2019 年 8 月 14 日
first sorry about my poor english!
actually I am working on a project based on FPGA! I should read this image in FPGA and when I search about that, the answer is that FPGA can not read image and I have to convert the image to binary file. So the integer values of pixel should convert to 8 binary bits.
Can you explain more about your last sentence? I just read the image from drive 'F'. How it can be a binary file?

サインインしてコメントする。

回答 (3 件)

Neuropragmatist
Neuropragmatist 2019 年 8 月 14 日
If you mean you want to convert the image intensity values to an actual binary representation you might want to look at:
or
For some reason by binary I think you might mean logical (i.e. you want a matrix the same size containing only true or false [0 1] values). For that you will want to look at:
Or probably what you want considering the range of values in the image:
Hope this helps,
M.

  4 件のコメント

表示 1 件の古いコメント
Neuropragmatist
Neuropragmatist 2019 年 8 月 14 日
OK, looking at the link you gave Guillaume below you can read your image and save it as a text file which looks like the one in the example like this:
img = dec2bin(rgb2gray(imread('peppers.png')));
fid = fopen('test_binary_file.txt','w');
fprintf(fid,'%c%c%c%c%c%c%c%c\r\n',img');
fclose(fid);
The contents look like this:
...
01100111
10101010
00101010
00000000
01111001
00000001
11111111
11111101
10100111
11111111
11110000
10111111
...
Next it says:
"Then, save the image binary file as "IMAGE_FILE.MIF" and put it to the project folder. Now, write a VHDL code to read this image binary text file and initialize it into a block memory during synthesis or simulation. Below is the VHDL code for reading image files into FPGA. The code is synthesizable."
So Guillaume was right that the file has to be initialised into block memory, so you should follow the rest of the instructions to do that.
Hope this helps,
M.
Guillaume
Guillaume 2019 年 8 月 14 日
Careful!
fprintf(fid,'%c%c%c%c%c%c%c%c\r\n',img);
If you do that, you'd better transpose img. Remember that matlab is column-major, the above is equivalent to:
fprintf(fid,'%c%c%c%c%c%c%c%c\r\n',img(:));
so you'll be priting the 0/1 along the columns instead of the rows as you intended.
Oh, and I'll repeat: THE VHDL code shown in the link is not code that will run on the FPGA. As metioche citation above says "initialize it into a block memory during synthesis or simulation". The file reading will be done by the computer doing the synthesis or simulation, not by the FPGA.
Neuropragmatist
Neuropragmatist 2019 年 8 月 14 日
Oh drat you're right! Good catch. I've edited that.
I usually stay away from clunky fprintf and text files in general so this is good practice for me.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2019 年 8 月 14 日
fopen the file. fwrite(fid, im_gry, 'uint8'). fclose()

  3 件のコメント

Neuropragmatist
Neuropragmatist 2019 年 8 月 14 日
Hmmm, you think the OP literally means a binary file as opposed to an image? I didn't think about that. Aren't all files binary in that sense though?
Walter Roberson
Walter Roberson 2019 年 8 月 14 日
Yes, I think that. The other question from the user involves writing out windows of pixels as a text file, so I believe that they are doing some processing that is based upon pixel values.
vafa knm
vafa knm 2019 年 8 月 14 日
first sorry about my poor english!
actually I am working on a project based on FPGA! I should read this image in FPGA and when I search about that, the answer is that FPGA can not read image and I have to convert the image to binary file. So the integer values of all pixels should convert to 7 or 8 binary bits.

サインインしてコメントする。


Guillaume
Guillaume 2019 年 8 月 14 日
編集済み: Guillaume 2019 年 8 月 14 日
You don't mean file at all. FPGAs can't access files.
If your FPGA need access to the whole image, the best approach would probably be to store the image pixels in the FPGA block memory (using logic gates for that would be expensive). How you ge the image into the block memory is down to whatever chip is connected to your FPGA. The img_gry array that you show in your question could be stored as is in an FPGA block memory.
I think you need to learn a bit more about FPGAs.

  3 件のコメント

vafa knm
vafa knm 2019 年 8 月 14 日
FPGA can read from files! You can read more here:
Guillaume
Guillaume 2019 年 8 月 14 日
I'll repeat: FPGAs can't access files. FPGAs are just a bunch of logic gates, memory blocks, usually some DSP blocks and reconfigurable connections to link all these and the outside world.
The VHDL in your link is not synthesisable. The file IO bit is not code that can run on an FPGA, but it can be used to test your FPGA design. Note that all the code does is read from a file to initialise the memory block of the FPGA (as I suggested).
As far as using vhdl textio to read an image from a file, using a bit-vector seems like a waste of time. You could just read the pixels as integer.But that's a question for a VHDL forum.
Anyway, to generate the text file for that code you link:
pixels = rgb2gray(imread('F:\out.png')); %read image
pixels_bin = cellstr(dec2bin(pixels, 8)); %convert to textual representation of binary
fid = fopen('F:\out.mif', 'w'); %open output file
assert(fid > 0, 'File open failed');
fprintf(fid, '%s\n', pixels_bin{:}); %write one pixel per line
fclose(fid);
Note that since this is matlab the pixels are written in column-major order. If you want row-major transpose pixels in the dec2bin call.
Walter Roberson
Walter Roberson 2019 年 8 月 15 日
Normally I would suggest that someone doing this kind of work look at the Vision HDL toolbox, https://www.mathworks.com/help/hdlcoder/vision-hdl-toolbox.html which has facilities for converting 2D images into pixel streams for input into FPGA and re-arrangement into an image once inside the FPGA.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by