opencv_2javascript
1 圖像的基本操做 |
1.4 圖像的基本信息 |
圖像的基本信息有:行、列、通道、像素數目和圖像數據類型css
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'] = '*'
# draw a cvMat using plt.
def draw(image):
plt.figure() # 設置畫布
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.show()
# 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()
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函數實現html
函數原型:putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])html5
參數依次是:圖像、文本信息、位置、字體、顏色(RGB)、線粗(缺省值爲1)、等等java
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)
img = pic.img
cv2.putText(img, 'Hello World', (500, 350), 0, 0.5, (255, 255, 255), 1, 100) # 最後一個參數是100,讓字體更平滑
draw(img)
1.6 圖像平移 |
圖像的平移其實就是仿射變換的特殊形式,下一節會仔細講仿射變換。python
示例代碼(將圖像平移到到點(200,100)處):jquery
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三個通道分別進行操做linux
示例代碼:android
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))