我想创建积分图像,为此我创建了代码:
Mat integralImage(Mat image) {
//since we will use it more, there is no need to evaluate them each time
int width = image.size().width;
int height = image.size().height;
//empty matrices for calculations
Mat ii = Mat(height, width, DataType<int>::type, 0.0);
Mat s = Mat(height, width, DataType<int>::type, 0.0);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
s.at<Vec3i>(y, x)[0] = ((y - 1 < 0) ? 0 : s.at<Vec3i>(y - 1, x)[0]) + (int)image.at<Vec3b>(y, x)[0];
s.at<Vec3i>(y, x)[1] = ((y - 1 < 0) ? 0 : s.at<Vec3i>(y - 1, x)[1]) + (int)image.at<Vec3b>(y, x)[1];
s.at<Vec3i>(y, x)[2] = ((y - 1 < 0) ? 0 : s.at<Vec3i>(y - 1, x)[2]) + (int)image.at<Vec3b>(y, x)[2];
ii.at<Vec3i>(y, x)[0] = ((x - 1 < 0) ? 0 : ii.at<Vec3i>(y, x - 1)[0]) + s.at<Vec3i>(y, x)[0];
ii.at<Vec3i>(y, x)[1] = ((x - 1 < 0) ? 0 : ii.at<Vec3i>(y, x - 1)[1]) + s.at<Vec3i>(y, x)[1];
ii.at<Vec3i>(y, x)[2] = ((x - 1 < 0) ? 0 : ii.at<Vec3i>(y, x - 1)[2]) + s.at<Vec3i>(y, x)[2];
cout << "(" << ii.at<Vec3i>(y, x)[0] << ", " << ii.at<Vec3i>(y, x)[1] << ", " << ii.at<Vec3i>(y, x)[2] << ") ";
//cout << (int)image.at<Vec3b>(y, x)[0] << " " << (int)image.at<Vec3b>(y, x)[1] << " " << (int)image.at<Vec3b>(y, x)[2] << "; ";
}
cout << endl;
}
return ii;
}
当我尝试在图像中显示像素时,这一切都很好,但是当我尝试显示结构的像素时,它会引发运行时错误,如下所示: Result after crash Memory structure