1. 原理
见下图,假设原图为单通道的灰度图,想求图像中某点Q(x,y)的灰度值。
2. 代码实现
#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <string>
#include<opencv2/opencv.hpp>
#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
using namespace cv;// 只接受单通道
template <typename T>
bool bilinearInterp(const cv::Mat& img, float x, float y, float& value)
{if (img.empty() || x < 0.0 || y < 0.0|| x > img.cols - 1 || y > img.rows - 1){value = 0.0;return false;}if (img.channels() != 1){value = 0.0;return false;}int x1 = floor(x);int x2 = ceil(x);int y1 = floor(y);int y2 = ceil(y);int x2_sub_x1 = x2 - x1;int y2_sub_y1 = y2 - y1;if (x2_sub_x1 == 0 || y2_sub_y1 =