我正在尝试对图像应用滤镜,滤镜窗口(3x3)的输出存储在矢量中(只有非零值丢弃零值)。我想添加vector的前四个值并将答案存储在输出图像中。运行程序会生成错误"向量下标超出范围"。不知道我在做错的地方。
Mat Diff;//input image
int window[9];
Mat road;//output image
road = Diff.clone();
for (int y = 0; y < Diff.rows; y++)
for (int x = 0; x < Diff.cols; x++)
road.at<uchar>(y, x) = 0;
//Apply road filter
for (int y = 1; y < Diff.rows - 1; y++){
for (int x = 1; x < Diff.cols - 1; x++){
// Pick up window element
window[0] = abs(Diff.at<uchar>(y - 1, x - 1) - Diff.at<uchar>(y, x));
window[1] = abs(Diff.at<uchar>(y, x - 1) - Diff.at<uchar>(y, x));
window[2] = abs(Diff.at<uchar>(y + 1, x - 1) - Diff.at<uchar>(y, x));
window[3] = abs(Diff.at<uchar>(y - 1, x) - Diff.at<uchar>(y, x));
window[4] = abs(Diff.at<uchar>(y, x) - Diff.at<uchar>(y, x));
window[5] = abs(Diff.at<uchar>(y + 1, x) - Diff.at<uchar>(y, x));
window[6] = abs(Diff.at<uchar>(y - 1, x + 1) - Diff.at<uchar>(y, x));
window[7] = abs(Diff.at<uchar>(y, x + 1) - Diff.at<uchar>(y, x));
window[8] = abs(Diff.at<uchar>(y + 1, x + 1) - Diff.at<uchar>(y, x));
int road_detector = 0;
vector<int>nonzero_elements;
for (int n = 0; n < 9;n++){
if (window[n]>0){
nonzero_elements.push_back(window[n]);
// select the first non-zero four values of window and sum them
//road.at<uchar>(y, x) = window[0]+window[1]+window[2]+window[3];
if (nonzero_elements.size() <4){
road_detector += nonzero_elements[n];
}
else{
// sort the window
sort(nonzero_elements.begin(), nonzero_elements.end());
road_detector = nonzero_elements.at(0) + nonzero_elements.at(1) + nonzero_elements.at(2) + nonzero_elements.at(3);
}
}
}road.at<uchar>(y, x) = road_detector;
nonzero_elements.clear();
}
}
答案 0 :(得分:0)
假设您在FirebaseObjectObservable
中有所有负值,除了存储在最后一个索引中的值。现在,您将仅推送window[]
中的一个元素,因此其大小为1.但您正在访问nonzero_elements
的第8个元素,因此超出范围。