Subversion Repositories OpenCV2-Cookbook

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 2 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
5
 
6
   This program is free software; permission is hereby granted to use, copy, modify,
7
   and distribute this source code, or portions thereof, for any purpose, without fee,
8
   subject to the restriction that the copyright notice may not be removed
9
   or altered from any source or altered source distribution.
10
   The software is released on an as-is basis and without any warranties of any kind.
11
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
12
   The author disclaims all warranties with regard to this software, any use,
13
   and any consequent failure, is purely the responsibility of the user.
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
17
 
18
#include <opencv2/core/core.hpp>
19
#include <opencv2/highgui/highgui.hpp>
20
 
21
void salt(cv::Mat &image, int n) {
22
 
23
        int i,j;
24
        for (int k=0; k<n; k++) {
25
 
26
                // rand() is the MFC random number generator
27
                i= rand()%image.cols;
28
                j= rand()%image.rows;
29
 
30
 
31
                if (image.channels() == 1) { // gray-level image
32
 
33
                        image.at<uchar>(j,i)= 255;
34
 
35
                } else if (image.channels() == 3) { // color image
36
 
37
                        image.at<cv::Vec3b>(j,i)[0]= 255;
38
                        image.at<cv::Vec3b>(j,i)[1]= 255;
39
                        image.at<cv::Vec3b>(j,i)[2]= 255;
40
                }
41
        }
42
}
43
 
44
int main()
45
{
46
        srand(cv::getTickCount()); // init random number generator
47
 
48
        cv::Mat image= cv::imread("boldt.jpg",0);
49
 
50
        salt(image,3000);
51
 
52
        cv::namedWindow("Image");
53
        cv::imshow("Image",image);
54
 
55
        cv::imwrite("salted.bmp",image);
56
 
57
        cv::waitKey(5000);
58
 
59
        return 0;
60
}
61
 
62