opencv:求一幅图片的LBP纹理图(c++版本)

感觉还是使用c++版本opencv更加方便,LBP是描述的图片的局部特征,即纹理特征,有关公式和理论网上有很多我就不再重复,直接上代码。

/*
 *
 */

#include "opencv2/opencv.hpp"  
using namespace cv ;
using namespace std ;

Mat LBP(Mat src_image)
{
    bool affiche=true;
    cv::Mat Image(src_image.rows, src_image.cols, CV_8UC1);  //建立一个与src_image等高等宽的单通道图像Image
    cv::Mat lbp(src_image.rows, src_image.cols, CV_8UC1);    //建立一个与src_image等高等宽的单通道图像lbp
 
    if (src_image.channels() == 3)
        cvtColor(src_image, Image, CV_BGR2GRAY);             //LBP只能处理灰度图像,这里如果传过来的是彩色照片,要转化为灰度图
 
    unsigned center = 0;                                     //提取需要计算LBP值得中心点的灰度值
    unsigned center_lbp = 0;                                 //计算center处的LBP值
    //计算LBP图像
    for (int row = 1; row < Image.rows-1; row++)            
    {
        for (int col = 1; col < Image.cols-1; col++)
        {
            center = Image.at<uchar>(row, col);
            center_lbp = 0;
 
            if (center <= Image.at<uchar>(row - 1, col - 1))
                center_lbp += 1;
 
            if (center <= Image.at<uchar>(row - 1, col))
                center_lbp += 2;
 
            if (center <= Image.at<uchar>(row - 1, col + 1))
                center_lbp += 4;
 
            if (center <= Image.at<uchar>(row, col - 1))
                center_lbp += 8;
 
            if (center <= Image.at<uchar>(row, col + 1))
                center_lbp += 16;
 
            if (center <= Image.at<uchar>(row + 1, col - 1))
                center_lbp += 32;
 
            if (center <= Image.at<uchar>(row + 1, col))
                center_lbp += 64;
 
            if (center <= Image.at<uchar>(row + 1, col + 1))
                center_lbp += 128;
            lbp.at<uchar>(row, col) = center_lbp;         //把center处计算好的LBP值存放在lbp图像的相应位置
        }
    }
 if(affiche == true)
            {
              cv::imshow("image LBP", lbp);
              waitKey(10);
              cv::imshow("grayscale",Image);
              waitKey(10);
            }
 
            else
            {
              cv::destroyWindow("image LBP");
              cv::destroyWindow("grayscale");
            }
 
    return lbp;
}
int main()
{
    Mat frame1;
    frame1= imread("2.jpg");
    LBP(frame1);
	cvWaitKey(0);
    return 0;
}
实验结果:


1 原图

2 LBP图

参考内容:http://www.codeproject.com/Questions/863970/LBP-opencv-cplusplus-problem

                     

阅读更多
换一批

没有更多推荐了,返回首页