#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespace cv; using namespace std; Mat src, dst,dst2,gray_src; char* INPUT_WIN = "input image"; char* OUTPUT_WIN = "binary image"; int threshold_value = 127; int threshold_max = 255; int type_value = 2; int type_max = 4; int main() { src = imread(".//pic//kate.png"); namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE); namedWindow(OUTPUT_WIN, CV_WINDOW_AUTOSIZE); imshow(INPUT_WIN, src); GaussianBlur(src, dst, Size(3, 3), 0, 0); Mat gray_src; cvtColor(dst, gray_src, CV_BGR2GRAY); imshow(OUTPUT_WIN, gray_src); Mat xgrad, ygrad; //cv_16s 输出图像的深度 //X方向1阶导,Y方向不求导 //kernel大小是3 /*Sobel(gray_src, xgrad, CV_16S, 1,0, 3); Sobel(gray_src, ygrad, CV_16S, 0, 1, 3);*/ //Scharr算子:中心元素占的权重更重,边缘得到更大加强 Scharr(gray_src, xgrad, CV_16S, 1, 0, 3); Scharr(gray_src, ygrad, CV_16S, 0, 1, 3); //图像增强 convertScaleAbs(xgrad, xgrad,1.5,10); convertScaleAbs(ygrad, ygrad); imshow("xgrad", xgrad); imshow("ygrad", ygrad); //x,y方向都加进来 Mat res; addWeighted(xgrad, 0.5, ygrad, 0.5, 0, res); imshow("res", res); waitKey(0); return 0; }