Planning Motivation Control

Preliminary image processing. Basic Image Processing Deep Learning for Image Processing

With all element-by-element transformations, the probability distribution law that describes the image changes. Let us consider the mechanism of this change using an example of an arbitrary transformation with a monotonic characteristic described by a function (Figure 2.8), which has a single-valued inverse function. Suppose the random variable obeys a probability density. Let be an arbitrary small interval of values ​​of a random variable, and - the corresponding interval of the transformed random variable.

A value falling into an interval entails a value falling into an interval, which means the probabilistic equivalence of these two events. Therefore, taking into account the smallness of both intervals, we can write an approximate equality:

,

where the modules take into account the dependence of the probabilities on the absolute lengths of the intervals (and the independence from the signs of the increments and). Calculating from this the probability density of the transformed quantity, substituting instead of its expression through the inverse function and performing the passage to the limit at (and, therefore,), we obtain:

. (2.4)

This expression allows you to calculate the probability density of the transformation product, which, as can be seen from it, does not coincide with the distribution density of the original random variable. It is clear that the performed transformation has a significant effect on the density, since (2.4) includes its inverse function and its derivative.

Relationships become somewhat more complex if the transformation is described by a non-one-to-one function. An example of such a more complex characteristic with ambiguous inverse function the sawtooth characteristic of Fig. 2.4, K. However, in general, the meaning of probabilistic transformations does not change in this case.

All element-by-element transformations of images considered in this chapter can be considered from the point of view of the change in the probability density described by expression (2.4). Obviously, for none of them, the probability density of the output product will not coincide with the probability density of the original image (except, of course, a trivial transformation). It is easy to verify that with linear contrasting the form of the probability density is preserved, however, in the general case, that is, for arbitrary values ​​of the parameters of the linear transformation, the parameters of the probability density of the transformed image change.

Determination of the probabilistic characteristics of images that have undergone nonlinear processing is a direct task of the analysis. When deciding practical tasks of image processing, the inverse problem can be posed: by the known form of the probability density and the desired form, determine the required transformation to which the original image should be subjected. In the practice of digital image processing, the transformation of the image to an equiprobable distribution often leads to a useful result. In this case

where and are the minimum and maximum brightness values ​​of the converted image. Let us determine the characteristic of the converter deciding this task... Let and are related by function (2.2), and and are the integral distribution laws of the input and output quantities. Taking into account (2.5), we find:

.

Substituting this expression into the condition of probabilistic equivalence

after simple transformations we obtain the relation

which is characteristic (2.2) in the problem being solved. According to (2.6), the original image undergoes a nonlinear transformation, the characteristic of which is determined by the integral distribution law of the original image itself. After that, the result is brought to the specified dynamic range using the linear contrasting operation.

Similarly, solutions can be obtained for other similar problems in which it is required to bring the distribution laws of the image to a given form. In the table of such conversions is given. One of them, the so-called hyperbolization of the distribution, involves reducing the probability density of the transformed image to a hyperbolic form:

(2.7)

If we take into account that when light passes through the eye, the input brightness is logarithm by its retina, then the final probability density turns out to be uniform. Thus, the difference from the previous example lies in taking into account the physiological properties of vision. It can be shown that an image with probability density (2.7) is obtained at the output of a nonlinear element with the characteristic

also determined by the integral distribution law of the original image.

Thus, the transformation of the probability density assumes knowledge of the cumulative distribution for the original image. As a rule, there is no reliable information about him. The use of analytical approximations for the purposes under consideration is also of little use, since their small deviations from the true distributions can lead to significant differences in the results from the required ones. Therefore, in the practice of image processing, the transformation of distributions is performed in two stages.

At the first stage, the histogram of the original image is measured. For a digital image, the gray scale of which, for example, belongs to the integer range 0 ... 255, the histogram is a table of 256 numbers. Each of them shows the number of points in the frame with a given brightness. Dividing all the numbers in this table by the total sample size equal to the number of used image points, an estimate of the probability distribution of the image brightness is obtained. We denote this estimate ... Then the estimate of the cumulative distribution is obtained by the formula:

.

At the second stage, the nonlinear transformation (2.2) itself is performed, which provides the necessary properties of the output image. In this case, instead of the unknown true cumulative distribution, its estimate based on the histogram is used. Taking this into account, all methods of element-by-element transformation of images, the purpose of which is to modify the distribution laws, are called histogram methods. In particular, the transformation in which the output image has a uniform distribution is called equalization (equalization) of histograms.

Note that the histogram conversion procedures can be applied both to the image as a whole and to its individual fragments. The latter can be useful when processing non-stationary images, the content of which differs significantly in their characteristics in different areas. In this case, the best effect can be achieved by applying histogram processing to individual areas.

The use of relations (2.4) - (2.8), which are valid for images with a continuous distribution of brightness, is not entirely correct for digital images. It should be borne in mind that as a result of processing it is not possible to obtain an ideal probability distribution of the output image, therefore it is useful to control its histogram.

a) original image

b) the result of processing

Rice. 2.9. Example of image equalization

Figure 2.9 shows an example of equalization performed in accordance with the described method. A characteristic feature of many images obtained in real imaging systems is a significant proportion of dark areas and a relatively small number of areas with high brightness. Equalization is designed to correct the picture by aligning the integral areas of areas with different brightness. Comparison of the original (Fig. 2.9.a) and processed (Fig. 2.9.b) images shows that the redistribution of brightness that occurs during processing leads to an improvement in visual perception.

Hello everyone. Now my supervisor and I are preparing a monograph for publication, where we are trying in simple words talk about the basics of digital imaging. This article reveals a very simple, but at the same time very effective technique for improving image quality - histogram equalization.

For simplicity, let's start with monochrome images (i.e. images containing information only about brightness, but not about the color of pixels). An image histogram is a discrete function H defined on a set of values, where bpp is the number of bits allocated to encode the brightness of one pixel. Although not required, histograms are often normalized to a range by dividing each value of the H [i] function by total amount image pixels. Table. 1 shows examples of test images and histograms built on their basis:
Tab. 1. Images and their histograms

Having carefully studied the corresponding histogram, you can draw some conclusions about the original image itself. For example, histograms of very dark images are characterized by the fact that non-zero values ​​of the histogram are concentrated near zero brightness levels, and for very light images, on the contrary, all non-zero values ​​are concentrated on the right side of the histogram.
Intuitively, we can conclude that the most convenient for human perception will be an image in which the histogram is close to a uniform distribution. Those. to improve the visual quality, a transformation must be applied to the image so that the histogram of the result contains all possible brightness values ​​and, at the same time, in approximately the same amount. This conversion is called histogram equalization and can be done with the code in Listing 1.
Listing 1. Implementation of the histogram equalization procedure

  1. procedure TCGrayscaleImage. HistogramEqualization;
  2. const
  3. k = 255;
  4. h: array [0 .. k] of double;
  5. i, j: word;
  6. begin
  7. for i: = 0 to k do
  8. h [i]: = 0;
  9. h [round (k * self. Pixels [i, j])]: = h [round (k * self. Pixels [i, j])] + 1;
  10. for i: = 0 to k do
  11. h [i]: = h [i] / (self. Height * self. Width);
  12. for i: = 1 to k do
  13. h [i]: = h [i - 1] + h [i];
  14. for i: = 0 to self. Height - 1 do
  15. for j: = 0 to self. Width - 1 do
  16. self. Pixels [i, j]: = h [round (k * self. Pixels [i, j])];
  17. end;

As a result of histogram equalization, in most cases, the dynamic range of the image is significantly expanded, which makes it possible to display previously unnoticed details. This effect is especially pronounced in dark images, which is shown in Table. 2. In addition, it is worth noting one more important feature of the equalization procedure: unlike most filters and gradation transformations, which require adjustment of parameters (apertures and gradation transform constants), the histogram can be equalized completely automatic mode without operator participation.
Tab. 2. Images and their histograms after equalization


You can easily see that the histograms after equalization have some kind of noticeable breaks. This is due to the fact that the dynamic range of the output image is wider than the range of the original one. Obviously, in this case, the mapping shown in Listing 1 cannot provide nonzero values ​​in all pockets of the histogram. If, nevertheless, it is necessary to achieve a more natural look of the output histogram, you can use a random distribution of the values ​​of the i-th pocket of the histogram in some of its vicinity.
Obviously, the equalization of histograms makes it easy to improve the quality of monochrome images. Naturally, I would like to apply a similar mechanism to color images.
Most inexperienced developers present an image as three RGB color channels and try to apply the histogram equalization procedure to each color channel separately. In some rare cases, this can be successful, but in most cases the result is not so-so (the colors are unnatural and cold). This is because the RGB model does not accurately represent human color perception.
Let's remember another color space - HSI. This color model (and others related to it) is very widely used by illustrators and designers as it allows them to operate with more familiar concepts of color tone, saturation and intensity.
If we consider the projection of the RGB cube in the direction of the diagonal white-black, we get a hexagon, the corners of which correspond to the primary and secondary colors, and all gray shades(lying on the diagonal of the cube) are projected to the central point of the hexagon (see Fig. 1):

Rice. 1. Projection of the color cube
To use this model to encode all the colors available in the RGB model, you need to add a vertical axis of lightness (or intensity) (I). The result is a hexagonal cone (Fig. 2, Fig. 3):


Rice. 2. HSI pyramid (tops)
In this model, the hue (H) is specified by the angle relative to the red axis, the saturation (S) characterizes the purity of the color (1 means completely pure color, and 0 means a shade of gray). At zero saturation, the hue is meaningless and undefined.


Rice. 3. HSI pyramid
Table. 3 shows the decomposition of the image into HSI components (white pixels in the tone channel correspond to zero saturation):
Tab. 3. HSI color space


It is believed that to enhance the quality of color images, it is most effective to apply equalization to the intensity channel. This is exactly what is demonstrated in Table. 4
Tab. 4. Equalization of various color channels


I hope this material seemed to you at least interesting, as useful as possible. Thanks.

Image preprocessing- the process of improving the quality of the image, which aims to obtain, based on the original, the most accurate and adapted for automatic analysis of the image.

Among the defects of a digital image, the following types can be distinguished:

  • Digital noise
  • Color defects (insufficient or excessive brightness and contrast, wrong color tone)
  • Blurring (defocusing)

Image preprocessing methods depend on the research objectives and may include the following types of work:

Filtering noisy images

Digital image noise- image defect introduced by the photosensors and electronics of the devices that use them. To suppress it, the following methods are used:

Linear averaging of points by neighbors - the simplest kind of noise removal algorithms. Their main idea is to take the arithmetic mean of points in some neighborhood as a new point value.

Physically, such filtering is implemented by traversing image pixels with a convolution matrix, which looks like this:

Example:

div is the coefficient of normalization so that the average intensity remains unchanged. It is equal to the sum of the coefficients of the matrix, in the example div = 6.

Gaussian blur(a kind of linear convolution) is implemented by traversing the image pixels with a convolution matrix, which looks like this:

The 5 × 5 matrix is ​​filled in according to the normal (Gaussian) law. Below is the same matrix where the coefficients are already normalized so that the div for this matrix is ​​one.

The strength of the blur depends on the size of the matrix.

The upper left pixel has no “neighbors” to the left and from the top, therefore, we have nothing to multiply the matrix coefficients by!

The solution to this problem requires the creation of an intermediate image. The idea is to create a temporary image with dimensions

width + 2 gap / 2, height + 2 gap / 2, where

width and height - width and height of the filtered image,

gap - dimension of the convolution matrix.

The input image is copied to the center of the image, and the edges are filled with the outer pixels of the image. The blur is applied to the intermediate buffer, and then the result is retrieved from it.

Median filter is a window filter that sequentially scans the image and returns at each step one of the elements that fell into the filter window.

The pixels that "hit" the window are sorted in ascending order, and the value in the middle of the sorted list is selected.

A median filter is commonly used to reduce noise or "smooth" an image.

To improve clarity image uses the following filter (div = 1):

Morphological transformations

Morphological filtering is used to expand (dilute) or narrow (erode) elements of a binary image.

Dilation(morphological extension) - convolution of an image or a selected area of ​​an image with a certain template. The template can be of any shape and size. At the same time, the only one that stands out in it leading position(anchor), which is aligned with the current pixel when calculating convolution.

A binary image is an ordered set (ordered set) of black and white dots (pixels). The maximum intensity of the image pixels is equal to one, and the minimum is zero.

The application of dilatation is reduced to the passage of the pattern over the entire image and the application of the operator to search for the local maximum intensity of the image pixels, which are covered by the pattern. If the maximum is 1, then the point at which the template anchor is located will be white. Such an operation causes the growth of light areas in the image. In the figure, pixels are marked in gray, which will be white as a result of dilatation.

Erosion(morphological narrowing) - an operation inverse to dilatation. The action of erosion is similar to dilatation, the only difference is that the operator is used to search for a local minimum. If the minimum is 0, then the point at which the template anchor is located will be black. In the picture on the right, the gray color marks the pixels that will turn black as a result of erosion.

Operation " Dilation"- analogue of the logical" or ", the operation" Erosion"- analogue of the logical" and ".

The result of morphological operations is largely determined by the applied template (structural element). By choosing a different structural element, you can solve different image processing tasks:

  • Noise suppression.
  • Selection of object boundaries.
  • Selection of the skeleton of the object.

Image brightness and contrast correction

Brightness is a characteristic that determines how much the colors of pixels differ from black. For example, if a digitized photograph was taken in sunny weather, then its brightness will be significant. On the other hand, if the photo was taken in the evening or at night, then its brightness will be low.

Contrast is a measure of how widely the colors of the pixels in an image are scattered. The greater the variation in the pixel color values, the greater the contrast in the image.

There are three main methods for enhancing the contrast of an image:

  • linear stretching of the histogram (linear contrasting),
  • histogram normalization,
  • equalization (linearization or equalization) of the histogram.

Linear stretch is reduced to assigning new intensity values ​​to each pixel of the image. If the intensities of the original image changed in the range from to, then it is necessary to linearly "stretch" the specified range so that the values ​​change from 0 to 255. To do this, it is enough to recalculate the old intensity values ​​for all pixels according to the formula, where the coefficients are simply calculated based on that the border should go to 0, and - to 255.

Normalizing histogram Unlike the previous method, it does not stretch the entire range of intensity variation, but only its most informative part. The informative part is understood as a set of histogram peaks, i.e. intensities that are most common in the image. The bins corresponding to rare intensities are discarded during the normalization process, then the usual linear stretching of the resulting histogram is performed.

Alignment bar charts are one of the most common methods. The goal of equalization is to ensure that all brightness levels have the same frequency, and the histogram conforms to a uniform distribution law. Let's say you have a grayscale image that has pixel resolution. The number of levels of quantization of the brightness of pixels (the number of bins) is. Then, on average, for each brightness level, pixels. The basic math lies in comparing two distributions. Let - random variables describing the change in the intensity of pixels in the images, - the density of the intensity distribution in the original image, - the desired distribution density. It is necessary to find a transformation of distribution densities, which would allow obtaining the desired density:

Let us denote by and integral laws of distribution of random variables and. From the condition of probabilistic equivalence it follows that ... Let us write down the integral distribution law by definition:

Hence we get that

It remains to figure out how to estimate the integral distribution law. To do this, you must first build a histogram of the original image, then normalize the resulting histogram by dividing the value of each bin by the total number of pixels. The bin values ​​can be considered as an approximate value of the distribution density function. Thus, the value of the cumulative distribution function can be represented as a sum of the following form:

The constructed estimate can be used to calculate new intensity values. Note that the listed transformations of histograms can be applied not only to the entire image, but also to its individual parts.

The OpenCV library implements the equalizeHist function, which enhances the contrast of an image by equalizing the histogram [,]. The function prototype is shown below.

void equalizeHist (const Mat & src, Mat & dst)

The function works in four stages:

Below is an example of a program that flattens a histogram. The application takes the name of the original image as a command line argument. After performing the histogram equalization operation, the original image 1 is displayed The image used is from the PASACL VOC 2007 database. converted to grayscale (Figure 7.11, left), and a flattened histogram image (Figure 7.11, right).

#include #include using namespace cv; const char helper = "Sample_equalizeHist.exe \ n \ \ t - image file name \ n "; int main (int argc, char * argv) (const char * initialWinName =" Initial Image ", * equalizedWinName =" Equalized Image "; Mat img, grayImg, equalizedImg; if (argc< 2) { printf("%s", helper); return 1; } // загрузка изображения img = imread(argv, 1); // преобразование в оттенки серого cvtColor(img, grayImg, CV_RGB2GRAY); // выравнивание гистограммы equalizeHist(grayImg, equalizedImg); // отображение исходного изображения и гистограмм namedWindow(initialWinName, CV_WINDOW_AUTOSIZE); namedWindow(equalizedWinName, CV_WINDOW_AUTOSIZE); imshow(initialWinName, grayImg); imshow(equalizedWinName, equalizedImg); waitKey(); // закрытие окон destroyAllWindows(); // осовобождение памяти img.release(); grayImg.release(); equalizedImg.release(); return 0; }


Rice. 7.11.

With all element-by-element transformations, the probability distribution law that describes the image changes. In the case of linear contrasting, the form of the probability density is preserved, however, in the general case, i.e. for arbitrary values ​​of the parameters of the linear transformation, the parameters of the probability density of the transformed image change.

Determination of the probabilistic characteristics of images that have undergone nonlinear processing is a direct task of the analysis. When solving practical problems of image processing, the inverse problem can be posed: according to the known form of the probability density p f(f) and the desired form p g(g) determine the required transformation g= ϕ( f) to which the original image should be subjected. In the practice of digital image processing, the transformation of the image to an equiprobable distribution often leads to a useful result. In this case

where g min and g max - minimum and maximum brightness values ​​of the converted image. Let us determine the characteristics of the converter that solves this problem. Let be f and g linked by function g(n, m) = j ( f(n, m)), a P f(f) and Pg(g) - integral distribution laws of input and output brightness. Taking into account (6.1), we find:

Substituting this expression into the condition of probabilistic equivalence

after simple transformations we obtain the relation

representing the characteristic g(n, m) = j ( f(n, m)) in the problem to be solved. According to (6.2), the original image undergoes a nonlinear transformation, the characteristic of which is P f(f) is determined by the integral distribution law of the original image. After that, the result is brought to the specified dynamic range using the linear contrasting operation.

Thus, the transformation of the probability density assumes knowledge of the cumulative distribution for the original image. As a rule, there is no reliable information about him. Approximation by analytical functions, due to approximation errors, can lead to a significant difference in the results from the required ones. Therefore, in the practice of image processing, the transformation of distributions is performed in two stages.



At the first stage, the histogram of the original image is measured. For a digital image, the gray scale of which, for example, belongs to an integer range, the histogram is a table of 256 numbers. Each of them shows the number of points in the image (frame) that have a given brightness. Dividing all the numbers in this table by the total sample size equal to the number of samples in the image, an estimate of the probability distribution of the image brightness is obtained. We denote this estimate by q p f(f q), 0 ≤ f q≤ 255. Then the estimate of the cumulative distribution is obtained by the formula:

At the second stage, the nonlinear transformation (6.2) itself is performed, which provides the necessary properties of the output image. In this case, instead of the unknown true cumulative distribution, its estimate based on the histogram is used. Taking this into account, all methods of element-by-element transformation of images, the purpose of which is to modify the distribution laws, are called histogram methods. In particular, a transformation in which the output image has a uniform distribution is called equalization (equalization) of the histogram.

Note that the histogram conversion procedures can be applied both to the image as a whole and to its individual fragments. The latter can be useful when processing non-stationary images, the characteristics of which differ significantly in different areas. In this case, the best effect can be achieved by applying histogram processing to individual areas - regions of interest. However, this will change the values ​​of the readings and all other areas. Figure 6.1 shows an example of equalization performed in accordance with the described method.

A characteristic feature of many images obtained in real imaging systems is a significant proportion of dark areas and a relatively small number of areas with high brightness.

Figure 6.1 - An example of image histogram equalization: a) the original image and its histogram c); b) the transformed image and its histogram d)

Equalization of the histogram results in equalization of the integral areas of uniformly distributed brightness ranges. Comparison of the original (Figure 6.1 a) and processed (Figure 6.1 b) images shows that the redistribution of brightness that occurs during processing leads to an improvement in visual perception.