OpenCV imgproc组件之 图像处理

Opencv图像处理部分

该部分可分为普通滤波器(boxFilter\blur\GaussianBlur\medianBlur\bilatrealFilter\filter2D)、形态学滤波器(dilate\erode\morphologyEx)、漫水填充(floodFill)、图像尺寸缩放(pyrUp\pyrDown\resize)和阈值化(Threshold\adaptivethreshold)操作几个部分。

下面给出一个简单的例子:

#include
#include
#define max(a,b) a>b?a:b
using namespace std;
enum class FilterType
{
	boxFilter,
	blur ,
	GaussianBlur,
	medianBlur ,
	bilatrealFilter,
	filter2D
}; 
//滤波器(boxFilter\blur\GaussianBlur\medianBlur\bilatrealFilter\filter2D)
void filter(cv::Mat &src, cv::Mat &dst, FilterType filterType,cv::Size size)
{
	cv::Mat srcTemp;
	float k[9] = { 0, -1, 0, -1, 5, -1, 0, -1, 0 };
	cv::Mat kenrel(3, 3, CV_32FC1, k);
	switch (filterType)
	{
	case FilterType::boxFilter:
		boxFilter(src, dst, -1, size); break;
	case FilterType::blur:
		cv::blur(src, dst, size); break;
	case FilterType::GaussianBlur:
		cv::GaussianBlur(src, dst, size,1,0); break;
	case FilterType::medianBlur:
		cv::medianBlur(src, dst, 3); break;
	/*case FilterType::bilatrealFilter:
		cv::bilateralFilter(); break;*/
	case FilterType::filter2D:		
		std::cout << kenrel << std::endl;
		cv::resize(src, srcTemp, cv::Size(), 3.0, 3.0, 0);
		cv::filter2D(srcTemp, dst, -1, kenrel);
		break;
	default:
		break;
	}
}
enum class MorphologyEx
{
	dilate,
	erode,
	close,
	open,
	tophat,
	blackhat,
	gradient
};
//形态学(dilate\erode\morphologyEx)
bool morphology(cv::Mat &src, cv::Mat &dst, const cv::Mat &kernel, MorphologyEx morphologyex)
{
	switch (morphologyex)
	{
	case MorphologyEx::dilate:
		morphologyEx(src, dst, cv::MORPH_DILATE, kernel); break;
	case MorphologyEx::erode:
		morphologyEx(src, dst, cv::MORPH_ERODE, kernel); break;
	case MorphologyEx::close:
		morphologyEx(src, dst, cv::MORPH_CLOSE, kernel); break;
	case MorphologyEx::open:
		morphologyEx(src, dst, cv::MORPH_OPEN, kernel); break;
	case MorphologyEx::tophat:
		morphologyEx(src, dst, cv::MORPH_TOPHAT, kernel); break;
	case MorphologyEx::blackhat:
		morphologyEx(src, dst, cv::MORPH_BLACKHAT, kernel); break;
	case MorphologyEx::gradient:
		morphologyEx(src, dst, cv::MORPH_GRADIENT, kernel); break;
	default:
		break;
	}
	return true;
}

//漫水填充(floodFill)
bool flood(cv::Mat &src, cv::Mat &dst)
{	
	cv::Mat spilt[3];
	cv::Mat spilt0;
	cv::Rect  ccomp;
	ccomp.x = 100;
	ccomp.y = 100; ccomp.width = 100; ccomp.height = 100;
	dst = src.clone();
	split(dst, spilt);
	spilt0 = spilt[1];
	//cv::cvtColor(dst, spilt0,cv::COLOR_RGB2GRAY);
	cv::resize(spilt0, spilt0, cv::Size(335,502), 0 ,0);cv::medianBlur(spilt0, spilt0, 5);
	cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5, 5)); 
	cv::Canny(spilt0, spilt0,3,60);
	cv::morphologyEx(spilt0, spilt0, cv::MORPH_CLOSE, kernel, cv::Point(-1, -1));
	cv::imshow("边缘",spilt0);
	cv::floodFill(dst, spilt0, cv::Point(30, 50), cv::Scalar(55, 55, 55), &ccomp, cv::Scalar(20, 20, 20), cv::Scalar(20, 20, 20));
	
	return true;
}
enum class ResizeType
{
	pyrUp,
	pyrDown,
	resize,
};
//图像尺寸缩放(pyrUp\pyrDown\resize)
bool sizeChange(cv::Mat &src, cv::Mat &dst, int sizeRows, int sizeCols, ResizeType resizetype)
{
	switch (resizetype)
	{
	case ResizeType::pyrUp:
		cv::pyrUp(src, dst); break;
	case ResizeType::pyrDown:
		cv::pyrDown(src, dst); break;
	case ResizeType::resize:
		cv::resize(src, dst, cv::Size(sizeRows, sizeCols), 0, 0); break;
	default:
		break;
	}
	return true;
}
//和阈值化(Threshold\adaptivethreshold)
bool threshold(cv::Mat &src, cv::Mat &dst)
{
	if (src.channels() == 1)
		//cv::threshold(src, dst, 135, 255, CV_THRESH_BINARY);
		cv::adaptiveThreshold(src, dst,125,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY_INV,3,0);
	else
		;
	return true;
}

int main()
{	
	cv::Mat src = cv::imread("D:\\OpenCVprivate\\5.jpg");
	cv::Mat morphDst, resizeDst, floodDst, filterDst, thsDst;
	cv::imshow("原图", src);	
	
	cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(9, 9));
	morphology(src, morphDst, kernel, MorphologyEx::blackhat);
	imshow("morphResult", morphDst);

	sizeChange(src, resizeDst, 300, 600, ResizeType::resize);
	imshow("resizeDst", resizeDst);

	flood(src, floodDst);
	imshow("floodDst", floodDst);

	cv::Size size(5,5);
	filter(src, filterDst, FilterType::GaussianBlur, size);
	imshow("filterDst", filterDst);

	cv::cvtColor(src, thsDst, cv::COLOR_RGB2GRAY);
	threshold(thsDst, thsDst);
	imshow("thsDst", thsDst);

	cv::waitKey();
	return 0;
}

你可能感兴趣的:(图像处理,OpenCV基础)