#include "stdafx.h"
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
 
using namespace std;
using namespace cv;
 
void AddNoise(Mat image, int numPts);                 // 加入雜訊
int _tmain(int argc, _TCHAR* argv[])
{
    char *path = "C:\\OpenCV\\images\\lena.jpg";
    Mat image;
 
    // 載入圖檔
    image = imread(path, CV_LOAD_IMAGE_COLOR);
 
 
    // 檢查讀檔是否成功
    if(! image.data )
    {
        cout <<  "無法開啟或找不到圖檔" << std::endl ;
        return -1;
    }
 
 
 
    namedWindow("original image",  CV_WND_PROP_AUTOSIZE);
    imshow("original image", image);
    //-------------------------------------    
    int numPts = 10000;
    AddNoise(image, numPts);
    namedWindow("image with noise",  CV_WND_PROP_AUTOSIZE);
    imshow("image with noise",  image);
    waitKey(0);
    getchar();
    return 0;
}

image

加入雜訊

void AddNoise(Mat image, int numPts)
{
    int i, row, col;
    if(image.channels() == 3)    // 彩色
    {
        for(i=0; i<numPts; i++)
        {
            row = rand() % image.rows;
            col = rand() % image.cols;
            image.at<cv::Vec3b>(row, col)[0] = 255;
            image.at<cv::Vec3b>(row, col)[1] = 255;
            image.at<cv::Vec3b>(row, col)[2] = 255;
        }
    }else                       // 灰階 
    {
        for(i=0; i<numPts; i++)
        {
            row = rand() % image.rows;
            col = rand() % image.cols;
            image.at<cv::Vec3b>(row, col) = 255;
        }
    }
}

結果

image

下載AccessPixelVS2010OpenCV249

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()