【openCV】matlabにおけるbwareaopenとbwmorph(src,'thin',Inf)opencvでの実装

4803 ワード

最近opencvで伝統的な画像処理の方面のいくつかの仕事をして、matlabの下のいくつかのコードをC++で実現する必要があって、その中のbwareapenは私自身で実現して、bwmorphの細分化はこの文章のクリックしてリンクを開けて実現して、現在の機能はすでにあって、まだ最適化するかもしれません.くだらないことは言わないで、コードをつけましょう.
void Matlab::bwareaopen(Mat src, Mat &dst, double min_area){
	dst = src.clone();
	vector > 	contours;
	vector 			hierarchy;
	findContours(src, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point());
	if(!contours.empty() && !hierarchy.empty()){
		vector >::const_iterator itc = contours.begin();
		while(itc != contours.end()){
			Rect rect = boundingRect(Mat(*itc));
			double area = contourArea(*itc);
			if(area < min_area){
				for(int i=rect.y;i(i);
					for(int j=rect.x;j maxIterations) //              
			break;  
		std::vector mFlag; //            
		//      
		for (int i = 0; i < height ;++i){  
			uchar * p = dst.ptr(i);  
			for (int j = 0; j < width; ++j){  
				//        ,      
				//  p9 p2 p3  
				//  p8 p1 p4  
				//  p7 p6 p5  
				uchar p1 = p[j];  
				if (p1 != 1) continue;  
				uchar p4 = (j == width - 1) ? 0 : *(p + j + 1);  
				uchar p8 = (j == 0) ? 0 : *(p + j - 1);  
				uchar p2 = (i == 0) ? 0 : *(p - dst.step + j);  
				uchar p3 = (i == 0 || j == width - 1) ? 0 : *(p - dst.step + j + 1);  
				uchar p9 = (i == 0 || j == 0) ? 0 : *(p - dst.step + j - 1);  
				uchar p6 = (i == height - 1) ? 0 : *(p + dst.step + j);  
				uchar p5 = (i == height - 1 || j == width - 1) ? 0 : *(p + dst.step + j + 1);  
				uchar p7 = (i == height - 1 || j == 0) ? 0 : *(p + dst.step + j - 1);  
				if ((p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) >= 2 && (p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) <= 6){  
					int ap = 0;  
					if (p2 == 0 && p3 == 1) ++ap;  
					if (p3 == 0 && p4 == 1) ++ap;  
					if (p4 == 0 && p5 == 1) ++ap;  
					if (p5 == 0 && p6 == 1) ++ap;  
					if (p6 == 0 && p7 == 1) ++ap;  
					if (p7 == 0 && p8 == 1) ++ap;  
					if (p8 == 0 && p9 == 1) ++ap;  
					if (p9 == 0 && p2 == 1) ++ap;  

					if (ap == 1 && p2 * p4 * p6 == 0 && p4 * p6 * p8 == 0){  
						//    
						mFlag.push_back(p+j);  
					}  
				}  
			}  
		}  

		//         
		for (std::vector::iterator i = mFlag.begin(); i != mFlag.end(); ++i){  
			**i = 0;  
		}  

		//       ,      
		if (mFlag.empty()){  
			break;  
		}  
		else{  
			mFlag.clear();// mFlag    
		}  

		//      
		for (int i = 0; i < height; ++i){  
			uchar * p = dst.ptr(i);  
			for (int j = 0; j < width; ++j){  
				//        ,      
				//  p9 p2 p3  
				//  p8 p1 p4  
				//  p7 p6 p5  
				uchar p1 = p[j];  
				if (p1 != 1) continue;  
				uchar p4 = (j == width - 1) ? 0 : *(p + j + 1);  
				uchar p8 = (j == 0) ? 0 : *(p + j - 1);  
				uchar p2 = (i == 0) ? 0 : *(p - dst.step + j);  
				uchar p3 = (i == 0 || j == width - 1) ? 0 : *(p - dst.step + j + 1);  
				uchar p9 = (i == 0 || j == 0) ? 0 : *(p - dst.step + j - 1);  
				uchar p6 = (i == height - 1) ? 0 : *(p + dst.step + j);  
				uchar p5 = (i == height - 1 || j == width - 1) ? 0 : *(p + dst.step + j + 1);  
				uchar p7 = (i == height - 1 || j == 0) ? 0 : *(p + dst.step + j - 1);  

				if ((p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) >= 2 && (p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) <= 6){  
					int ap = 0;  
					if (p2 == 0 && p3 == 1) ++ap;  
					if (p3 == 0 && p4 == 1) ++ap;  
					if (p4 == 0 && p5 == 1) ++ap;  
					if (p5 == 0 && p6 == 1) ++ap;  
					if (p6 == 0 && p7 == 1) ++ap;  
					if (p7 == 0 && p8 == 1) ++ap;  
					if (p8 == 0 && p9 == 1) ++ap;  
					if (p9 == 0 && p2 == 1) ++ap;  

					if (ap == 1 && p2 * p4 * p8 == 0 && p2 * p6 * p8 == 0){  
						//    
						mFlag.push_back(p+j);  
					}  
				}  
			}  
		}  

		//         
		for (std::vector::iterator i = mFlag.begin(); i != mFlag.end(); ++i){  
			**i = 0;  
		}  

		//       ,      
		if (mFlag.empty()){  
			break;  
		}else{  
			mFlag.clear();// mFlag    
		}  
	}  
}