1. OpenCV reads pictures
1.1 brief introduction
The method of OpenCV to read the picture is cv2.imread(). The format of the read picture is BGR, which is the opposite of the format of the regular color image (RGB). This must be noted.
OpenCV displays the picture in cv2.imshow(), and the format is BGR.
Xiaosheng said in this O(∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩.
1.2 examples
1.2.1 Code
1 import cv2 2 3 4 def cv2_imread(): 5 # Picture path, relative path 6 image_path = "./fusion_datasets/1.jpg" 7 # Read picture,Format is BGR 8 image = cv2.imread(image_path) 9 # Show picture shapes 10 print("image_shape: ", image.shape) 11 # Zoom picture 12 width = int(image.shape[0] / 2) 13 height = int(image.shape[1] / 2) 14 image = cv2.resize(image, (height, width), interpolation=cv2.INTER_CUBIC) 15 # display picture 16 cv2.imshow('girl', image) 17 cv2.waitKey(0) 18 cv2.destroyAllWindows() 19 20 21 if __name__ == '__main__': 22 cv2_imread()
1.2.2 result display
2. OpenCV save picture
2.1 brief introduction
OpenCV saves the picture with cv.imwrite(filename, img), filename is the saved path name, and img is the picture to be saved.
But it's not so simple in practical application, (* ^ ^ *) I'll figure it out, O(∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩∩.
The following points should be noted:
1. There are three dimensions (height, width, channel) saved by cv2.
2. The saved image format is also BGR.
3. Convert numpy to np.uint8 format
2.2 examples
2.2.1 Code
1 import cv2 2 import numpy as np 3 import torchvision.transforms as transforms 4 5 6 def cv2_imwrite(): 7 # =============== The simulated picture is tensor data ============ # 8 # Picture path, relative path 9 image_path = "./fusion_datasets/1.jpg" 10 # Read the picture in the format( height, width, channel) 11 image = cv2.imread(image_path) 12 # Image to tensor,The picture changes to( channel, height, width),And set 0-255 Normalized to 0-, Reduce calculation 13 input_transform = transforms.Compose([ 14 transforms.ToTensor(), 15 ]) 16 # unsqueeze(0): The extension dimension is from three-dimensional to four-dimensional,(bathch_size, channel, height, width) 17 # This may be encountered in the actual experiment, so I can expand the dimension, or not 18 image = input_transform(image).unsqueeze(0) 19 20 # =============== Data preparation completed, start saving ============ # 21 # 1.First of all tensor Dimension reduction, 4-dimension reduction to 3-dimension reduction:image.squeeze(0) 22 # 2.tensor Turn to numpy:image.squeeze(0).numpy() 23 # 3.Transform dimension, from( channel, height, width)Change to( height, width, channel) 24 # 4.Data type by numpy Transform dimension np.uint8(This is the format of the picture.) 25 # 5.Save picture, execute function cv2.imwrite(filename, img),filename:Save pathname to, img:Saved pictures 26 image = np.transpose(image.squeeze(0).numpy() * 255.0, (1, 2, 0)).astype(np.uint8) 27 cv2.imwrite("./results/" + "girl.jpg", image) 28 29 30 if __name__ == '__main__': 31 cv2_imwrite()
2.2.2 result display
Picture saved~~
Open the picture to view ~ ~, wow, so beautiful(* ^ ▽ ^ *)!
3. Summary
Try to love everyone around you, pay, not necessarily harvest, but not pay, there must be no harvest! Give change to the street entertainer, not bargain with the peddler who is still at the stall in the middle of the night. May my blog help you!