图像质量
import cv2 as cv
img = cv.imread("/home/xxc/1.jpg",1)
cv.imwrite("image.jpg",img,[cv.IMWRITE_JPEG_QUALITY,90])
#0-100,有损压缩
img[100,100] = (255,0,0) #(100,100位置写入蓝色bgr
图片裁剪
import cv2
img = cv2.imread("1.jpg", 1)
cv2.imshow("input image", img)
imgInfo = img.shape
#裁剪位置x:100-200,y:100-300
dst = img[100:200, 100:300]
cv2.imshow("image", dst)
cv2.waitKey(0)
图片位移
import cv2
import numpy as np
img = cv2.imread("1.jpg", 1)
cv2.imshow("input image", img)
imgInfo = img.shape #获取宽高信息
height = imgInfo[0]
width = imgInfo[1]
matShift = np.float32([[1, 0, 100], [0, 1, 200]])#2*3的矩阵
dst = cv2.warpAffine(img, matShift, (height, width))
cv2.imshow("image", dst)
cv2.waitKey(0)
import cv2
import numpy as np
img = cv2.imread("1.jpg", 1)
cv2.imshow("input image", img)
imgInfo = img.shape #获取宽高信息
dst = np.zeros(img.shape, np.uint8)
height = imgInfo[0]
width = imgInfo[1]
for i in range(0, height):
for j in range(0, width-100):
dst[i, j+100] = img[i, j]
cv2.imshow("image", dst)
cv2.waitKey(0)
图片镜像
import cv2
import numpy as np
img = cv2.imread("1.jpg", 1)
cv2.imshow("input image", img)
imgInfo = img.shape #获取宽高信息
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
newImgInfo = (height*2, width, deep)
dst = np.zeros(newImgInfo, np.uint8)
for i in range(0, height):
for j in range(0, width):
dst[i, j] = img[i, j]
dst[height*2-i-1, j] = img[i, j]
for i in range(0, width):
dst[height, i] = (255, 0, 0)#蓝线
cv2.imshow("image", dst)
cv2.waitKey(0)
图片缩放
import cv2
import numpy as np
img = cv2.imread("1.jpg", 1)
cv2.imshow("input image", img)
imgInfo = img.shape #获取宽高信息
height = imgInfo[0]
width = imgInfo[1]
matScale = np.float32([[0.5, 0, 0], [0, 0.5, 0]])
dst = cv2.warpAffine(img, matScale, (int(width/2), int(height/2)))
cv2.imshow("image", dst)
cv2.waitKey(0)
仿射变换
原图片src上三个点(左上,左下,右上)映射到目标dst图片
import cv2
import numpy as np
img = cv2.imread("1.jpg", 1)
cv2.imshow("input image", img)
imgInfo = img.shape #获取宽高信息
height = imgInfo[0]
width = imgInfo[1]
matSrc = np.float32([[0, 0], [0, height-1], [width-1, 0]])
matDst = np.float32([[50, 50], [300, height-200], [width-300, 100]])
matAffine = cv2.getAffineTransform(matSrc, matDst)
dst = cv2.warpAffine(img, matAffine, (width, height))
cv2.imshow("仿射变换", dst)
cv2.waitKey(0)