Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i want to calculate LBP for an image.
i use this code:
C++
cv::Mat LBP(string src_image)
{
	cv::Mat temp_image = imread(src_image.c_str(), 1);
	cv::Mat Image(temp_image.rows, temp_image.cols, CV_8UC1);
	cv::Mat lbp(temp_image.rows, temp_image.cols, CV_8UC1);

	if (temp_image.channels() == 3)
		cvtColor(temp_image, Image, CV_BGR2GRAY);

	imshow("src_image", Image);

	int center = 0;
	int center_lbp = 0;

	for (int row = 1; row < Image.rows; row++)
	{
		for (int col = 1; col < Image.cols; col++)
		{
			center = Image.at<int>(row, col);
			center_lbp = 0;

			if (center <= Image.at<int>(row - 1, col - 1))
				center_lbp += 1;

			if (center <= Image.at<int>(row - 1, col))
				center_lbp += 2;

			if (center <= Image.at<int>(row - 1, col + 1))
				center_lbp += 4;

			if (center <= Image.at<int>(row, col - 1))
				center_lbp += 8;

			if (center <= Image.at<int>(row, col + 1))
				center_lbp += 16;

			if (center <= Image.at<int>(row + 1, col - 1))
				center_lbp += 32;

			if (center <= Image.at<int>(row + 1, col))
				center_lbp += 64;

			if (center <= Image.at<int>(row + 1, col + 1))
				center_lbp += 128;

			cout << "center lbp value: " << center_lbp << endl;
			lbp.at<int>(row, col) = center_lbp;
		}
	}

	imshow("lbp_image", lbp);
	waitKey(0);
	destroyAllWindows();

	return lbp;
}


but it throw an exception "unable to read memory" at this instruction
C++
center = Image.at<int>(row, col);

and this is image for locals when i debug the program
https://dl.dropboxusercontent.com/u/89320576/Capture.PNG[^]

what is the problem??
Posted
Comments
Richard MacCutchan 11-Jan-15 6:37am    
What are the values of row and col when the exception is thrown?
Esraa Saady 11-Jan-15 6:53am    
128*64
Richard MacCutchan 11-Jan-15 6:57am    
Well, since the number of rows is 128, and the number of columns is 64, that would seem to be illegal index values. Although I cannot see how this would happen in those for loops.
Esraa Saady 11-Jan-15 6:59am    
what should i do to fix this error?
Richard MacCutchan 12-Jan-15 3:44am    
The only thing you can do is to step through the code with your debugger to try and discover how those for loop indices are getting invalid values.

In some of your expressions you try to access pixels with coordinates row+1 and col+1. That is outside the permitted range, as your row and col variables run to Image.rows - 1 respectively Image.cols - 1. The +1 just gets yourself one over the edge.

To fix it, just reduce the loops to
C++
for (int row = 1; row < Image.rows-1; row++)
{
    for (int col = 1; col < Image.cols-1; col++)
 
Share this answer
 
XML
Mat LBP(Mat src_image)
{
    bool affiche=true;
    cv::Mat Image(src_image.rows, src_image.cols, CV_8UC1);
    cv::Mat lbp(src_image.rows, src_image.cols, CV_8UC1);

    if (src_image.channels() == 3)
        cvtColor(src_image, Image, CV_BGR2GRAY);

    unsigned center = 0;
    unsigned center_lbp = 0;

    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;
        }
    }
 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("path image")
       LBP(frame1);
    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900