c – cv :: SimpleBlobDetector detect()在Visual Studio 2010中产生访问冲突异常

首先是一些背景

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

我遇到的问题是该功能用于跨平台项目.在我的OSX 10.8机器上使用Xcode中的OpenCV,这完美无瑕.但是,当我尝试在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()之前是必要的吗?

完整代码段:

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

最佳答案 尝试使用它:

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