meanshift 图像分割及漫水填充

//meanshift程序
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/core/core.hpp"  
#include "opencv2/imgproc/imgproc.hpp"  

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
	Mat src_img = imread("C:/Users/YP/Desktop/运行图片/Right20.bmp");           //读入图像,RGB三通道    
	imshow("src 1210", src_img);
	Mat dst_img;                    //分割后图像  
	int spatialRad = 30;        //空间窗口大小  
	int colorRad = 30;          //色彩窗口大小  
	int maxPyrLevel = 2;        //金字塔层数  
	pyrMeanShiftFiltering(src_img, dst_img, spatialRad, colorRad, maxPyrLevel); //色彩聚类平滑滤波  
	imshow("dst 1210", dst_img);
	RNG rng = theRNG();
	Mat mask(dst_img.rows + 2, dst_img.cols + 2, CV_8UC1, Scalar::all(0));  //掩模  
	for (int y = 0; y < dst_img.rows; y++)
	{
		for (int x = 0; x < dst_img.cols; x++)
		{
			if (mask.at<uchar>(y + 1, x + 1) == 0)  //非0处即为1,表示已经经过填充,不再处理  
			{
				Scalar newVal(rng(256), rng(256), rng(256));
				floodFill(dst_img, mask, Point(x, y), newVal, 0, Scalar::all(5), Scalar::all(5)); //执行漫水填充  
			}
		}
	}
	imshow("漫水填充", dst_img);
	waitKey();
	return 0;
}

结果图:
(1)漫水填充后
meanshift 图像分割及漫水填充_第1张图片
(2)分割后
meanshift 图像分割及漫水填充_第2张图片

你可能感兴趣的:(计算机视觉,计算机,计算机视觉,c++)