一起学opencv2(二) 诠释像素+添加salt-and-pepper 噪声
2016-10-26 10:19
330 查看
图像:
图像是以数值矩阵的形式存储,因此我们可以通过cv::mat 数据结构对其进行处理;每个矩阵元素代表一个像素;对于灰度图,像素为 unsigned8-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). |
---|
相关文章推荐
- OpenCV 造轮子(一) salt and pepper噪声函数
- OpenCV给图像添加柏林噪声
- OpenCV 2 学习笔记(5): 像素的访问:给图像加入椒盐噪声
- Python-图像加噪实现(Gaussian noise+salt and pepper noise)
- Opencv 图像添加噪声
- opencv学习笔记(1)----图像读写;添加噪声
- 【OpenCV数字图像处理】(5) 【Python编程】(5) 像素访问之添加椒盐实例 通道分离与合并
- salt and pepper noise
- 图像模拟添加(产生)高斯噪声的OpenCV代码!
- 使用opencv给图像添加高斯噪声
- 学习《OpenCV 2 计算机视觉编程手册》第二章存取像素值案例--椒盐噪声
- opencv 降低图像亮度 + 添加噪声处理
- (原創) 如何對圖片加入Salt and Pepper Noise? (.NET) (C/C++) (C++/CLI) (GDI+) (Image Processing)
- [转]图像去噪的OpenCV添加噪声和去噪
- Episode Eight:opencv2.4.9实现向图像添加噪声和对图像滤波去噪
- 图像添加噪声【OpenCV学习笔记1】
- 用OpenCV的RNG类为图像添加高斯噪声
- Opencv2--像素点操作(加噪声)
- 【OpenCV】给图像添加噪声
- 我的OpenCV学习笔记(三):利用操作像素完成简单的图像处理:加入椒盐噪声、图像翻转、改变对比度、图像锐化