Python image processing learning record

The difference between list in Python and array in numpy

  • Lists in python and arrays in numpy are two completely different things. Lists can store different types of data, such as int, float, str, and even boolean types; while the data types stored in a numpy array must all be the same. For example int or float.

  • The data type in the list stores the address where the data is stored, that is, the pointer rather than the data (the bottom layer is C language, so it is normal to think about it this way). For example, a=[1,2,3,4] requires 4 pointers. and four data, which increases storage and CPU consumption, while a=np.array([1,2,3,4]) only needs to store four data, making reading and calculation more convenient.

  • Therefore, the list List can store different types of data, so the size of each element in the list can be the same or different, so it does not support reading one column at a time. Even for standard two-dimensional number lists ([[1,2,3,4]]), it is best to use the numpy data type to operate purely numbers.

  • The access methods of multidimensional lists and arrays are also different. List only supports the access method a[0][0], while numpy's array supports two access methods: a[0][0] and a[0,0].

#List的访问方法
a = [[1,2,'list'],[4,5,6]]
print(a[0][2])
#numpy.array的访问方法
import numpy as np
b = np.array([[1,2,3],[4,5,6]], np.uint8)
print(b[0][0])
print(b[0,0])

Python remainder operation

The remainder operation is not complicated, such as 23 % 5 = 3, but the remainder operation containing negative numbers can sometimes be confusing, such as -23 % 5 = ? ,23% -5 = ? , -23 % -5 = ? . In fact, the remainder operation is closely related to the computer division and rounding method. There are several methods for rounding the division result on the computer:

  • Rounding up, taking the integer closest to the exact value in the +∞ direction, that is, taking the smallest integer that is slightly larger than the actual result, is also called Ceiling rounding. In this rounding method, 17 / 10 == 2, 5 / 2 == 3, -9 / 4 == -2.
  • Round down and take the integer closest to the exact value in the -∞ direction, that is, take the largest integer that is slightly smaller than the actual result, also called Floor rounding. In this rounding method, 17 / 10 == 1, 5 / 2 == 2, -9 / 4 == -3.
  • Rounding toward zero takes the integer closest to the exact value in the 0 direction. In other words, the decimal part is discarded, so it is also called truncation. In this rounding method, 17 / 10 == 1, 5 / 2 == 2, -9 / 4 == -2.

Python's method for rounding division results is to round down, that is, Floor rounding. You can use floor remover//inspection.
The specific calculation method of Python's remainder operation is: __ x%y = x - (x//y)*y __. The following is a code to test Python's remainder operation method:

def remainder(x, y):
   return (x - (x//y)*y)

x, y = -100, 13
print(x % y, remainder(x, y))

Data analysis of RGB images

Use a color image of 5x6 pixels:
Insert image description here
Enlarged image:
Picture name
As can be seen from the picture, the colors of the pixels in the 1st to 5th rows are black, red, green, blue, and white. Read the image data and print it out. Analysis shows:

  1. The color image data is a 5x6x3 three-dimensional array img, and the data type is uint8;
  2. Each pixel is a 1x3 matrix corresponding to the three primary colors of red, green and blue RGB, where the first element is blue B, the second element is green G, and the first element is red R;
  3. img[0,:,:] corresponds to the pixels in the first row, img[:,0,:] corresponds to the pixels in the first column, img[:,:,0] corresponds to the blue component of each pixel, and so on.

Code:

import cv2

img = cv2.imread("img\\bmp5_6.bmp",cv2.IMREAD_COLOR)
print('图像数据:\n', img)
print('数据结构', img.shape)
print('数据类型', img.dtype)
print('第一行数据\n', img[0,:,:])
print('第一列数据\n', img[:,0,:])
print('图像的蓝色分量\n', img[:,:,0])
cv2.imshow("bmp5_6.bmp", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Print the result:

图像数据:
 [[[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]]

 [[  0   0 255]
  [  0   0 255]
  [  0   0 255]
  [  0   0 255]
  [  0   0 255]
  [  0   0 255]]

 [[  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]
  [  0 255   0]]

 [[255   0   0]
  [255   0   0]
  [255   0   0]
  [255   0   0]
  [255   0   0]
  [255   0   0]]

 [[255 255 255]
  [255 255 255]
  [255 255 255]
  [255 255 255]
  [255 255 255]
  [255 255 255]]]
数据结构 (5, 6, 3)
数据类型 uint8
第一行数据
 [[0 0 0]
 [0 0 0]
 [0 0 0]
 [0 0 0]
 [0 0 0]
 [0 0 0]]
第一列数据
 [[  0   0   0]
 [  0   0 255]
 [  0 255   0]
 [255   0   0]
 [255 255 255]]
图像的蓝色分量
 [[  0   0   0   0   0   0]
 [  0   0   0   0   0   0]
 [  0   0   0   0   0   0]
 [255 255 255 255 255 255]
 [255 255 255 255 255 255]]

Python implementation of image scaling nearest neighbor interpolation

The principle of image scaling nearest neighbor interpolation:

  1. Original image coordinates (x, y), scaled image coordinates (X, Y)
  2. The scaling ratio is scale[0,1], (X,Y) and (x,y) satisfy the following relationship:
    X = x * scale[0], that is, x = X / scale[0]
    Y = y * scale[1 ], that is, y = Y / scale[1]
  3. The image of the scaled coordinates (X, Y) selects the image of the point closest to the original image coordinates (X/scale[0], Y/scale[1])—because X/scale[0] and Y/scale[1] May not be an integer
  4. The closest points to (X/scale[0], Y/scale[1]) can only be the following four:
    0: (int(X/scale[0]), int(Y/scale[1]))
    1: (int(X/scale[0])+1, int(Y/scale[1])) 2: (
    int(X/scale[0]), int(Y/scale[1])+1)
    3: (int(X/scale[0])+1, int(Y/scale[1])+1)

Based on the principle of nearest neighbor interpolation, the implementation function of the nearest neighbor method for image scaling is written. The code is as follows:


import numpy as np
import cv2
import math as m

#图像近邻法缩放
#------------------------------------------------------------------------------
#函数名称:ft_nearestScaling
#输入参数:
#   image,输入图像,数据类型:array
#   scale,缩放比例系数, 数据类型:2元素元组,(x轴缩放比例,y轴缩放比例)
#输出参数:
#   imgOut,输出图像,数据类型:array
#------------------------------------------------------------------------------
def ft_nearestScaling(image, scale):
    h, w = image.shape[:2]       
    H = int(h * scale[0])
    W = int(w * scale[1])
    if len(image.shape) == 3:
        imgOut = np.zeros((H,W,3), np.uint8)
    else:
        imgOut = np.zeros((H,W), np.uint8)
    for X in range(0, H):
        for Y in range(0, W):
            x = X / scale[0]
            y = Y / scale[1]
            x_int = int(x)
            y_int = int(y)
            dist = {
    
    }
            dist[0] = abs(x - x_int) + abs(y - y_int)
            dist[1] = abs(x - (x_int + 1)) + abs(y - y_int)
            dist[2] = abs(x - x_int) + abs(y - (y_int + 1))
            dist[3] = abs(x - (x_int + 1)) + abs(y - (y_int + 1))
            min_dist = min(dist)
            if min_dist == 0:
                imgOut[X,Y] = image[x_int, y_int]
            elif min_dist == 1:
                imgOut[X,Y] = image[x_int + 1, y_int]
            elif min_dist == 2:
                imgOut[X,Y] = image[x_int, y_int + 1]
            elif min_dist == 3:
                imgOut[X,Y] = image[x_int + 1, y_int + 1]
    return imgOut

img = cv2.imread("explorer.png", cv2.IMREAD_COLOR)
img_out = ft_nearestScaling(img, (1.35,1.3))
cv2.imshow('img', img)
cv2.imshow('img_out', img_out)

cv2.waitKey(0)
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/family5love/article/details/117148765