C++ 如何从轮廓中选择全部?
我有一个二值图像的轮廓,我得到一个最大的物体,我想选择所有的物体来画它。我有以下代码:C++ 如何从轮廓中选择全部?,c++,opencv,C++,Opencv,我有一个二值图像的轮廓,我得到一个最大的物体,我想选择所有的物体来画它。我有以下代码: vector<vector<Point> > contours; findContours( img.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); vector<Rect> boundSheet( contours.size() ); int largest_area=0; for( int i
vector<vector<Point> > contours;
findContours( img.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
vector<Rect> boundSheet( contours.size() );
int largest_area=0;
for( int i = 0; i< contours.size(); i++ )
{
double a= contourArea( contours[i],false);
if(a>largest_area){
largest_area=a;
boundSheet[i] = boundingRect(contours[i]);
}
}
矢量轮廓;
findContours(img.clone()、等高线、等高线、等高线外部、等高线链近似简单);
向量边界表(contours.size());
int最大面积=0;
对于(int i=0;i最大面积){
最大面积=a;
boundSheet[i]=boundingRect(等高线[i]);
}
}
我想用
drawContours
绘制边界外的所有东西,如何选择全部轮廓?看看我的答案。它演示了如何使用drawContours()将给定轮廓绘制到遮罩上以遮罩轮廓。相反,如果您希望忽略轮廓并保留轮廓以外的所有内容,只需将遮罩的原始值设置为正值,然后使用drawContours()将遮罩中的轮廓区域填充为0。也就是说,颠倒链接中给出的示例中的值。谢谢!!!!,它对我有用:D,只是颜色有问题,我想把它涂成灰色,有什么需要改变的?
using namespace cv;
int main(void)
{
// 'contours' is the vector of contours returned from findContours
// 'image' is the image you are masking
// Create mask for region within contour
Mat maskInsideContour = Mat::zeros(image.size(), CV_8UC1);
int idxOfContour = 0; // Change to the index of the contour you wish to draw
drawContours(maskInsideContour, contours, idxOfContour,
Scalar(255), CV_FILLED); // This is a OpenCV function
// At this point, maskInsideContour has value of 255 for pixels
// within the contour and value of 0 for those not in contour.
Mat maskedImage = Mat(image.size(), CV_8UC3); // Assuming you have 3 channel image
// Do one of the two following lines:
maskedImage.setTo(Scalar(180, 180, 180)); // Set all pixels to (180, 180, 180)
image.copyTo(maskedImage, maskInsideContour); // Copy pixels within contour to maskedImage.
// Now regions outside the contour in maskedImage is set to (180, 180, 180) and region
// within it is set to the value of the pixels in the contour.
return 0;
}