The topic of the exercise:

1. Read a picture and decompose RGB three channels

/ * * * * * * * * * * * * exercise 1 * * * * * * * * * * * * * * * * * * * * * * /
int main(a)
{
	Mat img1 = imread("D:\\opencv_picture_test\\miku2.jpg".2|4);			/ / gray
	if (img1.empty())
	{
		printf("Could not find the image! \n");
		return - 1;
	}
	std::vector<cv::Mat> channels;
	cv::split(img1,channels);
	Mat B = channels.at(0);
	Mat G = channels.at(1);
	Mat R = channels.at(2);
	imshow("blue", B);
	imshow("green", G);
	imshow("red", R);
	waitKey(0);
	return 0;
}
Copy the code

2. Call the local camera

/ * * * * * * * * * * * * practice 2 * * * * * * * * * * * * * * * * * * * * * * /
int main(a)
{
	// instantiate while initializing
	// Call the camera
	VideoCapture capture(0);		Int a=1;
	/* Instantiate VideoCapture capture; Open ("D:\\opencv_picture_test\\video1.avi"); * /
	while (1)
	{
		Mat frame;	// Store the image of each frame
		capture >> frame;	// Reads the current frame
		imshow("Original video", frame);	// Displays the current frame
		// Perform corrosion operations
		Mat element = getStructuringElement(MORPH_RECT, Size(15.15));	// Return the kernel matrix
		Mat dstImage;
		erode(frame, dstImage, element);					// Corrosion operation
		imshow("Processed video.", dstImage);	// Displays the current frame
		if(waitKey(10) > =0) break;	/ / delay 10 ms
	}
	return 0;
}
Copy the code

3, OpencV basic drawing function

int main(a)
{
	Mat displayMat = imread("D:\\opencv_picture_test\\RGB solid color picture \\blue.jpg".0);			/ / gray
	if (displayMat.empty())
	{
		printf("Could not find the image! \n");
		return - 1;
	}
	/ / draw circles
	Point pt;		/ / circle
	pt.x = 90;
	pt.y = 90;
	circle(displayMat, pt, 20.CV_RGB(0.255.0), 1.8.0);		// Draw the target image of the circle
	// The line thickness of the circle is -1 to draw the solid circle collar relation and offset. The default values are 8 and 0
	/ / draw line segments
	Point pt1;		/ / starting point
	pt1.x = 100;		
	pt1.y = 100;
	Point pt2;		/ / the end
	pt2.x = 300;
	pt2.y = 300;
	line(displayMat, pt1, pt2, CV_RGB(0.255.0), 10.8.0);
	// The target image starts and ends
	// Line thickness, collar relation and offset, generally set the default values, 8 and 0
	// Draw a rectangle
	Rect rect;		
	rect.x = 20;
	rect.y = 20;
	rect.width = 20;
	rect.height = 20;
	rectangle(displayMat, rect, CV_RGB(0.255.0), 1.8.0);
	// The target image is the target rectangle
	// Line thickness, take -1 to draw solid rectangle collar relation and offset, generally set the default values, 8 and 0
	imshow("Image", displayMat);
	waitKey(0);
	return 0;

}
Copy the code

Running effect

Build a histogram

/ / / * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - draw a d gray histogram exercise 4 】 【 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /
Mat	My_Rraw_histogram(Mat* srcImage)		// Input: grayscale image to process Output: histogram of the image
{
	// [2] Define variables
	MatND dstHist;
	int dims = 1;		// The dimension of the histogram to be calculated
	float grayranges[] = { 0.255 };
	const float* ranges[] = { grayranges };	// This needs to be const
	int size = 256;			// Divide the gray value into equal parts
	int channels = 0;	// Grayscale map has only one 0 channel

	// [3] Calculate image histogram
	calcHist(srcImage,	// Enter an array
		1.// Number of arrays
		&channels,	// Channel index
		Mat(),// Do not use mask
		dstHist,	// Output the target histogram
		dims,	// The dimension of the histogram to be calculated
		&size,	// An array of histogram sizes for each dimension
		ranges);	// The range of values for each dimension
	int scale = 1;		//scale The number of squares per pixel

	Mat dstImage(size * scale, size, CV_8U, Scalar(0));		// Length: size*scale, width: size, value 0

	// [4] Get the maximum and minimum values
	double minVal = 0;
	double maxVal = 0;
	minMaxLoc(dstHist, &minVal, &maxVal, 0.0);		// Get the maximum and minimum values in the histogram

	[5] draw the histogram
	int hpt = saturate_cast<int> (0.9 * size);			//saturate_cast overflow protection: if(data<0) data = 0; else if (data > 255) data = 255;
	for (int i = 0; i <256; i++) {float binVal = dstHist.at<float>(i);
		int realVal = saturate_cast<int>(binVal * hpt / maxVal);		// The height on the image = pixel value/maximum pixel value * 0.9*256
		rectangle(dstImage, Point(i * scale, size - 1), Point((i + 1) * scale - 1, size - realVal), Scalar(255));
		Color (RGB) or brightness (grayscale) of the upper right vertex line on the diagonal of the lower left vertex matrix of the target image rectangle to be drawn 256 rectangles in total
	}
	return dstImage;
}
/ / the main function
int main(a)
{
	// [1] Load the original image
	Mat srcImage = imread("D:\\opencv_picture_test\\ Niigaki Noyi \\test2.jpg".0);			// Grayscale of the original image
	namedWindow("Grayscale", WINDOW_NORMAL);//WINDOW_NORMAL allows users to freely scale Windows
	imshow("Grayscale", srcImage);
	if (srcImage.empty())
	{
		printf("Could not find the image! \n");
		return - 1;
	}
	Mat dstImage = My_Rraw_histogram(&srcImage);
	namedWindow("One-dimensional histogram", WINDOW_NORMAL);//WINDOW_NORMAL allows users to freely scale Windows
	imshow("One-dimensional histogram", dstImage);
	waitKey(0);
	return 0;
}
Copy the code

Running results:





Hey hey, water an article.