[X]关闭

[米联客-XILINX-H3_CZ08_7100] FPGA_图像入门连载-16FPGA 实现图像二值化腐蚀处理

文档创建者:FPGA课程
浏览次数:23
最后更新:2024-10-19
文档课程分类-AMD-ZYNQ
AMD-ZYNQ: ZYNQ-FPGA部分 » 2_FPGA实验篇(仅旗舰) » 8-FPGA图像入门
本帖最后由 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 图像二值化腐蚀算法简介
腐蚀操作是形态学图像处理的基础。
腐蚀操作是“收缩”或“细化”二值图像中的物体。以 3x3 的滑动模板为例,具体操作是当这九个像素点全为白色 (ℼ1”)时输出白色(“1”),否则输出黑色(“0”)。使用逻辑与运算进行操作,就可以实现腐蚀的效果。
2 设计分析
2.1Matlab代码分析
源代码如下:
  1. clear;clear all;clc;

  2. image_in = imread('lena_1280x720.jpg'); [row,col,n] = size(image_in);
  3. image_gray    = rgb2gray(image_in);


  4. image_binary=zeros(row,col);
  5. for i=1:row
  6. for j=1:col
  7. if image_gray(i,j) > 92 %阈值判断
  8. image_binary(i,j)=255; else
  9. image_binary(i,j)=0; end
  10. end end

  11. image_erode_0=zeros(row,col); for i = 2:1:row-1
  12. for j = 2:1:col-1
  13. image_erode_0(i,j) =...
  14. 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);
  15. end end
  16. image_erode_ 1=zeros(row,col); for i = 2:1:row-1
  17. for j = 2:1:col-1
  18. image_erode_ 1(i,j) =...
  19. 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);
  20. end end
  21. image_erode_2=zeros(row,col); for i = 2:1:row-1
  22. for j = 2:1:col-1
  23. image_erode_2(i,j) =...
  24. 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);
  25. end end

  26. subplot(321);
  27. imshow(image_gray); title('the image gray image'); subplot(322);
  28. imshow(image_binary); title('the image binary image'); subplot(323);
  29. imshow(image_erode_0); title('the image erode 0 image'); subplot(324);
  30. imshow(image_erode_ 1); title('the image erode 1 image'); subplot(325);
  31. imshow(image_erode_2); title('the image erode 2 image');
复制代码

2.2Verilog代码分析
  1. always@(posedge i_clk ornegedge i_rst_n) begin
  2. if(!i_rst_n) begin
  3. erode_and <= 'd0;
  4. end
  5. else
  6. begin
  7. 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];
  8. end end

  9. always@(posedge i_clk ornegedge i_rst_n) begin
  10. if(!i_rst_n) begin
  11. binary_reg <= 'd0; end
  12. else if(erode_and) begin
  13. binary_reg <= 'd255;
  14. end
  15. else
  16. begin
  17. binary_reg <= 'd0;
  18. end end
复制代码

2.3 工程结构分析

我们将图像算法的模块做成 IP 后,在vivado 中进行工程的搭建,工程结构如图所示:
51c516b3c05945078fd1716aea1516ec.jpg
3 仿真及结果
3.1Matlab实验结果
我们对实验图像进行了二值化操作,然后进行了三次腐蚀操作,每次腐蚀的结果显示在图中,我们可以看到腐 蚀的次数越多,白色线条变的越来越细。
e1001cff3e544e47848aeaefd55ac419.jpg
3.2Modelsim实验结果

1d8d7c88bfde447d87498d9812dcc4dd.jpg
4 搭建 Vitis-sdk 工程
创建 soc_base  sdk  platform  和 APP  工程的过程不再重复,可以阅读 3-3-01_sdk_base_app。以下给出创建好 soc_base sdk platform 的截图和对应工程 APP 的截图。
4.1 创建 SDKPlatform工程
26ff7f14126b46dda239090455aa0fae.jpg
4.2SDKAPP工程

2fda1ef127644bf1bdb995541742cd72.jpg
5 硬件连接
硬件连接如图所示:
c06787701e124b00af4973b50938e769.jpg
6 上板实验结果
实验结果如图所示:

17ccc863c469483b9f7a89887f139c76.jpg

a12290ff7d8145e29cac208395ba8c3b.jpg



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则