1、二值化图像简介
前面已经完成了摄像头图像的采集和显示,以及RGB图像转灰度图。二值化图像在图像处理领域同样有广泛的应用,本节介绍如何用FPGA实现灰度转二值化图形。灰度实现二值化的原理很简单,只需要设置一个阈值,将每个像素的灰度和阈值比较,大于阈值就输出255,小于阈值就输出0。
2、二值化图像matlab仿真
这边简单添加了二值化的matlab代码,代码如下图所示。
clc;
clear all;
img_rgb=imread('dog.png');
%high and width
h=size(img_rgb,1);
w=size(img_rgb,2);
%rgb dog show
subplot(231);
imshow(img_rgb);
title('rgb dog');
% Relized method 2:myself Algorithm realized
% Y = ( R*77 + G*150 + B*29) >>8
% Cb = (-R*44 - G*84 + B*128) >>8
% Cr = ( R*128 - G*108 - B*20) >>8
img_rgb=double(img_rgb);
img_y=zeros(h,w);
img_u=zeros(h,w);
img_v=zeros(h,w);
for i = 1 : h
for j = 1 : w
img_y(i,j) = bitshift(( img_rgb(i,j,1)*77 + img_rgb(i,j,2)*150 + img_rgb(i,j,3)*29),-8);
img_u(i,j) = bitshift((-img_rgb(i,j,1)*44 - img_rgb(i,j,2)*84 + img_rgb(i,j,3)*128 + 32678),-8);
img_v(i,j) = bitshift(( img_rgb(i,j,1)*128 - img_rgb(i,j,2)*108 - img_rgb(i,j,3)*20 + 32678),-8);
end
end
img_y = uint8(img_y);
img_u = uint8(img_u);
img_v = uint8(img_v);
% gray to bin
% greater then threshold:1
% smaller than threshold:0
img_bin=zeros(h,w);
img_bin1=zeros(h,w);
THRESHOLD_DATA=80;
THRESHOLD_DATA1=120;
for i=1:h
for j=1:w
if(img_y(i,j)>THRESHOLD_DATA)
img_bin(i,j)=255;
else
img_bin(i,j)=0;
end
if(img_y(i,j)>THRESHOLD_DATA1)
img_bin1(i,j