The interpolate function is used to get intensity of a point which is not on exactly a pixel.

The code is written in C++. Because it is template function, so they should be put in header file.

// Interpolates pixel intensity with subpixel accuracy.
// Abount bilinear interpolation in Wikipedia:
// http://en.wikipedia.org/wiki/Bilinear_interpolation
template <class T>
float interpolate(const Mat &mat, float x, float y)
{
// Get the nearest integer pixel coords (xi;yi).
int xi = cvFloor(x);
int yi = cvFloor(y); float k1 = x - xi; // Coefficients for interpolation formula.
float k2 = y - yi; bool b1 = xi < mat.cols - 1; // Check that pixels to the right
bool b2 = yi < mat.rows - 1; // and to down direction exist. float UL = mat.at<T>(Point(xi, yi));
float UR = b1 ? mat.at<T>( Point (xi + 1, yi ) ) : 0.f;
float LL = b2 ? mat.at<T>( Point ( xi, yi + 1) ) : 0.f;
float LR = b1 & b2 ? mat.at <T>( Point ( xi + 1, yi + 1 ) ) : 0.f; // Interpolate pixel intensity.
float interpolated_value = (1.0f - k1) * (1.0f - k2) * UL + k1 * (1.0f - k2) * UR +
(1.0f - k1) * k2 * LL + k1 * k2 * LR; return interpolated_value;
} //Get the intensity along an input line
template <class T>
int GetIntensityOnLine ( const Mat &mat, const Point &start, const Point &end, vector<float> &vecOutput )
{
if ( start.x >= mat.cols || start.y >= mat.rows )
return -1;
if ( end.x >= mat.cols || end.y >= mat.rows )
return -1; float fLineLen = (float)sqrt ( ( end.x - start.x ) * ( end.x - start.x ) + ( end.y - start.y ) * ( end.y - start.y ) );
if ( fLineLen < 1 )
return -1; float fCos = ( end.x - start.x ) / fLineLen;
float fSin = ( end.y - start.y ) / fLineLen; float fCurrentLen = 0.f;
while ( fCurrentLen < fLineLen ) {
float fX = start.x + fCos * fCurrentLen;
float fY = start.y + fSin * fCurrentLen;
float fIntensity = interpolate<T> ( mat, fX, fY );
vecOutput.push_back ( fIntensity ); ++ fCurrentLen;
} return 0;
}

  

Get Intensity along a line based on OpenCV的更多相关文章

  1. logoff remote desktop sessions via command line tools

    This trick I learned from my one of ex-college.  In Windows servers, only two remote desktop session ...

  2. Build OpenCV text(OCR) module on windows

    Background. AOI software needs to use the OCR feature to recognize the texts on the chips. Because o ...

  3. OpenCV Template Matching Subpixel Accuracy

    OpenCV has function matchTemplate to easily do the template matching. But its accuracy can only reac ...

  4. OpenCV CommandLineParser 的用法

    OpenCV CommandLineParser 的用法 去百度了一下,关键字:OpenCV CommandLineParser  发现,最多的讲解是:opencv源码解析之(5):CommandLi ...

  5. Android OpenCV实现图片叠加,水印

    关于如何用纯OpenCV实现图片叠加的例子实在是太少,太多的是使用 C++,JNI实现的,如果要用C++的话,我们为啥不转行做C++ 下面的例子基于 Android JavaCV 实现了在im_bea ...

  6. 基于OpenCV的面部交换

    需要装python库 OpenCV dlib docopt(根据打开方式选择是否装) # -*- coding: UTF-8 #本电脑试运行 命令 python F:\python_project\s ...

  7. opencv霍夫变换

    霍夫变换不仅可以找出图片中的直线,也可以找出圆,椭圆,三角形等等,只要你能定义出直线方程,圆形的方程等等. 不得不说,现在网上的各种博客质量真的不行,网上一堆文章,乱TM瞎写,误人子弟.本身自己就没有 ...

  8. opencv之霍夫曼变换

    霍夫变换不仅可以找出图片中的直线,也可以找出圆,椭圆,三角形等等,只要你能定义出直线方程,圆形的方程等等. 不得不说,现在网上的各种博客质量真的不行,网上一堆文章,乱TM瞎写,误人子弟.本身自己就没有 ...

  9. 行为识别(action recognition)相关资料

    转自:http://blog.csdn.net/kezunhai/article/details/50176209 ================华丽分割线=================这部分来 ...

随机推荐

  1. VPS/服务器优化网络、加速方法总结与参考

    在国外的服务器上因为受各种因素影响,即使国外的服务器都是百兆共享或者G口到国内下载速度都不是很让人满意,大部分人购买国外服务器是用作存储下载或者扶墙,速度慢的所以话影响我们的使用体验.所以就搞出了很多 ...

  2. Redis -- 03 持久化

    Redis提供了两种不同的方法来将数据存储到硬盘里面,一种叫内存快照,另一种叫只追加文件(AOF),这两种方法既可以同时使用课可以单独使用,也可以都不使用,取决于场景. 快照 快照是将某一时刻的所有数 ...

  3. 在Angular1.X中使用CSS Modules

    在Angular1.5中,增加了一个Component方法,并且定义了组件的若干生命周期hook,在代码规范中也是推崇组件化开发,但是很遗憾的是,CSS模块化组件化的问题并没有得到解决,大部分项目的打 ...

  4. js post提交

    /* js跳转页面 url跳转地址,params参数列表 */ function Posturl(url,params){ //创建form表单 var temp_form=document.crea ...

  5. Head First 设计模式之观察者模式(Observer Pattern)

    前言: 这一节开始学习观察者模式,开始讲之前会先像第一节那样通过一个应用场景来引入该模式.具体场景为:气象站提供了一个WeatherData对象,该对象可以追踪获取天气的温度.气压.湿度信息,Weat ...

  6. zabbix3.0安装之图形界面显示异常【server】

    前面记录过Zabbix3.0的安装过程,遇到一些坑,当时就在博文最后提到过,显示界面只有文字没有样式的问题.今天就解决这个小问题. 首先, 我们的安装是基于nginx作为web服务器的,不是传统的用A ...

  7. WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED解决方法

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@    WARNING: REMOTE HOST IDENTIFICATION ...

  8. 最近发现了个js传图预览的函数和大家分享下

    由于浏览器js沙箱,存在不同域,js无法直接调用电脑文件.在之前做传图预览的时候,基本的思路就是, 将图片先保存到服务器中缓存,然后返回一个服务器路径,并显示出图片,然而这样很耗缓存目录文件的.方法也 ...

  9. Ubuntu 16.04中安装Chromium浏览器

    引用:http://www.linuxidc.com/Linux/2016-05/131097.htm 通过ppa,安装最新版本的Chromium浏览器 sudo add-apt-repository ...

  10. vmware12无法打开内核设备“\\.\Global\vmx86”

    vmware12 无法打开内核设备"\\.\Global\vmx86": 系统找不到指定的文件.你想要在安装 VMware Workstation 前重启吗? 打开vmware12 ...