Introduce the three basic functions of using OpenCV and the type analysis and processing of noise.

Summarize the basics of image processing under learning.

This is the first, a brief introduction to the three basic functions of using OpenCV:

• Image reading • Image display • Access to image pixel values

Then outline the type of image noise and add two common noises to the image: Gaussian noise and salt and pepper noise. Finally, median filtering and averaging filtering are used to process the image with noise.

OpenCV basics

In OpenCV, to complete the image input and output and display, only the following functions are needed:

NamedWindow

Create a window that can be referenced by its name. The first parameter, set the name of the window, can refer to the window by name; the second parameter sets the size of the window. There are several options:

• WINDOW_NORMAL or WINDOW_AUTOSIZE adjusts the size of the window to fit the image. The difference is that WINDOW_NORMAL can be used to manually resize the window; WINDOW_AUTOSIZE cannot resize the window. • WINDOW_FREERATIO or WINDOW_KEEPRATIO Whether to keep the image's ratio unchanged when changing the window, did not find any difference between the two.

Imshow

Display image

Imread

Read image data into Mat, the first parameter is the file name of the image; the second parameter is the flag, which identifies how to handle the color of the image. Several commonly used options:

• IMREAD_UNCHANGED and the original image remain the same • IMREAD_GRAYSCALE Converts the image to a single-channel grayscale image • IMREAD_COLOR Converts the image to a 3-channel BGR, default option • IMREAD_REDUCED_GRAYSCALE_2 IMREAD_REDUCED_GRAYSCALE_4 IMREAD_REDUCED_GRAYSCALE_8 Single-channel grayscale image is read into the image and reduced The size of the image. The reduced value is 1/2, 1/4, 1/8• IMREAD_REDUCED_COLOR_2 IMREAD_REDUCED_COLOR_4 IMREAD_REDUCED_COLOR_2 The 3-channel BGR reads in the image and reduces the size of the image. The reduced value is 1/2, 1/4, 1/8

4. Mat

Mat is the most important data structure in OpenCV, and it is basically the operation of the structure when doing image processing.

Mat consists of two parts: matrix header and matrix data. The matrix header is small. Each Mat instance created has a matrix header. Matrix data usually occupies a large space. OpenCV manages this part of memory space by reference counting. When calling the assignment operator and the copy constructor, it does not copy only the matrix header, and does not copy the matrix data, just incrementing its reference count by one. E.g:

The a, b, and m in the above code each have their own matrix header, and the data they refer to point to the same copy. In other words, modifying any of them will affect the other two.

To copy matrix data, you can call the functions clone and copyTo

After reading the image into the Mat, there are three ways to access the data in the Mat:

• By pointer • Using an iterator • Calling at

Image noise

Image noise is the interference of random signals in the process of acquisition or transmission. Some random, discrete and isolated pixel points appear on the image, which will interfere with the analysis of image information by the human eye. The noise of an image is usually complicated, and it is often regarded as a multidimensional stochastic process, so noise can be described by means of a random process, that is, a probability distribution function and a probability density function are used.

The image has a lot of noise and its nature is very different. The noise can be classified by different methods.

According to the reasons:

• External noise • Internal noise

This classification method helps to understand the source of noise generation, but it can only serve as a principle for the noise reduction algorithm.

The relationship between noise and image signals can be divided into:

• Additive noise, additive noise and image signal strength are not correlated. This type of noise can look at the sum of the ideal noise-free image f and noise. • Multiplicative noise, multiplicative noise and image signals are correlated and often vary with image signal changes

For the convenience of analysis and processing, the multiplicative noise is often considered to be additive noise, and it is always assumed that the signal and noise are independent of each other.

The most important thing is to classify by probability density function (PDF):

• Gaussian noise, Gaussian noise models are often used in practice. • Impulse noise (salt and salt noise), a point on the image, also known as shot and spike noise. • Gamma noise • Rayleigh noise • Exponentially distributed noise • Evenly distributed noise

This classification method introduces a mathematical model and is useful for designing filtering algorithms.

Add noise to the image

According to the specified noise type, generate a random number, then add the random number to the source pixel value, and put the obtained value into the [0, 255] interval.

C++11 random number generator

The new random number generator is abstracted into two parts: the distribution of the random number generation engine and the random number to be generated.

There are three types of random number engines:

• linear_congruential_engine linear congruence algorithm • mersenne_twister_engine Mason rotation algorithm • subtract_with_carry_engine linear congruence algorithm with carry

The first one is the most commonly used, and the speed is faster; the second one is called the best pseudo-random number generator.

Add image noise

Add two noises to the image using C++'s random number generator: salt and pepper noise and Gaussian noise. Salt and pepper noise is a discretely distributed white or black point in the image. The code is as follows:

In the above code, ptr() is used to obtain the first pointer of a line of the image, and after obtaining the first pointer of the line, the pixel value of the changed line can be arbitrarily accessed.

Gaussian noise is an additive noise. The code to add Gaussian noise to an image is as follows:

A random number that conforms to the Gaussian distribution is randomly generated, and then the value is added to the original pixel value of the image, and the obtained sum is compressed into the interval [0, 255].

Introduce the three basic functions of using OpenCV and the type analysis and processing of noise

On the left is the original image, in the middle is the image after adding Gaussian noise, and on the far right is the image after adding salt and pepper noise.

Use filters to remove noise

Depending on the type of noise, choose a different filter to filter out the noise. Generally, for the salt and pepper noise, select the Median Filter to remove the noise without blurring the image. For Gaussian noise, choose the Mean Filter to remove the noise, but it will cause a certain image. Blurred.

In OpenCV, the function corresponding to the averaging filter is blur. This function requires 5 parameters. Usually only the first 3 and the last two are used. Blur(m, m2, Size(5, 5)); the first parameter is the input image, the second parameter is the output image, and the third parameter is the size of the filter. Here 5×55× is used. 5 rectangles.

The function corresponding to the median filter is medianBlur(m1, m3, 5); the first two parameters are the input and output images, the third parameter is the size of the filter, since the median value is selected, the size of the filter Usually an odd number.

The figure below shows the result of using a filter on a noisy image. The middle is the original image. The left side is the result of filtering the Gaussian noise using the mean filter. The right side is the result of filtering the salt and pepper noise using the median filter. It can be clearly seen that both filters can remove the noise of the image well, but it will cause some blurring of the image, especially the blur caused by the mean filter is obvious.

Introduce the three basic functions of using OpenCV and the type analysis and processing of noise

to sum up

This article is the first article, a brief introduction to the basic use of OpenCV; then access the pixels in the image, and with the help of C + + 11 random number library, add Gaussian noise and salt and pepper noise to the image; finally use the median filter And the averaging filter removes the image and compares the results.

Trolley Speaker

Trolley Speaker,Bluetooth Trolley Speaker,Portable Speaker Trolley,Trolly Speaker With Mic

GUANGZHOU SOWANGNY ELECTRONIC CO.,LTD , https://www.jerry-power.com

Posted on