存档

文章标签 ‘opencv’

zz:opencv内存管理

2010年8月5日 cvchina 1 条评论

感谢cvLee的投递。来源

cvCreateMemStorage调用过程:
CvMemStorage* cvCreateMemStorage(int block_size) —>void* cvAlloc( size_t size )—>
static void* icvDefaultAlloc( size_t size, void* )—->void* cvAlignPtr( const void* ptr, int align=32 )

阅读全文…

分类: 新闻 标签: , ,

OpenCV WinCE/WM移植

2010年7月29日 cvchina 2 条评论

首先声明,本文来自hellogv,很好很强大,可以围观,可以搭讪。

上次写了在C#玩OpenSURF的演示,这次就写写如何把opencv1.10移植到wince/WM。因为如果懂得裁剪opencv,那么就可以在更多设备(PC,手机,开发板)上玩更多更好玩的算法,因此,移植和裁剪opencv还是很有必要的。我已经移植到wince/WM上的opencv1.10工程可以到这里:http://www.pudn.com/downloads270/sourcecode/embed/detail1235697.html,工程只含cv和cvcore这两个核心项目,highgui与系统粘合度太大,不好移植,迟点会贴出实现部分highgui功能的例子。

本文参考:http://www.computer-vision-software.com/blog/2009/03/running-opencv-facedetect-sample-on-pocket-pc/

接下来,废话不说,直接把移植cv和cvcore的步骤贴上:

阅读全文…

分类: 新闻 标签: , , ,

视觉艺术:hand from above

2010年6月27日 cvchina 3 条评论

一个基于计算机视觉的装置艺术。很有趣。

阅读全文…

分类: 新闻 标签: , , ,

Diving into gaussian filter implementation

2010年5月7日 cvchina 9 条评论

高斯滤波(高斯平滑)是图像处理,计算机视觉里面最常见的操作。平时,我们都是用matlab或者opencv的函数调用:imfilter或者cvSmooth,并不关心底层的实现。然而当开发者要实做高斯滤波的时候,往往就会很迷惘,往往会被以下几个问题困扰:

  1. 给定sigma,即标准偏差,怎么确定离散化后滤波器的窗口大小?
  2. 给定窗口大小,怎么计算高斯核的sigma,即标准方差?
  3. 怎么实现可分离滤波器?

我在google上搜了一下,还真没几个人把实现的细节讲清楚了。这里,我尝试结合三份源码,做个小小的总结。

三份源码分别是:

  1. opencv中的cvfilter.cpp
  2. autopano-sift-c中的GaussianConvolution.c
  3. GIMP中的blur-gauss.c和unsharp-mask.c

在图像处理中,高斯滤波一般有两种实现方式,一是用离散化窗口滑窗卷积,另一种通过傅里叶变换。最常见的就是第一种滑窗实现,只有当离散化的窗口非常大,用滑窗计算量非常大(即使用可分离滤波器的实现)的情况下,可能会考虑基于傅里叶变化的实现方法。这里我们只讨论第一种方法。

二维高斯函数的形式是这样的:

f(x,y) = A e^{- \left(\frac{(x-x_o)^2}{2\sigma_x^2} + \frac{(y-y_o)^2}{2\sigma_y^2} \right)}.

有着如下的形状,形状很激凸,怎么会叫高斯平滑呢,分明是高斯激凸嘛:

基本上,离散化的主旨就是保留高斯函数中心能量最集中的中间部分,忽略四周能量很小的平坦区域。这只是个很感性的描述,具体实现起来,就会出现千奇百怪的版本。下面结合三份源码,看看现实世界里的高斯平滑到底长的什么样子。

首先是第一个问题:给定sigma,怎么计算窗口大小?

直接上opencv的源码,在cvFilter函数中:

param1 = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1;

opencv认为半径为3*sigma的窗口就是高斯函数能量最集中的区域。(为什么在图像深度不是8U的时候,使用4*sigma半径的窗口就不得而知了,有高人指点一下?)

autopan0-sift-c是图像拼接软件hugin里面的sift实现,在实现DoG的时候需要做不同尺度的高斯平滑,实现如下,在GaussianConvolution_new1函数中:

dim = 1 + 2 * ((int) (3.0 * sigma));

可见autopano也是实现的3*sigma半径的窗口。

在GIMP里,实现比较奇特,在blur_gauss.c的make_rle_curve函数里面,

const gdouble sigma2 = 2 * sigma * sigma;
const gdouble l = sqrt (-sigma2 * log (1.0 / 255.0));
int n = ceil (l) * 2;
if ((n % 2) == 0)
n += 1;

我是没看懂那个 log (1.0 / 255.0)是干嘛的。。。惭愧。效果来看,这个实现的窗口半径是约等于2.2*sigma。

然后是第二个问题:给定窗口大小,怎么计算sigma?

opencv的实现,在cvFilter.cpp的init_gaussian_kernel函数中:

sigmaX = sigma > 0 ? sigma : (n/2 – 1)*0.3 + 0.8;

再次不可解。。。乘以0.3还可以接受,加上0.8是为嘛啊?

autopano没有实现这个特性。

GIMP的实现:

/* we want to generate a matrix that goes out a certain radius
* from the center, so we have to go out ceil(rad-0.5) pixels,
* inlcuding the center pixel. Of course, that’s only in one direction,
* so we have to go the same amount in the other direction, but not count
* the center pixel again. So we double the previous result and subtract
* one.
* The radius parameter that is passed to this function is used as
* the standard deviation, and the radius of effect is the
* standard deviation * 2. It’s a little confusing.
*/
radius = fabs (radius) + 1.0;

std_dev = radius;
radius = std_dev * 2;
/* go out ‘radius’ in each direction */
matrix_length = 2 * ceil (radius – 0.5) + 1;

注释讲的很清楚了,基本上就是认为sigma应该等于窗口半径的一半。

看完这三分源码,结论就是,关于sigma和半径,你爱怎么算就怎么算吧,差不多就行。。。(额。。费了半天劲,就得到这么一句废话啊)。

第三个问题是可分离滤波器:

由于高斯函数可以写成可分离的形式,因此可以采用可分离滤波器实现来加速。所谓的可分离滤波器,就是可以把多维的卷积化成多个一维卷积。具体到二维的高斯滤波,就是指先对行做一维卷积,再对列做一维卷积。这样就可以将计算复杂度从O(M*M*N*N)降到O(2*M*M*N),M,N分别是图像和滤波器的窗口大小。问题是实现时候怎么计算一维的卷积核呢?

其实很简单,按照前面计算出来的窗口大小,计算所有离散点上一维高斯函数的权值,最后别忘了将权值之和归一化到1.
有码有真相,来自opencv:

for( i = 0; i <= n/2; i++ )
{
double t = fixed_kernel ? (double)fixed_kernel[i] : exp(scale2X*i*i);
if( type == CV_32FC1 )
{
cf[(n/2+i)*step] = (float)t;
sum += cf[(n/2+i)*step]*2;
}
else
{
cd[(n/2+i)*step] = t;
sum += cd[(n/2+i)*step]*2;
}
}

sum = 1./sum;
for( i = 0; i <= n/2; i++ )
{
if( type == CV_32FC1 )
cf[(n/2+i)*step] = cf[(n/2-i)*step] = (float)(cf[(n/2+i)*step]*sum);
else
cd[(n/2+i)*step] = cd[(n/2-i)*step] = cd[(n/2+i)*step]*sum;
}

终于写完了,希望对各位有所帮助。不对之处请指正哈。

zz:在android上编译opencv

2010年4月29日 cvchina 3 条评论

身体很虚,转载一枚,等买了android手机来试一把。来源

点击下面链接看全文攻略。

阅读全文…

OpenCV 2.1 发布

2010年4月10日 cvchina 1 条评论

Open Computer Vision Library IconOpen Computer Vision Library IconOpen Computer Vision Library IconOpen Computer Vision Library IconOpen Computer Vision Library IconOpen Computer Vision Library IconOpen Computer Vision Library IconOpen Computer Vision Library IconOpen Computer Vision Library IconOpen Computer Vision Library Icon

来源如下:

http://sourceforge.net/mailarchive/forum.php?thread_name=s2y619b2d671004051902n71c46746m5dc42581ca1d4a79@mail.gmail.com&forum_name=opencvlibrary-devel

贴个changelog里的新添加的features:

>>> New functionality, features:
- cxcore, cv, cvaux:
* Grabcut (http://en.wikipedia.org/wiki/GrabCut) image segmentation algorithm has been implemented.
See opencv/samples/c/grabcut.cpp
* new improved version of one-way descriptor is added. See opencv/samples/c/one_way_sample.cpp
* modified version of H. Hirschmuller semi-global stereo matching algorithm that we call SGBM
(semi-global block matching) has been created. It is much faster than Kolmogorov’s graph
cuts-based algorithm and yet it’s usually better than the block matching StereoBM algorithm.
See opencv/samples/c/stereo_matching.cpp.
* existing StereoBM stereo correspondence algorithm by K. Konolige was noticeably improved:
added the optional left-right consistency check and speckle filtering,
improved performance (by ~20%).
* User can now control the image areas visible after the stereo rectification
(see the extended stereoRectify/cvStereoRectify), and also limit the region
where the disparity is computed (see CvStereoBMState::roi1, roi2; getValidDisparityROI).
* Mixture-of-Gaussian based background subtraction algorithm has been rewritten for better performance
and better accuracy. Alternative C++ interface BackgroundSubtractor has been provided,
along with the possibility to use the trained background model to segment the foreground
without updating the model. See opencv/samples/c/bgfg_segm.cpp.
还有几个链接:

The packages are available at SourceForge (
https://sourceforge.net/projects/opencvlibrary/files/).
The detailed ChangeLog is here:
https://code.ros.org/svn/opencv/trunk/opencv/doc/ChangeLog.htm.
The installation guide is here:
http://opencv.willowgarage.com/wiki/InstallGuide

Opencv 2010计划

2010年3月9日 cvchina 5 条评论

OpenCVOpenCVOpenCVOpenCVOpenCVOpenCVOpenCVOpenCVOpenCVOpenCVOpenCVOpenCVOpenCVOpenCV

突然想起八卦一下opencv 2010的计划,找到这个http://opencv.willowgarage.com/wiki/GSOC_OpenCV2010页面。

看来下一个版本(可能)会有更多的非sift的feature,(可能)会有Place recognition ,Ground plane detection,Texture recognition等等用来支持增强现实,(可能)会有更好的gui(缩放,当前点位置,灰度值等等),(可能)会有opengl支持,(可能)truetype字体渲染等等,(可能)会有图像拼接,(可能)会支持android等等。。

下面照抄:

Mentors

  • Background subtraction, feature based tracking
     Nicolas Saunier, Ph.D.
     Professeur Adjoint / Assistant Professor
     Département des génies civil, géologique et des mines (CGM)
     École Polytechnique de Montréal
    
    http://nicolas.saunier.confins.net
  • Write code that uses QTKit.framework to read videos on Mac OS X.
  • Write GUI code that uses 64bit Carbon.
  • Implement some well known CV algorithms.
     Mark Asbach
     Fraunhofer IAIS
     Schloss Birlinghoven
     Sankt Augustin, Germany
    
    http://mmprec.iais.fraunhofer.de/asbach.html
  • Image stitching and/or image collage
     Gary Bradski
     Senior Scientist, Willow Garage
     Consulting Prof. Stanford U.
     OpenCV Founder, Technical Content Owner
  • ?
     Vadim Pisarevsky
     OpenCV founding team/Czar
  • HighGUI enhancements:
    • saving/restoring window positions on every platform,
    • advanced image views: zoom, scrolling, displaying position + pixel values
    • OpenGL support
    • truetype text rendering, transparency, gradients etc. (via libcairo? or Qt?)
    • possible cross-platform Qt backend
  • Parallelization of various algorithms using Intel TBB (below are possible candidates):
    • Calonder & One-way descriptors and the related algorithms
    • Dense optical flow
    • HOG-, Haar-, LBP-based object detectors (already threaded using OpenMP)
    • Background/foreground segmentation
     Victor Eruhimov
     OpenCV founding team/Senior Researcher
     Argus/Itseez founder

Ideas from the Core OpenCV Team

  • Make it easy to use computer vision on the Android phone.
    • This may involve some feature processing on the Phone, but also a link with a server (Amazon cloud?) and/or perhaps with Google’s own Streetview and map data.
    • We want to enable:
      • Image stitching and placement –
        • can we add an off road trail, biking path, indoor scene or city park to Google Streetview?
      • Place recognition and Ground plane detection for augmented reality
        • can we allow game makers to easily overlay a real scene?
      • Texture recognition with geometry — can we allow game makers to augment a simple scene such as a board game with augmented reality overlay?
    • Using:
      • Visual odemetry (using techniques developed at Willow that should be ready by summer)
        • Place recognition
        • Texture + geometry based object recognition, again using techniques
        • Image stitching
  • Image collage
    • Putting image collections into useful summary image collage.
    • Can also work with Android above.
    • Easy to post to web, pdf or other formats.
    • Segmentation.
  • Other functionality:
    • Denoising, motion stabilization, lighting balance, image enhancement
    • Segmentation in video/video effects
    • Substitutes for SIFT
    • 3D model capture, silhouetts + strip

请关注这个页面: http://opencv.willowgarage.com/wiki/GSOC_OpenCV2010

Stay tuned.