内存位置未处理的异常

时间:2013-12-19 09:51:44

标签: c++ image visual-studio-2010 opencv matrix

我是OpenCV的新手,我正在尝试编写一个简单的代码来获取图像中块大小的平均值。我编写了以下代码,构建是可以的,但是,调试在内存位置给了我一个未处理的异常。此异常位于以下行:

mean_img.at<double>(i/block_size, j/block_size) = mean_img.at<double>(i/block_size,j/block_size) + new_img.at<double>(i + x, j + y) / (mean);

所以,如果有人给我一些提示,我将不胜感激。在此先感谢,这是完整的代码:

    #include "opencv2/highgui/highgui.hpp" // Include Libs for OpenCV and Image Processing
#include <opencv2/opencv.hpp> // check that
#include "opencv2/core/core.hpp" // check that 
#include <iostream> // Include Libs for C++
#include "opencv2/imgproc/imgproc.hpp" // Include Libs for OpenCV and Image Processing
#include <math.h>

using namespace cv; // namespace parameters not important in OpenCV2.4.6 
using namespace std; // namespace parameters not important in OpenCV2.4.6 

int main( int argc, const char** argv )
{
     /*This part is to compute the parameters(block size, resize parameter) of the new_img*/
     int resize_parameter; // resize parameter must be multiplication of 2
     resize_parameter = 500;
     int block_size; // block parameter must be divisable by of block size
     block_size = 50;
     if ((resize_parameter % 2) != 0)  resize_parameter = resize_parameter - (resize_parameter % 2); 
     while ((resize_parameter % block_size) != 0) block_size = block_size - 1;
     int mean_size = resize_parameter/block_size; // this is the size of the mean matrix
     int mean = block_size * block_size; // this no is ti get the mean of every element in the matrix
     //int mean_img [mean_size][mean_size] = {}; // the mean image matrix initialized by zero 
     /*This part is to allocate the array with dynamic size*/
     //int** mean_img = new int*[mean_size];
     //for(int x = 0; x < mean_size; x++)
     //mean_img[x] = new int[mean_size];
     /*Then we can use the array*/
     /*This part is to fill all the elements of the mean matrix with zeros*/
     //memset(mean_img, 0, sizeof(mean_img[0][0]) * mean_size * mean_size);

     /*This part is the definition of the matrices that are used for the images*/
     Mat mean_img = Mat(mean_size,mean_size,CV_64FC4, cv::Scalar(0));   // define a new matrix with meansize*meansize elements to compute the mean
     Mat mean_img_full = Mat(resize_parameter,resize_parameter,CV_64FC4, cv::Scalar(0));   // define a new matrix with resizeparameter*resizeparameter elements to compute the mean
     Mat new_img = Mat(resize_parameter,resize_parameter,CV_64FC4);  // define a new matrix with resize_parameter*resize_parameter elements 
     Mat original_img = imread("Desert.JPG", CV_LOAD_IMAGE_GRAYSCALE); //define a new matrix and read the image data in the file "Desert.JPG" and store it in 'original_img'
     // notes: the location of the image must be in the same directory of the C++ file 

     if (original_img.empty()) //check whether the image is loaded or not
     {
          cout << "Error : Image cannot be loaded..!!" << endl;
          //system("pause"); //wait for a key press
          return -1;
     }

     // explicitly specify dsize=dst.size(); fx and fy will be computed from that.
     // resize( src matrix, dst matrix, dst.size to get the size of the dst matrix, 0, 0 "to deal with the dst matrix size, may be 0.5 or any fraction from the src size, "AREA,CUBIC,LINEAR")
     resize(original_img, new_img, new_img.size(), 0, 0, CV_INTER_AREA);

     /*This part is to compute the mean of each block*/
     for ( int i = 0; i < resize_parameter; i = i + block_size) // i represents the index of the raw
     {
         for ( int j = 0; j  < resize_parameter; j = j + block_size) // for the blocks in the same raw with different columns
         {
             for ( int x = 0; x < block_size; x++) // x represents the index of the raw
             {
                 for ( int y = 0; y < block_size; y++) // y represents the index of the column
                 {
                     //cout << i ; //cout << "\n"; //cout << j ; //cout << "\n"; //cout << x ; //cout << "\n"; //cout << y ; //cout << "\n";
                     mean_img.at<double>(i/block_size, j/block_size) = mean_img.at<double>(i/block_size,j/block_size) + new_img.at<double>(i + x, j + y) / (mean);

                 }
             } 
         }
     }
     /*This is the end of the part to compute the mean of each block*/
     /*This part is to fill all the resize matrix with the mean value*/
     for ( int x = 0; x < resize_parameter/block_size; x++) // x represents the index of the raw in the mean matrix
     {
         for ( int y = 0; y < resize_parameter/block_size; y++) // y represents the index of the column in the mean matrix
         {
             for ( int i = 0; i < block_size; i++) // i represents the index of the raw in the mean_full matrix
             {
                 for ( int j = 0; j < block_size; j++) // j represents the index of the column in the mean_full matrix
                 {
                     mean_img_full.at<double>((x*block_size)+i,(y*block_size)+j) = mean_img.at<double>(x,y);
                 }
             }
         }
     }
     //cout << cv::getBuildInformation() << endl;

     /*This is the end of the part to fill all the resize matrix with the mean value*/

     namedWindow("OriginalImage", CV_WINDOW_AUTOSIZE); //create a window with the name "OriginalImage"
     imshow("OriginalImage", original_img); //display the image which is stored in the 'original_img' in the "OriginalImage" window
     namedWindow("NewImage", CV_WINDOW_AUTOSIZE); //create a window with the name "NewImage"
     imshow("NewImage", new_img); //display the image which is stored in the 'new_img' in the "NewImage" window
     namedWindow("MeanImage", CV_WINDOW_AUTOSIZE); //create a window with the name "MeanImage"
     imshow("MeanImage", mean_img); //display the image which is stored in the 'mean_img' in the "MeanImage" window
     namedWindow("MeanFullImage", CV_WINDOW_AUTOSIZE); //create a window with the name "MeanFullImage"
     imshow("MeanFullImage", mean_img_full); //display the image which is stored in the 'mean_img_full' in the "MeanFullImage" window

     waitKey(0); //wait infinite time for a keypress
     destroyWindow("OriginalImage"); //destroy the window with the name, "OriginalImage"
     destroyWindow("NewImage"); //destroy the window with the name, "NewImage"
     destroyWindow("MeanImage"); //destroy the window with the name, "MeanImage"
     destroyWindow("MeanFullImage"); //destroy the window with the name, "MeanImage"
     return 0;
}

1 个答案:

答案 0 :(得分:0)

问题出在每个矩阵类型的定义上。它必须是8位无符号字符。它现在正在运作。非常感谢,,,