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

本文介绍如何使用OpenCV在图像上添加椒盐噪声。通过随机选择像素并将其设置为白色或黑色,模拟数据通信中常见的图像失真现象。代码示例展示了如何实现这一过程,并提供了详细的参数解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

图像:

图像是以数值矩阵的形式存储,因此我们可以通过 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值