|
分類:[C/C++]
こんにちは
ネットで拾った下記のコードを試しているのですが、"ココ"の所で
>OpenCV Error: Assertion failed (mask.empty() || mask.type() == CV_8U) in setTo,
>file D:\Devz\opencv\sources\modules\core\src\copy.cpp, line 347
>terminate called after throwing an instance of 'cv::Exception'
> what(): D:\Devz\opencv\sources\modules\core\src\copy.cpp:347: error: (-215) m
>ask.empty() || mask.type() == CV_8U in function setTo
と表示して落ちます。cv::Mat::setTo()の意味が判らないのでエラーの意味も
さっぱりなのですが、識者の方よろしくお願いします。
#include "opencv2/opencv.hpp"
#include <vector>
std::vector<cv::Point> bhContoursCenter(const std::vector<std::vector<cv::Point> >& contours,bool centerOfMass,int contourIdx=-1) {
std::vector<cv::Point> result;
if (contourIdx > -1) {
if (centerOfMass) {
cv::Moments m = cv::moments(contours[contourIdx],true);
result.push_back( cv::Point(m.m10/m.m00, m.m01/m.m00));
}
else {
cv::Rect rct = cv::boundingRect(contours[contourIdx]);
result.push_back( cv::Point(rct.x + rct.width / 2 ,rct.y + rct.height / 2));
}
}
else {
if (centerOfMass) {
for (int i=0; i < (int)contours.size();i++) {
cv::Moments m = cv::moments(contours[i],true);
result.push_back( cv::Point(m.m10/m.m00, m.m01/m.m00));
}
}
else {
for (int i=0; i < (int)contours.size(); i++) {
cv::Rect rct = boundingRect(contours[i]);
result.push_back(cv::Point(rct.x + rct.width / 2 ,rct.y + rct.height / 2));
}
}
}
return result;
}
std::vector<cv::Point> bhFindLocalMaximum(cv::Mat const& src,int neighbor=2) {
cv::Mat peak_img = src.clone();
dilate(peak_img,peak_img,cv::Mat(),cv::Point(-1,-1),neighbor);
peak_img = peak_img - src;
cv::Mat flat_img ;
erode(src,flat_img,cv::Mat(),cv::Point(-1,-1),neighbor);
flat_img = src - flat_img;
threshold(peak_img,peak_img,0,255,CV_THRESH_BINARY);
threshold(flat_img,flat_img,0,255,CV_THRESH_BINARY);
bitwise_not(flat_img,flat_img);
cv::imshow("AAA", flat_img);
cv::imshow("BBB", peak_img);
cv::waitKey();
peak_img.setTo(cv::Scalar::all(255),flat_img); //ココ
bitwise_not(peak_img,peak_img);
std::vector<std::vector<cv::Point> > contours;
findContours(peak_img,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
return bhContoursCenter(contours,true);
}
int main(void) {
cv::namedWindow("AAA", 1);
cv::namedWindow("BBB", 1);
cv::Mat src = cv::imread("test.bmp", 1);
cv::imshow("AAA", src);
cv::waitKey();
std::cout << bhFindLocalMaximum(src).size() << std::endl;
}
|