基本操作
imread(filename[,color_flag]):读取图片。
flags:
- 0:灰度
- 1:彩色
- -1:Loads image as such including alpha channel
- opencv读取图像是BGR,而matplotlib显示图像是RGB,所以在显示彩色图像时应该进行颜色转换。
imwrite(filename,img[,params]):保存图片 - filename – Name of the file.
- image – Image to be saved.
- params – Format-specific save parameters encoded as pairs paramId_1, paramValue_1, paramId_2, paramValue_2, … . The following parameters are currently supported:
- For JPEG, it can be a quality ( CV_IMWRITE_JPEG_QUALITY ) from 0 to 100 (the higher is the better). Default value is 95.
- For WEBP, it can be a quality ( CV_IMWRITE_WEBP_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.
- For PNG, it can be the compression level (CV_IMWRITE_PNG_COMPRESSION ) from 0 to 9. A higher value means a smaller size and longer compression time. Default value is 3.
- For PPM, PGM, or PBM, it can be a binary format flag ( CV_IMWRITE_PXM_BINARY ), 0 or 1. Default value is 1.
cvtColor(src,code[,dst[,dstcn]]):颜色格式变化
- src – input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision floating-point.
- dst – output image of the same size and depth as src.
- code – color space conversion code (see the description below).
- dstCn – number of channels in the destination image; if the parameter is 0, the number of the channels is derived automatically from src and code .
imshow(winname,mat): 展示图片
图片缩放
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation] ] ] ] ) → \rightarrow →out: dst
- src:input image
- dst: output image
- fx : scale factor along the horizontal axis,when it equals 0, it is computed as (double)dsize.width/src.cols 。大于0小于1,是缩小,大于1则为放大。
- fy : scale factor along the vertical axis; when it equals 0, it is computed as
(double)dsize.height/src.rows。大于0小于1,是缩小,大于1则为放大。 - interpolation – interpolation method:
- – INTER_NEAREST - a nearest-neighbor interpolation
- – INTER_LINEAR - a bilinear interpolation (used by default)
- – INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
- – INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
- – INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
图片仿射变换
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) → \rightarrow →dst
- src – input image.
- dst – output image that has the size dsize and the same type as src .
- M – 2 × 3 transformation matrix.对应图片上的M矩阵。
- dsize – size of the output image.
- flags – combination of interpolation methods (see resize() ) and the optional flag
WARP_INVERSE_MAP that means that M is the inverse transformation ( dst ! src ). - borderMode – pixel extrapolation method (see borderInterpolate()); when
borderMode=BORDER_TRANSPARENT , it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function. - borderValue – value used in case of a constant border; by default, it is 0.
最后的两个参数可以是元组出现的形式,表示图片的边界是多少,定义仿射变换完成之后的图片的shape大小。可以是原图像,或者是原图像的扩展图像。
图片平移矩阵
先得到图片的平移矩阵
再利用仿射变换达到目的
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
→
\rightarrow
→dst
如下例子:
M = np.float32([[1,0,100],[0,1,50]])
dst = cv2.warpAffine(img,M,(cols+100,rows+50))
图片旋转矩阵
先得到图片的旋转矩阵
cv2.getRotationMatrix2D(center, angle, scale)
→
\rightarrow
→retval(返回值类型)
通过给出的角度,计算一个2维旋转的矩阵。
- center – Center of the rotation in the source image.(旋转的中心点)
- angle – Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).(旋转的角度)
- scale – Isotropic scale factor.(各同乡的比例因子)
- map_matrix – The output affine transformation, 2x3 floating-point matrix.(输出的映射矩阵)
再利用仿射变换来达到目的
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
→
\rightarrow
→dst
得到仿射变换矩阵
cv2.getAffineTransform(src, dst) → \rightarrow →retval
- src – Coordinates of triangle vertices(三角形顶点) in the source image.
- dst – Coordinates of the corresponding triangle vertices(三角形顶点) in the destination image.
- 输出:为仿射变换矩阵M。
再利用仿射变换来达到目的
cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
→
\rightarrow
→dst
透视变换矩阵
先得到透视变换矩阵:
cv2.getPerspectiveTransform(src, dst)
→
\rightarrow
→retval
- src – Coordinates of quadrangle vertices(四边形顶点) in the source image.
- dst – Coordinates of the corresponding quadrangle vertices in the destination image.
- The function calculates the 3 × 3 matrix of a perspective transform so that:
[ t i x i ′ t i y i ′ t i ] = m a p _ m a t r i x ∗ [ x i y i 1 ] \begin{bmatrix} tixi'\\ tiyi'\\ ti \end{bmatrix} = map\_matrix* \begin{bmatrix} xi\\ yi\\ 1 \end{bmatrix} ⎣⎡tixi′tiyi′ti⎦⎤=map_matrix∗⎣⎡xiyi1⎦⎤
再利用仿射变换来达到目的
cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
→
\rightarrow
→dst
- src – input image.
- dst – output image that has the size dsize and the same type as src .
M – 3 × 3 transformation matrix. - dsize – size of the output image.元组形式
- flags – combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation ( dst ! src ).
- borderMode – pixel extrapolation method (BORDER_CONSTANT or BORDER_REPLICATE).
- borderValue – value used in case of a constant border; by default, it equals 0.