基于FPGA的图像最近邻插值算法verilog实现,包括tb测试文件和MATLAB辅助验证

89 阅读2分钟

1.算法运行效果图预览

7792a2945fee1fd590bc06beb0f1cb29_82780907_202403022019570015876240_Expires=1709382597&Signature=HjVCGIOH%2Bvn0MQIoQua3tMEsKfs%3D&domain=8.jpeg   e9419a71ee071b6e86f8c5ced57a22af_82780907_202403022019570108586686_Expires=1709382597&Signature=SMdjMpvbEi27KT2As2QOFDfMDVM%3D&domain=8.jpeg

将FPGA数据导入matlab显示图片,效果如下:

c565fb2500069feb5f3195e701733291_82780907_202403022019160642892074_Expires=1709382556&Signature=ld5GmJMsLIf6LsdhvPZHsIoMmYE%3D&domain=8.jpeg  

2.算法运行软件版本

vivado2019.2,matlab2022a

 

3.算法理论概述

         图像插值是一种图像处理技术,用于通过已知的像素值来估计未知位置的像素值。最邻近插值(Nearest Neighbor Interpolation)是其中最简单的一种插值方法。在这种方法中,未知位置的像素值被赋予与其最邻近的已知像素相同的值。最邻近插值算法的原理非常简单。对于目标图像中的每个像素点,计算其在源图像中对应的位置。由于源图像和目标图像的分辨率可能不同,因此这个位置可能不是整数坐标。最邻近插值算法会选择距离这个位置最近的整数坐标处的像素值,作为目标像素点的值。

 

        最近邻插值,是指将目标图像中的点,对应到源图像中后,找到最相邻的整数点,作为插值后的输出。如下图所示:

ad85b2af3a252a9c849976984657187a_82780907_202403022018200005371109_Expires=1709382500&Signature=lLqsKD4yhy6jgWP5B94RwkfGqtE%3D&domain=8.png  

        目标图像中的某点如果投影到原图像中的位置为点P,则此时取P最邻近点Q11,即 f ( P ) = f ( Q 11 ) f(P)=f(Q11) f(P)=f(Q11)。

 

       具体实现时,首先要确定目标图像的每个像素点在源图像中对应的位置,由于源图像和目标图像的分辨率可能不同,因此这个位置可能不是整数坐标。最邻近插值算法会选择距离这个位置最近的整数坐标处的像素值,作为目标像素点的值。

 

        在应用中,最邻近插值算法被广泛用于图像缩放等处理中。虽然其插值质量可能不如其他更复杂的插值算法,但由于其计算量小、实现简单,因此在许多实时图像处理应用中仍然被广泛使用。

 

 

 

 

4.部分核心程序 ``timescale 1ns / 1ps

//

// Company:

// Engineer:

//

// Create Date: 2022/07/28 01:51:45

// Design Name:

// Module Name: test_image

// Project Name:

// Target Devices:

// Tool Versions:

// Description:

//

// Dependencies:

//

// Revision:

// Revision 0.01 - File Created

// Additional Comments:

//

//

 

 

module test_image;

 

reg i_clk;

reg i_clk_4;

reg i_clk_2;

 

reg i_rst;

reg i_en;

reg [7:0] image_buff [0:100000];

 

wire [7:0] o_image;

 

integer fids,jj=0,dat;

 

//D:\FPGA_Proj\FPGAtest\codepz

 

initial

begin

        fids = $fopen("D:\FPGA_Proj\FPGAtest\code\data.bmp","rb");

        dat  = $fread(image_buff,fids);

        $fclose(fids);

end

 

initial

begin

i_clk=1;

i_clk_4=1;

i_clk_2=1;

i_rst=1;

#2000;

i_rst=0;

end

 

always #40  i_clk=~i_clk;

always #10  i_clk_4=~i_clk_4;

always #20  i_clk_2=~i_clk_2;

 

reg [7:0] II0;

always@(posedge i_clk)

begin

     if(i_rst)

     begin

         II0<=0;

         jj<=0;

     end

     else

     begin

     if(jj<=66536+1078 & jj>=1079)

     i_en<=1'b1;

     else

     i_en<=1'b0;

         II0<=image_buff[jj];

         jj<=jj+1;

         end

end

 

 

tops tops_u(

.i_clk              (i_clk),

.i_clk_4            (i_clk_4),

.i_clk_2            (i_clk_2),

.i_rst              (i_rst),

.i_en               (i_en),

.i_I0               (II0),

.o_image            (o_image)

);

 

reg[19:0]cnts;

always @(posedge i_clk_4 or posedge i_rst)

begin

     if(i_rst)

     begin

         cnts<=20'd0;

     end

else begin

         cnts<=cnts+20'd1;

     end

end 

 

 

 

integer fout1;

integer fout2;

initial begin

  fout1 = $fopen("flager.txt","w");

end

 

 

always @ (posedge i_clk_4)

 begin

    if(cnts <= 66514*4)

    begin

        $fwrite(fout1,"%d\n",o_image);

        end

        else

    begin

        $fwrite(fout1,"%d\n",0);

        end

       

end

 

endmodule`