基于OpenCV的LBP算法(OpenCV1.0版本)

来源:互联网 发布:简谱制作软件app 编辑:程序博客网 时间:2024/05/22 03:13
#include <opencv2/opencv.hpp>#include <iostream>#include <cassert>using namespace std;using namespace cv;//基于旧版本的opencv的LBP算法opencv1.0  // 3 x 3 矩阵如下所示// [ 1, 2, 3]// [ 8, ij,4]// [ 7, 6, 5]void LBP(IplImage *src, IplImage *dst){// 进行处理的原图像为单通道图像assert(src != NULL && src->nChannels == 1);int tmp[8] = { 0 };int rows = src->height - 1;int cols = src->width - 1;for (int i = 1; i < rows; ++i){for (int j = 1; j < cols; ++j){int sum = 0;double val = cvGetReal2D(src, i, j);double tempVal = 0.0;tempVal = cvGetReal2D(src, i - 1, j - 1);// 左上角tempVal > val ? tmp[0] = 255 : tmp[0] = 0;tempVal = cvGetReal2D(src, i, j - 1);// 正上方tempVal > val ? tmp[1] = 255 : tmp[1] = 0;tempVal = cvGetReal2D(src, i + 1, j - 1);// 右上角tempVal > val ? tmp[2] = 255 : tmp[2] = 0;tempVal = cvGetReal2D(src, i + 1, j);// 右侧tempVal > val ? tmp[3] = 255 : tmp[3] = 0;tempVal = cvGetReal2D(src, i + 1, j + 1);// 右下角tempVal > val ? tmp[4] = 255 : tmp[4] = 0;tempVal = cvGetReal2D(src, i, j + 1);// 正下方tempVal > val ? tmp[5] = 255 : tmp[5] = 0;tempVal = cvGetReal2D(src, i - 1, j + 1);// 左下角tempVal > val ? tmp[6] = 255 : tmp[6] = 0;tempVal = cvGetReal2D(src, i - 1, j);// 左侧tempVal > val ? tmp[7] = 255 : tmp[7] = 0;// 计算 LBP 编码for (int k = 0; k < 8; ++k){sum += tmp[k] * pow(2, k);}cvSetReal2D(dst, i, j, sum);}}}// 圆形 LBP 算子void ELBP(IplImage* src, IplImage* dst, int radius, int neighbors){// 处理的图像为单通道图像assert(src->nChannels == 1);for (int i = 0; i < neighbors; ++i){// 正弦弧度double sRadian = sin(2.0 * CV_PI * i / static_cast<double>(neighbors));// 余弦弧度double cRadian = cos(2.0 * CV_PI * i / static_cast<double>(neighbors));// 采样点的计算double x = static_cast<double>(-radius * sRadian);double y = static_cast<double>(radius * cRadian);// 下取整的值int fx = static_cast<int>(floor(x));int fy = static_cast<int>(floor(y));// 上取整的值int cx = static_cast<int>(ceil(x));int cy = static_cast<int>(ceil(y));// 小数部分double tx = x - fx;double ty = y - fy;// 设置插值权重double w1 = (1 - tx) * (1 - ty);double w2 = tx * (1 - ty);double w3 = (1 - tx) * ty;double w4 = tx * ty;// 循环处理图像数据for (int rows = radius; rows < src->height - radius; ++rows){for (int cols = radius; cols < src->width - radius; ++cols){// 计算插值double t1 = w1 * cvGetReal2D(src, rows + fy, cols + fx);double t2 = w2 * cvGetReal2D(src, rows + fy, cols + cx);double t3 = w3 * cvGetReal2D(src, rows + cy, cols + fx);double t4 = w4 * cvGetReal2D(src, rows + cy, cols + cx);double t = t1 + t2 + t3 + t4;double val = cvGetReal2D(src, rows, cols);double epsilon = std::numeric_limits<double>::epsilon();uchar c = ((t > val) || abs(t - val) < epsilon);uchar tmp = c * pow(2, i);double v = cvGetReal2D(dst, rows - radius, cols - radius);v += tmp;cvSetReal2D(dst, rows - radius, cols - radius, v);}}}}

0 0
原创粉丝点击