opencv 金字塔图像分割
我所知的opencv中分割函数:watershed(只是看看效果,不能返回每类pixel类属),cvsegmentImage,cvPyrSegmentation(返回pixel类属)
金字塔分割原理篇在这里,本文只提供代码。
Segment函数:
- #include<cv.h>
- #include <cvaux.h>
- #include <opencv\cxcore.hpp>
- #include <opencv.hpp>
- #include <nonfree.hpp>
- #include <core/core.hpp>
- #include <imgproc/imgproc.hpp>
- #include <imgproc/imgproc_c.h>
- #include <vector>
- #include <map>
- #include <highgui.h>
- using namespace std;
- using namespace cv;
- void Segment(IplImage* I)
- {
- ////////////////////parameters initialization//////////////////////
- int mode = CV_RETR_LIST;
- int level = 2, block_size = 1000;
- CvMemStorage* storage = cvCreateMemStorage(block_size);
- CvSeq* components;
- double threshold1 = 100,threshold2 = 50;
- IplImage *src = cvCreateImage(cvGetSize(I),IPL_DEPTH_8U,1);
- cvCvtColor(I,src,CV_RGB2GRAY);
- IplImage* dst = cvCreateImage(cvGetSize(I),IPL_DEPTH_8U,1);
- src->width = dst->width = (I->width & -(1 << level));
- src->height = dst->height = (I->height & -(1 << level));
- //////////////////////Segment/////////////////////////
- cvPyrSegmentation(src,dst,storage,&components,level,threshold1,threshold2);
- int ncomp = components->total;
- cout<<"segemented into "<<ncomp<<" components."<<endl;
- //////////////////////////Get Components/////////////////////////////
- map<int, int>mapping;//map color value to component id (classify)
- for (int i = 0; i<ncomp; i++)//foreach connection component
- {
- CvConnectedComp* cc = (CvConnectedComp*) cvGetSeqElem(components,i);
- 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));
- mapping.insert(pair<int,int>(cc->value.val[0],i));
- }
- cvShowImage("segementation",dst);
- waitKey();
- cvReleaseMemStorage(&storage);
- }
彩色图分割版本:
- void Segment(IplImage* I)
- {
- ////////////////////parameters initialization//////////////////////
- int mode = CV_RETR_LIST;
- int level = 2, block_size = 1000;
- CvMemStorage* storage = cvCreateMemStorage(block_size);
- CvSeq* components;
- double threshold1 = 10,threshold2 = 50;
- IplImage *src = cvCreateImage(cvGetSize(I),IPL_DEPTH_8U,3);
- src = cvCloneImage(I);
- IplImage* dst = cvCreateImage(cvGetSize(I),IPL_DEPTH_8U,3);
- src->width = dst->width = (I->width & -(1 << level));
- src->height = dst->height = (I->height & -(1 << level));
- //////////////////////Segment/////////////////////////
- cvPyrSegmentation(src,dst,storage,&components,level,threshold1,threshold2);
- int ncomp = components->total;
- cout<<"segemented into "<<ncomp<<" components."<<endl;
- //////////////////////////Get Components/////////////////////////////
- map<int, int>mapping;//map color value to component id (classify)
- for (int i = 0; i<ncomp; i++)//foreach connection component
- {
- CvConnectedComp* cc = (CvConnectedComp*) cvGetSeqElem(components,i);
- 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));
- mapping.insert(pair<int,int>(cc->value.val[0],i));
- }
- cvShowImage("segementation",dst);
- waitKey();
- cvReleaseMemStorage(&storage);
- }
- // from: http://blog.csdn.net/abcjennifer/article/details/18215953
opencv 金字塔图像分割的更多相关文章
- OpenCV 之 图像分割 (一)
1 基于阈值 1.1 基本原理 灰度阈值化,是最简单也是速度最快的一种图像分割方法,广泛应用在硬件图像处理领域 (例如,基于 FPGA 的实时图像处理). 假设输入图像为 f,输出图像为 g,则经 ...
- OpenCV meanshift 图像分割代码
参考:这个帖子的主要代码有错误,根据回帖改了一些 http://www.cnblogs.com/tornadomeet/archive/2012/06/06/2538695.html // means ...
- opencv::分水岭图像分割
分水岭分割方法原理 (3种) - 基于浸泡理论的分水岭分割方法 (距离) - 基于连通图的方法 - 基于距离变换的方法 图像形态学操作: - 腐蚀与膨胀 - 开闭操作 分水岭算法运用 - 分割粘连对象 ...
- opencv::KMeans图像分割
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- opencv kmeans 图像分割
利用kmeans算法,将彩色图像的像素点作为样本,rgb值作为样本的属性, 对图像所有的像素点进行分类,从而实现对图像中目标的分割. c++代码(openCV 2.4.11) Scalar color ...
- OpenCV 1 图像分割--分水岭算法代码
// watershed_test20140801.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // // ch9_watershed ...
- opencv 彩色图像分割(inrange)
灰度图像大多通过算子寻找边缘和区域生长融合来分割图像. 彩色图像增加了色彩信息,可以通过不同的色彩值来分割图像,常用彩色空间HSV/HSI, RGB, LAB等都可以用于分割! 笔者主要介绍inran ...
- OpenCV 金字塔图像缩放
// image_pyramid.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <string> #incl ...
- 图像金字塔及其在 OpenCV 中的应用范例(下)
前言 本文将主要讲解如何使用 OpenCV 实现图像分割,这也是图像金字塔在 OpenCV 中的一个重要应用. 关于图像分割 在计算机视觉领域,图像分割(Segmentation)指的是将数字图像细分 ...
随机推荐
- preseed.cfg分区设定案例
很久之前做ubuntu的PXE配置ubuntu的preseed费了很大的力气,总结的不多,现在温习一下. 就我所接触的,有分区普通磁盘,LVM,和raid三种方式.其中前两中方式比较多,raid方式是 ...
- nscd
作用: Nscd is a daemon that provides a cache for the most common name service requests 可以缓存passwd,grou ...
- Python中的List,Tuple,Dic,Set
Python中的List,Tuple,Dic,Set List定义 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 ...
- 断点续传 (HTTP) 归纳
由于最近项目中要上传较大的文件,基于公司原有的底层框架的局限性,对于大文件的传输都束手无策,基于文件传输的安全性,考虑用断点续传(HTTP)以及FTP上传两种方式实现下面归纳下HTTP续传和FTP上传 ...
- SQL Server性能优化(3)使用SQL Server Profiler查询性能瓶颈
关于SQL Server Profiler的使用,网上已经有很多教程,比如这一篇文章:SQL Server Profiler:使用方法和指标说明.微软官方文档:https://msdn.microso ...
- Mybatis代码调试问题总结(一)
问题: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 原因排查: 1.检查map ...
- 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 ...
- java 验证身份证号
- matrix_last_acm_5
password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97960#problem/A 题意:给国王生日可能区间[a, ...
- Load hlsl
这个函数和sample差不多 不过没有samplestate和filter http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb5096 ...