我所知的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. preseed.cfg分区设定案例

    很久之前做ubuntu的PXE配置ubuntu的preseed费了很大的力气,总结的不多,现在温习一下. 就我所接触的,有分区普通磁盘,LVM,和raid三种方式.其中前两中方式比较多,raid方式是 ...

  2. nscd

    作用: Nscd is a daemon that provides a cache for the most common name service requests 可以缓存passwd,grou ...

  3. Python中的List,Tuple,Dic,Set

    Python中的List,Tuple,Dic,Set List定义 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 ...

  4. 断点续传 (HTTP) 归纳

    由于最近项目中要上传较大的文件,基于公司原有的底层框架的局限性,对于大文件的传输都束手无策,基于文件传输的安全性,考虑用断点续传(HTTP)以及FTP上传两种方式实现下面归纳下HTTP续传和FTP上传 ...

  5. SQL Server性能优化(3)使用SQL Server Profiler查询性能瓶颈

    关于SQL Server Profiler的使用,网上已经有很多教程,比如这一篇文章:SQL Server Profiler:使用方法和指标说明.微软官方文档:https://msdn.microso ...

  6. Mybatis代码调试问题总结(一)

    问题: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):   原因排查: 1.检查map ...

  7. ORA-12505, TNS:listener does not currently know of SID given in connect descriptor (二)

    异常及解决 在连接sqldeveloper出现的异常信息 在ORA-12505, TNS:listener does not currently know of SID given in connec ...

  8. java 验证身份证号

  9. matrix_last_acm_5

    password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97960#problem/A 题意:给国王生日可能区间[a, ...

  10. Load hlsl

    这个函数和sample差不多 不过没有samplestate和filter http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb5096 ...