OpenCV学习笔记(十)

图像质量

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)


在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值