概述
图像旋转的本质是实现坐标变换公式。基于 CORDIC 算法获取正余弦数值后,即可进行旋转计算。
图像旋转代码设计思路
2.1 旋转后的图像尺寸
图像旋转后像素位置发生变化,总像素面积不变但显示范围需调整。通常以原始图像对角线长度作为旋转后图像的显示范围,确保完整显示。
reg [12:0] row_size ; reg [12:0] col_size ; assign Pixel_X = row_size ; assign Pixel_Y = col_size ; wire [31:0] cosout_abs = (cosout[31]) ? -cosout : cosout; wire [31:0] sinout_abs = (sinout[31]) ? -sinout : sinout; always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) begin row_size <= 'd0 ; col_size <= 'd0 ; end else begin // h --> row // w --> col row_size <= (ROW*cosout_abs + COL*sinout_abs) >>14 ; // h col_size <= (COL*cosout_abs + ROW*sinout_abs) >>14 ; // w end end
2.2 旋转后图像的有效位置
旋转后的有效位置可自由设定,此处设定为屏幕中心位置。具体设计如下:
//parameter define localparam H_SYNC = 11'd41 , //行同步 H_BACK = 11'd2 , //行时序后沿 H_LEFT = 11'd0 , //行时序左边框 H_VALID = 11'd480 , //行有效数据 H_RIGHT = 11'd0 , //行时序右边框 H_FRONT = 11'd2 , //行时序前沿 H_TOTAL = 11'd525 ; //行扫描周期 localparam V_SYNC = 11'd10 , //场同步 V_BACK = 11'd2 , //场时序后沿 V_TOP = 11'd0 , //场时序左边框 V_VALID = 11'd272 , //场有效数据 V_BOTTOM = 11'd0 , //场时序右边框 V_FRONT = 11'd2 , //场时序前沿 V_TOTAL = 11'd286 ; //场扫描周期 //cnt_h:行扫描计数器 //cnt_v:场扫描计数器 //data_req:数据请求信号 wire data_req = (((cnt_h >= (((H_VALID - Pixel_X)>>1) + H_SYNC + H_BACK - 'd5)) && (cnt_h < (((H_VALID - Pixel_X)>>1) + Pixel_X + H_SYNC + H_BACK - 'd5))) &&((cnt_v >= ((V_VALID - Pixel_Y)>>1) + V_SYNC + V_BACK - 'd5) && ((cnt_v < (((V_VALID - Pixel_Y)>>1) + Pixel_Y + V_SYNC + V_BACK - 'd5)))));
2.3 第一级流水线
在图像有效信号有效时进行行场计数。
always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) r_rotate_valid <= 1'b0 ; else r_rotate_valid <= data_req ; end always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) r_rotate_end <= 'd0 ; else if (r_rotate_valid && (vcnt == row_abs - 1) && (hcnt == col_abs - 2)) r_rotate_end <= 'd1 ; else r_rotate_end <= 'd0 ; end always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) hcnt <= 'd0 ; else if (r_rotate_valid && (r_rotate_end || (hcnt == col_abs - 1))) hcnt <= 'd0 ; else if (r_rotate_valid) hcnt <= hcnt + 'd1 ; end always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) vcnt <= 'd0 ; else if (r_rotate_valid && r_rotate_end) vcnt <= 'd0 ; else if (r_rotate_valid && (hcnt == col_abs - 1)) vcnt <= vcnt + 'd1 ; end
2.4 第二级流水线
计算图像旋转公式。以中心点为起始坐标,相当于坐标系的 (0,0) 点,四个象限的所有坐标点均可表示。按照公式组合起来即可,最终右移 14 位。
reg signed [12:0] x_cos ; reg signed [12:0] y_sin ; reg signed [12:0] y_cos ; reg signed [12:0] x_sin ; assign row_abs = row_size; assign col_abs = col_size; // 得到旋转后图片的中点 assign row1 = row_abs >> 1 ; assign col1 = col_abs >> 1 ; always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) x_cos <= 'd0 ; else if(r_rotate_valid_1d) x_cos <= ((hcnt - col1 ) * cosout) >>>14; else x_cos <= x_cos ; end always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) y_sin <= 'd0 ; else if(r_rotate_valid_1d) y_sin <= ((vcnt-row1 ) * sinout) >>>14; else y_sin <= y_sin ; end always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) y_cos <= 'd0 ; else if(r_rotate_valid_1d) y_cos <= ((vcnt - row1 ) * cosout) >>>14; else y_cos <= y_cos ; end always @(posedge clk_i, negedge rstn_i) begin if (!rstn_i) x_sin <= 'd0 ; else if(r_rotate_valid_1d) x_sin <= ((hcnt - col1 ) * sinout) >>>14; else x_sin <= x_sin ; end







