【问题标题】:cv::SimpleBlobDetector detect() produce access violation exception in Visual Studio 2010cv::SimpleBlobDetector detect() 在 Visual Studio 2010 中产生访问冲突异常
【发布时间】:2013-10-30 07:09:54
【问题描述】:

首先是一些背景

我编写了一个 C++ 函数,该函数使用 OpenCV 检测 RGB 图像中某种颜色的区域。该函数用于使用 FeatureDetector 隔离一个小的彩色区域:SimpleBlobDetector

我遇到的问题是这个函数是在跨平台项目中使用的。在我在 Xcode 中使用 OpenCV 的 OSX 10.8 机器上,这可以完美运行。但是,当我尝试在 Visual Studio 中使用 OpenCV 在 Windows 上运行相同的代码时,每当我使用此代码时都会崩溃:

blobDetector.detect(imgThresh, keypoints)

出现如下错误:

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in unknown function, file C:\slave\builds\WinInstallerMegaPack\src\opencv\modules\core\include\opencv2/core/mat.hpp, line 545

这是迄今为止唯一给我带来问题的 OpenCV 代码。我尝试了几种解决方案,例如这里建议的解决方案 Using FeatureDetector in OpenCV gives access violationAccess violation reading in FeatureDetector OpenCV 2.4.5 。但无济于事。

对我的问题的某种解决方案是在我调用 .detect() 之前添加一个 threshold() 调用,这似乎使它工作。但是我不喜欢这种解决方案,因为它迫使我做一些我不需要做的事情(据我所知),而且由于某种原因没有必要在我的 Mac 上做。

问题

谁能解释为什么下面这行:

threshold(imgThresh, imgThresh, 100, 255, 0);

在 Windows 上是必需的,但在 OSX 上不需要,就在以下代码中调用 .detect() 之前?

完整代码sn-p:

#include "ColorDetector.h"

using namespace cv;
using namespace std;

Mat ColorDetection(Mat img, Scalar colorMin, Scalar colorMax, double alpha, int beta)
{
    initModule_features2d();
    initModule_nonfree();

    //Define matrices
    Mat contrast_img = constrastImage(img, alpha, beta);
    Mat imgThresh;
    Mat blob;

    //Threshold based on color ranges (Blue/Green/Red scalars)
    inRange(contrast_img, colorMin, colorMax, imgThresh); //BGR range

    //Apply Blur effect to make blobs more coherent
    GaussianBlur(imgThresh, imgThresh, Size(3,3), 0);

    //Set SimpleBlobDetector parameters
    SimpleBlobDetector::Params params;
    params.filterByArea = false;
    params.filterByCircularity = false;
    params.filterByConvexity = false;
    params.filterByInertia = false;
    params.filterByColor = true;
    params.blobColor = 255;
    params.minArea = 100.0f;
    params.maxArea = 500.0f;

    SimpleBlobDetector blobDetector(params);
    blobDetector.create("SimpleBlob");

    //Vector to store keypoints (center points for a blob)
    vector<KeyPoint> keypoints;

    //Try blob detection
    threshold(imgThresh, imgThresh, 100, 255, 0);
    blobDetector.detect(imgThresh, keypoints);

    //Draw resulting keypoints
    drawKeypoints(img, keypoints, blob, CV_RGB(255,255,0), DrawMatchesFlags::DEFAULT);

    return blob;
}

【问题讨论】:

  • 你搞定了吗?
  • 抱歉回复时间长。是的,我确实让它工作了。我不确定为什么我的修复工作,但是当您为某些函数覆盖目标矩阵中的值时,Windows 上的问题似乎发生了。 blobDetector.detect(imgThresh, keypoints) 引发异常的原因是 GaussianBlur(imgThresh, imgThresh...) 使 imgThresh var 无效(因此访问冲突)。调用 threshold(imgThresh, imgThresh...) 重新初始化变量并填充它。解决方法是为每个函数调用创建一个变量,例如:threshold(img, new_img_var, 100, 255, 0) 并且不重复使用相同的变量。

标签: c++ visual-studio opencv feature-detection


【解决方案1】:

尝试这样使用它:

Ptr<SimpleBlobDetector> sbd = SimpleBlobDetector::create(params);
vector<cv::KeyPoint> keypoints;
sbd->detect(imgThresh, keypoints);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多