Get Intensity along a line based on OpenCV

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;
}

  

时间: 2024-10-14 03:09:20

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

【转载】Data Science at the Command Line

Data Science at the Command Line Data Science at the Command Line is a new book written by Jeroen Janssens. This website contains information about the upcoming workshop in London, the webcast from August 20th, instructions on how to install the Data

Instances Based on Opencv2: A Simple Analysis of MVC Architecture

A few months ago, one of my friends asked me for a favor that he wanted me to help him out with his digital recognition project. Fortunately, I had decided to go further for a master degree and I had plenty of time, moreover I was totally interested

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 sessions allowed with administrative access. There are situations, you can't connect to the server via remote desktop because of two sessions already active.

Build OpenCV text(OCR) module on windows

Background. AOI software needs to use the OCR feature to recognize the texts on the chips. Because our vision software is based on OpenCV, so the first choice is text module in opencv_contrib. Procedures. 1. OCR module is not in standard OpenCV packa

How to Use OpenCV in Android Studio

Create a libraries folder underneath your project main directory. For example, if your project is OpenCVExamples, you would create a OpenCVExamples/libraries folder. Go to the location where you have SDK "\OpenCV-2.4.8-android-sdk\sdk" here you

opencv霍夫变换

霍夫变换不仅可以找出图片中的直线,也可以找出圆,椭圆,三角形等等,只要你能定义出直线方程,圆形的方程等等. 不得不说,现在网上的各种博客质量真的不行,网上一堆文章,乱TM瞎写,误人子弟.本身自己就没有理解的很清楚,又不去读算法实现的源码,写的云山雾罩的,越看越懵逼. **霍夫变换本身的思路是很简明的.**这篇文章我们就以霍夫直线变换说明算法的思想. 霍夫变换 思考一下,二维平面里我们怎么表达直线. 有两种表达方式: 直角坐标系(也叫笛卡尔坐标系) 极坐标系(也叫球坐标系) 第一种就是最常见的直角

opencv之霍夫曼变换

霍夫变换不仅可以找出图片中的直线,也可以找出圆,椭圆,三角形等等,只要你能定义出直线方程,圆形的方程等等. 不得不说,现在网上的各种博客质量真的不行,网上一堆文章,乱TM瞎写,误人子弟.本身自己就没有理解的很清楚,又不去读算法实现的源码,写的云山雾罩的,越看越懵逼. 霍夫变换本身的思路是很简明的.这篇文章我们就以霍夫直线变换说明算法的思想. 霍夫变换 思考一下,二维平面里我们怎么表达直线. 有两种表达方式: 直角坐标系(也叫笛卡尔坐标系) 极坐标系(也叫球坐标系) 第一种就是最常见的直角坐标系下

Motion sensing game (Ping Pong Game)

Project Demonstration Here is the source code of the project based on OpenCV anc C++. Before you run this code on Linux, you should install the OpenCV library  first. #include<opencv/highgui.h> #include<opencv2/opencv.hpp> #include<string&g

xapp1167与TRD14.4 关系

 xapp1167与TRD14.4 关系 xapp1167与TRD14.4 都是学习zynq的最佳参考设计.都是基于ZC702,而不是zedboard的,如果要用zedboard那么必须修改硬件配置,因为在702板子中HDMI用的是软核,而zedboard中用的芯片,另外还有网卡等等都不一样配置也不一样. 1:xapp1167   :http://xgoogle.xilinx.com/search?getfields=*&numgm=5&filter=0&proxystyles