【python-opencv】opencv基础操作之二
opencv_2
1 图像的基本操作 |
1.4 图像的基本信息 |
图像的基本信息有:行、列、通道、像素数目和图像数据类型
In [1]:
import cv2
import requests
import time
from PIL import Image
from io import BytesIO
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
# set None proxy
import os
os.environ['no_proxy'] = '*'
In [2]:
# draw a cvMat using plt.
def draw(image):
plt.figure() # 设置画布
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()
In [3]:
# get file(of Internet picture)
class InternetPicture:
def __init__(self, url):
# attribute
self.url = url
self.img = None
# initial method
self.get() # self代表类的实例, 而非类本身,所以self.get(self)--> self.get()
self.show()
self.info()
# get Internet Picture
def get(self):
for i in range(5): # 多获取几次,增加成功率
start=time.time()
file = requests.get(self.url)
img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1) #file.content 是读取的远程文件的字节流
print(i + 1, ' time of Requesting Time:', time.time()-start)
self.img = img
print('get a file of:', type(img))
# show image
def show(self):
#using plt draw picture
plt.figure() # 设置画布
plt.imshow(cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB))
plt.show()
def info(self):
# using cv2 to read Internet file of picture
color_img = self.img
gray_img = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
# 获取信息:行,列,通道,像素数目,图像数据类型
print('获取信息:行,列,通道,像素数目,图像数据类型')
print(color_img.shape, color_img.size, color_img.dtype)
print(gray_img.shape, gray_img.size, gray_img.dtype)
print('self.img: get a numpy image')
print('self.show(): show a numpy image using plt')
# using PIL.Image to read Internet file of picture
## image = Image.open(BytesIO(file.content))
## image.show()
In [4]:
url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565165195&di=0ae43289971ee5b3cdc36eb9c9612a0a&imgtype=jpg&er=1&src=http%3A%2F%2Fvpic.video.qq.com%2F3388556%2Fx0540ujyh6i_ori_3.jpg"
pic = InternetPicture(url)
1.5 在图像上输出文本 |
在处理图片的时候,我们经常会需要把一些信息直接以文字的形式输出在图片上,我们可以用cv2.putText函数实现
函数原型:putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
参数依次是:图像、文本信息、位置、字体、颜色(RGB)、线粗(缺省值为1)、等等
In [5]:
url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565234351&di=92352e792456d5f5b366cf9a7f9ba266&imgtype=jpg&er=1&src=http%3A%2F%2Fvpic.video.qq.com%2F3388556%2Fo0398rwyrwy_ori_3.jpg"
pic = InternetPicture(url)
In [6]:
img = pic.img
cv2.putText(img, 'Hello World', (500, 350), 0, 0.5, (255, 255, 255), 1, 100) # 最后一个参数是100,让字体更平滑
draw(img)
1.6 图像平移 |
图像的平移其实就是仿射变换的特殊形式,下一节会仔细讲仿射变换。
示例代码(将图像平移到到点(200,100)处):
In [7]:
img = pic.img
print(type(img))
rows, cols , _= img.shape # 默认读取了3个通道
M = np.float32([[1, 0, 200], [0, 1, 100]])
dst = cv2.warpAffine(img, M, (cols, rows))
draw(dst)
1.6 图像平移 |
有时需要将图像对R、G、B三个通道分别进行操作
示例代码:
In [8]:
img = pic.img
img2 = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
b, g, r = cv2.split(img2)
# 创建画布, 把四个图画在一个画布里面
plt.figure(figsize=(15,10))
# (1,1)
ax1 = plt.subplot(2, 2, 1)
ax2 = plt.subplot(2, 2, 2)
ax3 = plt.subplot(2, 2, 3)
ax4 = plt.subplot(2, 2, 4)
# 选择ax1
plt.sca(ax1)
plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
# 选择ax2
plt.sca(ax2)
plt.imshow(cv2.cvtColor(b, cv2.COLOR_BGR2RGB))
# 选择ax3
plt.sca(ax3)
plt.imshow(cv2.cvtColor(g, cv2.COLOR_BGR2RGB))
# 选择ax4
plt.sca(ax4)
plt.imshow(cv2.cvtColor(r, cv2.COLOR_BGR2RGB))
Out[8]:
In [ ]: