convert image pixels to binary file

47 次查看(过去 30 天)
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
编辑: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 个评论
Guillaume
Guillaume 2019-8-14
编辑: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 个评论
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 个评论
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Computer Vision Toolbox Supported Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by