影像基本操作
環境配置地址:
- Anaconda
- Python_whl
- eclipse:按照自己的喜好,選擇一個能debug就好

資料讀取-影像
- cv2.IMREAD_COLOR:彩色影像
- cv2.IMREAD_GRAYSCALE:灰度影像
import cv2 #opencv讀取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
img=cv2.imread('cat.jpg')
img
array([[[142, 151, 160],
? [146, 155, 164],
? [151, 160, 169],
? …,
? [156, 172, 185],
? [155, 171, 184],
? [154, 170, 183]],
? [[107, 118, 126],
? [112, 123, 131],
? [117, 128, 136],
? …,
? [155, 171, 184],
? [154, 170, 183],
? [153, 169, 182]],
? [[108, 119, 127],
? [112, 123, 131],
? [118, 129, 137],
? …,
? [154, 170, 183],
? [153, 169, 182],
? [152, 168, 181]],
? …,
? [[162, 186, 198],
? [157, 181, 193],
? [142, 166, 178],
? …,
? [181, 204, 206],
? [170, 193, 195],
? [149, 172, 174]],
? [[140, 164, 176],
? [147, 171, 183],
? [139, 163, 175],
? …,
? [167, 187, 188],
? [123, 143, 144],
? [104, 124, 125]],
? [[154, 178, 190],
? [154, 178, 190],
? [121, 145, 157],
? …,
? [185, 198, 200],
? [130, 143, 145],
? [129, 142, 144]]], dtype=uint8)
#影像的顯示,也可以創建多個視窗
cv2.imshow('image',img)
# 等待時間,毫秒級,0表示任意鍵終止
cv2.waitKey(0)
cv2.destroyAllWindows()
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img.shape
(414, 500, 3)
img=cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
img
array([[153, 157, 162, …, 174, 173, 172],
? [119, 124, 129, …, 173, 172, 171],
? [120, 124, 130, …, 172, 171, 170],
? …,
? [187, 182, 167, …, 202, 191, 170],
? [165, 172, 164, …, 185, 141, 122],
? [179, 179, 146, …, 197, 142, 141]], dtype=uint8)
img.shape
(414, 500)
#影像的顯示,也可以創建多個視窗
cv2.imshow('image',img)
# 等待時間,毫秒級,0表示任意鍵終止
cv2.waitKey(10000)
cv2.destroyAllWindows()
#保存
cv2.imwrite('mycat.png',img)
True
type(img)
numpy.ndarray
img.size
207000
img.dtype
dtype(‘uint8’)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291267.html
標籤:其他
