您的位置:首页 > 运维架构

一起学opencv2(二) 诠释像素+添加salt-and-pepper 噪声

2016-10-26 10:19 330 查看

图像:

图像是以数值矩阵的形式存储,因此我们可以通过cv::mat 数据结构对其进行处理;每个矩阵元素代表一个像素;对于灰度图,像素为 unsigned
8-bit 
类型,0代表白 ,255代表黑;对于彩色图由三个色彩频道{Red, Green, Blue} 组成,每个像素元素由这三个值构成;
像素类型:CV_8U ,CV_8UC3 ,CV_16SC3,CV_32F 。

添加salt-and-pepper noise 噪声:

salt-and-pepper noise是指图像中某些像素点被白色或黑色像素替换所致,通常是由于在数据通信时候某些像素点丢失造成的。

程序:

/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 2 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011.

This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.

Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

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

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 (image.channels() == 1) { // gray-level image

image.at<uchar>(j,i)= 255;
//彩色图彩色图想素有红绿蓝三个参数构成,因此关联向量为三个8-bit值,向量格式为cv::Vec3b,即表示一个3 unsigned chars的向量
} 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()
{
srand(cv::getTickCount()); // init random number generator

cv::Mat image= cv::imread("boldt.jpg",-1);
/*说明
/* 8bit, color or not
CV_LOAD_IMAGE_UNCHANGED  =-1,
/* 8bit, gray
CV_LOAD_IMAGE_GRAYSCALE  =0,
/* ?, color
CV_LOAD_IMAGE_COLOR      =1,
/* any depth, ?
CV_LOAD_IMAGE_ANYDEPTH   =2,
/* ?, any color
CV_LOAD_IMAGE_ANYCOLOR   =4
*/
salt(image,3000);   //尝试3000个随机的salt-and-pepper noise 像素点

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

cv::imwrite("salted.bmp",image);

cv::waitKey(5000);

return 0;
}

为了迎合大众口味,图像素材不在采用老虎图片,而采用beautiful girl picture



参数说明:本人太懒了,翻译工作就留给以后吧

typedef Vec<uchar,3>Vec3b; 
cv::Vec<T,N>

T:type,N:number of vector elements
Mat imread( const string& filename, int flags=1 );

Parameters:filename – Name of file to be loaded.
flags –
Flags specifying the color type of a loaded image:
CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one

>0 Return a 3-channel color image.

Note
 

In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.

=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).

image.at<cv::Vec3b>(j,i)[channel]= value
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: