Python-Opencv基础知识

一、Opencv在Python下表示图像

opencv在python中以numpy的array来表示

单通道的灰度图像C++下表示为cv::Mat矩阵           
多通道的彩色图像H x W x C(高度 x 宽度 x 通道)

注:对于常见的RGB彩色图像,opencv使用的是BGR格式,如下例:

import numpy as np
import cv2
import matplotlib.pyplot as plt

img = np.array([
    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
    [[255, 255, 0], [255, 0, 255], [0, 255, 255]],
    [[255, 255, 255], [128, 128, 128], [0, 0, 0]],
                ], dtype=np.uint8)

# 用matplotlib存储
plt.imsave('img_pyplot.png', img)

# 用OpenCV存储
cv2.imwrite('img_cv2.png', img)
图1-1  matplotlib画出的图像RGB
图1-2 opencv的BGR图像

二、图像基本操作

1.图像创建、读取、复制、显示、保存

参考博客:https://blog.csdn.net/sunny2038/article/details/9057415

创建直接使用numpy中的矩阵创建方法
读取cv2.imread()
复制copy()
显示cv2.namedWindow() + cv2.imshow()
保存cv2.imwrite()
import numpy as np
import cv2

#读取图像
img1 = cv2.imread('scene.jpg')
#创建图像
img2 = np.zeros(img1.shape, np.uint8)
#复制图像
img3 = img1.copy()

#显示图像
cv2.namedWindow("Island")
cv2.imshow("Island",img1)
cv2.waitKey(0)
cv2.destroyAllWindows()
#保存图像
cv2.imwrite("scene_copy.jpg",img3)

2.图像的几何变换操作

平移cv2.warpAffine(变换的图像,平移矩阵,变换后大小)
缩放cv2.resize(变换的图像,缩放后大小,缩放比例因子,插值方法)
旋转cv2.getRotationMatrix2D(旋转中心,旋转角度,缩放比例)
翻转cv2.flip(para) para=0:纵向翻转 para=1:横向翻转 para=-1:同时都翻
仿射(旋转并拉伸)cv2.getAffineTransform(pos1,pos2)3个点前后位置  +  cv2.warpAffine(变换的图像,平移矩阵,变换后大小)
透射cv2.getPerspectiveTransform(pts1,pts2)4个点前后位置 + cv2.warpAffine()

示例代码:

  1. 平移

import cv2
import numpy as np
import matplotlib.pyplot as plt 

#读取图像
img = cv2.imread('scene.jpg')
print(img.shape)
#定义平移矩阵
M = np.float32([[1,0,100],
               [0,1,50]])
#变换后的大小
rows,cols = img.shape[:2]
#平移
translation = cv2.warpAffine(img,M,(cols,rows))     #############不知道到底是啥########
#画图(plt与cv2通道是不一样的)
plt.subplot(121)
##cv2.namedWindow("original")
##cv2.imshow("original",img)
plt.imshow(img)
plt.subplot(122)
##cv2.namedWindow("Translation")
##cv2.imshow("Translation",translation)
plt.imshow(translation)
plt.show()
图2-1 平移示例图

2.缩放

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('scene.jpg')
# 插值:interpolation
# None本应该是放图像大小的位置的,后面设置了缩放比例,
#所有就不要了
res1 = cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
#直接规定缩放大小,这个时候就不需要缩放因子
height,width = img.shape[:2]
res2 = cv2.resize(img,(2*width,2*height),interpolation=cv2.INTER_CUBIC)
plt.subplot(131)
plt.imshow(img)
plt.subplot(132)
plt.imshow(res1)
plt.subplot(133)
plt.imshow(res2)
plt.show()
图2-2 缩放示例(x,y分别扩大2倍)

3.旋转

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('scene.jpg')
rows,cols = img.shape[:2]
#第一个参数旋转中心,第二个参数旋转角度,第三个参数:缩放比例
M = cv2.getRotationMatrix2D((cols/2,rows/2),45,1)
#第三个参数:变换后的图像大小
res = cv2.warpAffine(img,M,(cols,rows))

plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(res)
plt.show()
图2-3 旋转

4.翻转

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('scene.jpg')
#横向翻转
res1 = cv2.flip(img,1)
#纵向翻转
res2 = cv2.flip(img,0)
#同时翻转
res3 = cv2.flip(img,-1)
plt.subplot(141)
plt.imshow(img)
plt.subplot(142)
plt.imshow(res1)
plt.subplot(143)
plt.imshow(res2)
plt.subplot(144)
plt.imshow(res3)
plt.show()
图2-4 翻转

5.仿射

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('scene.jpg')
rows,cols = img.shape[:2]
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
#第三个参数:变换后的图像大小
res = cv2.warpAffine(img,M,(cols,rows))
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(res)
plt.show()
图2-5 仿射

6.透射

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('scene.jpg')
rows,cols = img.shape[:2]
pts1 = np.float32([[56,65],[238,52],[28,237],[239,240]])
pts2 = np.float32([[0,0],[200,0],[0,200],[200,200]])
M = cv2.getPerspectiveTransform(pts1,pts2)
res = cv2.warpPerspective(img,M,(200,150))
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
plt.imshow(res)
plt.show()
图2-6 透射

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值