C++ 为什么堆已损坏?

C++ 为什么堆已损坏?,c++,opencv,C++,Opencv,当我运行此代码时,我得到一个堆已损坏 这是我的密码: #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

当我运行此代码时,我得到一个堆已损坏

这是我的密码:

#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;
    }
}
#包括“opencv2\opencv.hpp”
使用名称空间cv;
//这个程序需要拍摄一段视频,将其分为蓝色、绿色和红色通道,并显示它们。
//然后,它将视频转换为HSV,并对其色调、饱和度和值通道执行相同的操作
int main(){
Mat src、hsv、hls;
Mat蓝、绿、红、hue1、饱和1、值、hue2、光、饱和2;
bool-quit=false;
视频捕获视频捕获(0);
//创建窗口
cv::namedWindow(“拆分图像”);
而(!退出){
//读入图像
视频捕获>>src;
//将图像转换为hsv和hsl
CVT颜色(src、hsv、CV_BGR2HSV);
CVT颜色(src、hls、CV_BGR2HLS);
//将src复制到蓝色、绿色和红色矩阵
src.copyTo(蓝色);
src.copyTo(绿色);
src.copyTo(红色);
//将hsv复制到hue1、饱和1和值矩阵
hsv.copyTo(hue1);
hsv.copyTo(饱和1);
hsv.copyTo(价值);
//将hls复制到hue2、灯光和饱和度2
hls.copyTo(hue2);
hls.copyTo(光);
hls.copyTo(饱和2);
//调整窗口大小
尺寸=尺寸(200200);
调整大小(src,src,size);
调整大小(hsv,hsv,大小);
调整大小(hls、hls、大小);
调整大小(蓝色、蓝色、大小);
调整大小(绿色、绿色、大小);
调整大小(红色、红色、大小);
调整大小(hue1,hue1,大小);
调整大小(饱和1,饱和1,大小);
调整大小(值、值、大小);
调整大小(hue2,hue2,大小);
调整大小(灯光、灯光、大小);
调整大小(饱和2,饱和2,大小);
//获取行、列和通道数
int nRows=尺寸。高度;
int channels=src.channels();
int nCols=size.width*通道;
//将矩阵放入数组
Mat矩阵[9]={蓝、绿、红、hue1、饱和1、值、hue2、光、饱和2};
//声明将访问矩阵数据的指针
uchar*p[9];
//为了便于阅读,我将使用
//这些是常数而不是数字
常数int blueIdx=0,绿色IDX=1,redIdx=2,
hue1Idx=3,sat1Idx=4,valueIdx=5,
hue2Idx=6,lightIdx=7,sat2Idx=8;
//使蓝色矩阵为蓝色,绿色矩阵为绿色,红色矩阵为红色
//使色调矩阵只有色调,饱和度矩阵只有饱和度,值矩阵只有值
对于(int行=0;行//std::cout您不应该将col*通道相乘。您已经在循环中将它向前推进了许多通道。因此您应该编写例如

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

etc

您不应该将col*通道相乘。您已经在循环中按多个通道前进了。因此您应该编写例如

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

etc

blue这样的数组操作的数量。数据[row*nCols+col*channels+1]=0
是写入数组末尾的主要机会。您的代码有一堆这样的操作。本例假设
row*nCols+col*channels+1
小于数组中的元素数量(因为数组索引是基于零的)否则将通过数组末尾进行访问。根据AddressSanitizer,堆缓冲区溢出至少出现在这一行:
blue.data[row*nCols+col*channels+1]=0;
,这个错误将在接下来的类似行中重复。这是一个非常糟糕的错误(而且容易出错)实现,尤其是循环逐元素清零18个矩阵。两个s和s以及一个单独的零点应该以更容易遵循的方式完成这项工作。甚至可能?像
blue.data[row*nCols+col*channels+1]这样的数组操作的数量=0
是写入超过数组末尾的主要机会。您的代码有一堆。此示例假定
行*nCols+col*channels+1
小于数组中的元素数(因为数组索引是从零开始的)否则将通过数组末尾进行访问。根据AddressSanitizer,堆缓冲区溢出至少出现在这一行:
blue.data[row*nCols+col*channels+1]=0;
,这个错误将在接下来的类似行中重复。这是一个非常糟糕的错误(而且容易出错)实现,特别是循环逐元素清零18个矩阵。两个s和s以及一个单独的零点应该以更容易遵循的方式完成这项工作。甚至可能?