我编写了一个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 violation和Access violation reading in FeatureDetector OpenCV 2.4.5建议的解决方案。但无济于事。
我的问题的一个解决方案是在我调用.detect()之前添加一个threshold()调用,这似乎使它工作。但是我不喜欢这个解决方案,因为它迫使我做一些我不需要做的事情(据我所知),因为出于某种原因没有必要在我的Mac上做。
任何人都可以解释为什么以下这一行:
threshold(imgThresh, imgThresh, 100, 255, 0);
在以下代码中调用.detect()之前,Windows上是否需要,而不是在OSX上?
完整的代码段:
#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;
}
答案 0 :(得分:2)
尝试以这种方式使用它:
Ptr<SimpleBlobDetector> sbd = SimpleBlobDetector::create(params);
vector<cv::KeyPoint> keypoints;
sbd->detect(imgThresh, keypoints);