从图像中减去除面积最大的轮廓之外的所有轮廓
Subtract all contours from image except the one with the largest area
cv::Mat thr;
std::vector<std::vector<cv::Point> > contours;
std::vector<std::vector<cv::Vec4i> > hierarchy;
int largest_area = 0;
int largest_contour_index = 0;
cv::findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
for( int i = 0; i < contours.size(); i++ ) // iterate through each contour.
{
double a = contourArea( contours[i], false ); // Find the area of contour
if(a > largest_area)
{
largest_area = a;
largest_contour_index = i; // Store the index of largest contour
}
}
找到最大等高线的索引后该怎么办?如何删除所有其他轮廓及其内部区域?图像是二进制的(cv::Mat thr)。只有黑色背景与白色区域。谢谢。
在您的情况下,删除轮廓及其内部区域等于将其填充为黑色。这可以通过用黑色绘制轮廓区域来完成:
for (size_t i=0; i<contours.size(); ++i) {
if (i != largest_contour_index) { // not the largest one
cv::drawContours(thr, contours, i, cv::Scalar(0,0,0), CV_FILLED);
}
}
找到轮廓后,找到最大轮廓的索引并在垫子上绘制该轮廓。
int indexOfBiggestContour = -1;
int sizeOfBiggestContour = 0;
for (int i = 0; i < contours.size(); i++)
{
if (contours[i].size() > sizeOfBiggestContour)
{
sizeOfBiggestContour = contours[i].size();
indexOfBiggestContour = i;
}
}
cv::Mat newImage;
drawContours(newImage, contours, indexOfBiggestContour, Scalar(255), CV_FILLED, 8, hierarchy);
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何使用 OpenCv 从图像中"cut"轮廓?
- 在OpenCV中使用遮罩和ROI计算轮廓内浮器的图像像素值(例如平均值)
- 图像绘制使用NDK显示轮廓
- 如何在 OpenCv 中使用 HSV 阈值和轮廓从图像中提取皮肤
- 如何从图像中删除矩形轮廓
- 从图像中减去除面积最大的轮廓之外的所有轮廓
- 如何比较比较两个图像的轮廓
- 如何匹配轮廓来检测图像上字母的ASCII
- 实现图像轮廓绘制的基本思想
- OpenCV:在处理后的图像中选择轮廓
- 如何在OpenCV中找到图像中的轮廓
- 绘制从二值图像中检索到的轮廓
- 图像轮廓检测错误:OpenCV,C++
- 尝试从深度图像中提取手部轮廓时出现无效矩阵错误