OpenCV2: Chapter VII image processing

I. Introduction

Grayscale (grayscale image), the white to shades of gray between black into 256

 

FIG three color components of the RGB, assuming 800 * 800 FIG pixel, there are three 800 * 800 matrix representing RGB

 

 Binarization process a set threshold value, the pixel value becomes a threshold value (white), the pixel value is outside the threshold value becomes 0 (black)

 

II. Grayscale

1. Implement

cv :: Mat srcImage; 
cv :: Mat srcGray; 
cv :: cvtColor (srcImage, srcGray, CV_RGB2GRAY);

 

2. Principle

(1) floating-point arithmetic

Gray=R*0.3+G*0.59+B*0.11

(2) Method integer

Gray=(R*30+G*59+B*11)/100

(3) shifting method

 Gray=(R*76+G*151+B*28)>>8;

(4) average method

Gray=(R+G+B)/3

(5) take only the green

 Gray=G

 

III. Binarized

cv::Mat imgGray;
cv::Mat result;

cv::threshold(imgGray,result,100,255,CV_THRESH_BINARY);

 

IV. Join noise

void salt(cv::Mat image,int n){
 
	int i,j;
 
	for(int k=0;k<n;k++){
		i=std::rand()%image.cols;  //行数
		j=std::rand()%image.rows;  //列数
 
		if(image.type()==CV_8UC1){
			//灰度图像
			image.at<uchar>(j,i)=255;
		}
		else if(image.type()==CV_8UC3){
			//彩色图像
			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()
{
	
	cv::Mat imag1=cv::imread("a.jpg");
	
	salt(imag1,3000);
 
	cv::namedWindow("Image");
	cv::imshow("Image",imag1);
 
 
	cv::waitKey(0);
 
	system("pause");
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/k5bg/p/11078142.html