请看下面的代码
#include <iostream>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\core\core.hpp>
using namespace std;
using namespace cv;
Mat PeperAndSalt(Mat *,int);
int main()
{
Mat image,image2;
try
{
image = imread("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg");
if(!image.data)
{
throw 1;
}
}
catch(int a)
{
cout << "Error. Image does not exist" << endl;
exit(0);
}
//Display Normal Image
namedWindow("Normal Image");
imshow("Normal Image",image);
//Edited Image
image2 = PeperAndSalt(&image,3000);
namedWindow("Edited Image");
imshow("Edited Image",image2);
waitKey(0);
return 0;
}
Mat PeperAndSalt(Mat *imagePtr, int numberOfPixels)
{
srand(numberOfPixels);
Mat newMat;
imagePtr->copyTo(newMat);
for(int a=0;a<numberOfPixels;a++)
{
int column = rand()%newMat.cols;
int row = rand()%newMat.rows;
if(newMat.channels()==1)
{
//Grey Image
newMat.at<uchar>(column,row)= 255;
}
else if(newMat.channels()==3)
{
//Colour Image
newMat.at<Vec3b>(column,row)[0]=255;
newMat.at<Vec3b>(column,row)[1]=255;
newMat.at<Vec3b>(column,row)[2]=255;
}
}
return newMat;
}
此代码生成以下错误
Unhandled exception at 0x756a9617 in OpenCV1.exe: Microsoft C++ exception: cv::Exception at memory location 0x003bf2a0..
为什么是这样?请帮忙!
编辑
这是发生在 Mat.hpp 中的错误,它是 OpenCV 的内置类。我在调试时得到了这个
编辑
错误在这一行
image2 = PeperAndSalt(&image,3000);