Digital image processing experiment – Convert “original image.bmp” into a grayscale image, and perform histogram equalization on the resulting grayscale image; use histogram specification algorithm to process the three RGB channels of “original image.bmp” The histograms are changed to be consistent with “reference image.bmp”

1. Experimental objectives

1. Understand and apply the concept and application of histograms to analyze and process images.

2. Understand and apply the concept of histogram equalization and perform related image processing

3. Master the histogram specification algorithm and perform histogram specification processing and analysis on samples

4. Convert the “original image.bmp” into a grayscale image, and perform histogram equalization on the resulting grayscale image.

5. Use the histogram specification algorithm to change the histograms of the three RGB channels of “original image.bmp” to be consistent with “reference image.bmp”.

2. Experimental environment

Operating environment: Windows 10

Experimental platform: PyCharm Community Edition 2022.3.1

Processor: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz 2.59 GHz

Memory: 16.0 GB

Python interpreter: Python 3.9

3. Experimental principle

1. The definition of histogram:

Histogram is an important means of statistics on image grayscale distribution. The grayscale histogram represents the number of pixels with a certain grayscale in the image, reflecting the frequency of each grayscale in the image. Generally, the abscissa of the grayscale histogram is the gray level, and the ordinate is the number of occurrences of the gray level, which is the most basic statistical feature of the image. Through the histogram, you can intuitively understand the contrast and brightness distribution of the image, and modifying the shape of the histogram can achieve image enhancement.

2. Properties of histogram:

(1) The histogram is a statistical result of the number of occurrences (or frequency) of the gray value of each pixel in an image. It does not reflect the location of a certain gray value pixel, that is, the position information is lost.

(2) An image corresponds to a histogram, but different images may have the same histogram. That is, there is a many-to-one mapping relationship between images and histograms.

(3) The sum of the histograms of each sub-region of an image is equal to the histogram of the entire image.

3. Mathematical representation related to histogram:

From a probability point of view, the frequency of grayscale occurrence can be regarded as the probability of its occurrence, so the histogram corresponds to the probability density function p(r)(pdf, probability density function), and the probability distribution function P(r) is the cumulative sum of the histogram, that is, the integral of the probability density function.

4. Histogram calculation:

If the image has L-level grayscale (usually L=256 levels, 8-bit grayscale), then the grayscale histogram hist[0,…,L-1 of the grayscale image f(x, y) with size M×N ] can be obtained by the following algorithm:

(1) Initialization: hist[k]=0; k=0, …, L-1.

(2) Statistics: hist[f(x, y)] + + ; x=0, …, M-1; y = 0, …, N-1.

(3) Normalization: hist[f(x, y)]/=MN.

Among them, the normalization of the histogram is an option, and you do not need to perform this operation if no special processing is required.

5. Histogram equalization:

(1) Definition:

According to the entropy theory, it can be seen that when h[0], h[1],…, h[L-1] are equal, the amount of image information is the largest. The purpose of equalization is to make each h[i] equal, which is to transform the histogram of the original image into a uniformly distributed form. The number of image pixels is n, and L is the number of effective gray levels. For a balanced histogram, it should be on each gray level. >n/L pixels.

(2) Entropy: is a measure of the amount of information, defined as:

Among them, pi is the occurrence of symbol i The probability. In an image, pr is the gray levelr< /em>The probability of occurrence can be proved that when p0=p1=…= When p255=1/256, the value of H is the largest, that is, the amount of information in the image is the largest.

(3) Two cases of histogram equalization:

By transformingg=T(f) To obtain the effect of histogram equalization, two situations need to be considered:

1) Multiple to one: If the sum of the frequencies of consecutive n gray levels is equal to n/ L will cause them to be merged into one gray level.

2) One to multiple: If the frequency of a certain gray level is n/L< em>R times, it must be divided into R different gray levels, each with < em>n/L pixels.

Suppose the image histogram before transformation is h1, and the image histogram obtained after transformation is required to be h2 , if the original gray level f is transformed into gray level g, it must satisfy:

Thereby:

Where H1(f) is the pixel distribution function (cumulative function), L is the number of valid grayscales, L≤256.

(4) Steps for histogram equalization:

Method 1:

  • Find the grayscale histogram of the original image, recorded as h
  • Calculate the cumulative distribution of each gray level of the imageH
  • Calculate the pixel gray value of the new image

Method 2:

  • Find the grayscale histogram of the original image, recorded as h
  • Find the number of pixels in the image, recorded as n
  • Calculate the pdf of each gray level, that is, the proportion of each type of pixels in the entire image.

  • Calculate the cumulative distribution of each gray level of the imagehp

  • Calculate the pixel gray value of the new image

(5) Histogram equalization analysis:

1) The result after equalization is not balanced because it is only merged but not decomposed.

2) The amount of information does not increase, but decreases, because the effective gray level of the equalized image is generally <L

3) Able to increase the global contrast of the image. For low-contrast images, the contrast is increased, and the gray level with a particularly large number of pixels becomes farther away from the front and rear gray levels, making the pixels of this gray level particularly conspicuous in the equalized image.

6. Histogram specification:

(1)Definition:

Transform the histogram into a specific shape to selectively enhance the contrast within a certain grayscale range.

(2) Histogram specification steps:

Assume that the original image is f, and the image obtained after the histogram is specified is g, to be matched (reference) The image ism

  • Calculate the normalization of f and m Grayscale histogram, denoted as pf and p m, that is:

  • Calculate cumulative histogram for f and m picture:

  • For each pixel valuef(x, y< /em>), find the t* that minimizes the formula (2), which is the gray value g obtained after stipulation >(x, y), that is, formula (1).

(3) Comparison of histogram specification and histogram equalization:

Histogram equalization: automatic enhancement, the effect is not easy to control, and the result of full image enhancement is always obtained

Histogram specification: selective enhancement, the required histogram must be given, and the result of enhancement can be specified

Histogram equalization can be regarded as a histogram specification where the target histogram is uniformly distributed.

4. Main code and results

1. Convert the “original image.bmp” into a grayscale image, and perform histogram equalization on the resulting grayscale image.

The code is implemented as follows:

import cv2

#Specify image file path

image_path = 'C:/Users/86198/PycharmProjects/pythonProject5/shuzituxiangchuli/yuantu.bmp'


# read image

img = cv2.imread(image_path)


#Convert the image to grayscale

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


# Perform histogram equalization on grayscale images

equ_img = cv2.equalizeHist(gray_img)


# Display original image and processed image

cv2.imshow('Original Image', img)

cv2.imshow('Gray Image', gray_img)

cv2.imshow('Equalized Image', equ_img)


cv2.waitKey(0)

cv2.destroyAllWindows()

The experimental results are as follows:

Figure 1 shows the original image, Figure 2 shows the image converted from the original image to grayscale, and Figure 3 shows the image after histogram equalization of the grayscale image.

Figure 1 Original picture

Figure 2 Grayscale image

Figure 3 Grayscale image after histogram equalization

2. Use the histogram specification algorithm to change the histograms of the three RGB channels of “original image.bmp” to be consistent with “reference image.bpm”.

The code is implemented as follows:

import cv2

import cv2


# Read the original image

original_image = cv2.imread("yuantu.bmp")

inference = cv2.imread('example.bmp')

# Convert to grayscale image

gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)


# Perform histogram equalization

equalized_image = cv2.equalizeHist(gray_image)


# Save the processed image

cv2.imwrite("gray_img.bmp", gray_image)

cv2.imwrite("equalized_img.bmp", equalized_image)


b, g, r = cv2.split(original_image)

b_i, g_i, r_i = cv2.split(inference)


b_matched = cv2.equalizeHist(b, b_i)

g_matched = cv2.equalizeHist(g, g_i)

r_matched = cv2.equalizeHist(r, r_i)


matched_image = cv2.merge((b_matched, g_matched, r_matched))

cv2.imwrite('matched_image.bmp', matched_image)

cv2.imshow('matched_image.bmp', matched_image)

cv2.waitKey(0)

The experimental results are as follows:

Figure 4 shows the image after histogram specification based on the reference image.

Figure 4 Histogram specification

5. Problems encountered and solution process

1. The problem that opencv cannot be used.

Problem description: I have downloaded opencv in pycharm, but it keeps showing that there is no opencv.

solution:

① Check the path, it is python under anaconda, not under opencv.

② Change the path and find that after changing the path to opencv under anaconda, it still doesn’t work.

③ You can use opencv after changing the path to opencv under python3.9.

2. The image is not read in during runtime.

Problem description: When running the code, an error was reported and the reference image was not read. It is suspected that the image is too large to be read.

solution:

① First try to re-insert the picture, but still get an error.

② Next, modify the path to an absolute path, but an error is still reported.

③ Change the file format from bmp to jpg. The code runs successfully and the desired image appears, but the reference image still cannot be opened.

6. Experience

Both experiments are common operations in image processing, the first is to convert a color image to a grayscale image, and the second is to perform histogram equalization on the image.

For the first experiment, converting a color image to grayscale is a simple image processing method. By calculating the weighted average of the RGB values of each pixel, the gray value of the pixel can be obtained. This experiment can help us understand the concept and generation method of grayscale images.

For the second experiment, histogram equalization is a commonly used image enhancement method. It can improve the contrast and brightness of images, making them clearer. By adjusting the histogram of the original image to that of the reference image, you can make the two images more visually similar. This experiment can help us understand the principles and applications of histogram equalization.

In general, these two experiments are relatively simple, but they can help us understand the basic concepts and methods of image processing. Through hands-on practice, we can better understand these concepts and apply them to real-world problems.

syntaxbug.com © 2021 All Rights Reserved.