均值漂移图像分割

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
//颜色通道分离
static void MergeSeg(Mat& img
	, const Scalar& colorDiff = Scalar::all(1))
{
	CV_Assert(!img.empty());
	RNG rng = theRNG();
	// 定义掩码图像
	Mat mask(img.rows + 2, img.cols + 2, 
		CV_8UC1, Scalar::all(0));
	for (int y = 0; y < img.rows; y++)
	{
		for (int x = 0; x < img.cols; x++)
		{
			if (mask.at<uchar>(y + 1, x + 1) == 0)
			{
				// 颜色定义
				Scalar newVal(rng(256), rng(256), rng(256));
				// 泛洪合并
				floodFill(img, mask, Point(x, y)
					, newVal, 0, colorDiff, colorDiff);
			}
		}
	}
}
int main(int argc, char** argv)
{
	cv::Mat srcImg = imread("..\\images\\sea.jpg");
	if (srcImg.empty())
		return -1;
	// 参数设定
	int spatialRad = 20;
	int colorRad = 20;
	int maxPyrLevel = 6;
	cv::Mat resImg;
	// 均值漂移分割
	pyrMeanShiftFiltering(srcImg, resImg, 
		spatialRad, colorRad, maxPyrLevel);
	// 颜色通道分离合并
	MergeSeg(resImg, Scalar::all(2));
	cv::imshow("src", srcImg);
	cv::imshow("resImg", resImg);
	cv::waitKey();
	return 0;
}



转载:http://blog.csdn.net/zhuwei1988

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值