OpenCV Lesson 1: Basic I/O scripts

in opencv-3 •  7 years ago  (edited)

I decided to document what I learn about OpenCV as my days going.
This is day 1: input/output scripts.

Let's dive right into it.
We have input fuction: cv2.imread(filename, flags)
and output function: cv2.imwrite(filename, img, params)

These function is so basic and simple as Hello world! for OpenCV.
So the difference between opencv 2 and opencv 3 is that in opencv 3x, they name flags in a manner that relate to the function to which it refers to. So in cv2.imread(), flags would be something like cv2.IMREAD_

For example: we need to read an image directly to grayscale in the main folder of the project (which is the same folder of the running code):


import cv2

image_grayscale = cv2.imread("sample.jpg", cv2.IMREAD_GRAYSCALE)

And this is the table about all the flags for cv2.imread() in opencv 3.3:

FlagsContent
IMREAD_UNCHANGEDIf set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALEIf set, always convert image to the single channel grayscale image.
IMREAD_COLORIf set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTHIf set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLORIf set, the image is read in any possible color format.
IMREAD_LOAD_GDALIf set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2If set, always convert image to the single channel grayscale image and the image size reduced 1/2
IMREAD_REDUCED_COLOR_2If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATIONIf set, do not rotate the image according to EXIF's orientation flag.

And this table is for cv2.imwrite() params:

ParamsContent
IMWRITE_JPEG_QUALITYFor JPEG, it can be a quality from 0 to 100 (the higher is the better). Default value is 95.
IMWRITE_JPEG_PROGRESSIVEEnable JPEG features, 0 or 1, default is False.
IMWRITE_JPEG_OPTIMIZEEnable JPEG features, 0 or 1, default is False.
IMWRITE_JPEG_RST_INTERVALJPEG restart interval, 0 - 65535, default is 0 - no restart.
IMWRITE_JPEG_LUMA_QUALITYSeparate luma quality level, 0 - 100, default is 0 - don't use.
IMWRITE_JPEG_CHROMA_QUALITYSeparate chroma quality level, 0 - 100, default is 0 - don't use.
IMWRITE_PNG_COMPRESSIONFor PNG, it can be the compression level from 0 to 9. A higher value means a smaller size and longer compression time. If specified, strategy is changed to IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY). Default value is 1 (best speed setting).
IMWRITE_PNG_STRATEGYOne of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_RLE.
IMWRITE_PNG_BILEVELBinary level PNG, 0 or 1, default is 0.
IMWRITE_PXM_BINARYFor PPM, PGM, or PBM, it can be a binary format flag, 0 or 1. Default value is 1.
IMWRITE_WEBP_QUALITYFor WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used.
IMWRITE_PAM_TUPLETYPEFor PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format.

Source: opencv 3.3.0 documents

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://docs.opencv.org/master/d4/da8/group__imgcodecs.html