OpenCV圖像的基本操做 :使用包cv2,numpy數組
import cv2 import numpy as np img = cv2.imread('1.jpg') cv2.imshow('image',img) k = cv2.waitKey(0) "把一個RGB圖片當作一個3維的數組" img2 = img[20:300, 100:400] cv2.imshow('img2',img2) k = cv2.waitKey(0) "快速矩陣運算是numpy的優勢,批量像素修改不須要使用循環,使用矩陣運算" px=img[100,100] print(px) blue = img[100,100,0] print(blue) img[100:200,100:200]=[255,255,255] print(img[101,101]) cv2.imshow('image',img) k = cv2.waitKey(0) img = cv2.imread('1.jpg') " img.shape能夠得到圖像的形狀,返回值是一個包含行數,列數,通道數的元組" " 灰度圖,返回值僅有行數和列數" print(img.shape) "img.size能夠返回圖像的像素數目" print(img.size) "圖像的特定區域 拷貝操做" logo = img[30:130, 330:430] img[100:200,100:200] = logo cv2.imshow('image',img) k = cv2.waitKey(0) "拆分及合併圖像通道 對RGB三個通道分別操做" "cv2.split()是比較耗時的操做,能用numpy就儘可能使用" r,g,b=cv2.split(img)#拆分 #img=cv2.merge(r,g,b)#合併 cv2.imshow('b',b) k = cv2.waitKey(0) img[:,:,2]=0 cv2.imshow('img',img) k = cv2.waitKey(0)