OpenCVを使用して、面積の小さい連結領域を除去します。


これは後期補充の部分で、前のコードと違います。
効果図

ソースコード

//  
void CCutImageVS2013Dlg::OnBnClickedTestButton1()
{
	vector<vector<Point> > contours;  //    
	vector<Point2d> centers;    //       
	vector<vector<Point> >::iterator itr; //     
	vector<Point2d>::iterator itrc;  //       
	vector<vector<Point> > con;   //    

	double area;
	double minarea = 1000;
	double maxarea = 0;
	Moments mom;       //    
	Mat image, gray, edge, dst;
	image = imread("D:\\66.png");
	cvtColor(image, gray, COLOR_BGR2GRAY);
	Mat rgbImg(gray.size(), CV_8UC3); //      
	blur(gray, edge, Size(3, 3));       //    
	threshold(edge, edge, 200, 255, THRESH_BINARY_INV); //     ,    
	//--------      ,       --------------------------
	findContours(edge, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); //    
	itr = contours.begin();    //           
	while (itr != contours.end())
	{
		area = contourArea(*itr);  //      
		if (area<minarea)    //          
		{
			itr = contours.erase(itr); //itr  erase,      
		}
		else
		{
			itr++;
		}
		if (area>maxarea)    //      
		{
			maxarea = area;
		}
	}
	dst = Mat::zeros(image.rows, image.cols, CV_8UC3);
	/*        ,      */
	Point2d center;
	itr = contours.begin();
	while (itr != contours.end())
	{
		area = contourArea(*itr);		
		con.push_back(*itr);   //      
		if (area == maxarea)
		{
			vector<Rect> boundRect(1); //        
			boundRect[0] = boundingRect(Mat(*itr));
			cvtColor(gray, rgbImg, COLOR_GRAY2BGR);
			Rect select;
			select.x = boundRect[0].x;
			select.y = boundRect[0].y;
			select.width = boundRect[0].width;
			select.height = boundRect[0].height;
			rectangle(rgbImg, select, Scalar(0, 255, 0), 3, 2); //       
			drawContours(dst, con, -1, Scalar(0, 0, 255), 2); //        
		}
		else
			drawContours(dst, con, -1, Scalar(255, 0, 0), 2); //        
		con.pop_back();
		//    
		mom = moments(*itr);
		center.x = (int)(mom.m10 / mom.m00);
		center.y = (int)(mom.m01 / mom.m00);
		centers.push_back(center);
		itr++;
	}
	imshow("rgbImg", rgbImg);
	//imshow("gray", gray);
	//imshow("edge", edge);
	imshow("origin", image);
	imshow("connected_region", dst);
	waitKey(0);
	return;
}
前の段階のやり方は違っているかもしれません。
まず効果図を見ます。
原図

処理前後図

二、ソースコードの実現

//=======    =====================================================================
void RemoveSmallRegion(Mat &Src, Mat &Dst, int AreaLimit, int CheckMode, int NeihborMode)
{
	int RemoveCount = 0;
	//            0   ,                ,0     ,1      ,2       (      ),3            
	//         0,    
	Mat PointLabel = Mat::zeros(Src.size(), CV_8UC1);
	if (CheckMode == 1)//            
	{
		//cout << "      .";
		for (int i = 0; i < Src.rows; i++)
		{
			for (int j = 0; j < Src.cols; j++)
			{
				if (Src.at<uchar>(i, j) < 10)
				{
					PointLabel.at<uchar>(i, j) = 3;//           ,   3 
				}
			}
		}
	}
	else//    ,      
	{
		//cout << "    ";
		for (int i = 0; i < Src.rows; i++)
		{
			for (int j = 0; j < Src.cols; j++)
			{
				if (Src.at<uchar>(i, j) > 10)
				{
					PointLabel.at<uchar>(i, j) = 3;//         ,     ,   3 
				}
			}
		}
	}
	vector<Point2i>NeihborPos;//        
	NeihborPos.push_back(Point2i(-1, 0));
	NeihborPos.push_back(Point2i(1, 0));
	NeihborPos.push_back(Point2i(0, -1));
	NeihborPos.push_back(Point2i(0, 1));
	if (NeihborMode == 1)
	{
		//cout << "Neighbor mode: 8  ." << endl;
		NeihborPos.push_back(Point2i(-1, -1));
		NeihborPos.push_back(Point2i(-1, 1));
		NeihborPos.push_back(Point2i(1, -1));
		NeihborPos.push_back(Point2i(1, 1));
	}
	else int a = 0;//cout << "Neighbor mode: 4  ." << endl;
	int NeihborCount = 4 + 4 * NeihborMode;
	int CurrX = 0, CurrY = 0;
	//     
	for (int i = 0; i < Src.rows; i++)
	{
		for (int j = 0; j < Src.cols; j++)
		{
			if (PointLabel.at<uchar>(i, j) == 0)//        0,            
			{ //     
				vector<Point2i>GrowBuffer;//           
				GrowBuffer.push_back(Point2i(j, i));
				PointLabel.at<uchar>(i, j) = 1;//        
				int CheckResult = 0;
				for (int z = 0; z < GrowBuffer.size(); z++)
				{
					for (int q = 0; q < NeihborCount; q++)
					{
						CurrX = GrowBuffer.at(z).x + NeihborPos.at(q).x;
						CurrY = GrowBuffer.at(z).y + NeihborPos.at(q).y;
						if (CurrX >= 0 && CurrX<Src.cols&&CurrY >= 0 && CurrY<Src.rows) //     
						{
							if (PointLabel.at<uchar>(CurrY, CurrX) == 0)
							{
								GrowBuffer.push_back(Point2i(CurrX, CurrY)); //     buffer 
								PointLabel.at<uchar>(CurrY, CurrX) = 1;   //          ,       
							}
						}
					}
				}
				if (GrowBuffer.size()>AreaLimit) //    (         ),1    ,2    
					CheckResult = 2;
				else
				{
					CheckResult = 1;
					RemoveCount++;//           
				}
				for (int z = 0; z < GrowBuffer.size(); z++)
				{
					CurrX = GrowBuffer.at(z).x;
					CurrY = GrowBuffer.at(z).y;
					PointLabel.at<uchar>(CurrY, CurrX) += CheckResult;//         ,    2 
				}
				//********        ********** 
			}
		}
	}
	CheckMode = 255 * (1 - CheckMode);
	//            
	for (int i = 0; i < Src.rows; ++i)
	{
		for (int j = 0; j < Src.cols; ++j)
		{
			if (PointLabel.at<uchar>(i, j) == 2)
			{
				Dst.at<uchar>(i, j) = CheckMode;
			}
			else if (PointLabel.at<uchar>(i, j) == 3)
			{
				Dst.at<uchar>(i, j) = Src.at<uchar>(i, j);
			}
		}
	}
	//cout << RemoveCount << " objects removed." << endl;
}
//=======    =====================================================================
//=======    =====================================================================
	Mat img;
	img = imread("D:\\1_1.jpg", 0);//    
	threshold(img, img, 128, 255, CV_THRESH_BINARY_INV);
	imshow("   ", img);
	Mat img1;
	RemoveSmallRegion(img, img, 200, 0, 1);
	imshow("   ", img);
	waitKey(0);
//=======    =====================================================================
以上のOpenCVを使って面積の小さい接続ドメインを取り除くのは小編纂がみんなのすべての内容に分かち合うので、みんなに1つの参考をあげることができることを望んで、みんながよけいに私達を支持することをも望みます。