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)指的是将数字图像细分 ...
随机推荐
- UVALive - 6529 找规律+dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/47664 Eleven Time Limit: 5000MS 问题描述 In this problem, we ...
- 04.Hibernate一对一关联
前言:本文主要介绍使用Hibernate映射一对一的关联关系的两种方式:使用外键映射.使用主键映射. 1.数据库表的一对一关联关系 本文根据客户信息表(tb_customer)和地址信 ...
- hdu 3549 Flow Problem 网络流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Network flow is a well-known difficult problem f ...
- 【BZOJ】【3166】【HEOI2013】Alo
可持久化Trie+set Orz zyf…… 搞区间中次大值不好搞,那么我们就反过来,找一个数,然后看它在哪些区间里是次大值…… (然而事实上我们并不用真的把这个区间具体是什么找见,只要知道它可以跟哪 ...
- NYOJ-206 矩形的个数 AC 分类: NYOJ 2013-12-29 22:19 265人阅读 评论(0) 收藏
这题目是小学奥数题目,方法可以百度到,但是,有个难点就是,数据类型大小不够,如果是1000x1000的矩阵,那么就会超过int的范围,所以,就引进了long long的数据类型 #include< ...
- 转载淘宝UED响应十日谈
响应式十日谈:楔子 响应式十日谈第一日:使用 rem 设置文字大小
- Rust: lifetime
Rust的lifetime算是它最重要的特性之一,也不大好理解,特别是官方文档的介绍有些太过简略,容易让人误解. 这篇文章: Rust Lifetimes 应该可以解答很多人疑惑,特别是有关lifet ...
- Linuxshell脚本之if条件判断
IF条件判断 .基本语法: if [ command ]; then 符合该条件执行的语句 fi .扩展语法: if [ command ];then 符合该条件执行的语句 elif [ comman ...
- 国内Jquery CDN
新浪CDN: <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js">< ...
- Delphi中有序型
有序类型包括:.integer(整型).character(字符型).boolean(布尔型).enumerated(枚举型).subrange(子界型)有序类型定义了一组被排序的值.每个相异值都有唯 ...