Canny边缘检测原理
1.使用高斯滤波器平滑噪声 2.计算图像中每个像素点的梯度强度和方向 3. 应用非极大值抑制,消除边缘检测带来的杂散响应 4.应用双阈值来确定真实的和潜在的边缘
1. 高斯滤波
高斯滤波器是一种可以使图像平滑的滤波器,用于去除噪声 它是一种线性平滑滤波 流程:对整张图像进行加权平均的过程,每一个像素点,都由其本身和邻域内像素的加权平均值取替代模板中心的像素值。
高斯滤波的权重服从二维正态分布,越靠近窗口中心点(也即当前滤波点),权重越大。对于(2n+1)*(2n+1)窗口,其权重计算公式如下,其中σ为标准差,σ越大,权重分布越均匀,滤波效果越好,同时图像越模糊,反之,σ越小,权重分布越偏向于窗口中心点,滤波效果越差,同时图像越能保留其原有清晰度。 实际上,为了使加权和之后的值不超过像素值本来的最大范围,需要对上式做一下归一化,使0<w(i,j)<1。如下式
2.计算梯度强度和方向
计算梯度幅值和方向.可选用soble算子、Prewitt算子、Roberts模板等等.这样就可以得图像在x和y方向梯度.如x和y方向的Sobel算子分别为: 其中梯度G和方向θ 如下图所示 当然,对梯度幅值也可以采用近似求法:
3.非极大值抑制
?极?值抑制是对除去?极?值以外的值的操作的总称,在这?,我们?较我们我们所关注的地?梯度的法线?向邻接的三个像素点的梯度幅值,如果该点的梯度值不?其它两个像素?,那么这个地?的值设置为0。 通俗意义上是指寻找像素点局部最大值.在每一点上,领域中心x与沿着其对应的梯度方向的两个像素相比,若中心像素为最大值,则保留,否则中心置0,这样可以抑制非极大值,保留局部梯度最大的点,以细化边缘. 如上图所示,需要将中心像素与其梯度方向的两个像素进行比较.但是,由于图像中的像素点是离散的二维矩阵,其梯度方向两侧并没有真实存在的像素,或者说是一个亚像素(sub pixel)点,对于这个不存在的点的位置与梯度就必须通过对其两侧的点进行插值来得到
由于利用插值求解梯度,运算量比较大,在John Canny提出Canny算子的论文中,非极大值抑制时,他采用了近似处理的方法**.将各点的梯度方向近似/量化到0°、90°、45°、135°四个梯度方向上进行**,每个像素点梯度方向按照距离这四个方向的相近程度用这四个方向来代替.通过量化,意味着图像中各点的梯度方向只能沿着0°、90°、45°、135°四个方向中的某一个. 如下图所示为选择的4个量化方向,每个方向对应邻域内的两个点,4个方向正好将邻域内的8个点应用完毕 如下为非极大值抑制示意图.图中以颜色深浅表示梯度幅值大小.经过NMS后,原边缘被细化.意味着在真实边缘处是单点响应.
4.应用双阈值来确定真实的和潜在的边缘
在施加非极大值抑制之后,剩余的像素可以更准确地表示图像中的实际边缘.然而,仍然存在由于噪声和颜色变化引起的一些边缘像素.为了解决这些杂散响应,必须用弱梯度值过滤边缘像素,并保留具有高梯度值的边缘像素,可以通过选择高低阈值来实现. Canny 推荐的 高:低 阈值比在 2:1 到3:1之间
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
cv::Mat BGR2GRAY(cv::Mat img){
int height = img.rows;
int width = img.cols;
int channel = img.channels();
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
out.at<uchar>(y, x) = (int)((float)img.at<cv::Vec3b>(y, x)[0] * 0.0722 + \
(float)img.at<cv::Vec3b>(y, x)[1] * 0.7152 + \
(float)img.at<cv::Vec3b>(y, x)[2] * 0.2126);
}
}
return out;
}
float clip(float value, float min, float max){
return fmin(fmax(value, 0), 255);
}
cv::Mat gaussian_filter(cv::Mat img, double sigma, int kernel_size){
int height = img.rows;
int width = img.cols;
int channel = img.channels();
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);
if (channel == 1) {
out = cv::Mat::zeros(height, width, CV_8UC1);
}
int pad = floor(kernel_size / 2);
int _x = 0, _y = 0;
double kernel_sum = 0;
float kernel[kernel_size][kernel_size];
for (int y = 0; y < kernel_size; y++){
for (int x = 0; x < kernel_size; x++){
_y = y - pad;
_x = x - pad;
kernel[y][x] = 1 / (2 * M_PI * sigma * sigma) * exp( - (_x * _x + _y * _y) / (2 * sigma * sigma));
kernel_sum += kernel[y][x];
}
}
for (int y = 0; y < kernel_size; y++){
for (int x = 0; x < kernel_size; x++){
kernel[y][x] /= kernel_sum;
}
}
double v = 0;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
if (channel == 3){
for (int c = 0; c < channel; c++){
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((x + dx) >= 0) && ((y + dy) >= 0) && ((x + dx) < width) && ((y + dy) < height)){
v += (double)img.at<cv::Vec3b>(y + dy, x + dx)[c] * kernel[dy + pad][dx + pad];
}
}
}
out.at<cv::Vec3b>(y, x)[c] = (uchar)clip(v, 0, 255);
}
} else {
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((x + dx) >= 0) && ((y + dy) >= 0) && ((x + dx) < width) && ((y + dy) < height)){
v += (double)img.at<uchar>(y + dy, x + dx) * kernel[dy + pad][dx + pad];
}
}
}
out.at<uchar>(y, x) = (uchar)clip(v, 0, 255);
}
}
}
return out;
}
cv::Mat sobel_filter(cv::Mat img, int kernel_size, bool horizontal){
int height = img.rows;
int width = img.cols;
int channel = img.channels();
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
double kernel[kernel_size][kernel_size] = {{1, 2, 1}, {0, 0, 0}, {-1, -2, -1}};
if (horizontal){
kernel[0][1] = 0;
kernel[0][2] = -1;
kernel[1][0] = 2;
kernel[1][2] = -2;
kernel[2][0] = 1;
kernel[2][1] = 0;
}
int pad = floor(kernel_size / 2);
double v = 0;
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
v = 0;
for (int dy = -pad; dy < pad + 1; dy++){
for (int dx = -pad; dx < pad + 1; dx++){
if (((y + dy) >= 0) && (( x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)){
v += (double)img.at<uchar>(y + dy, x + dx) * kernel[dy + pad][dx + pad];
}
}
}
out.at<uchar>(y, x) = (uchar)clip(v, 0, 255);
}
}
return out;
}
cv::Mat get_edge(cv::Mat fx, cv::Mat fy){
int height = fx.rows;
int width = fx.cols;
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
double _fx, _fy;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
_fx = (double)fx.at<uchar>(y, x);
_fy = (double)fy.at<uchar>(y, x);
out.at<uchar>(y, x) = (uchar)clip(sqrt(_fx * _fx + _fy * _fy), 0, 255);
}
}
return out;
}
cv::Mat get_angle(cv::Mat fx, cv::Mat fy){
int height = fx.rows;
int width = fx.cols;
cv::Mat out = cv::Mat::zeros(height, width, CV_8UC1);
double _fx, _fy;
double angle;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
_fx = fmax((double)fx.at<uchar>(y, x), 0.000001);
_fy = (double)fy.at<uchar>(y, x);
angle = atan2(_fy, _fx);
angle = angle / M_PI * 180;
if(angle < -22.5){
angle = 180 + angle;
} else if (angle >= 157.5) {
angle = angle - 180;
}
if (angle <= 22.5){
out.at<uchar>(y, x) = 0;
} else if (angle <= 67.5){
out.at<uchar>(y, x) = 45;
} else if (angle <= 112.5){
out.at<uchar>(y, x) = 90;
} else {
out.at<uchar>(y, x) = 135;
}
}
}
return out;
}
cv::Mat non_maximum_suppression(cv::Mat angle, cv::Mat edge){
int height = angle.rows;
int width = angle.cols;
int channel = angle.channels();
int dx1, dx2, dy1, dy2;
int now_angle;
cv::Mat _edge = cv::Mat::zeros(height, width, CV_8UC1);
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
now_angle = angle.at<uchar>(y, x);
if (now_angle == 0){
dx1 = -1;
dy1 = 0;
dx2 = 1;
dy2 = 0;
} else if(now_angle == 45) {
dx1 = -1;
dy1 = 1;
dx2 = 1;
dy2 = -1;
} else if(now_angle == 90){
dx1 = 0;
dy1 = -1;
dx2 = 0;
dy2 = 1;
} else {
dx1 = -1;
dy1 = -1;
dx2 = 1;
dy2 = 1;
}
if (x == 0){
dx1 = fmax(dx1, 0);
dx2 = fmax(dx2, 0);
}
if (x == (width - 1)){
dx1 = fmin(dx1, 0);
dx2 = fmin(dx2, 0);
}
if (y == 0){
dy1 = fmax(dy1, 0);
dy2 = fmax(dy2, 0);
}
if (y == (height - 1)){
dy1 = fmin(dy1, 0);
dy2 = fmin(dy2, 0);
}
if (fmax(fmax(edge.at<uchar>(y, x), edge.at<uchar>(y + dy1, x + dx1)), edge.at<uchar>(y + dy2, x + dx2)) == edge.at<uchar>(y, x)) {
_edge.at<uchar>(y, x) = edge.at<uchar>(y, x);
}
}
}
return _edge;
}
int Canny_step2(cv::Mat img){
cv::Mat gray = BGR2GRAY(img);
cv::Mat gaussian = gaussian_filter(gray, 1.4, 5);
cv::Mat fy = sobel_filter(gaussian, 3, false);
cv::Mat fx = sobel_filter(gaussian, 3, true);
cv::Mat edge = get_edge(fx, fy);
cv::Mat angle = get_angle(fx, fy);
edge = non_maximum_suppression(angle, edge);
cv::imshow("answer(edge)", edge);
cv::imshow("answer(angle)", angle);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
int main(int argc, const char* argv[]){
cv::Mat img = cv::imread("test.jpg", cv::IMREAD_COLOR);
Canny_step2(img);
return 0;
}
|