我所知的opencv中分割函数:watershed(只是看看效果,不能返回每类pixel类属),cvsegmentImage,cvPyrSegmentation(返回pixel类属)

金字塔分割原理篇在这里,本文只提供代码。

Segment函数:

  1. #include<cv.h>
  2. #include <cvaux.h>
  3. #include <opencv\cxcore.hpp>
  4. #include <opencv.hpp>
  5. #include <nonfree.hpp>
  6. #include <core/core.hpp>
  7. #include <imgproc/imgproc.hpp>
  8. #include <imgproc/imgproc_c.h>
  9. #include <vector>
  10. #include <map>
  11. #include <highgui.h>
  12. using namespace std;
  13. using namespace cv;
  14. void Segment(IplImage* I)
  15. {
  16. ////////////////////parameters initialization//////////////////////
  17. int mode = CV_RETR_LIST;
  18. int level = 2, block_size = 1000;
  19. CvMemStorage* storage = cvCreateMemStorage(block_size);
  20. CvSeq* components;
  21. double threshold1 = 100,threshold2 = 50;
  22. IplImage *src = cvCreateImage(cvGetSize(I),IPL_DEPTH_8U,1);
  23. cvCvtColor(I,src,CV_RGB2GRAY);
  24. IplImage* dst = cvCreateImage(cvGetSize(I),IPL_DEPTH_8U,1);
  25. src->width = dst->width = (I->width & -(1 << level));
  26. src->height = dst->height = (I->height & -(1 << level));
  27. //////////////////////Segment/////////////////////////
  28. cvPyrSegmentation(src,dst,storage,&components,level,threshold1,threshold2);
  29. int ncomp = components->total;
  30. cout<<"segemented into "<<ncomp<<" components."<<endl;
  31. //////////////////////////Get Components/////////////////////////////
  32. map<int, int>mapping;//map color value to component id (classify)
  33. for (int i = 0; i<ncomp; i++)//foreach connection component
  34. {
  35. CvConnectedComp* cc = (CvConnectedComp*) cvGetSeqElem(components,i);
  36. cvDrawRect(dst,cvPoint(cc->rect.x,cc->rect.y),cvPoint(cc->rect.x+cc->rect.width,cc->rect.y+cc->rect.height),cvScalar(255,255,0));
  37. mapping.insert(pair<int,int>(cc->value.val[0],i));
  38. }
  39. cvShowImage("segementation",dst);
  40. waitKey();
  41. cvReleaseMemStorage(&storage);
  42. }

彩色图分割版本:

    1. void Segment(IplImage* I)
    2. {
    3. ////////////////////parameters initialization//////////////////////
    4. int mode = CV_RETR_LIST;
    5. int level = 2, block_size = 1000;
    6. CvMemStorage* storage = cvCreateMemStorage(block_size);
    7. CvSeq* components;
    8. double threshold1 = 10,threshold2 = 50;
    9. IplImage *src = cvCreateImage(cvGetSize(I),IPL_DEPTH_8U,3);
    10. src = cvCloneImage(I);
    11. IplImage* dst = cvCreateImage(cvGetSize(I),IPL_DEPTH_8U,3);
    12. src->width = dst->width = (I->width & -(1 << level));
    13. src->height = dst->height = (I->height & -(1 << level));
    14. //////////////////////Segment/////////////////////////
    15. cvPyrSegmentation(src,dst,storage,&components,level,threshold1,threshold2);
    16. int ncomp = components->total;
    17. cout<<"segemented into "<<ncomp<<" components."<<endl;
    18. //////////////////////////Get Components/////////////////////////////
    19. map<int, int>mapping;//map color value to component id (classify)
    20. for (int i = 0; i<ncomp; i++)//foreach connection component
    21. {
    22. CvConnectedComp* cc = (CvConnectedComp*) cvGetSeqElem(components,i);
    23. cvDrawRect(dst,cvPoint(cc->rect.x,cc->rect.y),cvPoint(cc->rect.x+cc->rect.width,cc->rect.y+cc->rect.height),cvScalar(255,255,0));
    24. mapping.insert(pair<int,int>(cc->value.val[0],i));
    25. }
    26. cvShowImage("segementation",dst);
    27. waitKey();
    28. cvReleaseMemStorage(&storage);
    29. }
    30. // from: http://blog.csdn.net/abcjennifer/article/details/18215953

opencv 金字塔图像分割的更多相关文章

  1. OpenCV 之 图像分割 (一)

    1  基于阈值 1.1  基本原理 灰度阈值化,是最简单也是速度最快的一种图像分割方法,广泛应用在硬件图像处理领域 (例如,基于 FPGA 的实时图像处理). 假设输入图像为 f,输出图像为 g,则经 ...

  2. OpenCV meanshift 图像分割代码

    参考:这个帖子的主要代码有错误,根据回帖改了一些 http://www.cnblogs.com/tornadomeet/archive/2012/06/06/2538695.html // means ...

  3. opencv::分水岭图像分割

    分水岭分割方法原理 (3种) - 基于浸泡理论的分水岭分割方法 (距离) - 基于连通图的方法 - 基于距离变换的方法 图像形态学操作: - 腐蚀与膨胀 - 开闭操作 分水岭算法运用 - 分割粘连对象 ...

  4. opencv::KMeans图像分割

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  5. opencv kmeans 图像分割

    利用kmeans算法,将彩色图像的像素点作为样本,rgb值作为样本的属性, 对图像所有的像素点进行分类,从而实现对图像中目标的分割. c++代码(openCV 2.4.11) Scalar color ...

  6. OpenCV 1 图像分割--分水岭算法代码

    // watershed_test20140801.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // // ch9_watershed ...

  7. opencv 彩色图像分割(inrange)

    灰度图像大多通过算子寻找边缘和区域生长融合来分割图像. 彩色图像增加了色彩信息,可以通过不同的色彩值来分割图像,常用彩色空间HSV/HSI, RGB, LAB等都可以用于分割! 笔者主要介绍inran ...

  8. OpenCV 金字塔图像缩放

    // image_pyramid.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <string> #incl ...

  9. 图像金字塔及其在 OpenCV 中的应用范例(下)

    前言 本文将主要讲解如何使用 OpenCV 实现图像分割,这也是图像金字塔在 OpenCV 中的一个重要应用. 关于图像分割 在计算机视觉领域,图像分割(Segmentation)指的是将数字图像细分 ...

随机推荐

  1. 如何维护SSH安全

    遇到两次,一次是公司服务器搭建好后,有人尝试ssh暴力破解,auth.log不停出现错误提示 还有买的米国vps,很荣幸地遭到来自波兰的ssh破解尝试 不得不重视ssh的安全 方法: 修改sshd_c ...

  2. 原生javascript开发仿微信打飞机小游戏

    今天闲来无事,于是就打算教一个初学javascript的女童鞋写点东西,因此为了兼顾趣味性与简易程度,果断想到了微信的打飞机小游戏.. 本来想用html5做的,但是毕竟人家才初学,连jquery都还不 ...

  3. sql语句not in判断条件注意事项

    sql语句not in判断条件注意事项 问题描述:mysql数据库,存在两个表org表和kdorg表,用于存储组织信息.现在我需要从org表找出组织,条件为该组织不在kdorg表里. sql语句:se ...

  4. 运用.NIT将数据存入数据库、读取数据库(运用封装)陈老师作业

    我基础不好,根据所学的知识,书本的例题修改的,也不知道我理解的是否符合老师要求 运用C#将数据存入数据库.并且可以读取数据库里的数据,此项目我运用了封装.我运用了一个窗体将数据存读数据. 我首先创建了 ...

  5. 读书笔记:<我是一只IT小小鸟>

    <我是一只IT小小鸟>第一次听到这本书的时候,我便有了深深的好奇,虽然我是一名学习软件工程的大学生,但是还是第一次听到“IT”这个名词,既陌生又好奇.听到老师提起了这本书的意义以及看法,我 ...

  6. Fiddler2汉化版使用说明

    fiddler是一款免费且功能强大的数据包抓取软件,它能够快速的抓取HTTP会话以及支持监视.还可设置断点等诸多实用功能,非常适合计算机工作者们分析数据使用.本文就为大家详细介绍一下fiddler的功 ...

  7. 用c语言产生随机数的方法

    用c语言产生随机数的方法 在C语言中,rand()函数可以用来产生随机数,但是这不是真正意义上的随机数,是一个伪随机数,是根据一个数,我们可以称它为种子,为基准以某个递推公式推算出来的一系数,当这系列 ...

  8. 上海9000辆出租车上铺设免费微信连WiFi

    据了解,去年7月,2000辆贴有Wi-Fi标识的上海大众出租车已经正式上路.近期,为了加大方便市民的力度,上海云联将上海大众等9000辆出租车上铺设免费微信连WiFi,为上海乘客提供简单便捷的微信连W ...

  9. logback日志文件需要注意点

    1.支持的jar包 logback-access-1.1.1.jarlogback-classic-1.1.1.jarlogback-core-1.1.1.jar 2.logback.xml文件,we ...

  10. javascript中的JSON序列化与反序列化

    简单粗暴上代码: function create() { this.name = "jack"; this.sex = "man"; } create.prot ...