1. 程式人生 > 其它 >Matlab/Modelsim影象聯合模擬平臺

Matlab/Modelsim影象聯合模擬平臺

FPGA影象模擬平臺

1 引言

在使用modelsim進行影象演算法的功能模擬時,無法得到影象的實時預覽,因此直觀性有所欠缺。因此可配合matlab使用,通過modelsim讀出txt格式的影象,利用matlab進行轉換與顯示,從而既可驗證時序關係,又可直觀看到演算法的效果。

2 matlab程式碼

2.1 圖片讀取及通道轉換

注意事項:圖片與程式需放到同一資料夾內

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ProjectName : ycbcrcode.m
% Author : Huhao
% Description : image to txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc

img = imread('card.jpg');
ycbcr = rgb2ycbcr(img);

y=ycbcr(:,:,1);
cb=ycbcr(:,:,2);
cr=ycbcr(:,:,3);

2.2 圖片轉化為txt格式

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ProjectName : rgb2txt.m
% Author : Huhao
% Description : rgb to text
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc

img = imread('card.jpg');

r=img(:,:,1);
g=img(:,:,2);
b=img(:,:,3);

figure,
subplot(221),imshow(r),title('R');
subplot(222),imshow(g),title('G');
subplot(223),imshow(b),title('B');
subplot(224),imshow(img),title('RGB img');

r1=r';
g1=g';
b1=b';

fid1=fopen('img_r.txt','wt');
% fprintf(fid1,'%x\n',r1);%hexadecimal
fprintf(fid1,'%g\n',r1);%Binary
fclose(fid1); 

fid2=fopen('img_g.txt','wt');
% fprintf(fid2,'%x\n',g1);
fprintf(fid2,'%g\n',g1);
fclose(fid2); 

fid3=fopen('img_b.txt','wt');
% fprintf(fid3,'%x\n',b1);
fprintf(fid3,'%g\n',b1);  
fclose(fid3);  
  

2.3 modelsim輸出的txt文件重新轉化為圖片並顯示

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ProjectName : txt2rgb.m
% Author : Huhao
% Description : text to rgb
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
clc

r_load = load('img_r.txt'); 
g_load = load('img_g.txt'); 
b_load = load('img_b.txt'); 

length = 760;%image length
width  = 397;%image width
 
r2 = reshape(r_load,[length, width]);
r2 = uint8(r2');
g2 = reshape(g_load,[length, width]);
g2 = uint8(g2');
b2 = reshape(b_load,[length, width]);
b2 = uint8(b2');

rgb_img(:,:,1)=r2;
rgb_img(:,:,2)=g2;
rgb_img(:,:,3)=b2;

figure,
subplot(221),imshow(r2),title('R');
subplot(222),imshow(g2),title('G');
subplot(223),imshow(b2),title('B');
subplot(224),imshow(rgb_img),title('RGB img');

3 結果顯示

3.1 圖片轉化為txt結果

圖1 rgb to txt三通道圖片

圖2 rgb to txt得到的三通道txt資料

3.2 txt重新轉化為圖片結果

圖3 txt to rgb三通道圖片