参考:https://blog.csdn.net/quincuntial/article/details/50541815
旋转不变LBP特征
如图,通过对得到的LBP特征进行旋转,得到一系列的LBP特征值,最终将特征值最小的一个特征模式作为中心像素点的LBP特征。
//旋转不变圆形LBP特征计算 template <typename _tp> void getRotationInvariantLBPFeature(InputArray _src, OutputArray _dst, int radius, int neighbors) { Mat src = _src.getMat(); //LBP特征图像的行数和列数的计算要准确 _dst.create(src.rows - 2 * radius, src.cols - 2 * radius, CV_8UC1); Mat dst = _dst.getMat(); dst.setTo(0); for (int k = 0; k<neighbors; k++) { //计算采样点对于中心点坐标的偏移量rx,ry float rx = static_cast<float>(radius * cos(2.0 * CV_PI * k / neighbors)); float ry = -static_cast<float>(radius * sin(2.0 * CV_PI * k / neighbors)); //为双线性插值做准备 //对采样点偏移量分别进行上下取整 int x1 = static_cast<int>(floor(rx)); int x2 = static_cast<int>(ceil(rx)); int y1 = static_cast<int>(floor(ry)); int y2 = static_cast<int>(ceil(ry)); //将坐标偏移量映射到0-1之间 float tx = rx - x1; float ty = ry - y1; //根据0-1之间的x,y的权重计算公式计算权重,权重与坐标具体位置无关,与坐标间的差值有关 float w1 = (1 - tx) * (1 - ty); float w2 = tx * (1 - ty); float w3 = (1 - tx) * ty; float w4 = tx * ty; //循环处理每个像素 for (int i = radius; i<src.rows - radius; i++) { for (int j = radius; j<src.cols - radius; j++) { //获得中心像素点的灰度值 _tp center = src.at<_tp>(i, j); //根据双线性插值公式计算第k个采样点的灰度值 float neighbor = src.at<_tp>(i + x1, j + y1) * w1 + src.at<_tp>(i + x1, j + y2) *w2 \ + src.at<_tp>(i + x2, j + y1) * w3 + src.at<_tp>(i + x2, j + y2) *w4; //LBP特征图像的每个邻居的LBP值累加,累加通过与操作完成,对应的LBP值通过移位取得 dst.at<uchar>(i - radius, j - radius) |= (neighbor>center) << (neighbors - k - 1);//dst默认为CV_8UC1,因此只支持8个采样点 } } } //进行旋转不变处理 for (int i = 0; i<dst.rows; i++) { for (int j = 0; j<dst.cols; j++) { unsigned char currentValue = dst.at<uchar>(i, j); unsigned char minValue = currentValue; for (int k = 1; k<neighbors; k++) { //循环左移 unsigned char temp = (currentValue >> (neighbors - k)) | (currentValue << k); if (temp < minValue) { minValue = temp; } } dst.at<uchar>(i, j) = minValue; } } } int main() { cv::Mat src = imread("..\\..\\image\\keliamoniz1.jpg", 0); cv::Mat dst; //getOriginLBPFeature<uchar>(src, dst); //getCircularLBPFeatureOptimization<uchar>(src, dst, 1, 8); getRotationInvariantLBPFeature<uchar>(src, dst, 1, 8); return 0; }
文章来源: https://blog.csdn.net/zfjBIT/article/details/90638514