I am using imgread to read binary 0,1 data and have to ultimately send it from an FPGA to a monitor using VGA Interface.The entire process would be greatly facilitated if I could add zeros to a 480x640 matrix(had to transpose original 640x480 because fprintf prints column wise ?).I want to know how to write 0 as 000 or 1 as 111 (corresponding to black and white RGB values)?
fid = fopen('3bitbinary.txt','w');
fprintf("","","");
fclose(fid);
Thanks!

 채택된 답변

Chaya N
Chaya N 2016년 10월 18일

1 개 추천

From what I understand, it sounds like you need to generate a m x n x 3 matrix and the easiest way might be to simply do a repmat of your matrix. It should look something like this:
output_matrix = repmat(input_matrix,1,1,3);
When writing to file, the values have to be written depth-wise so use:
fprintf('%d %d %d\n', permute(output_matrix,[3 2 1]))
I hope this helps!

댓글 수: 4

Matlab Student
Matlab Student 2016년 10월 18일
Your code will work perfectly well for RGB images, but I have simple zeros and ones and intend to initially keep them as such.
Example
0 0 1 1 1 0
1 1 0 1 0 0
0 1 0 0 1 1
1 0 0 0 0 1
0 0 0 1 1 1
I just want to save them as :
000
000
111
111
111
...
The idea is still the same. Since you need each binary 0 or 1 to be represented by 3 bits (in RGB style not regular binary), you would create an m x n x 3 matrix and write it to a file.
The code above will write your example into a file as:
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
...
I suppose that the confusion here is in the spaces between the bits in each row that is written into the file. To simply remove these spaces, you would skip the spaces between the format specifiers in the fprintf command. You could even skip creating an extra variable output_matrix and do it inline as:
fprintf('%d%d%d\n', permute(repmat(input_matrix,1,1,3),[3 2 1]))
This rigmarole would be greatly simplified if your read-in values from the FPGA are in vector form. Then again, MATLAB reads and writes data column-wise so you would have to watch out for the direction of your vector (row or column). The above code should be able to handle vectors as well as matrices as input and write it in the format you require.
Alternately, you could try the following:
fprintf('%d%d%d\n', permute(kron(input_matrix,[1 1 1]),[2 1]))
This works too.
Matlab Student
Matlab Student 2016년 10월 19일
Wonderful! Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 10월 19일

0 개 추천

Not sure what you're asking, but perhaps you're looking for padarray(). This function will add zeros to a layer around the outside edges of the image, whichever of the 4 sides you specify.

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2016년 10월 18일

댓글:

2016년 10월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by