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
代碼,我得到同樣的錯誤。 (我發佈的是兩個應用程序中較爲簡單的)
陣列操作的像'blue.data數[行* NCOLS + COL *通道+ 1] = 0'是寫過去的陣列的端部的主要機會。你的代碼有一堆。此示例假設'row * nCols + col * channels + 1'小於數組中元素的數量(因爲數組索引是基於零的),否則將訪問數組的末尾。 – Peter
根據AddressSanitizer,你至少在這一行有堆緩衝區溢出: 'blue.data [row * nCols + col * channels + 1] = 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)? –