报错
0x00007FFEE28BFDEC 处(位于 test1.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x000000C13B12E260 处。
求各位支招,下面是主函数源码
#define _USE_MATH_DEFINES
#include
#include
#include
#include
#include "opencv2/calib3d/calib3d.hpp"
#include
#include
#include
using namespace std;
using namespace cv;
// 高斯函数
double gauss(double x, double y, double sigma)
{
return exp(-(x * x + y * y) / (2 * sigma * sigma));
}
// 计算高斯差分金字塔
vector gaussian_diff_pyramid(Mat img)
{
vector pyramid, puramid;
pyramid.push_back(img.clone());
for (int i = 1; i < 4; i++)
{
Mat downsampled;
pyrDown(pyramid[i - 1], downsampled);
pyramid.push_back(downsampled);
}
vector diffs;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Mat upsampled;
pyrUp(pyramid[i + 1], upsampled);
Mat diff = pyramid[i].clone() - upsampled;
diffs.push_back(diff);
}
}
return diffs;
}
// 计算特征点
vector compute_keypoints(vector diffs)
{
vector keypoints;
for (int i = 1; i < 3; i++)
{
for (int j = 1; j < 3; j++)
{
Mat diff = diffs[(i - 1) * 3 + j - 1];
Mat dx, dy;
Sobel(diff, dx, CV_64F, 1, 0);
Sobel(diff, dy, CV_64F, 0, 1);
Mat dxx, dxy, dyy;
Sobel(dx, dxx, CV_64F, 1, 0);
Sobel(dx, dxy, CV_64F, 0, 1);
Sobel(dy, dxy, CV_64F, 1, 0);
Sobel(dy, dyy, CV_64F, 0, 1);
for (int x = 1; x < diff.rows - 1; x++)
{
for (int y = 1; y < diff.cols - 1; y++)
{
double det = dxx.at<double>(x, y) * dyy.at<double>(x, y) - dxy.at<double>(x, y) * dxy.at<double>(x, y);
double trace = dxx.at<double>(x, y) + dyy.at<double>(x, y);
double hessian = det / trace;
if (abs(hessian) > 0.0004)
{
KeyPoint kp(y * 2, x * 2, 2);
keypoints.push_back(kp);
}
}
}
}
}
return keypoints;
}
// 计算特征描述符
Mat compute_descriptors(Mat img, vector keypoints)
{
Mat descriptors(keypoints.size(), 64, CV_32F);
for (int i = 0; i < keypoints.size(); i++)
{
cout << keypoints.size() << endl;
double x = keypoints[i].pt.x;
double y = keypoints[i].pt.y;
double scale = keypoints[i].size / 2;
double angle = keypoints[i].angle;
double yscale, xscale;
int border_size1 = 2 * scale; // 扩边的像素数
Mat img_border;
cv::copyMakeBorder(img, img_border, border_size1, border_size1, border_size1, border_size1, cv::BORDER_REFLECT_101);
cout << y - scale + border_size1 << endl;
cout << x - scale << endl;
Mat patch(img_border, cv::Rect(x - scale, y - scale + border_size1, 2 * scale, 2 * scale));
cv::Scalar mean, stddev;
cv::meanStdDev(img, mean, stddev);
double std = stddev.val[0];
double mean1 = mean.val[0];
resize(patch, patch, Size(16, 16));
patch.convertTo(patch, CV_32F);
patch = (patch - mean1) / std;
Mat patch_extended;
int border_size = 1; // 扩边的像素数
copyMakeBorder(patch, patch_extended, border_size, border_size, border_size, border_size, BORDER_REFLECT_101);
for (int j = 0; j < 16; j += 4)
{
for (int k = 0; k < 16; k += 4)
{
Mat subpatch(patch, Rect(k, j, 4, 4));
vector<float> hist(8, 0);
for (int m = 0; m < subpatch.rows; m++)
{
for (int n = 0; n < subpatch.cols; n++)
{
double dx = subpatch.at<float>(m, min(n + 1, subpatch.cols - 1)) - subpatch.at<float>(m, max(n - 1, 0));
double dy = subpatch.at<float>(min(m + 1, subpatch.rows - 1), n) - subpatch.at<float>(max(m - 1, 0), n);
double mag = sqrt(dx * dx + dy * dy);
double ori = atan2(dy, dx) * 180 / M_PI;
if (ori < 0) ori += 360;
int bin = ori / 45;
hist[bin] += mag;
}
}
Mat subdesc(1, 8, CV_32F);
for (int m = 0; m < 8; m++)
{
subdesc.at<float>(0, m) = hist[m];
}
Mat subdesc_norm;
normalize(subdesc, subdesc_norm);
for (int m = 0; m < 8; m++)
{
descriptors.at<float>(i, j / 4 * 8 + m) = subdesc_norm.at<float>(0, m);
}
}
}
}
return descriptors;
}
// 匹配特征点
vector match_keypoints(Mat descriptors1, Mat descriptors2)
{
vector matches;
cv::flann::Index kdtree(descriptors2, cv::flann::KDTreeIndexParams());
for (int i = 0; i < descriptors1.rows; i++)
{
Mat query(descriptors1, Rect(0, i, descriptors1.cols, 1));
vector<float> dists(2);
vector<int> indices(2);
kdtree.knnSearch(query, indices, dists, 2);
if (dists[0] < 0.6 * dists[1])
{
DMatch match(i, indices[0], dists[0]);
matches.push_back(match);
}
}
return matches;
}
int main()
{
Mat img1 = imread("D:\\111111.jpg");
Mat img2 = imread("D:\\22222.jpg");
vector diffs1 = gaussian_diff_pyramid(img1);
vector diffs2 = gaussian_diff_pyramid(img2);
vector keypoints1 = compute_keypoints(diffs1);
vector keypoints2 = compute_keypoints(diffs2);
Mat descriptors1 = compute_descriptors(img1, keypoints1);
Mat descriptors2 = compute_descriptors(img2, keypoints2);
vector matches = match_keypoints(descriptors1, descriptors2);
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
imshow("Matches", img_matches);
waitKey(0);
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话: