// 得到 LBP纹理特征值图 // 参数: // src 为单通道灰度图 // dst 为目标图 // 返回值: // 返回ture 表示运行正常 // 返回false 表示运行出错 bool GetLBPFeatureImage(IplImage *src, IplImage *dst) { if (! src || ! dst) return false; // 获取图像信息 const int height = src->height; const int width = src->width; const int widthStep = src->widthStep; const int channels = src->nChannels; // 通道数 const uchar * data = (uchar *)src->imageData; if (channels != 1 || data == NULL) { return false; } // 相邻点的八个方位 const int direct[8][2] = { {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1,-1} }; // 处理中的过程图 const int temHeight = src->height + 2; const int temWidth = src->width + 2; const int temWidthStep = src->widthStep + 2; const int temChannels = src->nChannels; // 通道数 int *imgTem = new int[temHeight * temWidthStep]; // 图像大小往外扩展一个单位像素 int row = 0, col = 0; imgTem[row * temWidthStep + col] = (int)data[0 * widthStep + 0]; row = 0; col = width + 1; imgTem[row * temWidthStep + col] = (int)data[0 * widthStep + (width-1)]; row = height + 1; col = 0; imgTem[row * temWidthStep + col] = (int)data[(height - 1) * widthStep + 0]; row = height + 1; col = width + 1; imgTem[row * temWidthStep + col] = (int)data[(height - 1) * widthStep + (width-1)]; row = 0; for (col = 1; col < width + 1; col ++) { imgTem[row * temWidthStep + col] = (int)data[0 * widthStep + (col - 1)]; } row = height + 1; for (col = 1; col < width + 1; col ++) { imgTem[row * temWidthStep + col] = (int)data[(height - 1) * widthStep + (col - 1)]; } col = 0; for (row = 1; row < height + 1; row ++) { imgTem[row * temWidthStep + col] = (int)data[(row - 1) * widthStep + 0]; } col = width + 1; for (row = 1; row < height + 1; row ++) { imgTem[row * temWidthStep + col] = (int)data[(row - 1) * widthStep + (width - 1)]; } for (row = 1; row < height + 1; row ++) { for (int col = 1; col < width + 1; col ++) { imgTem[row * temWidthStep + col] = (int)data[(row - 1) * widthStep + (col - 1)]; } } // 求LBP 特征值 for (row = 1; row < height + 1; row ++) { for (col = 1; col < width + 1; col ++) { int bin = 0; // 存放一个8位二进制数 for (int k = 0; k < 8; k ++) { int valueCenterPoint = imgTem[row * temWidthStep + col]; // 中心像素值 int valueDirectPoint = imgTem[ (row + direct[k][0]) * temWidthStep + col + direct[k][1] ]; // 相邻点的像素值 int b = valueCenterPoint > valueDirectPoint ? 0 : 1; bin += b * (int)pow(2, k); // 获得一个8位二进制数 } dst->imageData[(row - 1) * widthStep + (col - 1)] = (char)bin; } } return true; }
原文地址:http://blog.csdn.net/u011504498/article/details/42047403