检查灰度图像的每个像素时产生"unhandled exception X at memory location Y"



我试图解决一个问题,即我必须检查640x480 cv::Mat(Greyscale Video Image->cv::Matt MainImageSW(全局((中的每个像素,并检查上面、下面、左边和右边的像素是否等于或低于Thresholdvalue(GetTreshholdSW(((。

我的目标矩阵XY_ ThreshholdMat也是640x480并且被设置为等于Matrix255;255";。如果满足要求;XY_ ThreshholdMat";至";0";在那个地方。目标是使每个像素都满足req。黑人和其他白人。

为此,我编写了以下代码:

void GraphicsView_PP::Fill_XY_TreshholdMat()
{
cv::Mat Matrix255(GetPixmapWidth(), GetPixmapHeight(), CV_8UC1, cv::Scalar(255)); 
XY_ThreshholdMat = Matrix255;

for (int y = 1; y < (GetPixmapHeight()-1); y++)
{
for (int x = 1; x < (GetPixmapWidth()-1); x++)
{
if ((MainImageSW.at<uchar>(y, x)) <= GetTreshholdSW())
{
if ((MainImageSW.at<uchar>((y + 1), x) <= GetTreshholdSW()) &&  
(MainImageSW.at<uchar>((y - 1), x) <= GetTreshholdSW()) &&
(MainImageSW.at<uchar>(y, (x + 1)) <= GetTreshholdSW()) && 
(MainImageSW.at<uchar>(y, (x - 1)) <= GetTreshholdSW()))            
{
XY_ThreshholdMat.at<uchar>(y, x) = 0;  
}
}
}
}
QImage Image((uchar*)XY_ThreshholdMat.data, XY_ThreshholdMat.cols, XY_ThreshholdMat.rows, XY_ThreshholdMat.step, QImage::Format_Grayscale8);
QPixmap Pixmap = QPixmap::fromImage(Image);
ui.graphicsView_Bild->UpdateStream(Pixmap);
cv::waitKey(5000); 
}

因为我正在检查上面、下面、左边和右边的像素,所以我在X/Y=1处开始循环,并让它结束于(MaxWidth-1(和(MaxHeight-1(。

在检查了每个单独的像素之后,我想要保存新的Mat;0";而其他一切都是";255〃;并将其显示在我的自定义QGraphicsView元素中。

一旦我运行代码;在存储器位置Y"处的未处理的异常X;错误,但我不知道为什么。。

此外:我对编程还很陌生,我知道代码的设计方式并不是最有效的方式。重要的是它有效。。

有人知道如何解决这个问题吗?

cv::Mat Matrix255(GetPixmapWidth(), GetPixmapHeight(), ...

应该是

cv::Mat Matrix255(GetPixmapHeight(), GetPixmapWidth(), ...

由于这个错误,您访问的元素超出了界限,因此您的程序具有未定义的行为。

相关内容

最新更新