The second phase of python-opencv: detailed explanation of imwrite 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, and opencv is a third party that provides technical and functional support for computer vision programming. The library is naturally a content that needs to be studied. This article will introduce one of the more basic functions - the imwrite function, which stores our edited image matrix in the form of a file , and forms an echo relationship with our imread function in the first issue. ( Sing folk songs (bushi) )

This article still invites the character "Sister Wendy" in "Famine" as our example operation object today ( Wendy: Where are my flowers ), as shown in the figure below. Not much to say, I am Mr. Kamen Black, and I will start today's study immediately.

201bd4c6cf90495aa32a1f22efa10d00.png

Text content:

56b8d441e6d74b53b6c2f8848c71de66.jpeg

 

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

① Preparation before use:

First of all, it is still to call the third-party library of opencv, as the major premise for us to use the imwrite function.

import cv2

②Grammar description:

cv2.imwrite(filename,img,params)

Among them, the data type of filename is const String&, and the parameter to be filled in here is the path we choose to save the edited picture. It should be noted that suffixes such as jpg and png should be added, otherwise there will be terrible Punishment ( referring to reporting an error, it is really horrible! );

           The data type of img is array (that is, the array type). Generally, the image to be filled in here is an 8-bit single-channel or 3-channel (with BGR channel sequence) image, but there are exceptions to everything (rules are used to break de ! ), as follows:

The imwrite function selects the format of the image based on the file extension. In general, only 8-bit single-channel or 3-channel (with BGR channel order) images can be saved using this function, with the following exceptions:

▶ For PNG, JPEG2000 and TIFF formats, 16-bit unsigned (CV_16U) images can be saved.


▶ 32-bit floating point (CV_32F) images can be saved in PFM, TIFF, OpenEXR and Radiance HDR formats; 3-channel (CV_32FC3) TIFF images can be saved using LogLuv high dynamic range encoding (4 bytes per pixel).


▶ You can use this function to save PNG images with an alpha channel. For this, create an 8-bit (or 16-bit) 4-channel image BGRA with the alpha channel last. Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535.


If format, depth or channel order is different, use Mat::convertTo and cv::cvtColor to convert before saving. Alternatively, use the generic FileStorage I/O functions to save images in XML or YAML format.

             The data type of params is const int&, which can not be written (there is a default state), or can be written multiple times ( just for fun ). For the specific format, please refer to the example operation section. The parameters to be filled in here are used to indicate that the picture is to be saved in a specific format, and some parameters can also be represented by corresponding integers, as shown below:

The classification and function of params
cv2.IMWRITE_JPEG_QUALITY For JPEG it can be a quality from 0 to 100 (higher is better). The default value is 95.
cv2.IMWRITE_JPEG_PROGRESSIVE Enable JPEG function, 0 or 1, default is False.
cv2.IMWRITE_JPEG_OPTIMIZE Enable JPEG function, 0 or 1, default is False.
cv2.IMWRITE_JPEG_RST_INTERVAL JPEG restart interval, 0 - 65535, default 0 - no restart.
cv2.IMWRITE_JPEG_LUMA_QUALITY Separate brightness quality level, 0 - 100, default 0 - not used.
cv2.IMWRITE_JPEG_CHROMA_QUALITY Separate chroma quality level, 0 - 100, default 0 - not used.
cv2.IMWRITE_PNG_COMPRESSION For PNG, it can be a compression level from 0 to 9. Higher values ​​mean smaller sizes and longer compression times. If specified, the strategy changes to IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY). The default is 1 (the best speed setting).
cv2.IMWRITE_PNG_STRATEGY One of the varieties: ImwritePNGFlags , which defaults to IMWRITE_PNG_STRATEGY_RLE.
cv2.IMWRITE_PNG_BILEVEL Binary level PNG, 0 or 1, default is 0.
cv2.IMWRITE_PXM_BINARY For PPM, PGM or PBM, it can be a binary format flag, 0 or 1. The default is 1.
cv2.IMWRITE_EXR_TYPE  
cv2.IMWRITE_WEBP_QUALITY

Override EXR storage type (default is FLOAT (FP32))

For WEBP it can be a quality from 1 to 100 (higher is better). By default (without any parameters), if the quality is higher than 100, lossless compression is used.

cv2.IMWRITE_PAM_TUPLETYPE For PAM, set the TUPLETYPE field to the appropriate string value defined for the format.
cv2.IMWRITE_TIFF_RESUNIT For TIFF, used to specify the DPI resolution unit to set; see libtiff documentation for valid values.
cv2.IMWRITE_TIFF_XDPI For TIFF, it is used to specify the X-direction DPI.
cv2.IMWRITE_TIFF_YDPI For TIFF, it is used to specify the DPI in the Y direction.
cv2.IMWRITE_TIFF_COMPRESSION For TIFF, specifies the image compression scheme. See libtiff for the integer constants corresponding to the compressed format. Note that for images with a depth of CV_32F, only libtiff's SGILOG compression scheme is used. For other supported depths, the compression scheme can be specified via this flag; LZW compression is the default.
cv2.IMWRITE_JPEG2000_COMPRESSION_X1000 For JPEG2000, specifies the target compression ratio (multiplied by 1000). The value can be from 0 to 1000. The default value is 1000

 ( Whispering BB: There are so many, but there are not many commonly used ones

③Instance operation:

1. Normal Wendy sisters:

>>> import cv2
>>> img=cv2.imread("F://wendy.png")
>>> cv2.imwrite("F://1.jpeg",img)
True

6cde8861276b4b8ea722ff48d0141f43.jpeg

2. Lossless version of Wendy sisters:

>>> cv2.imwrite("F://4.jpeg",img,[cv2.IMWRITE_JPEG_QUALITY,100])
True

3b9aa501dba4485a8873eead163140ad.jpeg

3. Battle-damaged Wendy sisters:

>>> cv2.imwrite("F://2.jpeg",img,[cv2.IMWRITE_JPEG_QUALITY,10])
True

5506883d5ae54521af4c0acedf6c6b0b.jpeg

4. Super High School Level Battle Damaged Wendy Sisters:

>>> cv2.imwrite("F://3.jpeg",img,[cv2.IMWRITE_JPEG_QUALITY,2])
True

a1f4acd104e94721b19488038ed4dd51.jpeg

5. Multi-parameter demonstration:

>>> cv2.imwrite("F://5.jpeg",img,[cv2.IMWRITE_JPEG_LUMA_QUALITY,10,cv2.IMWRITE_JPEG_QUALITY,100])
True

954871c634a0431195a5fdb76462729b.jpeg

>>> cv2.imwrite("F://6.jpeg",img,[cv2.IMWRITE_JPEG_QUALITY,100,cv2.IMWRITE_JPEG_LUMA_QUALITY,10])
True

eb901841e9f140f9a9ce66548d97921c.jpeg

Through the above two chestnut examples, we can conclude that if there is a conflict, the parameter settings at the back end dominate. Maybe it's the so-called Qingmei who can't beat the sky, and the latecomers will prevail.

Conclusion: Well, the above is all the content. I hope you will pay more attention, like, and bookmark. This will be of great help to me. Guokang Jiaan, see you next time! ! ! yo-yo~~

26d53210a1ce44f293c2d4a2ded15e82.jpeg7cf5685363524cf89f3e960333b03482.jpeg

Reference article: OpenCV Note 1: Use imwrite function to save pictures

 

 

 

Guess you like

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