查看: 1107|回复: 0

[原创] 米尔MYD-C7Z020开发板开发(五)膨胀算法实现

[复制链接]
  • TA的每日心情
    开心
    2019-12-13 10:32
  • 签到天数: 9 天

    连续签到: 2 天

    [LV.3]偶尔看看II

    发表于 2019-12-20 19:31:12 | 显示全部楼层 |阅读模式
    分享到:
    膨胀是将与物体接触的所有背景点合并到该物体中,使边界向外部扩张的过程。可以用来填补物体中的空洞。用3X3的结构元素,扫描图像的每一个像素,用结构元素与其覆盖的二值图像做操作,如果都为0,结果图像的该像素为0,。否则为1。结果:使二值图像扩大一圈。
    先腐蚀后膨胀的过程称为开运算。用来消除小物体、在纤细点处分离物体、平滑较大物体的边界的同时并不明显的改变其面积。先膨胀后腐蚀的过程称为比运算,用来填充物体内细小空间、连接邻近物体、平滑其边界的同时并不明显改变其面积。
    算法的实现,可以用下式子来表示,即3x3像素的运算:
    P = P11 || P12 || P13 || P21 || P22 || P23 || P31 || P32 || P33                      3.3


    HDL中,为了通过面积去换速度,我们将上式改变如下
    P1 = P11 || P12 || P13
    P2 = P21 || P22 || P23
    P3 = P31 || P32 || P33                                  3.4
    P = P1 || P2 || P3
    通过2个时钟/步骤的运算,便能实现腐蚀运算的结果
    代码同样参考了bingo的代码
    1. `timescale 1ns/1ns
    2. module VIP_Bit_Dilation_Detector
    3. #(
    4.         parameter        [9:0]        IMG_HDISP = 10'd640,        //640*480
    5.         parameter        [9:0]        IMG_VDISP = 10'd480
    6. )
    7. (
    8.         //global clock
    9.         input                                clk,                                  //cmos video pixel clock
    10.         input                                rst_n,                                //global reset

    11.         //Image data prepred to be processd
    12.         input                                per_frame_vsync,        //Prepared Image data vsync valid signal
    13.         input                                per_frame_href,                //Prepared Image data href vaild  signal
    14.         input                                per_frame_clken,        //Prepared Image data output/capture enable clock
    15.         input                                per_img_Bit,                //Prepared Image Bit flag outout(1: Value, 0:inValid)
    16.        
    17.         //Image data has been processd
    18.         output                                post_frame_vsync,        //Processed Image data vsync valid signal
    19.         output                                post_frame_href,        //Processed Image data href vaild  signal
    20.         output                                post_frame_clken,        //Processed Image data output/capture enable clock
    21.         output                                post_img_Bit                //Processed Image Bit flag outout(1: Value, 0:inValid)
    22. );

    23. //----------------------------------------------------
    24. //Generate 1Bit 3X3 Matrix for Video Image Processor.
    25. //Image data has been processd
    26. wire                        matrix_frame_vsync;        //Prepared Image data vsync valid signal
    27. wire                        matrix_frame_href;        //Prepared Image data href vaild  signal
    28. wire                        matrix_frame_clken;        //Prepared Image data output/capture enable clock       
    29. wire                        matrix_p11, matrix_p12, matrix_p13;        //3X3 Matrix output
    30. wire                        matrix_p21, matrix_p22, matrix_p23;
    31. wire                        matrix_p31, matrix_p32, matrix_p33;
    32. VIP_Matrix_Generate_3X3_1Bit        u_VIP_Matrix_Generate_3X3_1Bit
    33. (
    34.         //global clock
    35.         .clk                                        (clk),                                  //cmos video pixel clock
    36.         .rst_n                                        (rst_n),                                //global reset

    37.         //Image data prepred to be processd
    38.         .per_frame_vsync                (per_frame_vsync),                //Prepared Image data vsync valid signal
    39.         .per_frame_href                        (per_frame_href),                //Prepared Image data href vaild  signal
    40.         .per_frame_clken                (per_frame_clken),                //Prepared Image data output/capture enable clock
    41.         .per_img_Bit                        (per_img_Bit),                        //Prepared Image brightness input

    42.         //Image data has been processd
    43.         .matrix_frame_vsync                (matrix_frame_vsync),        //Processed Image data vsync valid signal
    44.         .matrix_frame_href                (matrix_frame_href),        //Processed Image data href vaild  signal
    45.         .matrix_frame_clken                (matrix_frame_clken),        //Processed Image data output/capture enable clock       
    46.         .matrix_p11(matrix_p11),        .matrix_p12(matrix_p12),         .matrix_p13(matrix_p13),        //3X3 Matrix output
    47.         .matrix_p21(matrix_p21),         .matrix_p22(matrix_p22),         .matrix_p23(matrix_p23),
    48.         .matrix_p31(matrix_p31),         .matrix_p32(matrix_p32),         .matrix_p33(matrix_p33)
    49. );


    50. //Add you arithmetic here
    51. //----------------------------------------------------------------------------
    52. //----------------------------------------------------------------------------
    53. //----------------------------------------------------------------------------
    54. //-------------------------------------------
    55. //-------------------------------------------
    56. //Dilation Parameter
    57. //      Original         Dilation                          Pixel
    58. // [   0  0   0  ]   [   1        1   1 ]     [   P1  P2   P3 ]
    59. // [   0  1   0  ]   [   1  1   1 ]     [   P4  P5   P6 ]
    60. // [   0  0   0  ]   [   1  1        1 ]     [   P7  P8   P9 ]
    61. //P = P1 | P2 | P3 | P4 | P5 | P6 | P7 | 8 | 9;
    62. //---------------------------------------
    63. //Dilation with or operation,1 : White,  0 : Black
    64. //Step1
    65. reg        post_img_Bit1,        post_img_Bit2,        post_img_Bit3;
    66. always@(posedge clk or negedge rst_n)
    67. begin
    68.         if(!rst_n)
    69.                 begin
    70.                 post_img_Bit1 <= 1'b0;
    71.                 post_img_Bit2 <= 1'b0;
    72.                 post_img_Bit3 <= 1'b0;
    73.                 end
    74.         else
    75.                 begin
    76.                 post_img_Bit1 <= matrix_p11 | matrix_p12 | matrix_p13;
    77.                 post_img_Bit2 <= matrix_p21 | matrix_p22 | matrix_p23;
    78.                 post_img_Bit3 <= matrix_p21 | matrix_p32 | matrix_p33;
    79.                 end
    80. end

    81. //Step 2
    82. reg        post_img_Bit4;
    83. always@(posedge clk or negedge rst_n)
    84. begin
    85.         if(!rst_n)
    86.                 post_img_Bit4 <= 1'b0;
    87.         else
    88.                 post_img_Bit4 <= post_img_Bit1 | post_img_Bit2 | post_img_Bit3;
    89. end

    90. //------------------------------------------
    91. //lag 2 clocks signal sync  
    92. reg        [1:0]        per_frame_vsync_r;
    93. reg        [1:0]        per_frame_href_r;       
    94. reg        [1:0]        per_frame_clken_r;
    95. always@(posedge clk or negedge rst_n)
    96. begin
    97.         if(!rst_n)
    98.                 begin
    99.                 per_frame_vsync_r <= 0;
    100.                 per_frame_href_r <= 0;
    101.                 per_frame_clken_r <= 0;
    102.                 end
    103.         else
    104.                 begin
    105.                 per_frame_vsync_r         <=         {per_frame_vsync_r[0],         matrix_frame_vsync};
    106.                 per_frame_href_r         <=         {per_frame_href_r[0],         matrix_frame_href};
    107.                 per_frame_clken_r         <=         {per_frame_clken_r[0],         matrix_frame_clken};
    108.                 end
    109. end
    110. assign        post_frame_vsync         =         per_frame_vsync_r[1];
    111. assign        post_frame_href         =         per_frame_href_r[1];
    112. assign        post_frame_clken         =         per_frame_clken_r[1];
    113. assign        post_img_Bit                =        post_frame_href ? post_img_Bit4 : 1'b0;

    114. endmodule
    复制代码


    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-26 08:51 , Processed in 0.107946 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.