• OpenCV中遇到Microsoft C++ 异常 cv::Exception


    我在实现《OpenCV2计算机视觉编程手册》第2章 2.2 节 存取像素值 中的椒盐噪声例子中遇到的程序错误。

    原始输入程序:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv.hpp>
    //2.2节中的程序,但是出现问题
    using namespace cv;
    void salt(Mat & , int );
    int main(){
    	Mat image = imread("C:\Baboon.jpg");
    	salt(image,2000);
    	namedWindow("Miffy");
    	imshow("Miffy",image);
    	waitKey(0);
    	return 1;
    }
    void salt(Mat &image, int n){
    	for (int k = 0; k < n; k++){
    		int i=rand();
    		int j=rand();
    		if (image.channels()==1){
    			image.at<uchar>(j,i)=255;
    		}else if (image.channels()==3){
    			image.at<Vec3b>(j,i)[0]=255;
    			image.at<Vec3b>(j,i)[1]=255;
    			image.at<Vec3b>(j,i)[2]=255;
    		}
    	}
    }
    


    程序出现的问题如下:

    sample_1.exe 中的 0x7534c6e3 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x0012f52c 处的 cv::Exception。


    在找BUG的时候,毫无头绪,在网上去搜索相关的问题,发现这个问题主要出现的是指针的问题。

    自己分析程序,感觉rand()上面可能会出现问题。

    —— rand()函数

    int rand(void);从srand (seed)中指定的seed开始,返回一个[seed,RAND_MAX(0x7fff)]间的随机整数。

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv.hpp>
    //修改后程序
    using namespace cv;
    void salt(Mat & , int );
    int main(){
    	Mat image = imread("C:\Baboon.jpg");
    	salt(image,2000);
    	namedWindow("Miffy");
    	imshow("Miffy",image);
    	waitKey(0);
    	return 1;
    }
    void salt(Mat &image, int n){
    	int im_rows=image.rows;
    	int im_cols=image.cols;
    	for (int k = 0; k < n; k++){
    		int i=rand()%im_cols;
    		int j=rand()%im_rows;
    		if (image.channels()==1){
    			image.at<uchar>(j,i)=255;
    		}else if (image.channels()==3){
    			image.at<Vec3b>(j,i)[0]=255;
    			image.at<Vec3b>(j,i)[1]=255;
    			image.at<Vec3b>(j,i)[2]=255;
    		}
    	}
    }
    



          



  • 相关阅读:
    CF1174D Ehab and the Expected XOR Problem
    CF1083B The Fair Nut and Strings
    CF1088D Ehab and another another xor problem
    CF1168A Increasing by Modulo
    CF1166C A Tale of Two Lands
    CF1142A The Beatles
    CF1105D Kilani and the Game
    【uva11248】网络扩容
    【sam复习】用sam实现后缀排序
    【Educational Codeforces Round 19】
  • 原文地址:https://www.cnblogs.com/pangblog/p/3313248.html
Copyright © 2020-2023  润新知