首页
学习
活动
专区
圈层
工具
发布

基于FPGA的图像差分处理

基于FPGA的图像差分处理

1背景知识

差分图像就是目标场景在连续时间点图像相减所构成的图像,广义的差分图像定义为目标场景在时间点tk和tk+L所成图像的差别。差分图像是由目标场景在相邻时间点的图像相减得到的,从而能够得到目标场景随时间的变换。

差分图像在许多领域得到了广泛的应用,比如:视频压缩,生物医学诊断,天文学,遥感,人脸识别等。

2 matlab仿真

MATLAB源码:

Main.m

I = imread('flower.bmp'); figure, imshow(I);

I_gray = rgb2gray(I);figure,imshow(I_gray);

Id = mipcentraldiff(I_gray,'dx'); figure, imshow(Id);

Mipcentraldiff.m

function dimg = mipcentraldiff(img,direction)

% MIPCENTRALDIFF Finite difference calculations

%

% DIMG = MIPCENTRALDIFF(IMG,DIRECTION)

%

% Calculates the central-difference for?a given direction

% IMG : input image

% DIRECTION : 'dx'?or 'dy'

% DIMG : resultant image

%

% See also MIPFORWARDDIFF MIPBACKWARDDIFF MIPSECONDDERIV

% MIPSECONDPARTIALDERIV

% Omer Demirkaya, Musa Asyali, Prasana Shaoo, ...

% Medical Image Processing Toolbox

img = padarray(img,[1 1],'symmetric','both');

[row,col] = size(img);

dimg = zeros(row,col);

switch(direction)

case'dx',

dimg(:,2:col-1) = (img(:,3:col)-img(:,1:col-2))/2;

case'dy',

dimg(2:row-1,:) = (img(3:row,:)-img(1:row-2,:))/2;

otherwise,

disp('Direction is unknown');

end

dimg = dimg(2:end-1,2:end-1);

仿真结果:

图1 RGB原图

图2 gray

图3 central_diff

3 FPGA设计

图4 基于串口传图的中心差分

如图4所示,我们将RGB565格式转化为Ycbcr格式,Y通道进入中心差分模块,完成中心差分算法。

FPGA源码:

/*

Module name:

Description:

Data: 2018/03/16

Engineer: lipu

e-mail: 137194782@qq.com

微信公众号: FPGA开源工作室

*/

////////////////////////////////////////////////////////////////

wire [15:0] rgb;

wire hs;

wire vs;

wire de;

wire o_hs;

wire o_vs;

wire o_de;

wire[7 : 0]o_y_8b;

wire[7 : 0]o_cb_8b;

wire[7 : 0]o_cr_8b;

//assign TFT_rgb = {o_y_8b[7:3],o_y_8b[7:2],o_y_8b[7:3]}; //Y

//assign TFT_rgb = {o_cb_8b[7:3],o_cb_8b[7:2],o_cb_8b[7:3]}; //cb

//assign TFT_rgb = {o_cr_8b[7:3],o_cr_8b[7:2],o_cr_8b[7:3]}; //cr

//////////////////////////////////////////////////////////////

tft_ctrl tft_ctrl(

.Clk9M(clk9M),//系统输入时钟9MHZ

.Rst_n(Rst_n),//复位输入,低电平复位

.data_in({Rd_data[7:0],Rd_data[15:8]}),//待显示数据

.hcount(),//TFT行扫描计数器

.vcount(),//TFT场扫描计数器

.TFT_RGB(rgb),//TFT数据输出

.TFT_HS(hs),//TFT行同步信号

.TFT_VS(vs),//TFT场同步信号

.TFT_CLK(TFT_clk),//TFT像素时钟

.TFT_DE(de),//TFT数据使能

.TFT_begin(tft_begin),

.TFT_PWM(TFT_pwm)//TFT背光控制

);

rgb_to_ycbcr rgb_to_ycbcr_inst(

.clk(TFT_clk),

.i_r_8b({rgb[15:11],3'b0}),

.i_g_8b({rgb[10:5],2'b0}),

.i_b_8b({rgb[4:0],3'b0}),

.i_h_sync(hs),

.i_v_sync(vs),

.i_data_en(de),

.o_y_8b(o_y_8b),

.o_cb_8b(o_cb_8b),

.o_cr_8b(o_cr_8b),

.o_h_sync(o_hs),

.o_v_sync(o_vs),

.o_data_en(o_de)

);

reg [7:0] diff_data;

reg [7:0] o_y_8b_r0;

reg [7:0] o_y_8b_r1;

reg [7:0] o_y_8b_r2;

reg hs0;

reg hs1;

reg hs2;

reg vs0;

reg vs1;

reg vs2;

reg de0;

reg de1;

reg de2;

always @(posedge TFT_clk or negedge Rst_n) begin

if(!Rst_n) begin

hs0 <= 0;

hs1 <= 0;

hs2 <= 0;

vs0 <= 0;

vs1 <= 0;

vs2 <= 0;

de0 <= 0;

de1 <= 0;

de2 <= 0;

o_y_8b_r0 <= 0;

o_y_8b_r1 <= 0;

o_y_8b_r2 <= 0;

end

else begin

hs0 <= o_hs;

hs1 <= hs0;

hs2 <= hs1;

vs0 <= o_vs;

vs1 <= vs0;

vs2 <= vs1;

de0 <= o_de;

de1 <= de0;

de2 <= de1;

o_y_8b_r0 <= o_y_8b;

o_y_8b_r1 <= o_y_8b_r0;

o_y_8b_r2 <= o_y_8b_r1;

end

end

always @(posedge TFT_clk or negedge Rst_n) begin

if(!Rst_n)

diff_data <= 0;

else if(de2)

diff_data <= (o_y_8b_r2 - o_y_8b_r0) >>1;

else

diff_data <= diff_data;

end

assign TFT_rgb = {diff_data[7:3],diff_data[7:2],diff_data[7:3]};

assign TFT_de = de1;

assign TFT_hs = hs1;

assign TFT_vs = vs1;

Endmodule

结果展示:

图5 FPGA中心差分结果

如图5所示,由于手机拍摄原因,图片不是很清晰,但基本结果一致,实验成功。我们将把中心差分模块移植到基于ov5640的实时图像采集系统完成rgb三通道的彩色输出。

图6 基于ov5640的r/g/b通道彩色实时输出中心差分

实验结果视频:

实验结果成功,部分带有彩色。

举报
领券