#if !defined OFINDER
#define OFINDER #include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp> class ContentFinder { private: float hranges[2];
const float* ranges[3];
int channels[3]; float threshold;
cv::MatND histogram;
cv::SparseMat shistogram;
bool isSparse; public: ContentFinder() : threshold(0.1f), isSparse(false) { ranges[0]= hranges; // all channels have the same range
ranges[1]= hranges;
ranges[2]= hranges;
} // Sets the threshold on histogram values [0,1]
void setThreshold(float t) { threshold= t;
} // Gets the threshold
float getThreshold() { return threshold;
} // Sets the reference histogram
void setHistogram(const cv::MatND& h) { isSparse= false;
histogram= h;
cv::normalize(histogram,histogram,1.0);
} // Sets the reference histogram
void setHistogram(const cv::SparseMat& h) { isSparse= true;
shistogram= h;
cv::normalize(shistogram,shistogram,1.0,cv::NORM_L2);
} cv::Mat find(const cv::Mat& image) { cv::Mat result; hranges[0]= 0.0; // range [0,255]
hranges[1]= 255.0;
channels[0]= 0; // the three channels
channels[1]= 1;
channels[2]= 2; if (isSparse) { // call the right function based on histogram type cv::calcBackProject(&image,
1, // one image
channels, // vector specifying what histogram dimensions belong to what image channels
shistogram, // the histogram we are using
result, // the resulting back projection image
ranges, // the range of values, for each dimension
255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
); } else { cv::calcBackProject(&image,
1, // one image
channels, // vector specifying what histogram dimensions belong to what image channels
histogram, // the histogram we are using
result, // the resulting back projection image
ranges, // the range of values, for each dimension
255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
);
} // Threshold back projection to obtain a binary image
if (threshold>0.0)
cv::threshold(result, result, 255*threshold, 255, cv::THRESH_BINARY); return result;
} cv::Mat find(const cv::Mat& image, float minValue, float maxValue, int *channels, int dim) { cv::Mat result; hranges[0]= minValue;
hranges[1]= maxValue; for (int i=0; i<dim; i++)
this->channels[i]= channels[i]; if (isSparse) { // call the right function based on histogram type cv::calcBackProject(&image,
1, // we only use one image at a time
channels, // vector specifying what histogram dimensions belong to what image channels
shistogram, // the histogram we are using
result, // the resulting back projection image
ranges, // the range of values, for each dimension
255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
); } else { cv::calcBackProject(&image,
1, // we only use one image at a time
channels, // vector specifying what histogram dimensions belong to what image channels
histogram, // the histogram we are using
result, // the resulting back projection image
ranges, // the range of values, for each dimension
255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
);
} // Threshold back projection to obtain a binary image
if (threshold>0.0)
cv::threshold(result, result, 255*threshold, 255, cv::THRESH_BINARY); return result;
} }; #endif #if !defined COLHISTOGRAM
#define COLHISTOGRAM #include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
class ColorHistogram { private: int histSize[3];
float hranges[2];
const float* ranges[3];
int channels[3]; public: ColorHistogram() { // Prepare arguments for a color histogram
histSize[0]= histSize[1]= histSize[2]= 256;
hranges[0]= 0.0; // BRG range
hranges[1]= 255.0;
ranges[0]= hranges; // all channels have the same range
ranges[1]= hranges;
ranges[2]= hranges;
channels[0]= 0; // the three channels
channels[1]= 1;
channels[2]= 2;
} // Computes the histogram.
cv::MatND getHistogram(const cv::Mat &image) { cv::MatND hist; // BGR color histogram
hranges[0]= 0.0; // BRG range
hranges[1]= 255.0;
channels[0]= 0; // the three channels
channels[1]= 1;
channels[2]= 2; // Compute histogram
cv::calcHist(&image,
1, // histogram of 1 image only
channels, // the channel used
cv::Mat(), // no mask is used
hist, // the resulting histogram
3, // it is a 3D histogram
histSize, // number of bins
ranges // pixel value range
); return hist;
} // Computes the 1D Hue histogram with a mask.
// BGR source image is converted to HSV
cv::MatND getHueHistogram(const cv::Mat &image) { cv::MatND hist; // Convert to Lab color space
cv::Mat hue;
cv::cvtColor(image, hue, CV_BGR2HSV); // Prepare arguments for a 1D hue histogram
hranges[0]= 0.0;
hranges[1]= 180.0;
channels[0]= 0; // the hue channel // Compute histogram
cv::calcHist(&hue,
1, // histogram of 1 image only
channels, // the channel used
cv::Mat(), // no mask is used
hist, // the resulting histogram
1, // it is a 1D histogram
histSize, // number of bins
ranges // pixel value range
); return hist;
} cv::MatND getHueHistogram(const cv::Mat &image,int minSaturation)
{
cv::MatND hist;
cv::Mat hsv;
cv::cvtColor(image,hsv,CV_BGR2HSV);
cv::Mat mask;
if(minSaturation>0)
{
std::vector<cv::Mat>v;
cv::split(hsv,v);
cv::threshold(v[1],mask,minSaturation,255,cv::THRESH_BINARY);
}
hranges[0]=0.0;
hranges[1]=180.0;
channels[0]=0;
calcHist(&hsv,1,channels,mask,hist,1,histSize,ranges);
return hist;
} }; #endif #include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/video/video.hpp>
#include<iostream>
#include"colorhistogram.h"
#include"ContentFinder.h" using namespace std;
using namespace cv; int main()
{
Mat image=imread("d:/test/opencv/baboon1.jpg");
Mat imageROI=image(Rect(110,260,35,40));
int minSat=65;
ColorHistogram hc;
MatND colorhist=hc.getHueHistogram(imageROI,minSat); namedWindow("image 1");
imshow("image 1",image); ContentFinder finder;
finder.setHistogram(colorhist);
Mat hsv;
image=imread("d:/test/opencv/baboon3.jpg");
namedWindow("image 2");
imshow("image 2",image);
cvtColor(image,hsv,CV_BGR2HSV);
vector<Mat>v;
split(hsv,v);
threshold(v[1],v[1],minSat,255,THRESH_BINARY);
cv::namedWindow("Saturation");
cv::imshow("Saturation",v[1]);
int channel[1]={0};
Mat result=finder.find(hsv,0.0f,180.0f,channel,1); cv::namedWindow("Result Hue");
cv::imshow("Result Hue",result); cv::bitwise_and(result,v[1],result);
cv::namedWindow("Result Hue and");
cv::imshow("Result Hue and",result); finder.setThreshold(-1.0f);//
result= finder.find(hsv,0.0f,180.0f,channel,1);
cv::bitwise_and(result,v[1],result);
cv::namedWindow("Result Hue and raw");
cv::imshow("Result Hue and raw",result); cv::Rect rect(110,260,35,40);
cv::rectangle(image, rect, cv::Scalar(0,0,255)); cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER,10,0.01);
cout << "meanshift= " << cv::meanShift(result,rect,criteria) << endl;// cv::rectangle(image, rect, cv::Scalar(0,255,0));// // Display image
cv::namedWindow("Image 2 result");
cv::imshow("Image 2 result",image); cv::waitKey();
return 0; }

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. 在CentOS6.5下配置安装LVS

    先上一个图,在内网测试机上安装了一个虚拟机,已经安装好了CentOS 乖乖连wget都没有安装,先安装wget吧 sudo yum install wget

  2. hdu1290

    由二维的切割问题可知,平面切割与线之间的交点有关,即交点决定射线和线段的条数,从而决定新增的区域数. 当有n-1个平面时,切割的空间数为f(n-1).要有最多的空间数.则第n个平面需与前n-1个平面相 ...

  3. [jzoj 6087] [GDOI2019模拟2019.3.26] 获取名额 解题报告 (泰勒展开+RMQ+精度)

    题目链接: https://jzoj.net/senior/#main/show/6087 题目: 题解: 只需要统计$\prod_{i=l}^r (1-\frac{a_i}{x})$ =$exp(\ ...

  4. Pcap 数据报解析

    最近看了一下网络的书,信息系统也有实验任务,所以就学习了一下pcap包的解析. 主要是对内部以太网帧头,ip头部,tcp头部或者udp头部的解析.我因为用访问google.cn作为的样例,没有udp包 ...

  5. How To Do @Async in Spring--转

    原文地址:http://www.baeldung.com/spring-async 1. Overview In this article, we’ll explore the asynchronou ...

  6. java 获取线程id

    如何获取正在运行的线程的ID? 解决方法 下面的示例演示如何使用getThreadId() 方法一个正在运行线程的ID. public class Main extends Object implem ...

  7. windows共享如何重新登录,或用另外的用户登录

    使用net use * /del 可以结束已有的所有连接,或net use \\192.168.1.10  /del可以结束指定连接.比如想重新登录共享的话,就用这个命令结束原来的连接,就可以重新登录 ...

  8. hiho151周*

    题目链接 题目大意:依次在给定的三维坐标上垒方块,对于一个新的坐标需满足两个条件 1:六个方向有相邻的方块或者z==1[题目说明了初始状态是:所有z==0的位置都有方块] 2:该位置存在一条到无穷远处 ...

  9. EFcore笔记之创建模型

    排除属性:NotMapped NotMapped:排除属性在CodeFirst的时候在数据库里不创建该属性   public class Blog { public int BlogId { get; ...

  10. 用AI识别内部人威胁面临的道德规范

    用AI识别内部人威胁面临的道德规范 还记得汤姆·克鲁斯的<少数派报告>吗?人工智能可识别昭示未来风险的员工行为.该如何有效且有道德地使用这一数据呢? 为保护公司网络不受恶意软件.数据渗漏和 ...