First time here? Check out the FAQ!

Ask Your Question
0

My Open CV code doesnt do anything when i execute it

asked Jun 2 '13

updated Jun 2 '13

Guanta gravatar image

The below code was written to add salt and peper effect to an image. But it doesnt do anything when i execute it. I executed this code in terminal of ubuntu. It doesnt show any error during compile time or run time. but when i execute , it just doesnt do anything and simply goes to next line of command line.

#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"


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

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

        int i= rand()%image.cols;

        int j= rand()%image.rows;

        if (image.channels() == 1) { 
// gray-level image

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

        } else if (image.channels() == 3) { // 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;


            }

        }
    }

int main() {


    // open the image
    cv::Mat image= cv::imread("boldt.jpg");

    cv::namedWindow("Source");

    cv::imshow("Source",image);// call function to add noise

    salt(image,30000);

    // display image
    cv::namedWindow("Image");

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

    return(0);

    }
Preview: (hide)

1 answer

Sort by » oldest newest most voted
3

answered Jun 2 '13

Jawaher Khemakhem gravatar image

updated Jun 2 '13

berak gravatar image

May be you forget to put waitkey(0);

// display image
cv::namedWindow("Image");
cv::imshow("Image",image);
cv::waitKey(0);
return(0);
Preview: (hide)

Comments

Worked! Thanks a lot :-)

Question Tools

Stats

Asked: Jun 2 '13

Seen: 445 times

Last updated: Jun 02 '13