Professional and work are not involved in this, pure understanding, study notes, infringement can be deleted

Load, convert, save, adjust, crop images

When the imread method is called, the image data is converted to a familiar data type, a NumPy array

import cv2
import numpy as np
from matplotlib import pyplot asImage = cv2.imread("E:/sim_data-master/images/plane.jpg", cv2.imread_grayscale) # Each element contains three values corresponding to the blue, green and red components (BGR) image_bgr = cv2.imread("E:/sim_data-master/images/plane.jpg"#OpenCV uses BGR format by default, Image_rgb = cv2.cvtcolor (image_bgr, cv2.color_bgr2rgb) # Save the image cv2.imwrite("plane_new.jpg", image) # Resize the image to 50x50 pixels # Common image specifications in machine learning are:32x32,64x64,96x96256x256
image_50x50 = cv2.resize(image, (50.50)) # clipping selects all rows and front128Column image_cropped = image [: :128]
Copy the code

Smooth and sharpen the image

To smooth an image, we transform the value of each pixel into the average value of its neighbors. In sharpening, we use a core that highlights the pixel itself. Sharpening can make the edges of an image stand out more

Image_blurry = cv2. Blur (image, (5.5)) # The size of this core determines the degree of smoothness, the larger the core, the smoother the resulting image. # manually smooth the kernel = np.ones((5.5)) / 25.0
image_kernel = cv2.filter2D(image, -1, kernel) # sharpen kernel = np.array([[0, -1.0], [...1.5, -1], [0, -1.0]])
image_sharp = cv2.filter2D(image, -1, kernel)

Copy the code

Enhance image

# image_enhanced = cv2. EqualizeHist (image) # Image_yuv = cv2.cvtColor(image_bgr, cv2.color_bgr2YUv) # Apply histogram equalization to the image: will transform the image, so that the pixel intensity distribution is wider.0] = cv2.equalizeHist(image_yuv[:, :, 0[image_rgb = cv2.cvtcolor (image_yuv, cv2.color_yuV2RGB)Copy the code

Color separation

Convert BGR format to HSV format (H, S, V for hue, saturation, and brightness respectively). Image_hsv = cv2.cvtcolor (image_bgr, cv2.color_bgr2HSv)50.100.50])
upper_blue = np.array([130.255.255Mask = cv2.inRange(image_hsv, lower_blue, upper_blue) mask = cv2.bitwise_and(image_bgr, Image_bgr, mask=mask) # Transform from BGR format to RGB format image_rgb = cv2.cvtcolor (image_bgr_masked, cv2.color_bgr2rgb)Copy the code

Image binarization (black and white processing)

image_grey = cv2.imread("E:/sim_data-master/images/plane_256x256.jpg", cv2.imREAD_grayscale) # Apply adaptive threshold processing to max_output_value =255Neighborhood_size = is used to determine the maximum intensity of the output pixel99
subtract_from_mean = 10image_binarized = cv2.adaptiveThreshold(image_grey,max_output_value,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, The cv2.adaptive_thresh_gaussian_c parameter sets the threshold of a pixel to the weighted sum of the intensity of adjacent pixels, Its weight is determined by gauss window. # Parameter Neighborhood_size block size (used to determine the neighborhood size of the pixel threshold) # parameter subtract_from_mean A constant used to manually adjust the threshold (subtracting the constant from the calculated threshold) # We can set this parameter to Cv2. ADAPTIVE_THRESH_MEAN_C, using the average value of adjacent pixels as the threshold: ADAPTIVE_THRESH_MEAN_C image_mean_threshold = cv2.adaptiveThreshold(image_grey,max_output_value,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,neighborhood_size,subtract_from_mean)Copy the code

Remove the background

# load the image and convert it to RGB format image_bgr = cv2.imread('images/plane_256x256.jpg'Image_rgb = cv2.cvtcolor (image_bgr, cv2.color_bgr2rgb);0.56.256.150# create initial mask mask = np.zeros(image_rgb.shape[:2Hash = np.zeros()1.65), np.float64)
fgdModel = np.zeros((1.65GrabCut (image_rgb, # rectangle mask, # rectangle mask, # rectangle bgdModel, # rectangle bgdModel, # rectangle bgdModel, A temporary array of prospects5, # number of iterations cv2.gC_init_with_rect) # Initialize with a defined rectangle # Create a mask that sets the part that is identified or likely to be background to0The rest is set to1
mask_2 = np.where((mask==2) | (mask==0), 0.1).astype('uint8'Image_rgb_nobg = image_RGB * mask_2[:, :, np.newaxis] # Display image plt.imshow(image_rGB_nobg), plt.axis("off")
Copy the code

Edge detection

Edge detection can help us remove areas with low information content and separate the areas with the highest information content from the original image. The Harris detector looks for a window (also known as a neighborhood) in which a small movement (think of it as a shaking window) causes a large change in pixel values within the window.

# Calculate the median pixel intensity image_gray = cv2.imread("E:/sim_data-master/images/plane_256x256.jpg", cv2.imread_grayscale) median_intensity = Np.median (image_gray) #0, (1.0 - 0.33) * median_intensity))
upper_threshold = int(min(255, (1.0 + 0.33Canny = cv2.Canny(image_gray, lower_threshold, upper_threshold)Copy the code

Corner detection

# set corner detector parameter block_size =2Aperture = represents the size of the window in corner detection29# represents the size of the Sobel operator free_parameter =0.04# used to control the severity of diagonal detection, the greater the value, Detector_responses = cv2. CornerHarris (image_gray,block_size,aperture,free_parameter) # Detector_responses = Cv2. dilate(DETECtor_responses, None) # Only retains detection results greater than the threshold and marks them with white threshold =0.02
image_bgr[detector_responses >threshold *detector_responses.max()] = [255.255.255[image_gray = cv2.cvtcolor (image_bgr, cv2.color_bgr2gray)Copy the code

The image matrix is converted to 1 – d data

Image = cv2.imread("E:/sim_data-master/images/plane_256x256.jpg", cv2.IMREAD_GRAYSCALE) # convert the image size to10x10
image_10x10 = cv2.resize(image, (10.10) # Convert the image data to a one-dimensional vector image_10x10.flatten()Copy the code

Color averages are encoded as features

# load image in BGR format image_bgr = cv2.imread("E:/sim_data-master/images/plane_256x256.jpg"Cv2. mean(image_bgr) # Swap channels Observation = Np. array([(Channels [)2], channels[1], channels[0[)]) # Displays the average number of observations for each color channelCopy the code

Encode color histograms into features

Image_rgb = cv2.cvtcolor (image_bgr, Cv2.color_bgr2rgb) # Create a list of features features = [] # calculate the histogram colors = ("r"."g"."b"Compute the histogram for each channel and add it to the feature listfor i, channel inEnumerate (colors): histogram = cv2.calcHist([image_rgb], # image [I], # None of color channels, # Mask not used [256], # histogram size [0.256] # Range features.extend(histogram) # Expand the eigenvalues of the sample into a one-dimensional array of observations = np.array(features).flatten() # Display the front of the sample5Number of characteristic values, Observation [0:5Color = (color = ())"r"."g"."b") # Draw a histogram for each channelfor i, channel inEnumerate (colors): histogram = cv2.calcHist([image_rgb], # image [I], # None of color channels, # Mask not used [256], # histogram size [0.256Plot (histogram, color = channel) plt.xlim([)0.256] # display plt.show()Copy the code