【python-opencv】opencv基础操作之二
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【python-opencv】opencv基础操作之二,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含218279字,纯文字阅读大概需要312分钟。
内容图文
opencv_2
<style></style> <style></style> <style></style> <style></style>
?
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 time of Requesting Time: 0.04800081253051758 2 time of Requesting Time: 0.06759953498840332 3 time of Requesting Time: 0.043440818786621094 4 time of Requesting Time: 0.040445566177368164 5 time of Requesting Time: 0.040354013442993164 get a file of: <class 'numpy.ndarray'>? ?
获取信息:行,列,通道,像素数目,图像数据类型 (360, 640, 3) 691200 uint8 (360, 640) 230400 uint8 self.img: get a numpy image self.show(): show a numpy image using plt?
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)?
1 time of Requesting Time: 0.03394651412963867 2 time of Requesting Time: 0.03303980827331543 3 time of Requesting Time: 0.03547406196594238 4 time of Requesting Time: 0.035376787185668945 5 time of Requesting Time: 0.044396400451660156 get a file of: <class 'numpy.ndarray'>? ?
获取信息:行,列,通道,像素数目,图像数据类型 (426, 640, 3) 817920 uint8 (426, 640) 272640 uint8 self.img: get a numpy image self.show(): show a numpy image using pltIn?[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)?
<class 'numpy.ndarray'>? ?
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]:
<matplotlib.image.AxesImage at 0x7f0eb7616b70>? In?[?]: ?
内容总结
以上是互联网集市为您收集整理的【python-opencv】opencv基础操作之二全部内容,希望文章能够帮你解决【python-opencv】opencv基础操作之二所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。