#include"stdafx.h"
#include"stdlib.h"
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\core\core.hpp>
#include<opencv2\ml\ml.hpp>
#include<opencv2\imgproc\imgproc.hpp>
using namespace std;
using namespace cv;
#define Pai 3.1415926535897932384626
函数声明
Mat steerable_filter(Mat src, int angle);
Mat threshold(Mat srcimage);
Mat x2_Filter(Mat src);
int main()
{
Mat src = imread("101.bmp");
Mat gray;
cvtColor(src, gray, COLOR_BGR2GRAY);
Mat dst= steerable_filter(gray,45);
Mat augimg,cannyimg;
addWeighted(gray, 0.3, dst, 0.7, 0, augimg);
GaussianBlur(augimg, augimg, Size(3, 3), 0, 0);
Canny(augimg, cannyimg, 30, 100);
namedWindow("cannyimg", 0);
imshow("cannyimg", cannyimg);
namedWindow("augimg", 0);
imshow("augimg", augimg);
namedWindow("steerableFilter", 0);
imshow("steerableFilter", dst);
waitKey(0);
}
Mat steerable_filter(Mat src,int angle)src为灰度图像,angle为方向角度
{
double theta;
theta = (angle*(Pai / 180));
Mat Gxx, abs_Gxx, Gxy, Gyy,abs_Gyy, abs_Gxy;
///****************2阶Sobel,Gxx,Gyy,Gxy**********************/
Sobel(src,Gxx, CV_16S, 2, 0, 3);
Gxx.convertTo(abs_Gxx,CV_8U);
Sobel(src,Gyy, CV_16S, 0, 2, 3);
Gyy.convertTo(abs_Gyy, CV_8U);
Sobel(src,Gxy, CV_16S, 2, 2, 3);
Gxy.convertTo(abs_Gxy, CV_8U);
Mat output_16S(Gxx.size(), CV_16S, Scalar::all(0));
Mat output_8U;
for (int i = 0; i < Gxx.rows; i++)
{
uchar* value_Gxx = abs_Gxx.ptr<uchar>(i);
uchar* value_Gyy = abs_Gyy.ptr<uchar>(i);
uchar* value_Gxy = abs_Gxy.ptr<uchar>(i);
short* out_16 = output_16S.ptr<short>(i);
for (int j = 0; j < Gxx.cols; j++)
{
double t_Gxx, t_Gyy, t_Gxy;
t_Gxx = value_Gxx[j] * cos(theta) * cos(theta);
t_Gyy = value_Gyy[j] * sin(theta) * sin(theta);
t_Gxy = 2 * value_Gxy[j] * cos(theta) * sin(theta);
out_16[j] = abs(t_Gxx)+ abs(t_Gyy) +abs(t_Gxy);
}
}
//cout << output_16S << endl;
output_16S.convertTo(output_8U, CV_8U);
namedWindow("abs_Gxx", 0);
imshow("abs_Gxx", abs_Gxx);
namedWindow("abs_Gyy", 0);
imshow("abs_Gyy", abs_Gyy);
namedWindow("abs_Gxy", 0);
imshow("abs_Gxy", abs_Gxy);
namedWindow("output_8", 0);
imshow("output_8", output_8U);
return output_8U;
}