OpenCV方块:过滤输出
这是一个输出平方检测的例子,我的问题是过滤这个平方OpenCV方块:过滤输出,opencv,filter,detection,Opencv,Filter,Detection,这是一个输出平方检测的例子,我的问题是过滤这个平方 第一个问题是为同一区域绘制一条以上的线 第二,我只需要检测物体,而不是所有的图像 另一个问题是,我必须只拍摄最大的物体,除了所有的图像 以下是检测代码: 静态空心findSquares(常数矩阵和图像、向量>和正方形){ squares.clear(); Mat-pyr,timg,灰色0(image.size(),CV_8U),灰色; //缩小和放大图像以滤除噪声 pyrDown(image,pyr,Size(image.cols/2
- 第一个问题是为同一区域绘制一条以上的线李>
- 第二,我只需要检测物体,而不是所有的图像
静态空心findSquares(常数矩阵和图像、向量>和正方形){
squares.clear();
Mat-pyr,timg,灰色0(image.size(),CV_8U),灰色;
//缩小和放大图像以滤除噪声
pyrDown(image,pyr,Size(image.cols/2,image.rows/2));
pyrUp(pyr,timg,image.size());
矢量等值线;
//在图像的每个颜色平面中查找正方形
对于(int c=0;c<3;C++)
{
int ch[]={c,0};
混音通道(&timg,1,&0,1,ch,1);
//尝试几个阈值级别
对于(int l=0;l=(l+1)*255/N;
}
//找到等高线并将其全部存储为列表
findContours(灰色、等高线、等高线列表、等高线链近似简单);
向量近似;
//测试每个轮廓
对于(size_t i=0;i1000&&
isContourConvex(材料(近似)))
{
双最大余弦=0;
对于(int j=2;j<5;j++)
{
//求关节边之间角度的最大余弦
双余弦=fabs(角度(约[j%4],约[j-2],约[j-1]);
最大余弦=最大值(最大余弦,余弦);
}
如果(最大余弦<0.3)
正方形。推回(大约);
}
}
}
}
}
您需要查看的标志。您可以设置一个名为CV_RETR_EXTERNAL的标志,该标志将仅返回最外层的轮廓(其中的所有轮廓都将被丢弃)。这可能会返回整个帧,因此您需要缩小搜索范围,使其不会检查帧边界。使用函数copyMakeBorder()完成此操作。我还建议删除“放大”功能,因为它可能会导致线条两侧的轮廓重复(如果删除“放大”,甚至可能不需要边界)。以下是我的输出:
计算检测到的正方形的面积,然后取最大的一个。您可以通过检查检测到的正方形是否小于图像的95%等,尝试消除“整个图像正方形”。同时添加原始图像,所以用户可以对其进行操作并向您演示。原始图像iiro我以前考虑过这一点,但这需要时间进行处理,因为有时在同一区域绘制20条线,我给出的图像数组列表类似于40条或更多,不认为这是正确的方式谢谢您的回答
squares.clear();
Mat pyr, timg, gray0(image.size(), CV_8U), gray;
// down-scale and upscale the image to filter out the noise
pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
pyrUp(pyr, timg, image.size());
vector<vector<Point> > contours;
// find squares in every color plane of the image
for( int c = 0; c < 3; c++ )
{
int ch[] = {c, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);
// try several threshold levels
for( int l = 0; l < N; l++ )
{
// hack: use Canny instead of zero threshold level.
// Canny helps to catch squares with gradient shading
if( l == 0 )
{
// apply Canny. Take the upper threshold from slider
// and set the lower to 0 (which forces edges merging)
Canny(gray0, gray, 0, thresh, 5);
// dilate canny output to remove potential
// holes between edge segments
dilate(gray, gray, Mat(), Point(-1,-1));
}
else
{
// apply threshold if l!=0:
gray = gray0 >= (l+1)*255/N;
}
// find contours and store them all as a list
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
vector<Point> approx;
// test each contour
for( size_t i = 0; i < contours.size(); i++ )
{
approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);
if( approx.size() == 4 &&
fabs(contourArea(Mat(approx))) > 1000 &&
isContourConvex(Mat(approx)) )
{
double maxCosine = 0;
for( int j = 2; j < 5; j++ )
{
// find the maximum cosine of the angle between joint edges
double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
maxCosine = MAX(maxCosine, cosine);
}
if( maxCosine < 0.3 )
squares.push_back(approx);
}
}
}
}