直方图的反向投影(转自:http://ww…
来源:互联网 发布:macbook用office软件 编辑:程序博客网 时间:2024/05/19 12:38
直方图的反向投影
1.反向投影的作用是什么?
2.反向投影如何查找(工作)?
假设我们有一张100x100的输入图像,有一张10x10的模板图像,查找的过程是这样的:
(1)从输入图像的左上角(0,0)开始,切割一块(0,0)至(10,10)的临时图像;
(2)生成临时图像的直方图;
(3)用临时图像的直方图和模板图像的直方图对比,对比结果记为c;
(4)直方图对比结果c,就是结果图像(0,0)处的像素值;
(5)切割输入图像从(0,1)至(10,11)的临时图像,对比直方图,并记录到结果图像;
(6)重复(1)~(5)步直到输入图像的右下角。
(本图片引用自http://www.opencv.org.cn)
3.反向投影的结果是什么?
4.特殊情况怎么样?
直方图反向投影的示例如下:
private void btnCalcBackProjectPatch_Click(object sender,EventArgs e)
{
Stopwatch sw =new Stopwatch();
sw.Start();
//定义变量
Image<Bgr, Byte> imageSource = newImage<Bgr, byte>((Bitmap)pbSource.Image);
double scale =GetScale(); //用一个缩放因子减小源图像的大小,可以极大的提高处理速度
Image<Bgr, Byte> imageSource2 = imageSource.Resize(scale,INTER.CV_INTER_LINEAR);
Image<Bgr, Byte> imageTarget = newImage<Bgr, byte>((Bitmap)pbTarget.Image);
Image<Bgr, Byte> imageTarget2 = imageTarget.Resize(scale,INTER.CV_INTER_LINEAR);
Image<Gray, Single> imageDest = null;
Size patchSize =imageTarget2.Size;
string colorSpace = (string)cmbColorSpace.SelectedItem; //色彩空间的选择对处理速度也有很大的影响,更少的色彩空间数目能极大的提高处理速度
double normalizeFactor = 1d;//归一化因子,一般设置为1,且必须为double类型
Image<Gray, Byte>[] imagesSource;
int[]bins;
RangeF[] ranges;
string[]captions;
Color[] colors;
//得到源图像需要参与反向投影的各色彩图像集合
FormProcessHist.GetImagesBinsAndRanges(imageSource2,colorSpace, outimagesSource, out bins, out ranges, out captions, out colors);
//计算目标图像的直方图
DenseHistogram histTarget = CalcHist(imageTarget2.Bitmap);
//反向投影
HISTOGRAM_COMP_METHOD compareMethod= GetHistogramCompareMetho
{
imageDest = BackProjectPatch<Byte>(imagesSource, patchSize, histTarget,compareMethod, (float)normalizeFactor); //DenseHistogram类的方法BackProjectPath不能用,这里自己实现了一个类似的方法
}
catch(Exception ex)
{
txtResult.Text +=string.Format("在执行反向投影时发生错误,错误描述:{0},错误源:{1}\r\n",ex.Message, ex.Source);
}
//显示结果
if (imageDest != null)
pbResult.Image =imageDest.Bitmap;
sw.Stop();
txtResult.Text +=string.Format("直方图反向投影用时:{0:F05}毫秒,对比方式:{1:G},({2})\r\n",sw.Elapsed.TotalMilliseconds, compareMethod,GetOptions());
//释放资源
imageSource.Dispose();
imageSource2.Dispose();
imageTarget.Dispose();
imageTarget2.Dispose();
if (imageDest != null)
imageDest.Dispose();
foreach (Image<Gray, Byte> image in imagesSource)
image.Dispose();
histTarget.Dispose();
}
在左图(输入图像)中查找特定人脸(模板图像),结果如右图,中间亮白色的区域为最匹配区域
[DllImport("cv200.dll",EntryPoint ="cvCalcArrBackProjectPatc
public static extern void cvCalcBackProjectPatch(
IntPtr[] images,
IntPtr dst,
Size patchSize,
IntPtr hist,
HISTOGRAM_COMP_METHOD method,
double factor);
/// <summary>
/// 计算直方图的反向投影
/// </summary>
/// <paramname="srcs">Source images, all are of the same size andtype</param>
/// <paramname="factor">Normalization factor for histograms, will affectnormalization scale of destination image, pass 1. if unsure.</param>
/// <paramname="patchSize">Size of patch slid though the sourceimages.</param>
/// <paramname="method">Comparison method, passed tocvCompareHist.</param>
/// <typeparamname="TDepth">The type of depth of the image</typeparam>
/// <returns>Destinationback projection image of the same type as the sourceimages</returns>
public Image<Gray, Single> BackProjectPatch<TDepth>(Image<Gray, TDepth>[] srcs, Size patchSize, DenseHistogram hist,HISTOGRAM_COMP_METHOD method, double factor) where TDepth: new()
{
Debug.Assert(srcs.Length == hist.Dimension, "直方图的维数和源图像的数目必须相同");
IntPtr[] imgPtrs =
Array.ConvertAll<Image<Gray, TDepth>, IntPtr>(
srcs,
delegate(Image<Gray, TDepth> img) { return img.Ptr; });
Size size = srcs[0].Size;
size.Width =size.Width - patchSize.Width + 1;
size.Height =size.Height - patchSize.Height + 1;
Image<Gray, Single> res =new Image<Gray, float>(size);
cvCalcBackProjectPatch(imgPtrs, res.Ptr, patchSize, hist.Ptr,method, factor);
return res;
}
已经将发现的上述错误提交给OpenCv及EmguCv的BUG LIST,希望能尽快解决。
- 直方图的反向投影(转自:http://ww…
- 直方图的反向投影(转自:http://ww…
- 直方图的反向投影
- 直方图的反向投影
- 直方图的反向投影
- 直方图的反向投影
- 直方图反向投影的作用
- (转)直方图反向投影
- 直方图的反向投影的原理
- 图像直方图的反向投影的计算
- opencv中直方图的反向投影
- cvCalcBackProject-直方图反向投影
- 直方图反向投影
- opencv 直方图反向投影
- opencv 直方图反向投影
- 直方图反向投影
- opencv 直方图反向投影
- HSV三维直方图反向投影
- 怎样练习小提琴的空弦、音阶…
- Docker shipyard 笔记
- 学习小提琴,试奏能力不可忽…
- 学习小提琴,试奏能力不可忽…
- 直方图的反向投影(转自:http://ww…
- 直方图的反向投影(转自:http://ww…
- 模板匹配(转自:http://www.cnblog…
- 模板匹配(转自:http://www.cnblog…
- 轮廓的查找、表达、绘制、特性及匹…
- 轮廓的查找、表达、绘制、特性及匹…
- 安装基于vsftpd搭建的ftp服务器
- css属性之cursor的测试
- css属性之cursor的测试
- 转人类社会进化与时代应用的…