1. #if !defined OFINDER
  2. #define OFINDER
  3.  
  4. #include <opencv2\core\core.hpp>
  5. #include <opencv2\imgproc\imgproc.hpp>
  6.  
  7. class ContentFinder {
  8.  
  9. private:
  10.  
  11. float hranges[2];
  12. const float* ranges[3];
  13. int channels[3];
  14.  
  15. float threshold;
  16. cv::MatND histogram;
  17. cv::SparseMat shistogram;
  18. bool isSparse;
  19.  
  20. public:
  21.  
  22. ContentFinder() : threshold(0.1f), isSparse(false) {
  23.  
  24. ranges[0]= hranges; // all channels have the same range
  25. ranges[1]= hranges;
  26. ranges[2]= hranges;
  27. }
  28.  
  29. // Sets the threshold on histogram values [0,1]
  30. void setThreshold(float t) {
  31.  
  32. threshold= t;
  33. }
  34.  
  35. // Gets the threshold
  36. float getThreshold() {
  37.  
  38. return threshold;
  39. }
  40.  
  41. // Sets the reference histogram
  42. void setHistogram(const cv::MatND& h) {
  43.  
  44. isSparse= false;
  45. histogram= h;
  46. cv::normalize(histogram,histogram,1.0);
  47. }
  48.  
  49. // Sets the reference histogram
  50. void setHistogram(const cv::SparseMat& h) {
  51.  
  52. isSparse= true;
  53. shistogram= h;
  54. cv::normalize(shistogram,shistogram,1.0,cv::NORM_L2);
  55. }
  56.  
  57. cv::Mat find(const cv::Mat& image) {
  58.  
  59. cv::Mat result;
  60.  
  61. hranges[0]= 0.0; // range [0,255]
  62. hranges[1]= 255.0;
  63. channels[0]= 0; // the three channels
  64. channels[1]= 1;
  65. channels[2]= 2;
  66.  
  67. if (isSparse) { // call the right function based on histogram type
  68.  
  69. cv::calcBackProject(&image,
  70. 1, // one image
  71. channels, // vector specifying what histogram dimensions belong to what image channels
  72. shistogram, // the histogram we are using
  73. result, // the resulting back projection image
  74. ranges, // the range of values, for each dimension
  75. 255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
  76. );
  77.  
  78. } else {
  79.  
  80. cv::calcBackProject(&image,
  81. 1, // one image
  82. channels, // vector specifying what histogram dimensions belong to what image channels
  83. histogram, // the histogram we are using
  84. result, // the resulting back projection image
  85. ranges, // the range of values, for each dimension
  86. 255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
  87. );
  88. }
  89.  
  90. // Threshold back projection to obtain a binary image
  91. if (threshold>0.0)
  92. cv::threshold(result, result, 255*threshold, 255, cv::THRESH_BINARY);
  93.  
  94. return result;
  95. }
  96.  
  97. cv::Mat find(const cv::Mat& image, float minValue, float maxValue, int *channels, int dim) {
  98.  
  99. cv::Mat result;
  100.  
  101. hranges[0]= minValue;
  102. hranges[1]= maxValue;
  103.  
  104. for (int i=0; i<dim; i++)
  105. this->channels[i]= channels[i];
  106.  
  107. if (isSparse) { // call the right function based on histogram type
  108.  
  109. cv::calcBackProject(&image,
  110. 1, // we only use one image at a time
  111. channels, // vector specifying what histogram dimensions belong to what image channels
  112. shistogram, // the histogram we are using
  113. result, // the resulting back projection image
  114. ranges, // the range of values, for each dimension
  115. 255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
  116. );
  117.  
  118. } else {
  119.  
  120. cv::calcBackProject(&image,
  121. 1, // we only use one image at a time
  122. channels, // vector specifying what histogram dimensions belong to what image channels
  123. histogram, // the histogram we are using
  124. result, // the resulting back projection image
  125. ranges, // the range of values, for each dimension
  126. 255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
  127. );
  128. }
  129.  
  130. // Threshold back projection to obtain a binary image
  131. if (threshold>0.0)
  132. cv::threshold(result, result, 255*threshold, 255, cv::THRESH_BINARY);
  133.  
  134. return result;
  135. }
  136.  
  137. };
  138.  
  139. #endif
  140.  
  141. #if !defined COLHISTOGRAM
  142. #define COLHISTOGRAM
  143.  
  144. #include <opencv2\core\core.hpp>
  145. #include <opencv2\imgproc\imgproc.hpp>
  146. #include<opencv2/highgui/highgui.hpp>
  147. class ColorHistogram {
  148.  
  149. private:
  150.  
  151. int histSize[3];
  152. float hranges[2];
  153. const float* ranges[3];
  154. int channels[3];
  155.  
  156. public:
  157.  
  158. ColorHistogram() {
  159.  
  160. // Prepare arguments for a color histogram
  161. histSize[0]= histSize[1]= histSize[2]= 256;
  162. hranges[0]= 0.0; // BRG range
  163. hranges[1]= 255.0;
  164. ranges[0]= hranges; // all channels have the same range
  165. ranges[1]= hranges;
  166. ranges[2]= hranges;
  167. channels[0]= 0; // the three channels
  168. channels[1]= 1;
  169. channels[2]= 2;
  170. }
  171.  
  172. // Computes the histogram.
  173. cv::MatND getHistogram(const cv::Mat &image) {
  174.  
  175. cv::MatND hist;
  176.  
  177. // BGR color histogram
  178. hranges[0]= 0.0; // BRG range
  179. hranges[1]= 255.0;
  180. channels[0]= 0; // the three channels
  181. channels[1]= 1;
  182. channels[2]= 2;
  183.  
  184. // Compute histogram
  185. cv::calcHist(&image,
  186. 1, // histogram of 1 image only
  187. channels, // the channel used
  188. cv::Mat(), // no mask is used
  189. hist, // the resulting histogram
  190. 3, // it is a 3D histogram
  191. histSize, // number of bins
  192. ranges // pixel value range
  193. );
  194.  
  195. return hist;
  196. }
  197.  
  198. // Computes the 1D Hue histogram with a mask.
  199. // BGR source image is converted to HSV
  200. cv::MatND getHueHistogram(const cv::Mat &image) {
  201.  
  202. cv::MatND hist;
  203.  
  204. // Convert to Lab color space
  205. cv::Mat hue;
  206. cv::cvtColor(image, hue, CV_BGR2HSV);
  207.  
  208. // Prepare arguments for a 1D hue histogram
  209. hranges[0]= 0.0;
  210. hranges[1]= 180.0;
  211. channels[0]= 0; // the hue channel
  212.  
  213. // Compute histogram
  214. cv::calcHist(&hue,
  215. 1, // histogram of 1 image only
  216. channels, // the channel used
  217. cv::Mat(), // no mask is used
  218. hist, // the resulting histogram
  219. 1, // it is a 1D histogram
  220. histSize, // number of bins
  221. ranges // pixel value range
  222. );
  223.  
  224. return hist;
  225. }
  226.  
  227. cv::MatND getHueHistogram(const cv::Mat &image,int minSaturation)
  228. {
  229. cv::MatND hist;
  230. cv::Mat hsv;
  231. cv::cvtColor(image,hsv,CV_BGR2HSV);
  232. cv::Mat mask;
  233. if(minSaturation>0)
  234. {
  235. std::vector<cv::Mat>v;
  236. cv::split(hsv,v);
  237. cv::threshold(v[1],mask,minSaturation,255,cv::THRESH_BINARY);
  238. }
  239. hranges[0]=0.0;
  240. hranges[1]=180.0;
  241. channels[0]=0;
  242. calcHist(&hsv,1,channels,mask,hist,1,histSize,ranges);
  243. return hist;
  244. }
  245.  
  246. };
  247.  
  248. #endif
  249.  
  250. #include<opencv2/core/core.hpp>
  251. #include<opencv2/highgui/highgui.hpp>
  252. #include<opencv2/imgproc/imgproc.hpp>
  253. #include<opencv2/video/video.hpp>
  254. #include<iostream>
  255. #include"colorhistogram.h"
  256. #include"ContentFinder.h"
  257.  
  258. using namespace std;
  259. using namespace cv;
  260.  
  261. int main()
  262. {
  263. Mat image=imread("d:/test/opencv/baboon1.jpg");
  264. Mat imageROI=image(Rect(110,260,35,40));
  265. int minSat=65;
  266. ColorHistogram hc;
  267. MatND colorhist=hc.getHueHistogram(imageROI,minSat);
  268.  
  269. namedWindow("image 1");
  270. imshow("image 1",image);
  271.  
  272. ContentFinder finder;
  273. finder.setHistogram(colorhist);
  274. Mat hsv;
  275. image=imread("d:/test/opencv/baboon3.jpg");
  276. namedWindow("image 2");
  277. imshow("image 2",image);
  278. cvtColor(image,hsv,CV_BGR2HSV);
  279. vector<Mat>v;
  280. split(hsv,v);
  281. threshold(v[1],v[1],minSat,255,THRESH_BINARY);
  282. cv::namedWindow("Saturation");
  283. cv::imshow("Saturation",v[1]);
  284. int channel[1]={0};
  285. Mat result=finder.find(hsv,0.0f,180.0f,channel,1);
  286.  
  287. cv::namedWindow("Result Hue");
  288. cv::imshow("Result Hue",result);
  289.  
  290. cv::bitwise_and(result,v[1],result);
  291. cv::namedWindow("Result Hue and");
  292. cv::imshow("Result Hue and",result);
  293.  
  294. finder.setThreshold(-1.0f);//
  295. result= finder.find(hsv,0.0f,180.0f,channel,1);
  296. cv::bitwise_and(result,v[1],result);
  297. cv::namedWindow("Result Hue and raw");
  298. cv::imshow("Result Hue and raw",result);
  299.  
  300. cv::Rect rect(110,260,35,40);
  301. cv::rectangle(image, rect, cv::Scalar(0,0,255));
  302.  
  303. cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER,10,0.01);
  304. cout << "meanshift= " << cv::meanShift(result,rect,criteria) << endl;//
  305.  
  306. cv::rectangle(image, rect, cv::Scalar(0,255,0));//
  307.  
  308. // Display image
  309. cv::namedWindow("Image 2 result");
  310. cv::imshow("Image 2 result",image);
  311.  
  312. cv::waitKey();
  313. return 0;
  314.  
  315. }

opecv2 MeanShift 使用均值漂移算法查找物体的更多相关文章

  1. opencv2对读书笔记——使用均值漂移算法查找物体

    一些小概念 1.反投影直方图的结果是一个概率映射,体现了已知图像内容出如今图像中特定位置的概率. 2.概率映射能够找到最初的位置,从最初的位置開始而且迭代移动,便能够找到精确的位置,这就是均值漂移算法 ...

  2. Meanshift均值漂移算法

      通俗理解Meanshift均值漂移算法  Meanshift车手?? 漂移?? 秋名山???   不,不,他是一组算法,  今天我就带大家来了解一下机器学习中的Meanshift均值漂移. Mea ...

  3. 使用Opencv中均值漂移meanShift跟踪移动目标

    Mean Shift均值漂移算法是无参密度估计理论的一种,无参密度估计不需要事先知道对象的任何先验知识,完全依靠训练数据进行估计,并且可以用于任意形状的密度估计,在某一连续点处的密度函数值可由该点邻域 ...

  4. 基于MeanShift的目标跟踪算法及实现

    这次将介绍基于MeanShift的目标跟踪算法,首先谈谈简介,然后给出算法实现流程,最后实现了一个单目标跟踪的MeanShift算法[matlab/c两个版本] csdn贴公式比较烦,原谅我直接截图了 ...

  5. Opencv均值漂移pyrMeanShiftFiltering彩色图像分割流程剖析

    meanShfit均值漂移算法是一种通用的聚类算法,它的基本原理是:对于给定的一定数量样本,任选其中一个样本,以该样本为中心点划定一个圆形区域,求取该圆形区域内样本的质心,即密度最大处的点,再以该点为 ...

  6. 机器学习实战---K均值聚类算法

    一:一般K均值聚类算法实现 (一)导入数据 import numpy as np import matplotlib.pyplot as plt def loadDataSet(filename): ...

  7. Kosaraju 算法查找强连通分支

    有向图 G = (V, E) 的一个强连通分支(SCC:Strongly Connected Components)是一个最大的顶点集合 C,C 是 V 的子集,对于 C 中的每一对顶点 u 和 v, ...

  8. 回朔法/KMP算法-查找字符串

    回朔法:在字符串查找的时候最容易想到的是暴力查找,也就是回朔法.其思路是将要寻找的串的每个字符取出,然后按顺序在源串中查找,如果找到则返回true,否则源串索引向后移动一位,再重复查找,直到找到返回t ...

  9. 数据结构与算法--KMP算法查找子字符串

    数据结构与算法--KMP算法查找子字符串 部分内容和图片来自这三篇文章: 这篇文章.这篇文章.还有这篇他们写得非常棒.结合他们的解释和自己的理解,完成了本文. 上一节介绍了暴力法查找子字符串,同时也发 ...

随机推荐

  1. 【BZOJ 1221】 [HNOI2001] 软件开发

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] /* 设一个超级源点S和超级汇点T S和2*i-1各连一条容量为ni的边. 花费为0 表示每天都会产生ni条要洗的毛巾 S和2*i各 ...

  2. Action访问ServletAPI的三种方式

    一.前言 Struts是一种基于MVC设计模式的web应用框架,主要担任C的角色,用于分离页面显示和业务逻辑处理,那其实在我们学习jsp的时候学过一个具有类似功能的东西——servlet.其实Stru ...

  3. ZOJ 3829 模拟贪心

    2014牡丹江现场赛水题 给出波兰式,推断其是否合法.假设不合法有两种操作: 1:任何位置加一个数字或者操作符 2:随意两个位置的元素对调 贪心模拟就可以 先推断数字数是否大于操作符数,若不大于 an ...

  4. gpg 的使用

    GPG入门教程 GpG使用指南 1. 安装 源码编译安装:源码下载地址 ./configure make make install 直接安装编译好的二进制文件 # Debian / Ubuntu 环境 ...

  5. 93.快速搭建Web环境 Angularjs + Express3 + Bootstrap3

    转自:https://www.cnblogs.com/wawahaha/p/3946023.html 前言 Angularjs越用越顺手,不仅代码量比jQuery少很多,而且实现思路特别清晰,构建大型 ...

  6. 18.boost 图的拓扑排序

    运行结果: 代码示例: #include <iostream> #include <vector> #include <deque> #include <bo ...

  7. POJ 3268 Dijkstra+priority_queue或SPFA

    思路:正向建边,一遍Dijkstra,反向建边,再一遍Dijkstra.ans加在一起输出最大值. (SPFA也行--) // by SiriusRen #include <queue> ...

  8. sas与mysql连接方法

    2012年8月11日 sas 9.1.3 版本 与mysql 连接 测试,可以与数据库连接1  通过odbc 直接连通 pass through connect to odbc create tabl ...

  9. vs2015汉化

    VS2015汉化 VS2015安装打开后默认是英文的,将它改成中文的VS 1.安装下载好的语言包进行安装 2.正在安装 3.安装完成后关闭 4.打开VS2015默认不是中文的,点击Tools--> ...

  10. Windows常见软件故障及解决方案

    HM NIS Edit: HM NIS Edit 新建程序向导无效,提示“Please specify the setup lang” 说明 NSIS 安装不对.解决方案有二种: 1. 重装 NSIS ...