Matlab/Modelsim图像联合仿真平台

时间:2022-08-21 07:45:54

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结果

Matlab/Modelsim图像联合仿真平台

图1 rgb to txt三通道图片

Matlab/Modelsim图像联合仿真平台

图2 rgb to txt得到的三通道txt数据

3.2 txt重新转化为图片结果

Matlab/Modelsim图像联合仿真平台

图3 txt to rgb三通道图片