資源簡介
里面有Pixy源碼,同時有一個main文件,是將Pixy的方法移植到HSV空間了,效果還可以,可以作為參考~

代碼片段和文件信息
#include?
#include?
using?namespace?std;
using?namespace?cv;
const?char*?windows1?=?“rgb“;
const?char*?windows2?=?“dst“;
const?char*?windowsp?=?“parameter“;
Mat?rgb;?????????????//?原始圖像?????
Rect?rect;???????????//?選中的區域
bool?have?=?false;???//?是否進行了學習
float?m_ratio?=?0.8;?//?選中區域的hs通道大于百分之多少算好
struct?ColorSignature
{
ColorSignature()
{
m_uMin?=?m_uMax?=?m_uMean?=?m_vMin?=?m_vMax?=?m_vMean?=?m_type?=?0;
}
int?m_uMin;
int?m_uMax;
int?m_uMean;
int?m_vMin;
int?m_vMax;
int?m_vMean;
int?m_rgb;
int?m_type;
}sig;
void?calcRatios8(Mat&?uu?Mat&?vv?struct?ColorSignature?*sig?float?ratios[])
{
int?n?=?0?counts[4];
counts[0]?=?counts[1]?=?counts[2]?=?counts[3]?=?0;
for?(int?i?=?0;?i {
for?(int?j?=?0;?j {
uchar?m_u?=?uu.ptr(i)[j];
uchar?m_v?=?vv.ptr(i)[j];
if?(m_u>sig->m_uMin)
counts[0]++;
if?(m_um_uMax)
counts[1]++;
if?(m_v>sig->m_vMin)
counts[2]++;
if?(m_vm_vMax)
counts[3]++;
n++;
}
}
//?calc?ratios
ratios[0]?=?(float)counts[0]?/?n;
ratios[1]?=?(float)counts[1]?/?n;
ratios[2]?=?(float)counts[2]?/?n;
ratios[3]?=?(float)counts[3]?/?n;
//?calc?mean?(because?it‘s?cheap?to?do?it?here)
sig->m_uMean?=?(sig->m_uMin?+?sig->m_uMax)?/?2;
sig->m_vMean?=?(sig->m_vMin?+?sig->m_vMax)?/?2;
}
Mat?HSVGet(Mat&?rgb)
{
Mat?m_hsv;
//?先轉換為YUV格式
cvtColor(rgb?m_hsv?cv::COLOR_BGR2HSV);
//?通道分離
vector?splits;
split(m_hsv?splits);
////?對每個通道進行處理
int?scale?=?0;
float?ratios[4];
//?取得hs的上下限
for?(scale?=?1?<7?sig.m_uMin?=?sig.m_uMax?=?sig.m_vMin?=?sig.m_vMax?=?0;?scale?!=?0;?scale?>>=?1)
{
calcRatios8(splits[0]?splits[1]?&sig?ratios);
if?(ratios[0]>m_ratio)
sig.m_uMin?+=?scale;
else
sig.m_uMin?-=?scale;
if?(ratios[1]>m_ratio)
sig.m_uMax?-=?scale;
else
sig.m_uMax?+=?scale;
if?(ratios[2]>m_ratio)
sig.m_vMin?+=?scale;
else
sig.m_vMin?-=?scale;
if?(ratios[3]>m_ratio)
sig.m_vMax?-=?scale;
else
sig.m_vMax?+=?scale;
}
int?range?=?2.5;
sig.m_uMin?=?sig.m_uMean?+?(sig.m_uMin?-?sig.m_uMean)*range;
sig.m_uMax?=?sig.m_uMean?+?(sig.m_uMax?-?sig.m_uMean)*range;
range?=?3.5;
sig.m_vMin?=?sig.m_vMean?+?(sig.m_vMin?-?sig.m_vMean)*range;
sig.m_vMax?=?sig.m_vMean?+?(sig.m_vMax?-?sig.m_vMean)*range;
have?=?true;
return?Mat();
}
Mat?HSVUpdate(Mat&?rgb)
{
Mat?m_yuv;
Mat?m_ret;
//?先轉換為YUV格式
cvtColor(rgb?m_yuv?cv::COLOR_BGR2HSV);
//?通道分離
vector?splits;
split(m_yuv?splits);
Mat?imgh?imgs;
inRange(splits[0]?Scalar::all(sig.m_uMin)?Scalar::all(sig.m_uMax)?imgh);
inRange(splits[1]?Scalar::all(sig.m_vMin)?Scalar::all(sig.m_vMax)?imgs);
cv::bitwise_and(imgh?imgs?m_ret);
return?m_ret;
}
void?onMouse(int?event?int?x?int?y?int?flags?void?*ustc)
{
static?Point?pre_pt(-1?-1);
static?Point?cur_pt(-1?-1);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4664??2017-02-01?16:56??main.c
????.......????????66??2015-12-01?06:22??pixy-master源代碼\.gitignore
????.......????158647??2015-12-01?06:22??pixy-master源代碼\contributors.txt
????.......????101934??2015-12-01?06:22??pixy-master源代碼\doc\Doxyfile
????.......?????13769??2015-12-01?06:22??pixy-master源代碼\doc\images\Pixy01-21-136x76.png
????.......?????11321??2015-12-01?06:22??pixy-master源代碼\doc\images\Pixy01-21-300x168.jpg
????.......??????2726??2015-12-01?06:22??pixy-master源代碼\doc\images\Pixy01-21-89x50.jpg
????.......???????511??2015-12-01?06:22??pixy-master源代碼\license.txt
????.......??????5309??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......?????36420??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......?????36695??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......?????56800??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......?????????1??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......?????????1??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????42??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????42??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????42??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......???????143??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????42??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????58??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????58??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????58??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????58??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......???????751??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......??????3684??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????42??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......???????224??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????87??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......???????257??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
????.......????????26??2015-12-01?06:22??pixy-master源代碼\misc\gcc\.me
............此處省略1048個文件信息
評論
共有 條評論