本帖最后由 FPGA课程 于 2024-10-19 16:06 编辑
软件版本:VIVADO2021.1
操作系统:WIN10 64bit
硬件平台:适用 XILINX A7/K7/Z7/ZU/KU 系列 FPGA
实验平台:米联客-MLK-H3-CZ08-7100开发板
板卡获取平台:https://milianke.tmall.com/
登录“米联客”FPGA社区 http://www.uisrc.com 视频课程、答疑解惑!
1 图像 OTSU 算法简介
Otsu(大津法)算法是由日本学者 OTSU 于 1979 年提出的一种对图像进行二值化的高效算法。原理上来讲, 该方法又称作最大类间方差法,因为按照大津法求得的阈值进行图像二值化分割后,前景与背景图像的类间方差最 大,是二值化的最佳全局阈值的算法。 为了说明大津法的计算过程,先来说明二值化的数学计算过程,假设一幅灰度图像用f (x, y) 表示,其二值化 的全局阈值使用 T 表示,二值化后的图像使用g(x, y) 表示,那么可以使用下公式表示: 大津法可以得到最佳的全局阈值 T,计算过程如下: 假设一幅图像的直方图用公式表示如下,
q = 0,1, 2..., L−1 其中,n 是像素总数,nq是灰度级为 q 的像素数量,L 是图像的灰度级。 灰度级小于 T 的像素集合,C2 是一组灰度级大于 T 的像素集合。选择最佳阈值 t,最大类方差σB(2)(t) 定义为: σB(2)(t) = P1 (t)[m1 (t) − mG]2 + P2 (t)[m2 (t) − mG]2 这里,P1 (t) 是集合 C1 发生的概率: 比如,如果设置 t=0,那么拥有为 t 赋值的任何像素 C1 集合的概率为 0,类似的,集合 C2 的发生概率为
m1 (t) 和m2 (t) 分别是集合 C1 和 C2 中像素的平均灰度。mG是全局像素均值:
另外,直到灰度级 t 的平均灰度由公式给出: 展开 σB(2)(t) 的表达式后,根据
可以推导出类间方差如下公式:
这个最终的公式计算十分有效,对于所有的阈值 t 来说,只需要参数 m 和 P1 计算一次即可。
大津法的想法是,方差越大,就离完全分割图像的阈值更接近。注意,这个最佳阈值依赖于参数,可以直接从直方图中得到。另外, t 是出于 0 到 L-1 的所以整数,寻找最大方差,需要遍历所有的 t 值。 2 设计分析2.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); %graythresh 函数是输入一副图像,获取图像合适的阈值
- 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');
复制代码
2.2Verilog代码分析 - always@(posedge i_clk )
- begin
- i_vsyn_d <= i_vsyn;
- end
- always@(posedge i_clk )
- begin
- if(!i_rst_n)
- ap_clk <= 0;
- else
- ap_clk <= ~ap_clk;
- end
- //***************************************************
- //histogram
- //***************************************************
- always@(posedge i_clk )
- begin
- if(i_vsyn && !i_vsyn_d)
- begin
- hist_ram[0 ]<= 'd0;
- hist_ram[1 ]<= 'd0;
- hist_ram[2 ]<= 'd0;
- hist_ram[3 ]<= 'd0;
- hist_ram[4 ]<= 'd0;
- hist_ram[5 ]<= 'd0;
- hist_ram[6 ]<= 'd0;
- hist_ram[7 ]<= 'd0;
- hist_ram[8 ]<= 'd0;
- hist_ram[9 ]<= 'd0;
- hist_ram[10 ]<= 'd0;
- hist_ram[11 ]<= 'd0;
- hist_ram[12 ]<= 'd0;
- hist_ram[13 ]<= 'd0;
- hist_ram[14 ]<= 'd0;
- hist_ram[15 ]<= 'd0;
- hist_ram[16 ]<= 'd0;
- hist_ram[17 ]<= 'd0;
- hist_ram[18 ]<= 'd0;
- hist_ram[19 ]<= 'd0;
- hist_ram[20 ]<= 'd0;
- hist_ram[21 ]<= 'd0;
- hist_ram[22 ]<= 'd0;
- hist_ram[23 ]<= 'd0;
- hist_ram[24 ]<= 'd0;
- hist_ram[25 ]<= 'd0;
- hist_ram[26 ]<= 'd0;
- hist_ram[27 ]<= 'd0;
- hist_ram[28 ]<= 'd0;
- hist_ram[29 ]<= 'd0;
- hist_ram[30 ]<= 'd0;
- hist_ram[31 ]<= 'd0;
- hist_ram[32 ]<= 'd0;
- hist_ram[33 ]<= 'd0;
- hist_ram[34 ]<= 'd0;
- hist_ram[35 ]<= 'd0;
- hist_ram[36 ]<= 'd0;
- hist_ram[37 ]<= 'd0;
- hist_ram[38 ]<= 'd0;
- hist_ram[39 ]<= 'd0;
- hist_ram[40 ]<= 'd0;
- hist_ram[41 ]<= 'd0;
- hist_ram[42 ]<= 'd0;
- hist_ram[43 ]<= 'd0;
- hist_ram[44 ]<= 'd0;
- hist_ram[45 ]<= 'd0;
- hist_ram[46 ]<= 'd0;
- hist_ram[47 ]<= 'd0;
- hist_ram[48 ]<= 'd0;
- hist_ram[49 ]<= 'd0;
- hist_ram[50 ]<= 'd0;
- hist_ram[51 ]<= 'd0;
- hist_ram[52 ]<= 'd0;
- hist_ram[53 ]<= 'd0;
- hist_ram[54 ]<= 'd0;
- hist_ram[55 ]<= 'd0;
- hist_ram[56 ]<= 'd0;
- hist_ram[57 ]<= 'd0;
- hist_ram[58 ]<= 'd0;
- hist_ram[59 ]<= 'd0;
- hist_ram[60 ]<= 'd0;
- hist_ram[61 ]<= 'd0;
- hist_ram[62 ]<= 'd0;
- hist_ram[63 ]<= 'd0;
- hist_ram[64 ]<= 'd0;
- hist_ram[65 ]<= 'd0;
- hist_ram[66 ]<= 'd0;
- hist_ram[67 ]<= 'd0;
- hist_ram[68 ]<= 'd0;
- hist_ram[69 ]<= 'd0;
- hist_ram[70 ]<= 'd0;
- hist_ram[71 ]<= 'd0;
- hist_ram[72 ]<= 'd0;
- hist_ram[73 ]<= 'd0;
- hist_ram[74 ]<= 'd0;
- hist_ram[75 ]<= 'd0;
- hist_ram[76 ]<= 'd0;
- hist_ram[77 ]<= 'd0;
- hist_ram[78 ]<= 'd0;
- hist_ram[79 ]<= 'd0;
- hist_ram[80 ]<= 'd0;
- hist_ram[81 ]<= 'd0;
- hist_ram[82 ]<= 'd0;
- hist_ram[83 ]<= 'd0;
- hist_ram[84 ]<= 'd0;
- hist_ram[85 ]<= 'd0;
- hist_ram[86 ]<= 'd0;
- hist_ram[87 ]<= 'd0;
- hist_ram[88 ]<= 'd0;
- hist_ram[89 ]<= 'd0;
- hist_ram[90 ]<= 'd0;
- hist_ram[91 ]<= 'd0;
- hist_ram[92 ]<= 'd0;
- hist_ram[93 ]<= 'd0;
- hist_ram[94 ]<= 'd0;
- hist_ram[95 ]<= 'd0;
- hist_ram[96 ]<= 'd0;
- hist_ram[97 ]<= 'd0;
- hist_ram[98 ]<= 'd0;
- hist_ram[99 ]<= 'd0;
- hist_ram[100]<= 'd0;
- hist_ram[101]<= 'd0;
- hist_ram[102]<= 'd0;
- hist_ram[103]<= 'd0;
- hist_ram[104]<= 'd0;
- hist_ram[105]<= 'd0;
- hist_ram[106]<= 'd0;
- hist_ram[107]<= 'd0;
- hist_ram[108]<= 'd0;
- hist_ram[109]<= 'd0;
- hist_ram[110]<= 'd0;
- hist_ram[111]<= 'd0;
- hist_ram[112]<= 'd0;
- hist_ram[113]<= 'd0;
- hist_ram[114]<= 'd0;
- hist_ram[115]<= 'd0;
- hist_ram[116]<= 'd0;
- hist_ram[117]<= 'd0;
- hist_ram[118]<= 'd0;
- hist_ram[119]<= 'd0;
- hist_ram[120]<= 'd0;
- hist_ram[121]<= 'd0;
- hist_ram[122]<= 'd0;
- hist_ram[123]<= 'd0;
- hist_ram[124]<= 'd0;
- hist_ram[125]<= 'd0;
- hist_ram[126]<= 'd0;
- hist_ram[127]<= 'd0;
- hist_ram[128]<= 'd0;
- hist_ram[129]<= 'd0;
- hist_ram[130]<= 'd0;
- hist_ram[131]<= 'd0;
- hist_ram[132]<= 'd0;
- hist_ram[133]<= 'd0;
- hist_ram[134]<= 'd0;
- hist_ram[135]<= 'd0;
- hist_ram[136]<= 'd0;
- hist_ram[137]<= 'd0;
- hist_ram[138]<= 'd0;
- hist_ram[139]<= 'd0;
- hist_ram[140]<= 'd0;
- hist_ram[141]<= 'd0;
- hist_ram[142]<= 'd0;
- hist_ram[143]<= 'd0;
- hist_ram[144]<= 'd0;
- hist_ram[145]<= 'd0;
- hist_ram[146]<= 'd0;
- hist_ram[147]<= 'd0;
- hist_ram[148]<= 'd0;
- hist_ram[149]<= 'd0;
- hist_ram[150]<= 'd0;
- hist_ram[151]<= 'd0;
- hist_ram[152]<= 'd0;
- hist_ram[153]<= 'd0;
- hist_ram[154]<= 'd0;
- hist_ram[155]<= 'd0;
- hist_ram[156]<= 'd0;
- hist_ram[157]<= 'd0;
- hist_ram[158]<= 'd0;
- hist_ram[159]<= 'd0;
- hist_ram[160]<= 'd0;
- hist_ram[161]<= 'd0;
- hist_ram[162]<= 'd0;
- hist_ram[163]<= 'd0;
- hist_ram[164]<= 'd0;
- hist_ram[165]<= 'd0;
- hist_ram[166]<= 'd0;
- hist_ram[167]<= 'd0;
- hist_ram[168]<= 'd0;
- hist_ram[169]<= 'd0;
- hist_ram[170]<= 'd0;
- hist_ram[171]<= 'd0;
- hist_ram[172]<= 'd0;
- hist_ram[173]<= 'd0;
- hist_ram[174]<= 'd0;
- hist_ram[175]<= 'd0;
- hist_ram[176]<= 'd0;
- hist_ram[177]<= 'd0;
- hist_ram[178]<= 'd0;
- hist_ram[179]<= 'd0;
- hist_ram[180]<= 'd0;
- hist_ram[181]<= 'd0;
- hist_ram[182]<= 'd0;
- hist_ram[183]<= 'd0;
- hist_ram[184]<= 'd0;
- hist_ram[185]<= 'd0;
- hist_ram[186]<= 'd0;
- hist_ram[187]<= 'd0;
- hist_ram[188]<= 'd0;
- hist_ram[189]<= 'd0;
- hist_ram[190]<= 'd0;
- hist_ram[191]<= 'd0;
- hist_ram[192]<= 'd0;
- hist_ram[193]<= 'd0;
- hist_ram[194]<= 'd0;
- hist_ram[195]<= 'd0;
- hist_ram[196]<= 'd0;
- hist_ram[197]<= 'd0;
- hist_ram[198]<= 'd0;
- hist_ram[199]<= 'd0;
- hist_ram[200]<= 'd0;
- hist_ram[201]<= 'd0;
- hist_ram[202]<= 'd0;
- hist_ram[203]<= 'd0;
- hist_ram[204]<= 'd0;
- hist_ram[205]<= 'd0;
- hist_ram[206]<= 'd0;
- hist_ram[207]<= 'd0;
- hist_ram[208]<= 'd0;
- hist_ram[209]<= 'd0;
- hist_ram[210]<= 'd0;
- hist_ram[211]<= 'd0;
- hist_ram[212]<= 'd0;
- hist_ram[213]<= 'd0;
- hist_ram[214]<= 'd0;
- hist_ram[215]<= 'd0;
- hist_ram[216]<= 'd0;
- hist_ram[217]<= 'd0;
- hist_ram[218]<= 'd0;
- hist_ram[219]<= 'd0;
- hist_ram[220]<= 'd0;
- hist_ram[221]<= 'd0;
- hist_ram[222]<= 'd0;
- hist_ram[223]<= 'd0;
- hist_ram[224]<= 'd0;
- hist_ram[225]<= 'd0;
- hist_ram[226]<= 'd0;
- hist_ram[227]<= 'd0;
- hist_ram[228]<= 'd0;
- hist_ram[229]<= 'd0;
- hist_ram[230]<= 'd0;
- hist_ram[231]<= 'd0;
- hist_ram[232]<= 'd0;
- hist_ram[233]<= 'd0;
- hist_ram[234]<= 'd0;
- hist_ram[235]<= 'd0;
- hist_ram[236]<= 'd0;
- hist_ram[237]<= 'd0;
- hist_ram[238]<= 'd0;
- hist_ram[239]<= 'd0;
- hist_ram[240]<= 'd0;
- hist_ram[241]<= 'd0;
- hist_ram[242]<= 'd0;
- hist_ram[243]<= 'd0;
- hist_ram[244]<= 'd0;
- hist_ram[245]<= 'd0;
- hist_ram[246]<= 'd0;
- hist_ram[247]<= 'd0;
- hist_ram[248]<= 'd0;
- hist_ram[249]<= 'd0;
- hist_ram[250]<= 'd0;
- hist_ram[251]<= 'd0;
- hist_ram[252]<= 'd0;
- hist_ram[253]<= 'd0;
- hist_ram[254]<= 'd0;
- hist_ram[255]<= 'd0;
- end
- else if(i_en && i_vsyn)
- begin
- case(i_gray)
- 0 :hist_ram[0 ]<=hist_ram[0 ]+1'b1;
- 1 :hist_ram[1 ]<=hist_ram[1 ]+1'b1;
- 2 :hist_ram[2 ]<=hist_ram[2 ]+1'b1;
- 3 :hist_ram[3 ]<=hist_ram[3 ]+1'b1;
- 4 :hist_ram[4 ]<=hist_ram[4 ]+1'b1;
- 5 :hist_ram[5 ]<=hist_ram[5 ]+1'b1;
- 6 :hist_ram[6 ]<=hist_ram[6 ]+1'b1;
- 7 :hist_ram[7 ]<=hist_ram[7 ]+1'b1;
- 8 :hist_ram[8 ]<=hist_ram[8 ]+1'b1;
- 9 :hist_ram[9 ]<=hist_ram[9 ]+1'b1;
- 10 :hist_ram[10 ]<=hist_ram[10 ]+1'b1;
- 11 :hist_ram[11 ]<=hist_ram[11 ]+1'b1;
- 12 :hist_ram[12 ]<=hist_ram[12 ]+1'b1;
- 13 :hist_ram[13 ]<=hist_ram[13 ]+1'b1;
- 14 :hist_ram[14 ]<=hist_ram[14 ]+1'b1;
- 15 :hist_ram[15 ]<=hist_ram[15 ]+1'b1;
- 16 :hist_ram[16 ]<=hist_ram[16 ]+1'b1;
- 17 :hist_ram[17 ]<=hist_ram[17 ]+1'b1;
- 18 :hist_ram[18 ]<=hist_ram[18 ]+1'b1;
- 19 :hist_ram[19 ]<=hist_ram[19 ]+1'b1;
- 20 :hist_ram[20 ]<=hist_ram[20 ]+1'b1;
- 21 :hist_ram[21 ]<=hist_ram[21 ]+1'b1;
- 22 :hist_ram[22 ]<=hist_ram[22 ]+1'b1;
- 23 :hist_ram[23 ]<=hist_ram[23 ]+1'b1;
- 24 :hist_ram[24 ]<=hist_ram[24 ]+1'b1;
- 25 :hist_ram[25 ]<=hist_ram[25 ]+1'b1;
- 26 :hist_ram[26 ]<=hist_ram[26 ]+1'b1;
- 27 :hist_ram[27 ]<=hist_ram[27 ]+1'b1;
- 28 :hist_ram[28 ]<=hist_ram[28 ]+1'b1;
- 29 :hist_ram[29 ]<=hist_ram[29 ]+1'b1;
- 30 :hist_ram[30 ]<=hist_ram[30 ]+1'b1;
- 31 :hist_ram[31 ]<=hist_ram[31 ]+1'b1;
- 32 :hist_ram[32 ]<=hist_ram[32 ]+1'b1;
- 33 :hist_ram[33 ]<=hist_ram[33 ]+1'b1;
- 34 :hist_ram[34 ]<=hist_ram[34 ]+1'b1;
- 35 :hist_ram[35 ]<=hist_ram[35 ]+1'b1;
- 36 :hist_ram[36 ]<=hist_ram[36 ]+1'b1;
- 37 :hist_ram[37 ]<=hist_ram[37 ]+1'b1;
- 38 :hist_ram[38 ]<=hist_ram[38 ]+1'b1;
- 39 :hist_ram[39 ]<=hist_ram[39 ]+1'b1;
- 40 :hist_ram[40 ]<=hist_ram[40 ]+1'b1;
- 41 :hist_ram[41 ]<=hist_ram[41 ]+1'b1;
- 42 :hist_ram[42 ]<=hist_ram[42 ]+1'b1;
- 43 :hist_ram[43 ]<=hist_ram[43 ]+1'b1;
- 44 :hist_ram[44 ]<=hist_ram[44 ]+1'b1;
- 45 :hist_ram[45 ]<=hist_ram[45 ]+1'b1;
- 46 :hist_ram[46 ]<=hist_ram[46 ]+1'b1;
- 47 :hist_ram[47 ]<=hist_ram[47 ]+1'b1;
- 48 :hist_ram[48 ]<=hist_ram[48 ]+1'b1;
- 49 :hist_ram[49 ]<=hist_ram[49 ]+1'b1;
- 50 :hist_ram[50 ]<=hist_ram[50 ]+1'b1;
- 51 :hist_ram[51 ]<=hist_ram[51 ]+1'b1;
- 52 :hist_ram[52 ]<=hist_ram[52 ]+1'b1;
- 53 :hist_ram[53 ]<=hist_ram[53 ]+1'b1;
- 54 :hist_ram[54 ]<=hist_ram[54 ]+1'b1;
- 55 :hist_ram[55 ]<=hist_ram[55 ]+1'b1;
- 56 :hist_ram[56 ]<=hist_ram[56 ]+1'b1;
- 57 :hist_ram[57 ]<=hist_ram[57 ]+1'b1;
- 58 :hist_ram[58 ]<=hist_ram[58 ]+1'b1;
- 59 :hist_ram[59 ]<=hist_ram[59 ]+1'b1;
- 60 :hist_ram[60 ]<=hist_ram[60 ]+1'b1;
- 61 :hist_ram[61 ]<=hist_ram[61 ]+1'b1;
- 62 :hist_ram[62 ]<=hist_ram[62 ]+1'b1;
- 63 :hist_ram[63 ]<=hist_ram[63 ]+1'b1;
- 64 :hist_ram[64 ]<=hist_ram[64 ]+1'b1;
- 65 :hist_ram[65 ]<=hist_ram[65 ]+1'b1;
- 66 :hist_ram[66 ]<=hist_ram[66 ]+1'b1;
- 67 :hist_ram[67 ]<=hist_ram[67 ]+1'b1;
- 68 :hist_ram[68 ]<=hist_ram[68 ]+1'b1;
- 69 :hist_ram[69 ]<=hist_ram[69 ]+1'b1;
- 70 :hist_ram[70 ]<=hist_ram[70 ]+1'b1;
- 71 :hist_ram[71 ]<=hist_ram[71 ]+1'b1;
- 72 :hist_ram[72 ]<=hist_ram[72 ]+1'b1;
- 73 :hist_ram[73 ]<=hist_ram[73 ]+1'b1;
- 74 :hist_ram[74 ]<=hist_ram[74 ]+1'b1;
- 75 :hist_ram[75 ]<=hist_ram[75 ]+1'b1;
- 76 :hist_ram[76 ]<=hist_ram[76 ]+1'b1;
- 77 :hist_ram[77 ]<=hist_ram[77 ]+1'b1;
- 78 :hist_ram[78 ]<=hist_ram[78 ]+1'b1;
- 79 :hist_ram[79 ]<=hist_ram[79 ]+1'b1;
- 80 :hist_ram[80 ]<=hist_ram[80 ]+1'b1;
- 81 :hist_ram[81 ]<=hist_ram[81 ]+1'b1;
- 82 :hist_ram[82 ]<=hist_ram[82 ]+1'b1;
- 83 :hist_ram[83 ]<=hist_ram[83 ]+1'b1;
- 84 :hist_ram[84 ]<=hist_ram[84 ]+1'b1;
- 85 :hist_ram[85 ]<=hist_ram[85 ]+1'b1;
- 86 :hist_ram[86 ]<=hist_ram[86 ]+1'b1;
- 87 :hist_ram[87 ]<=hist_ram[87 ]+1'b1;
- 88 :hist_ram[88 ]<=hist_ram[88 ]+1'b1;
- 89 :hist_ram[89 ]<=hist_ram[89 ]+1'b1;
- 90 :hist_ram[90 ]<=hist_ram[90 ]+1'b1;
- 91 :hist_ram[91 ]<=hist_ram[91 ]+1'b1;
- 92 :hist_ram[92 ]<=hist_ram[92 ]+1'b1;
- 93 :hist_ram[93 ]<=hist_ram[93 ]+1'b1;
- 94 :hist_ram[94 ]<=hist_ram[94 ]+1'b1;
- 95 :hist_ram[95 ]<=hist_ram[95 ]+1'b1;
- 96 :hist_ram[96 ]<=hist_ram[96 ]+1'b1;
- 97 :hist_ram[97 ]<=hist_ram[97 ]+1'b1;
- 98 :hist_ram[98 ]<=hist_ram[98 ]+1'b1;
- 99 :hist_ram[99 ]<=hist_ram[99 ]+1'b1;
- 100:hist_ram[100]<=hist_ram[100]+1'b1;
- 101:hist_ram[101]<=hist_ram[101]+1'b1;
- 102:hist_ram[102]<=hist_ram[102]+1'b1;
- 103:hist_ram[103]<=hist_ram[103]+1'b1;
- 104:hist_ram[104]<=hist_ram[104]+1'b1;
- 105:hist_ram[105]<=hist_ram[105]+1'b1;
- 106:hist_ram[106]<=hist_ram[106]+1'b1;
- 107:hist_ram[107]<=hist_ram[107]+1'b1;
- 108:hist_ram[108]<=hist_ram[108]+1'b1;
- 109:hist_ram[109]<=hist_ram[109]+1'b1;
- 110:hist_ram[110]<=hist_ram[110]+1'b1;
- 111:hist_ram[111]<=hist_ram[111]+1'b1;
- 112:hist_ram[112]<=hist_ram[112]+1'b1;
- 113:hist_ram[113]<=hist_ram[113]+1'b1;
- 114:hist_ram[114]<=hist_ram[114]+1'b1;
- 115:hist_ram[115]<=hist_ram[115]+1'b1;
- 116:hist_ram[116]<=hist_ram[116]+1'b1;
- 117:hist_ram[117]<=hist_ram[117]+1'b1;
- 118:hist_ram[118]<=hist_ram[118]+1'b1;
- 119:hist_ram[119]<=hist_ram[119]+1'b1;
- 120:hist_ram[120]<=hist_ram[120]+1'b1;
- 121:hist_ram[121]<=hist_ram[121]+1'b1;
- 122:hist_ram[122]<=hist_ram[122]+1'b1;
- 123:hist_ram[123]<=hist_ram[123]+1'b1;
- 124:hist_ram[124]<=hist_ram[124]+1'b1;
- 125:hist_ram[125]<=hist_ram[125]+1'b1;
- 126:hist_ram[126]<=hist_ram[126]+1'b1;
- 127:hist_ram[127]<=hist_ram[127]+1'b1;
- 128:hist_ram[128]<=hist_ram[128]+1'b1;
- 129:hist_ram[129]<=hist_ram[129]+1'b1;
- 130:hist_ram[130]<=hist_ram[130]+1'b1;
- 131:hist_ram[131]<=hist_ram[131]+1'b1;
- 132:hist_ram[132]<=hist_ram[132]+1'b1;
- 133:hist_ram[133]<=hist_ram[133]+1'b1;
- 134:hist_ram[134]<=hist_ram[134]+1'b1;
- 135:hist_ram[135]<=hist_ram[135]+1'b1;
- 136:hist_ram[136]<=hist_ram[136]+1'b1;
- 137:hist_ram[137]<=hist_ram[137]+1'b1;
- 138:hist_ram[138]<=hist_ram[138]+1'b1;
- 139:hist_ram[139]<=hist_ram[139]+1'b1;
- 140:hist_ram[140]<=hist_ram[140]+1'b1;
- 141:hist_ram[141]<=hist_ram[141]+1'b1;
- 142:hist_ram[142]<=hist_ram[142]+1'b1;
- 143:hist_ram[143]<=hist_ram[143]+1'b1;
- 144:hist_ram[144]<=hist_ram[144]+1'b1;
- 145:hist_ram[145]<=hist_ram[145]+1'b1;
- 146:hist_ram[146]<=hist_ram[146]+1'b1;
- 147:hist_ram[147]<=hist_ram[147]+1'b1;
- 148:hist_ram[148]<=hist_ram[148]+1'b1;
- 149:hist_ram[149]<=hist_ram[149]+1'b1;
- 150:hist_ram[150]<=hist_ram[150]+1'b1;
- 151:hist_ram[151]<=hist_ram[151]+1'b1;
- 152:hist_ram[152]<=hist_ram[152]+1'b1;
- 153:hist_ram[153]<=hist_ram[153]+1'b1;
- 154:hist_ram[154]<=hist_ram[154]+1'b1;
- 155:hist_ram[155]<=hist_ram[155]+1'b1;
- 156:hist_ram[156]<=hist_ram[156]+1'b1;
- 157:hist_ram[157]<=hist_ram[157]+1'b1;
- 158:hist_ram[158]<=hist_ram[158]+1'b1;
- 159:hist_ram[159]<=hist_ram[159]+1'b1;
- 160:hist_ram[160]<=hist_ram[160]+1'b1;
- 161:hist_ram[161]<=hist_ram[161]+1'b1;
- 162:hist_ram[162]<=hist_ram[162]+1'b1;
- 163:hist_ram[163]<=hist_ram[163]+1'b1;
- 164:hist_ram[164]<=hist_ram[164]+1'b1;
- 165:hist_ram[165]<=hist_ram[165]+1'b1;
- 166:hist_ram[166]<=hist_ram[166]+1'b1;
- 167:hist_ram[167]<=hist_ram[167]+1'b1;
- 168:hist_ram[168]<=hist_ram[168]+1'b1;
- 169:hist_ram[169]<=hist_ram[169]+1'b1;
- 170:hist_ram[170]<=hist_ram[170]+1'b1;
- 171:hist_ram[171]<=hist_ram[171]+1'b1;
- 172:hist_ram[172]<=hist_ram[172]+1'b1;
- 173:hist_ram[173]<=hist_ram[173]+1'b1;
- 174:hist_ram[174]<=hist_ram[174]+1'b1;
- 175:hist_ram[175]<=hist_ram[175]+1'b1;
- 176:hist_ram[176]<=hist_ram[176]+1'b1;
- 177:hist_ram[177]<=hist_ram[177]+1'b1;
- 178:hist_ram[178]<=hist_ram[178]+1'b1;
- 179:hist_ram[179]<=hist_ram[179]+1'b1;
- 180:hist_ram[180]<=hist_ram[180]+1'b1;
- 181:hist_ram[181]<=hist_ram[181]+1'b1;
- 182:hist_ram[182]<=hist_ram[182]+1'b1;
- 183:hist_ram[183]<=hist_ram[183]+1'b1;
- 184:hist_ram[184]<=hist_ram[184]+1'b1;
- 185:hist_ram[185]<=hist_ram[185]+1'b1;
- 186:hist_ram[186]<=hist_ram[186]+1'b1;
- 187:hist_ram[187]<=hist_ram[187]+1'b1;
- 188:hist_ram[188]<=hist_ram[188]+1'b1;
- 189:hist_ram[189]<=hist_ram[189]+1'b1;
- 190:hist_ram[190]<=hist_ram[190]+1'b1;
- 191:hist_ram[191]<=hist_ram[191]+1'b1;
- 192:hist_ram[192]<=hist_ram[192]+1'b1;
- 193:hist_ram[193]<=hist_ram[193]+1'b1;
- 194:hist_ram[194]<=hist_ram[194]+1'b1;
- 195:hist_ram[195]<=hist_ram[195]+1'b1;
- 196:hist_ram[196]<=hist_ram[196]+1'b1;
- 197:hist_ram[197]<=hist_ram[197]+1'b1;
- 198:hist_ram[198]<=hist_ram[198]+1'b1;
- 199:hist_ram[199]<=hist_ram[199]+1'b1;
- 200:hist_ram[200]<=hist_ram[200]+1'b1;
- 201:hist_ram[201]<=hist_ram[201]+1'b1;
- 202:hist_ram[202]<=hist_ram[202]+1'b1;
- 203:hist_ram[203]<=hist_ram[203]+1'b1;
- 204:hist_ram[204]<=hist_ram[204]+1'b1;
- 205:hist_ram[205]<=hist_ram[205]+1'b1;
- 206:hist_ram[206]<=hist_ram[206]+1'b1;
- 207:hist_ram[207]<=hist_ram[207]+1'b1;
- 208:hist_ram[208]<=hist_ram[208]+1'b1;
- 209:hist_ram[209]<=hist_ram[209]+1'b1;
- 210:hist_ram[210]<=hist_ram[210]+1'b1;
- 211:hist_ram[211]<=hist_ram[211]+1'b1;
- 212:hist_ram[212]<=hist_ram[212]+1'b1;
- 213:hist_ram[213]<=hist_ram[213]+1'b1;
- 214:hist_ram[214]<=hist_ram[214]+1'b1;
- 215:hist_ram[215]<=hist_ram[215]+1'b1;
- 216:hist_ram[216]<=hist_ram[216]+1'b1;
- 217:hist_ram[217]<=hist_ram[217]+1'b1;
- 218:hist_ram[218]<=hist_ram[218]+1'b1;
- 219:hist_ram[219]<=hist_ram[219]+1'b1;
- 220:hist_ram[220]<=hist_ram[220]+1'b1;
- 221:hist_ram[221]<=hist_ram[221]+1'b1;
- 222:hist_ram[222]<=hist_ram[222]+1'b1;
- 223:hist_ram[223]<=hist_ram[223]+1'b1;
- 224:hist_ram[224]<=hist_ram[224]+1'b1;
- 225:hist_ram[225]<=hist_ram[225]+1'b1;
- 226:hist_ram[226]<=hist_ram[226]+1'b1;
- 227:hist_ram[227]<=hist_ram[227]+1'b1;
- 228:hist_ram[228]<=hist_ram[228]+1'b1;
- 229:hist_ram[229]<=hist_ram[229]+1'b1;
- 230:hist_ram[230]<=hist_ram[230]+1'b1;
- 231:hist_ram[231]<=hist_ram[231]+1'b1;
- 232:hist_ram[232]<=hist_ram[232]+1'b1;
- 233:hist_ram[233]<=hist_ram[233]+1'b1;
- 234:hist_ram[234]<=hist_ram[234]+1'b1;
- 235:hist_ram[235]<=hist_ram[235]+1'b1;
- 236:hist_ram[236]<=hist_ram[236]+1'b1;
- 237:hist_ram[237]<=hist_ram[237]+1'b1;
- 238:hist_ram[238]<=hist_ram[238]+1'b1;
- 239:hist_ram[239]<=hist_ram[239]+1'b1;
- 240:hist_ram[240]<=hist_ram[240]+1'b1;
- 241:hist_ram[241]<=hist_ram[241]+1'b1;
- 242:hist_ram[242]<=hist_ram[242]+1'b1;
- 243:hist_ram[243]<=hist_ram[243]+1'b1;
- 244:hist_ram[244]<=hist_ram[244]+1'b1;
- 245:hist_ram[245]<=hist_ram[245]+1'b1;
- 246:hist_ram[246]<=hist_ram[246]+1'b1;
- 247:hist_ram[247]<=hist_ram[247]+1'b1;
- 248:hist_ram[248]<=hist_ram[248]+1'b1;
- 249:hist_ram[249]<=hist_ram[249]+1'b1;
- 250:hist_ram[250]<=hist_ram[250]+1'b1;
- 251:hist_ram[251]<=hist_ram[251]+1'b1;
- 252:hist_ram[252]<=hist_ram[252]+1'b1;
- 253:hist_ram[253]<=hist_ram[253]+1'b1;
- 254:hist_ram[254]<=hist_ram[254]+1'b1;
- 255:hist_ram[255]<=hist_ram[255]+1'b1;
- default:;
- endcase
-
- end
- end
- always@(posedge i_clk or negedge i_rst_n)
- begin
- if(!i_rst_n)
- ap_start_flag <= 0;
- else if(i_vsyn_d && (~i_vsyn))
- ap_start_flag <= 1;
- else if(ap_start_cnt == 258)
- ap_start_flag <= 0;
- end
- always@(posedge i_clk or negedge i_rst_n)
- begin
- if(!i_rst_n)
- ap_start_cnt <= 0;
- else if(ap_start_flag)
- ap_start_cnt <= ap_start_cnt + 1;
- else
- ap_start_cnt <= 0;
- end
- assign ap_start = ap_start_flag;
- assign hist_q0 = hist_ram[hist_address0];
- assign height = 720;
- assign width = 1280;
- assign o_valid = ap_done&ap_ready;
- assign o_threshold = ap_return;
-
- xfOtsuKernel grp_xfOtsuKernel(
- .ap_clk (ap_clk ),
- .ap_rst (!i_rst_n ),
- .ap_start (ap_start ),
- .ap_done (ap_done ),
- .ap_idle ( ),
- .ap_ready (ap_ready ),
- .p_hist_address0(hist_address0 ),
- .p_hist_ce0 (hist_ce0 ),
- .p_hist_q0 (hist_q0 ),
- .p_height (height ),
- .p_width (width ),
- .ap_return (ap_return )
- );
- endmodule
复制代码
2.3 工程结构分析 我们将图像算法的模块做成 IP 后,在vivado 中进行工程的搭建,工程结构如图所示:
3 仿真及结果3.1Matlab 实验结果
3.2Vivadosim实验结果
4 搭建 Vitis-sdk 工程
创建 soc_base sdk platform 和 APP 工程的过程不再重复,可以阅读 3-3-01_sdk_base_app。以下给出创建好 soc_base sdk platform 的截图和对应工程 APP 的截图。 4.1 创建 SDKPlatform工程
4.2SDKAPP工程
5 硬件连接硬件连接如图所示:
6 上板实验结果实验结果如图所示:
|