0x00007FFEE28BFDEC 处(位于 test1.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception

报错
0x00007FFEE28BFDEC 处(位于 test1.exe 中)有未经处理的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x000000C13B12E260 处。

img

求各位支招,下面是主函数源码

#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;
}


不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt: 很抱歉,根据提供的信息无法准确判断问题的原因。需要更详细的代码和背景信息才能分析和解决问题。建议做如下尝试: 1. 查看异常报错信息的更详细描述,可能会提供更多的线索来解决问题。 2. 尝试使用调试工具,如Visual Studio等,在程序运行时捕捉异常,并打印出调用栈等信息,以定位异常出现的位置和原因。 3. 检查程序中与cv::Exception相关的代码段,确保格式正确,未发生语法错误等问题。 4. 检查运行test1.exe时的环境,如所用操作系统、依赖库、配置文件等等,确保运行环境正确且满足程序需求。 5. 尝试升级、重新编译cv::Exception对应的库文件,以确保程序使用的库文件版本正确且无异常。 如果以上尝试均未能解决问题,建议请教更有经验的程序员或相关技术支持人员协助解决。

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^