软件版本: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 图像二值化腐蚀算法简介
腐蚀操作是形态学图像处理的基础。
腐蚀操作是“收缩”或“细化”二值图像中的物体。以 3x3 的滑动模板为例,具体操作是当这九个像素点全为白色 (ℼ1”)时输出白色(“1”),否则输出黑色(“0”)。使用逻辑与运算进行操作,就可以实现腐蚀的效果。
2 设计分析
2.1Matlab代码分析
源代码如下:
clear;clear all;clc;image_in = imread('lena_1280x720.jpg'); [row,col,n] = size(image_in);
image_gray = rgb2gray(image_in);
image_binary=zeros(row,col);
for i=1:row
for j=1:col
if image_gray(i,j) > 92 %阈值判断
image_binary(i,j)=255; else
image_binary(i,j)=0; end
end end
image_erode_0=zeros(row,col); for i = 2:1:row-1
for j = 2:1:col-1
image_erode_0(i,j) =...
image_binary(i-1,j-1)&image_binary(i-1,j)&image_binary(i-1,j+1)&... image_binary(i,j-1) &image_binary(i,j) &image_binary(i,j+1) &... image_binary(i+1,j-1)&image_binary(i+1,j)&image_binary(i+1,j+1);
end end
image_erode_ 1=zeros(row,col); for i = 2:1:row-1
for j = 2:1:col-1
image_erode_ 1(i,j) =...
image_erode_0(i-1,j-1)&image_erode_0(i-1,j)&image_erode_0(i-1,j+1)&... image_erode_0(i,j-1) &image_erode_0(i,j) &image_erode_0(i,j+1) &... image_erode_0(i+1,j-1)&image_erode_0(i+1,j)&image_erode_0(i+1,j+1);
end end
image_erode_2=zeros(row,col); for i = 2:1:row-1
for j = 2:1:col-1
image_erode_2(i,j) =...
image_erode_ 1(i-1,j-1)&image_erode_ 1(i-1,j)&image_erode_ 1(i-1,j+1)&... image_erode_ 1(i,j-1) &image_erode_ 1(i,j) &image_erode_ 1(i,j+1) &... image_erode_ 1(i+1,j-1)&image_erode_ 1(i+1,j)&image_erode_ 1(i+1,j+1);
end end
subplot(321);
imshow(image_gray); title('the image gray image'); subplot(322);
imshow(image_binary); title('the image binary image'); subplot(323);
imshow(image_erode_0); title('the image erode 0 image'); subplot(324);
imshow(image_erode_ 1); title('the image erode 1 image'); subplot(325);
imshow(image_erode_2); title('the image erode 2 image');
2.2Verilog代码分析
always@(posedge i_clk ornegedge i_rst_n) beginif(!i_rst_n) begin
erode_and <= 'd0;
end
else
begin
erode_and <= r_temp_ 11[0] &&r_temp_ 12[0] && r_temp_ 13[0] && r_temp_21[0] && r_temp_22[0] && r_temp_23[0] && r_temp_31[0] && r_temp_32[0] && r_temp_33[0];
end end
always@(posedge i_clk ornegedge i_rst_n) begin
if(!i_rst_n) begin
binary_reg <= 'd0; end
else if(erode_and) begin
binary_reg <= 'd255;
end
else
begin
binary_reg <= 'd0;
end end
2.3 工程结构分析
我们将图像算法的模块做成 IP 后,在vivado 中进行工程的搭建,工程结构如图所示:

3 仿真及结果
3.1Matlab实验结果
我们对实验图像进行了二值化操作,然后进行了三次腐蚀操作,每次腐蚀的结果显示在图中,我们可以看到腐 蚀的次数越多,白色线条变的越来越细。

3.2Modelsim实验结果

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 上板实验结果
实验结果如图所示:

