Cv :: SimpleBlobDetector detect () throws an access violation exception in Visual Studio 2010

First a bit of background

I wrote a C ++ function that detects an area of ​​a specific color in an RGB image using OpenCV. The function is used to highlight a small colored area using FeatureDetector: SimpleBlobDetector.

The problem is that this feature is used in a cross-platform project. On my OSX 10.8 machine using OpenCV in Xcode, this works flawlessly. However, when I try to run the same code on Windows using OpenCV in Visual Studio, this code crashes when I use:

blobDetector.detect(imgThresh, keypoints)

with such an error as:

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

This is the only piece of OpenCV code that has given me problems so far. I tried several solutions, such as those suggested here. Using FeatureDetector in OpenCV gives access violation and Reading access violation in FeatureDetector OpenCV 2.4.5 . But to no avail.

Some solution to my problem was to add a threshold () call immediately before my .detect () call, which seems to make it work. However, I do not like this solution, because it forces me to do what I do not need (as far as I know), and because for some reason I do not need to do it on my Mac.

Question

Can someone explain why the following line:

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

is required on Windows, but not on OSX, before calling .detect () in the following code?

Full code snippet:

#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;
}
+4
1

:

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

All Articles