標籤:影像處理 algorithm opencv lbp
// 得到 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 intdirect[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;}
得到LBP特徵值圖