本帖最后由 UT发布 于 2025-4-3 08:55 编辑
软件版本:Anlogic -TD5.6.1-64bit 操作系统:WIN10 64bit 硬件平台:适用安路(Anlogic)FPGA 1概述本文简述了图像二值化的算法,讲解如何进行Verilog的算法实现,并进行上板实验。 2算法原理简介图像二值化是将图像原先的颜色转变成黑、白两种颜色,设置不同的转换阈值得到不同的二值图,求取二值化阈值的最佳算法是大津法。 Otsu(大津法)算法是由日本学者OTSU于1979年提出的一种对图像进行二值化的高效算法。原理上来讲,该方法又称作最大类间方差法,因为按照大津法求得的阈值进行图像二值化分割后,前景与背景图像的类间方差最大,是二值化的最佳全局阈值的算法。 3算法仿真
3.1Matlab算法仿真
3.1.1Matlab算法代码分析
- clear;clear all;clc;
-
- image_in = imread('lena_1280x720.jpg');
- [row,col,n] = size(image_in);
- image_gray=rgb2gray(image_in);
- threshold = graythresh(image_gray);
-
- image_ostu1=im2bw(image_gray,threshold);
- threshold = threshold *255;
- subplot(311);
- imshow(image_gray); title('the image gray image');
- subplot(312);
- imshow(image_ostu1); title('the image ostu1 image');
-
- N=row*col;
- L=256;
-
- for i=1:L
- count(i)=length(find(image_gray==(i-1)));
- p(i)=count(i)/(row*col);
- pp(i)=count(i);
- end
-
- m_g=0;
-
- for i=1:L
- m_g=m_g+p(i)*(i-1);
- m(i)=m_g;
- end
-
- for i=1:L
- p_k(i)=sum(p(1:i));
- end
-
- k=(m_g*p_k-m).^2./(p_k.*(1-p_k));
-
-
-
- m_g1=0;
- for i=1:L
- m_g1=m_g1+pp(i)*(i-1);
- m1(i)=m_g1;
- end
-
- for i=1:L
- pp_k(i)=sum(pp(1:i));
- end
- kk=((fix(m_g1/(row*col)))*(fix(pp_k/4096))-(fix(m1/4096))).^2./((fix(pp_k/4096)).*(225-(fix(pp_k/4096))));
-
- t=(m_g1/(row*col))*(pp_k/4096)-(m1/4096);
- t1=225-pp_k/4096;
- t2=(pp_k/4096).*(225-pp_k/4096);
- t3=(m1/4096);
- t4=(pp_k/4096);
-
- kkk=((m_g1/(row*col))*(pp_k/(row*col))-(m1/(row*col))).^2./((pp_k/(row*col)).*(1-pp_k/(row*col)));
-
- [y1,th1]=max(kk);
- [y2,th2]=max(kkk);
-
- [y,th]=max(k);
-
- for i=1:row
- for j=1:col
- if image_gray(i,j)>th
- image_ostu2(i,j)=255;
- else
- image_ostu2(i,j)=0;
- end
- end
- end
-
- subplot(313);
- imshow(image_ostu2); title('the image ostu2 image');
复制代码
3.1.2Matlab实验结果
3.2Verilog算法仿真
3.2.1Modelsim仿真
3.2.1.1仿真执行在件夹Algorithm_simulation下进行算法的仿真,分为sim,src和tb三个子文件夹。在sim文件夹下有win系统的快捷执行文件sim.bat,可以一键进行仿真,src文件下放的是Verilog的核心图像算法及其顶层与输入图像激励,tb文件下放的是测试激励文件及输出图像的保存。 双击执行sim文件夹下sim.bat,自动打开Modelsim仿真,自动添加仿真波形,执行完成后自动保存图像,仿真波形如图所示:
3.2.1.2仿真关键部分代码解析- #
- # Create work library
- #
- vlib work
- #
- # Compile sources
- #
- vlog "../src/*.v"
- vlog "../tb/*.v"
- #
- # Call vsim to invoke simulator
- #
- vsim -voptargs=+acc work.top_tb
- #
- # Add waves
- #
- do wave.do
- #
- # Run simulation
- #
- run -all
- #
- # End
复制代码
- reg en;
- reg [12:0] h_syn_cnt = 'd0;
- reg [12:0] v_syn_cnt = 'd0;
- reg [23:0] image [0 : H_ACTIVE*V_ACTIVE-1];
- reg [31:0] image_cnt = 'd0;
-
- //读取txt文件到image数组中
- initial begin
- $readmemh("../matlab_src/image_720_1280_3.txt", image);
- end
-
- // 行扫描计数器
- always@(posedge i_clk)
- begin
- if(h_syn_cnt == H_TOTAL_TIME-1)
- h_syn_cnt <= 0;
- else
- h_syn_cnt <= h_syn_cnt + 1;
- end
-
- // 列扫描计数器
- always@(posedge i_clk)
- begin
- if(h_syn_cnt == H_TOTAL_TIME-1)
- begin
- if(v_syn_cnt == V_TOTAL_TIME-1)
- v_syn_cnt <= 0;
- else
- v_syn_cnt <= v_syn_cnt + 1;
- end
- end
-
- // 行同步控制
- always@(posedge i_clk)
- begin
- if(h_syn_cnt < H_SYNC_TIME)
- o_hsyn <= 0;
- else
- o_hsyn <= 1;
- end
-
- // 场同步控制
- always@(posedge i_clk)
- begin
- if(v_syn_cnt < V_SYNC_TIME)
- o_vsyn <= 0;
- else
- o_vsyn <= 1;
- end
-
- // 坐标使能.
- always@(posedge i_clk)
- begin
- if(v_syn_cnt >= V_SYNC_TIME + V_BACK_PORCH && v_syn_cnt < V_SYNC_TIME + V_BACK_PORCH + V_ACTIVE)
- begin
- if(h_syn_cnt >= H_SYNC_TIME + H_BACK_PORCH && h_syn_cnt < H_SYNC_TIME + H_BACK_PORCH + H_ACTIVE)
- en <= 1;
- else
- en <= 0;
- end
- else
- en <= 0;
- end
-
- always@(posedge i_clk)
- begin
- if(en)
- begin
- o_r <= image[image_cnt][23:16];
- o_g <= image[image_cnt][15:8];
- o_b <= image[image_cnt][7:0];
- image_cnt <= image_cnt + 1;
- end
- else if(image_cnt == H_ACTIVE*V_ACTIVE)
- begin
- o_r <= 8'h00;
- o_g <= 8'h00;
- o_b <= 8'h00;
- image_cnt <= 'd0;
- end
- else
- begin
- o_r <= 8'h00;
- o_g <= 8'h00;
- o_b <= 8'h00;
- image_cnt <= image_cnt;
- end
- end
-
- always@(posedge i_clk)
- begin
-
- // if(image_cnt >= H_ACTIVE*V_ACTIVE)
- // o_en <= 0;
- // else
- o_en <= en;
- end
复制代码
- reg clk;
- reg rst_n;
-
- integer image_txt;
-
- reg [31:0] pixel_cnt;
- wire[23:0] data;
- wire de;
-
-
- top u_top
- (
- .i_clk (clk ),
- .i_rst_n (rst_n ),
- .o_gray_data (data ),
- .o_gray_de (de )
- );
-
- always #(1) clk = ~clk;
-
- initial
- begin
- clk = 1;
- rst_n = 0;
- #100
- rst_n = 1;
-
- end
-
- glbl glbl();
-
- initial
- begin
- image_txt = $fopen("../matlab_src/image_720_1280_3_out.txt");
- end
-
- always@(posedge clk or negedge rst_n)
- begin
- if(!rst_n)
- begin
- pixel_cnt <= 0;
- end
- else if(de)
- begin
- pixel_cnt = pixel_cnt + 1;
- $fwrite(image_txt,"%h\n",data);
- end
- end
-
- always@(posedge clk )
- begin
- if(pixel_cnt == 720*1280)
- begin
- $display("*******************************************************************************");
- $display("*** Success:image_720_1280_3_out.txt is output complete! %t", $realtime, "ps***");
- $display("*******************************************************************************");
- $fclose(image_txt);
- $stop;
- end
- end
复制代码
3.2.2Modelsim实验结果- clear;clear all;clc;
-
- row = 720;
- col = 1280;
- n = 1;
-
- image_sim_pass = uint8(zeros(row,col,n));
- fid = fopen('image_720_1280_1_out.txt','r');
- for x = 1:row
- for y = 1:col
- RGB = fscanf(fid,'%s',1);
- image_sim_pass(x,y,1) = uint8(hex2dec(RGB(1:2)));
- % image_sim_pass(x,y,2) = uint8(hex2dec(RGB(3:4)));
- % image_sim_pass(x,y,3) = uint8(hex2dec(RGB(5:6)));
- end
- end
- fclose(fid);
-
- image_1 = imread('lena_1280x720.jpg');
-
- subplot(121);
- imshow(image_1), title('The original image');
-
- subplot(122);
- imshow(image_sim_pass),title('After processing images');
-
- imwrite(image_sim_pass,'lena_1280x720_sim_pass.jpg');
复制代码
4工程实现4.1Verilog代码分析变量声明 - reg valid;
- reg [7:0] threshold;
复制代码
灰度公式 - // gray1 = 0.299 * image_in_r + 0.587 * image_in_g + 0.114 * image_in_b;
- // gray1 = 256*(0.299 * image_in_r + 0.587 * image_in_g + 0.114 * image_in_b)>>8;
- // gray1 = (77 * image_in_r + 150 * image_in_g + 29 * image_in_b)>>8;
复制代码
变量声明 - reg [15:0] r_d0;
- reg [15:0] g_d0;
- reg [15:0] b_d0;
- reg [15:0] gray_d0;
- reg [1:0] hsyn;
- reg [1:0] vsyn;
- reg [1:0] de;
复制代码
执行乘法运算 - always@(posedge i_clk or negedge i_rst_n)
-
- begin
-
- if(!i_rst_n)
-
- begin
-
- r_d0 <= 16'd0;
-
- g_d0 <= 16'd0;
-
- b_d0 <= 16'd0;
-
- end
-
- else
-
- begin
-
- r_d0 <= 77 * i_r;
-
- g_d0 <= 150 * i_g;
-
- b_d0 <= 29 * i_b;
-
- end
-
- end
复制代码
执行加法运算 - always@(posedge i_clk or negedge i_rst_n)
-
- begin
-
- if(!i_rst_n)
-
- begin
-
- gray_d0 <= 16'd0;
-
- end
-
- else
-
- begin
-
- gray_d0 <= r_d0 + g_d0 + b_d0;
-
- end
-
- end
复制代码
进行信号同步化 - always@(posedge i_clk )
- begin
- hsyn <= {hsyn[0],i_hsyn};
- vsyn <= {vsyn[0],i_vsyn};
- de <= {de[0],i_de};
- end
复制代码
进行阈值锁存 - always@(posedge i_clk or negedge i_rst_n)
- begin
- if(!i_rst_n)
- begin
- valid <= 'd0;
- threshold <= 'd0;
- end
- else if(i_valid)
- begin
- valid <= 'd1;
- threshold <= i_threshold;
- end
- else
- begin
- valid <= valid;
- threshold <= threshold;
- end
-
- end
复制代码
进行二值化计算 - always@(posedge i_clk or negedge i_rst_n)
- begin
- if(!i_rst_n)
- begin
- o_binary_hsyn <= 'd0;
- o_binary_vsyn <= 'd0;
- o_binary_data <= 'd0;
- o_binary_de <= 'd0;
- end
- else if(valid)
- begin
- o_binary_hsyn <= hsyn;
- o_binary_vsyn <= vsyn;
- o_binary_data <= gray_d0[15:8] > threshold ? 255 : 0;
- o_binary_de <= de ;
- end
- end
复制代码
4.2工程结构
图像数据通过摄像头采集进来,先缓存在fifo中,然后通过写状态机,将图像数据送进DDR进行缓存,缓存后的图像数据从DDR中取出,通过读状态机送出到fifo中,然后算法处理模块在fifo中取出数据,完成数据处理后送到LCD进行显示输出。 5上板实验
点击下载后,可以看到正常的输出如下所示,摄像头的分辨率为640x480,二值化可以实时进行,如图所示:
|