OpenCV-Python learning (17) - OpenCV image pixel type conversion and normalization (cv.normalize)

1. Learning Objectives

  1. Learn the type conversion of OpenCV image pixels;
  2. Learn the OpenCV normalization function.

2. Type conversion of OpenCV image pixels

Since [all image data in OpenCV-Python are numpy.array], the type conversion of pixels can directly use the type conversion method of numpy.

2.1 Convert pixels to float32

import numpy as np
import cv2 as cv

def check_type():
  img = cv.imread('./images/squirrel_cls.jpg')
  print('img_type',np.dtype(img[0][0][0]))
  print('img_data',img[0][0][0])
  cv.imshow("img", img)

  # 图像像素的类型转换为float32
  img_float32 = np.float32(img)
  print('img_float32_type',np.dtype(img_float32[0][0][0]))
  print('img_float32_data',img_float32[0][0][0])
  cv.imshow("img_float32", img_float32)

  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  check_type()

2.2 Data output result

Enter a picture description

2.3 Image output results

Enter a picture description

3. Normalization (cv.normalize)

Normalization is to limit the data that needs to be processed to a certain range that you need after processing (through a certain algorithm).

First of all, normalization is for the convenience of subsequent data processing, and secondly, it is to ensure faster convergence when the program is running. The specific function of normalization is to summarize the statistical distribution of uniform samples. Normalization is a statistical probability distribution between 0-1, and normalization is a statistical coordinate distribution on a certain interval. Normalization has the meaning of identity, unification and unification.

The purpose of normalization is to make the incomparable data comparable, while maintaining the relative relationship between the two compared data, such as the size relationship; or for drawing, it was difficult to draw on a picture, and after normalization, the relative position on the picture can be easily given.

3.1 cv.normalize() function usage

cv.normalize(src[, dst[, alpha[, beta[, norm_type[, dtype[, mask]]]]]]) → dst

3.2 Parameter description

parameter illustrate
src represents the input image.
dst Indicates the output image.
alpha In the case of range normalization, the normalized lower bound or the standard value of the lower bound.
beta Indicates the normalized upper range bound in the case of range normalization.
norm_type Indicates the type of normalization.
dtype Indicates that when negative, the output array is of the same type as src; otherwise, it has the same number of channels as src.

3.3 Norm_type value description

value illustrate
NORM_L1 Represents the sum of the absolute values ​​of the values ​​in the image matrix.
NORM_L2 Represents the root of the sum of squares of the values ​​in the image matrix.
NORM_INF Indicates the maximum value of the absolute value of the values ​​in the image matrix.
NORM_MINMAX Indicates that the values ​​in the image matrix are translated or scaled to a specified range, linearly normalized.

3.4 Norm_type value calculation formula

  1. NORM_L1 : (Manhattan distance) L1-norm (sum of absolute values) of the normalized array

  2. NORM_L2 : (Euclidean distance) L2-norm (square root of the sum of squares) of the normalized array

  3. NORM_INF : (Chebyshev distance) L∞-norm (maximum absolute value) of the normalized array

  4. NORM_MINMAX : The values ​​of the normalized array are translated or scaled to a specified range

3.5 Normalization Calculation Example

Enter a picture description

4. OpenCV normalization example code

4.1 Code

import numpy as np
import cv2 as cv

def check_type():
  img = cv.imread('./images/squirrel_cls.jpg')
  cv.imshow("img", img)

  # 图像像素的类型转换为float32
  img_float32 = np.float32(img)
  cv.imshow("img_float32", img_float32)

  # 归一化 NORM_L1
  img_NORM_L1 = img_float32.copy()
  cv.normalize(img_float32,img_NORM_L1,1,0,cv.NORM_L1)
  print(img_NORM_L1[0][0])
  cv.imshow("img_NORM_L1", img_NORM_L1)

  # 归一化 NORM_L2
  img_NORM_L2 = img_float32.copy()
  cv.normalize(img_float32,img_NORM_L2,1,0,cv.NORM_L2)
  print(img_NORM_L2[0][0])
  cv.imshow("img_NORM_L2", img_NORM_L2)

  # 归一化 NORM_INF
  img_NORM_INF = img_float32.copy()
  cv.normalize(img_float32,img_NORM_INF,1,0,cv.NORM_INF)
  print(img_NORM_INF[0][0])
  cv.imshow("img_NORM_INF", img_NORM_INF)

  # 归一化 NORM_MINMAX
  img_NORM_MINMAX = img_float32.copy()
  cv.normalize(img_float32,img_NORM_MINMAX,1,0,cv.NORM_MINMAX)
  print(img_NORM_MINMAX[0][0])
  cv.imshow("img_NORM_MINMAX", img_NORM_MINMAX)

  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  check_type()

4.2 Effect

Enter a picture description

4.3 Output the normalized value of the first pixel in different ways

Enter a picture description

5. Summary

  1. Normalization is to limit the data that needs to be processed to a certain range that you need after processing (through a certain algorithm).
  2. Understand the process of normalizing different calculations.

6. Reference

  1. Explanation of the principle of the normalization function normalize() in opencv
  2. Contrast enhancement (2): Histogram positive planning and gamma transformation cv.normal() function usage and principle
  3. Learn [Intel OpenCV Junior Certification Course - Image Pixel Type Conversion and Normalization]

7. Description

The picture of the calculation formula directly uses the picture in the principle explanation of the normalization function normalize() in opencv . If it is not allowed to be used, please contact me, and I will delete this part of the content! Thanks!

Guess you like

Origin blog.csdn.net/m0_38082783/article/details/128343246