算法最重要的一点是要sprintf函数实现动态读取文件名
比如:sprintf(s,"%d", 123); //产生"123"
#include <iostream>
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
for (int i = 1; i < 4;i++)
{
Mat grad_xplay, grad_yplay;
Mat abs_grad_xplay, abs_grad_yplay, dstplay;
char* buff;
buff = new char[20];
sprintf(buff, "%d_jump.bmp", i);
Mat imgplay = imread(buff);
//求x方向梯度
Sobel(imgplay, grad_xplay, CV_16S, 1, 0, 3, 1, 1, BORDER_DEFAULT);
convertScaleAbs(grad_xplay, abs_grad_xplay);
//求y方向梯度
Sobel(imgplay, grad_yplay, CV_16S, 0, 1, 3, 1, 1, BORDER_DEFAULT);
convertScaleAbs(grad_yplay, abs_grad_yplay);
//合并梯度
addWeighted(abs_grad_xplay, 0.5, abs_grad_yplay, 0.5, 0, dstplay);
namedWindow("整体方向soble", CV_WINDOW_NORMAL);
imshow("整体方向soble", dstplay);
waitKey(200);
delete[] buff;
}
waitKey(0);
return 1;
}