#include
#include
#include
using namespace std;
using namespace cv;
int g_nWay = 0, g_nDirection = 0, g_nValue = 0;
int main()
{
Mat srcImage = imread("1.jpg");
imshow("【原图】", srcImage);
Mat grayImage;
cvtColor(srcImage, grayImage, CV_BGR2GRAY);
blur(srcImage, srcImage, Size(3, 3));
blur(grayImage, grayImage, Size(3, 3));
Mat dstImage, XImage, YImage, XYImage;
Mat AbsXImage, AbsYImage, AbsXYImage;
namedWindow("【显示窗口】", 0);
createTrackbar("Gray/BGR", "【显示窗口】", &g_nWay, 1, 0);
createTrackbar("X/Y/XY", "【显示窗口】", &g_nDirection, 2, 0);
char key;
vector v;
while (1)
{
if (!g_nWay)
{
Scharr(grayImage, XImage, CV_16S, 1, 0, 1, 0);
//将得到的矩阵转换为8bit的矩阵
convertScaleAbs(XImage, AbsXImage);
Scharr(grayImage, YImage, CV_16S, 0, 1, 1, 0);
convertScaleAbs(YImage, AbsYImage);
/*v.push_back(AbsXImage);
v.push_back(AbsYImage);
merge(v, XYImage);*/
addWeighted(AbsXImage, 0.5, AbsYImage, 0.5, 0, XYImage);
if (g_nDirection == 0)
dstImage = AbsXImage;
if (g_nDirection == 1)
dstImage = AbsYImage;
if (g_nDirection == 2)
dstImage = XYImage;
}
if (g_nWay)
{
Scharr(srcImage, XImage, CV_16S, 1, 0, 1, 0);
//将得到的矩阵转换为8bit的矩阵
convertScaleAbs(XImage, AbsXImage);
Scharr(srcImage, YImage, CV_16S, 0, 1, 1, 0);
convertScaleAbs(YImage, AbsYImage);
addWeighted(AbsXImage, 0.5, AbsYImage, 0.5, 0, XYImage);
if (g_nDirection == 0)
dstImage = AbsXImage;
if (g_nDirection == 1)
dstImage = AbsYImage;
if (g_nDirection == 2)
dstImage = XYImage;
}
imshow("【显示窗口】", dstImage);
key = waitKey(10);
if (key == 27)
break;
}
}