自适应中值滤波(基于C++和OpenCV)Kinect深度图

<span style="font-family:Microsoft YaHei;font-size:14px;">#include <opencv2/opencv.hpp>
#include <vector>
#define uint unsigned int 
using namespace cv; 

const uint rowNumber = 480;
const uint colNumber = 640;

void AutoMedianFilter(Mat src, Mat dst, uint window){
	uint d = window / 2;
	for(uint px = d; px < (rowNumber-d); ++px){
		for(uint py = d; py < (colNumber-d); ++py){
			std::vector<uchar> v;
			for(uint sx = (px-d); sx < (px+d); ++sx){
				for(uint sy = (py-d); sy < (py+d); ++sy){
					v.push_back(src.at<uchar>(sx,sy));
				}
			}

			for(uint i = 0; i < (v.size()-1); ++i){
				for(uint j = (i+1); j < v.size(); ++j){
					if(v.at(i) > v.at(j)){ uchar temp; temp = v.at(i); v.at(i) = v.at(j); v.at(j) = temp; }
				}
			}

   			if( (src.at<uchar>(px,py) == v.at((v.size()-1))) || (src.at<uchar>(px,py) == v.at(0)) ){
 				dst.at<uchar>(px,py) = v.at( (v.size() / 2) );
   			}
		}
	}
}

int main( )
{ 
	Mat image=imread("2.jpg", CV_8UC1); 

	namedWindow( "中值滤波【原图】" ); 
 	namedWindow( "中值滤波【Auto】"); 
 	namedWindow( "中值滤波【opencv】"); 

	Mat out1 = image.clone(); 
	AutoMedianFilter(image, out1, 15);

 	Mat out2;
	medianBlur(image, out2, 15);

	imshow( "中值滤波【原图】", image ); 
	imshow( "中值滤波【Auto】" ,out1 ); 
 	imshow( "中值滤波【opencv】", out2); 

	waitKey( 0 );     
	return(0);
} </span>

原图:

自适应中值滤波:

opencv中的中值滤波:


你可能感兴趣的:(filter,opencv,kinect)