我有一个由canny边缘检测器过滤的图像。现在,我想检测blob并按宽度和高度进行一些过滤。我必须要考虑哪些功能?
答案 0 :(得分:4)
基于minAreaRect轮廓和minAreaRect点之间距离的替代方法。通过这种方式,可以按照样本结果图像上的角度过滤轮廓。
你可以改变宽度和宽度高度比和天使改变以下行
if(dist0 > dist1 *4) // dist0 and dist1 means width and height you can change as you wish
.
.
if( fabs(angle) > 35 & fabs(angle) < 150 ) // you can change angle criteria
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
//! Compute the distance between two points
/*! Compute the Euclidean distance between two points
*
* @param a Point a
* @param b Point b
*/
static double distanceBtwPoints(const cv::Point2f &a, const cv::Point2f &b)
{
double xDiff = a.x - b.x;
double yDiff = a.y - b.y;
return std::sqrt((xDiff * xDiff) + (yDiff * yDiff));
}
int main( int argc, char** argv )
{
Mat src,gray;
src = imread(argv[1]);
if(src.empty())
return -1;
cvtColor( src, gray, COLOR_BGR2GRAY );
gray = gray < 200;
vector<vector<Point> > contours;
findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
RotatedRect _minAreaRect;
for (size_t i = 0; i < contours.size(); ++i)
{
_minAreaRect = minAreaRect( Mat(contours[i]) );
Point2f pts[4];
_minAreaRect.points(pts);
double dist0 = distanceBtwPoints(pts[0], pts[1]);
double dist1 = distanceBtwPoints(pts[1], pts[2]);
double angle = 0;
if(dist0 > dist1 *4)
angle =atan2(pts[0].y - pts[1].y,pts[0].x - pts[1].x) * 180.0 / CV_PI;
if(dist1 > dist0 *4)
angle =atan2(pts[1].y - pts[2].y,pts[1].x - pts[2].x) * 180.0 / CV_PI;
if( fabs(angle) > 35 & fabs(angle) < 150 )
for( int j = 0; j < 4; j++ )
line(src, pts[j], pts[(j+1)%4], Scalar(0, 0, 255), 1, LINE_AA);
}
imshow("result", src);
waitKey(0);
return 0;
}
答案 1 :(得分:3)
在OpenCV 3.0中,您可以使用connectedComponentsWithStats
返回stats
数组,其中包含每个连接组件的宽度和高度:
statsv -
每个标签的统计输出,包括背景标签,请参阅下面的可用统计信息。统计数据通过statsv(label,COLUMN)访问,其中可用列定义如下。
- CC_STAT_LEFT最左边的(x)坐标,它是包含性的开始 水平方向上的边界框。
- CC_STAT_TOP最上面的(y)坐标,它是垂直方向上边界框的包含性开始。
- CC_STAT_WIDTH边界框的水平尺寸
- CC_STAT_HEIGHT边界框的垂直尺寸
- CC_STAT_AREA连接组件的总面积(以像素为单位)