2016-12-23 135 views
0

当我运行这段代码,我得到A heap has been corrupted为什么堆已损坏?

这里是我的代码:

#include "opencv2\opencv.hpp" 
using namespace cv; 

//This program with take a Video, separate it into it's blue, green and red channels and display them. 
//It will then convert the video into HSV and do the same with it's hue, saturation, and value channels 
int main() { 
    Mat src, hsv, hls; 
    Mat blue, green, red, hue1, saturation1, value, hue2, light, saturation2; 
    bool quit = false; 
    VideoCapture videoCapture(0); 
    //create window 
    cv::namedWindow("SplitImage"); 
    while (!quit) { 
     //read in image 
     videoCapture >> src; 
     //convert image to hsv and hsl 
     cvtColor(src, hsv, CV_BGR2HSV); 
     cvtColor(src, hls, CV_BGR2HLS); 

     //copy src to blue, green, and red matrices 
     src.copyTo(blue); 
     src.copyTo(green); 
     src.copyTo(red); 

     //copy hsv to hue1, saturation1, and value matrices 
     hsv.copyTo(hue1); 
     hsv.copyTo(saturation1); 
     hsv.copyTo(value); 

     //copy hls to hue2, light and saturation2 
     hls.copyTo(hue2); 
     hls.copyTo(light); 
     hls.copyTo(saturation2); 

     //resize windows 
     Size size = Size(200, 200); 
     resize(src, src, size); 
     resize(hsv, hsv, size); 
     resize(hls, hls, size); 
     resize(blue, blue, size); 
     resize(green, green, size); 
     resize(red, red, size); 
     resize(hue1, hue1, size); 
     resize(saturation1, saturation1, size); 
     resize(value, value, size); 
     resize(hue2, hue2, size); 
     resize(light, light, size); 
     resize(saturation2, saturation2, size); 

     //get number of rows, columns, and channnels 
     int nRows = size.height; 
     int channels = src.channels(); 
     int nCols = size.width * channels; 

     //put matrices in array 
     Mat matrices[9] = { blue, green, red, hue1, saturation1, value, hue2, light, saturation2 }; 
     //declare the pointers that will access the matrices' data 
     uchar * p[9]; 
     //for readability I will access the pointer with 
     //these constants instead of numbers 
     const int blueIdx = 0, greenIdx = 1, redIdx = 2, 
      hue1Idx = 3, sat1Idx = 4, valueIdx = 5, 
      hue2Idx = 6, lightIdx = 7, sat2Idx = 8; 
     //make blue matrix blue, green matrix green and red matrix red 
     //and make hue matrix only have hue, saturation matrix only have saturation, and value matrix only have value 
     for (int row = 0; row < nRows; row++) { 
      //each element in p points to the first element in the row of each matrix 
      //for (int i = 0; i < 9; i++) { 
      // p[i] = matrices[i].ptr<uchar>(row); 
      //} 
      for (int col = 0; col < nCols; col += channels) { 
       //std::cout <<"separating at pixel coordinate"<< "(" << col << ", " << row<<")" << std::endl; 
       // remember that pointer + 0 is the blue pixel, pointer + 1 is the green 
       // pixel and pointer + 2 is the red pixel 
       //turn pixel in blue matrix blue 
       /*p[blueIdx][col + 1] = 0; 
       p[blueIdx][col + 2] = 0;*/ 
       blue.data[row*nCols + col*channels + 1] = 0; 
       blue.data[row*nCols + col*channels + 2] = 0; 
       //turn pixel in green matric green 
       //p[greenIdx][col + 0] = 0; 
       //p[greenIdx][col + 2] = 0; 
       green.data[row*nCols + col*channels + 0] = 0; 
       green.data[row*nCols + col*channels + 2] = 0; 
       //turn pixel in red matrix red 
       //p[redIdx][col + 0] = 0; 
       //p[redIdx][col + 1] = 0; 
       red.data[row*nCols + col*channels + 0] = 0; 
       red.data[row*nCols + col*channels + 1] = 0; 

       // remember that pointer + 0 is the hue pixel, pointer + 1 is the saturation 
       // pixel and pointer + 2 is the value pixel 
       //turn pixel in hue matrix hue 
       /*p[hue1Idx][col + 1] = 0; 
       p[hue1Idx][col + 2] = 0;*/ 
       hue1.data[row*nCols + col*channels + 1] = 0; 
       hue1.data[row*nCols + col*channels + 2] = 0; 
       //turn pixel in saturation matric saturation 
       /*p[sat1Idx][col + 0] = 0; 
       p[sat1Idx][col + 2] = 0;*/ 
       saturation1.data[row*nCols + col*channels + 0] = 0; 
       saturation1.data[row*nCols + col*channels + 2] = 0; 
       //turn pixel in value matrix value 
       /*p[valueIdx][col + 0] = 0; 
       p[valueIdx][col + 1] = 0;*/ 
       value.data[row*nCols + col*channels + 0] = 0; 
       value.data[row*nCols + col*channels + 1] = 0; 

       //turn pixel in hue matrix hue 
       /*p[hue2Idx][col + 1] = 0; 
       p[hue2Idx][col + 2] = 0;*/ 
       hue2.data[row*nCols + col*channels + 1] = 0; 
       hue2.data[row*nCols + col*channels + 2] = 0; 
       //turn pixel in saturation matric saturation 
       /*p[lightIdx][col + 0] = 0; 
       p[lightIdx][col + 2] = 0;*/ 
       light.data[row*nCols + col*channels + 0] = 0; 
       light.data[row*nCols + col*channels + 2] = 0; 
       //turn pixel in light matrix value 
       /*p[sat2Idx][col + 0] = 0; 
       p[sat2Idx][col + 1] = 0;*/ 
       saturation2.data[row*nCols + col*channels + 0] = 0; 
       saturation2.data[row*nCols + col*channels + 1] = 0; 


      } 
     } 

     //put indentifying text on each matrix 
     Point textPosition = Point(5, 10); 
     putText(src, "src", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2);//THIS IS LINE 131 WHERE THE ERROR OCCURS 
     putText(hsv, "hsv", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(hls, "hls", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(blue, "blue", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(green, "green", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(red, "red", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(hue1, "hue1", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(saturation1, "saturation", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(value, "value", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(hue2, "hue", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(light, "light", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 
     putText(saturation2, "saturation", textPosition, FONT_HERSHEY_PLAIN, 1, Scalar(255, 255, 255), 2); 

     //concatenate images 
     Mat topRow, middleRow, bottomRow, finalImage; 
     std::vector<Mat> top = { src, blue, green, red }; 
     std::vector<Mat> middle = { hsv, hue1, saturation1, value }; 
     std::vector<Mat> bottom = { hls, hue2, light, saturation2 }; 
     hconcat(top, topRow); 
     hconcat(middle, middleRow); 
     hconcat(bottom, bottomRow); 
     std::vector<Mat> allRows = { topRow, middleRow, bottomRow }; 
     vconcat(allRows, finalImage); 

     //show matrices in window 
     cv::imshow("SplitVideo", finalImage); 

     if(cv::waitKey(30) == 'q') quit = true; 
    } 
} 

代码获取视频从网络摄像头显示的红,绿,蓝色通道,然后将其转换为HSV和显示色调饱和度和数值通道,然后将视频转换为HLS并显示色调光和饱和度通道。这些并排显示在一个窗口中

正如您在代码中所看到的,我已通过使用<Mat object>.ptr<uchar>(row)来获取指向每行的指针而不是使用<Mat object>.data来获取指针的另一种方法到所有的数据。当我使用注释掉的方法时,代码运行时没有错误。我可以使用它,但我想知道为什么<Mat object>.data方法不起作用。

我相信,<Mat object>.data线是什么导致错误,即使这些线上没有发生错误,因为我已经在另一个应用程序也得到了相同的错误,也使用相同的<Mat object>.data代码,我得到同样的错误。 (我发布的是两个应用程序中较为简单的)

+2

阵列操作的像'blue.data数[行* NCOLS + COL *通道+ 1] = 0'是写过去的阵列的端部的主要机会。你的代码有一堆。此示例假设'row * nCols + col * channels + 1'小于数组中元素的数量(因为数组索引是基于零的),否则将访问数组的末尾。 – Peter

+0

根据AddressSanitizer,你至少在这一行有堆缓冲区溢出: 'blue.data [row * nCols + col * channels + 1] = 0;',并且这个错误会在下一个类似的行中重复。 –

+0

这是一个相当差(和容易出错)的实现,尤其是for循环逐元元素18个矩阵。 ['split'](http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#split)s和['merge'] [http://docs.opencv.org/ 2.4/modules/core/doc/operations_on_arrays.html#merge)以及一整块零应该以更容易遵循的方式完成工作。甚至可能是['mixChannels'](http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#mixchannels)? –

回答

2

您不应该乘col *通道。您已经通过循环中的多个渠道推进它。所以你应该写例如

blue.data[row*nCols + col + 1] = 0;