我想将0像素值与Mask图像中具有0像素值的像素位置等同于grayimg12图像中的相同位置,即灰色图像。当我把for循环放在try-catch块中时,它给了我错误并且断言失败,没有使用try-catch错误是" 0x755b0f22处的未处理异常和内存位置0x004af338处的cv :: Exception ..我正在使用opencv 3.0.0 beta版和Visual Studio 2010.
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;
int main()
{
// Reading Mask and Creating New Image
Mat grayimg, grayimg12, input, Mask; int keyboard;
input = imread("peter.jpg");
cvtColor(input, grayimg, COLOR_BGR2GRAY);
grayimg.copyTo(grayimg12, grayimg);
namedWindow("Gray Converted Frame");
imshow("Gray Converted Frame", grayimg);
int r = input.rows; int c = input.cols;
Mask = grayimg > 100;
namedWindow("Binary Image");
imshow("Binary Image", Mask);
try
{
for (int i=1;i<=r;i++)
{
for (int j=1;j<=c; j++)
{
if (Mask.at<uchar>(i,j) == 0)
{
grayimg12.at<uchar>(i,j) = 0;
}
else
grayimg12.at<uchar>(i,j) = grayimg.at<uchar>(i,j);
}
}
}
catch(Exception)
{
cout<<"Hi..";
}
namedWindow("Gray Output Image");
imshow("Gray Output Image", grayimg12);
keyboard = waitKey( 10000 );
return 0;
}
答案 0 :(得分:0)
您的循环索引关闭一个,因此当您尝试访问超出图像边界的内存时会出现异常。变化:
for (int i=1;i<=r;i++)
{
for (int j=1;j<=c; j++)
{
为:
for (int i=0;i<r;i++) // for i = 0 to r-1
{
for (int j=0;j<c; j++) // for j = 0 to c-1
{
请注意,在C,C ++和相关语言中,数组是从零开始的。因此,大小为N
的数组的有效索引范围为0
到N-1
。