Python-opencv Issue 1: Detailed explanation of imread function

Summary: As we all know, computer vision ( Computer Version short for CV) is a major research direction in the development of artificial intelligence and robotics technology today , and opencv is a third party that specializes in providing technical and functional support for computer vision programming. Library is naturally a content that needs to be focused on. This blog will introduce one of the simpler functions in the python-opencv library: imread is the beginning of our study of cv, so as to open our door to the new world. ( Please show your true power in front of me. Sakura, who made a promise with you, orders you to lift the seal. )

 

This article still invites the protagonist "Wilson" from my favorite game "Don't Starve" as our example operation object today ( Wilson: I just stole the spider nest, please don't cue and thank you ), as shown in the picture below. Without further ado, let’s start today’s study immediately.

a76cbafd52864d17b223d72e4b8b1ac9.jpeg

Text part: 

print("祝大家每天快乐,love and peace!")

30a8ee630f6e44b39392c49cea024919.jpeg

①Preparation before use:

First of all, we still need to call the opencv library, but it is a bit special. Although it is called opencv, the imported one is cv2, maybe because of the second generation. ( What the second-generation Hokage ) (but when installing the library, the name of opencv is still used)

import cv2

② Grammar description:

img=cv2.imread(filename,flags)

Among them, the data type of filename is const string&, and what needs to be filled in here is the path of the image we want to read (usually the absolute path needs to be filled in, if conditions permit (self-experience), you can also fill in the relative path ) ;

           The data type of flags is int, which represents the loading flag. Its function is to specify the color type of the loaded image (the default value is 1). The following is a list of the various types of flags:

1. CV_LOAD_IMAGE_UNCHANGED = -1, this flag has been abandoned in the new version and can be ignored ( abandoned, don’t worry about it anymore )

2. CV_LOAD_IMAGE_GRAYSCALE = 0, returns gray image

3. CV_LOAD_IMAGE_COLOR = 1, return color image

4. CV_LOAD_IMAGE_ANYDEPTH = 2, if the image depth is 16 bits or 32 bits, the corresponding depth will be returned.

Otherwise, return an 8-bit image

5. CV_LOAD_IMAGE_ANYCOLOR = 4, returns all colors (English note: any color)

6. CV_LOAD_IMAGE_IGNORE_ORIENTATION = 128, ignore any rotation (English note: no rotate)

Blogger Wilson’s warm reminder:

1. If a conflict occurs (the numbers are different, does it necessarily conflict? You need to judge the specific situation), such as CV_LOAD_IMAGE_GRAYSCALE | CV_LOAD_IMAGE_COLOR, the smaller value will be used, and the gray picture will be returned here ( what Kong Rong let pear )

2. If you want to load the most realistic image , you need to select CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR 

3. Because flags is of int type and there is no floating point decimal situation, the value can also be obtained like this:

flag > 0 returns a 3-channel color image

flag = 0 returns gray image

flag < 0 returns the loaded image containing an alpha channel

③Example demonstration:

1. The most authentic Wilson:

import cv2
img1=cv2.imread("F://dontstarve.jpg",2|4)
cv2.namedWindow("truest",0)
cv2.resizeWindow('truest', 700, 500)
cv2.imshow("truest", img1)

0f6a596d017c406eaef294ef433b4e34.png

2. Colorful Wilson:

img2=cv2.imread("F://dontstarve.jpg",1)
cv2.namedWindow("colorful",0)
cv2.resizeWindow('colorful', 700, 500)
cv2.imshow("colorful", img2)

562ba20dc305445490c5698883fd6063.png

 3. Wilson, whose net suppresses the gray clouds:

img3=cv2.imread("F://dontstarve.jpg",0)
cv2.namedWindow("gray",0)
cv2.resizeWindow('gray', 700, 500)
cv2.imshow("gray", img3)
cv2.waitKey(0)
cv2.destroyAllWindows()

8e74c55e1a1d45a18d35a8fb11749fc8.png

Internal PS: I just stole the spider nest, and now I am being chased by spiders, and I have to make materials. Weibao is feeling miserable~~~ 

Conclusion: Okay, that’s all the content above. I hope everyone will pay more attention, like and collect it. This will be of great help to me. May the country be well and the family be safe, see you next time! ! ! Yo-yo~~

91a2a17e31304f82a286aa1bc6254d17.jpeg 

Guess you like

Origin blog.csdn.net/m0_55320151/article/details/127001967