FPGA实现图像浮雕效果
1 概述
浮雕在我们现实生活中处处可见,尤其是中国古代的建筑浮雕众多。浮雕既是一种刻在砖、石壁或木头上的一种雕塑。
图像处理算法原理:newpixel(i,j) = pixel(i,j)-pixel(i,j+1)+TH
i为图像高度,j为图像宽度,pixel为当前图像像素点,TH为阈值(0-255)。
2 matlab实现
Matlab实验TH均取100。
实验原图:
Matlab源码:
close all
clear all
clc
Irgb = imread('1.bmp');%
Igray= rgb2gray(Irgb);
[Height,Width,Dim] = size(Irgb);
Inew = zeros(Height,Width);
TH = 100;
for i = 1:Height
for j=1:Width-1
Inew(i,j)=Igray(i,j)-Igray(i,j+1)+TH;
%Inew(i,j)=Igray(i,j+1)-Igray(i,j)+100;
if Inew(i,j) >255
Inew(i,j) = 255;
elseif Inew(i,j) <0
Inew(i,j) = 0;
else
Inew(i,j) = Inew(i,j);
end
end
end
Inew = uint8(Inew);
subplot(221),imshow(Irgb);
subplot(222),imshow(Igray);
subplot(223),imshow(Inew);
subplot(224),imshow(Irgb);
Matlab实验结果:
3 FPGA实现
FPGA实现浮雕效果算法过程:
1,将RGB图像转换为灰度图像
2,对灰度图像进行浮雕算法处理实现浮雕效果
FPGA源码:
/**********************************
copyright@FPGA OPEN SOURCE STUDIO
微信公众号:FPGA开源工作室
Algorithm:emboss = img(i,j)-img(i,j+1)+TH
Description:i--Image height
j--Image width
TH --[0 255]
***********************************/
module emboss#(
parameter DW = 24,
parameter TH = 100
)(
input pixelclk,
input reset_n,
input [DW-1:0] din,//gray in
input i_hsync,
input i_vsync,
input i_de,
output [DW-1:0]dout,//emboss out
output o_hsync,
output o_vsync,
output o_de
);
wire [7:0] gray = din[23:16]; //img(i,j)
reg [7:0] gray_r;//img(i,j+1)
reg signed [9:0] emboss_r;//10bit signed -512 --511
reg [7:0] dout_r;
reg hsync_r0,hsync_r1;
reg vsync_r0,vsync_r1;
reg de_r0,de_r1;
assign o_hsync = hsync_r1;
assign o_vsync = vsync_r1;
assign o_de = de_r1;
assign dout = {dout_r,dout_r,dout_r};
//synchronization
always @(posedge pixelclk) begin
hsync_r0 <= i_hsync;
vsync_r0 <= i_vsync;
de_r0 <= i_de;
hsync_r1 <= hsync_r0;
vsync_r1 <= vsync_r0;
de_r1 <= de_r0;
end
//pipeline
always @(posedge pixelclk) begin
if(!reset_n)
gray_r <= 0;
else
gray_r <= gray;
end
always @(posedge pixelclk or negedge reset_n)begin
if(!reset_n) begin
emboss_r<= 0;
dout_r <= 0;
end
else begin
emboss_r<= gray-gray_r+TH;//max 355 min -155
if(emboss_r>255) dout_r <= 255;
else if(emboss_r<0) dout_r <= 0;
else dout_r <= emboss_r[7:0];
end
end
Endmodule
FPGA实现效果: