complie file but error during running

asked 2014-11-14 01:15:22 -0500

DGambhir gravatar image

include <opencv2 core="" core.hpp="">

include <opencv2 highgui="" highgui.hpp="">

include <opencv2 imgproc="" imgproc.hpp="">

include <stdlib.h> // for std::rand

void salt(cv::Mat image, int n) {

int i,j;
for (int k=0; k<n; k++) {

    // rand() is the random number generator
    i= std::rand()%image.cols;
    j= std::rand()%image.rows;


    if (image.type() == CV_8UC1) { // gray-level image

        image.at<uchar>(j,i)= 255; 

    } else if (image.type() == CV_8UC3) { // color image

        image.at<cv::Vec3b>(j,i)[0]= 255; 
        image.at<cv::Vec3b>(j,i)[1]= 255; 
        image.at<cv::Vec3b>(j,i)[2]= 255; 
    }
}

}

// This is an extra version of the function // to illustrate the use of cv::Mat_ // works only for a 1-channel image void salt2(cv::Mat image, int n) {

// use image with a Mat_ template
cv::Mat_<uchar> im2(image);

// or with references: // cv::Mat_<uchar>& im2= reinterpret_cast<cv::mat_<uchar>&>(image);

int i,j;
for (int k=0; k<n; k++) {

    // rand() is the MFC random number generator
    i= rand()%image.cols;
    j= rand()%image.rows;


    if (im2.type() == CV_8UC1) { // gray-level image

        im2(j,i)= 255; 

    } 
}

}

int main() { // open the image cv::Mat image= cv::imread("lena512color.tiff",1); // image is resized for book printing cv::resize(image, image, cv::Size(), 0.6, 0.6);

// call function to add noise
salt(image,3000);

// display result
cv::namedWindow("Image");
cv::imshow("Image",image);

// write on disk
cv::imwrite("salted.bmp",image);

cv::waitKey();

// test second version
image= cv::imread("lena512color.tiff",0);
// image is resized for book printing
cv::resize(image, image, cv::Size(), 0.6, 0.6);

salt2(image, 500);

cv::namedWindow("Image");
cv::imshow("Image",image);

cv::waitKey();

return 0;

}

The code is compiling fine but when I run it provides me following error

First-chance exception at 0x751a2f71 in addNoise_img.exe: Microsoft C++ exception: cv::Exception at memory location 0x0058b4d8.. Unhandled exception at 0x751a2f71 in addNoise_img.exe: Microsoft C++ exception: cv::Exception at memory location 0x0058b4d8.. The program '[1448] addNoise_img.exe: Native' has exited with code 0 (0x0).

edit retag flag offensive close merge delete

Comments

Hi , try to change salt(cv::Mat image, int n) to salt(cv::Mat &image, int n)

wuling gravatar imagewuling ( 2014-11-14 06:28:24 -0500 )edit