>>> import cv2>>> import numpy as np>>> img = cv2.imread('messi5.jpg')>>> px = img[100,100]>>> print px[157 166 200]# accessing only blue pixel,opencv圖像存儲為大端格式:BGR>>> blue = img[100,100,0]>>> print blue157>>> green = img[100,100,1]>>> print green166>>> red = img[100,100,2]>>> print red200# modify the pixel values>>> img[100,100] = [255,255,255]>>> print img[100,100][255 255 255]
Numpy 是經(jīng)過優(yōu)化的快速矩陣計算庫,單獨讀寫某一個像素點速度很慢,以上幾個像素操作方法,其實更適合操作一個圖像區(qū)域。如果要操作單個像素點,推薦使用array.item() and array.itemset()
# accessing RED value>>> img.item(10,10,2)59# modifying RED value>>> img.itemset((10,10,2),100)>>> img.item(10,10,2)100
圖像的屬性主要包括圖像的行、列、像素的通道數(shù)、圖像的類型、像素的個數(shù)等。以下幾個函數(shù)主要訪問圖像的屬性。
# img.shape屬性返回圖像的行、列、顏色通道數(shù)(如果是彩色圖像)# 如果是灰度圖像,此屬性只返回圖像的行、列大小>>> print img.shape(342, 548, 3)# 圖像的總像素個數(shù)>>> print img.size562248#圖像每一個像素數(shù)據(jù)類型>>> print img.dtypeuint8#img.dtype is very important while debugging because a large number of errors in OpenCV-Python code is caused by invalid datatype.
典型操作,例如人眼檢測,最好先進行人臉檢測,然后在檢測到的人臉范圍內(nèi)進行人眼檢測,眼睛總是在臉上,因此先進行臉部檢測,可以大大縮小眼睛檢測的范圍。從而提高人眼檢測速度。
圖像的區(qū)域操作同樣使用numpy
# 將圖像的一個區(qū)域復(fù)制到另一個區(qū)域>>> roi = img[280:340, 330:390]>>> img[273:333, 100:160] = roi
>>> b,g,r = cv2.split(img)>>> img = cv2.merge((b,g,r))#切片操作>>> b = img[:,:,0]>>> img[:,:,2] = 0
cv2.split()
函數(shù)是一個耗時操作,謹(jǐn)慎使用。
cv2.copyMakeBorder()
函數(shù)用于為圖像畫邊框 ,函數(shù)的參數(shù)說明如下:
import cv2import numpy as npfrom matplotlib import pyplot as pltBLUE = [255,0,0]img1 = cv2.imread('opencv_logo.png')replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')plt.show()
以上操作后畫出的邊框示例如下:
主要學(xué)習(xí) cv2.add(), cv2.addWeighted()
兩個函數(shù)
numpy相加為取模計算
opecv的add函數(shù)為飽和計算
>>> x = np.uint8([250])>>> y = np.uint8([10])>>> print cv2.add(x,y) # 250+10 = 260 => 255[[255]]>>> print x+y # 250+10 = 260 % 256 = 4[4]
圖像的融合公式:g(x) = (1-a)f0(x) + af1(x);a的取值范圍是0—1;
cv2.addWeighted()函數(shù)的圖像融合:g(x) = (1-a)f0(x) + af1(x) + b
img1 = cv2.imread('ml.png')img2 = cv2.imread('opencv_logo.jpg')dst = cv2.addWeighted(img1,0.7,img2,0.3,0)cv2.imshow('dst',dst)cv2.waitKey(0)cv2.destroyAllWindows()
融合圖像示例:
圖像位操作主要包括:AND、OR、 NOT、 XOR
# Load two imagesimg1 = cv2.imread('messi5.jpg')img2 = cv2.imread('opencv_logo.png')# I want to put logo on top-left corner, So I create a ROIrows,cols,channels = img2.shaperoi = img1[0:rows, 0:cols ]# Now create a mask of logo and create its inverse mask alsoimg2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)mask_inv = cv2.bitwise_not(mask)# Now black-out the area of logo in ROIimg1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)# Take only region of logo from logo image.img2_fg = cv2.bitwise_and(img2,img2,mask = mask)# Put logo in ROI and modify the main imagedst = cv2.add(img1_bg,img2_fg)img1[0:rows, 0:cols ] = dstcv2.imshow('res',img1)cv2.waitKey(0)cv2.destroyAllWindows()
位操作后圖像示例:
img1 = cv2.imread('messi5.jpg')e1 = cv2.getTickCount()for i in xrange(5,49,2): img1 = cv2.medianBlur(img1,i)e2 = cv2.getTickCount()t = (e2 - e1)/cv2.getTickFrequency()print t# Result I got is 0.521107655 seconds
# check if optimization is enabledIn [5]: cv2.useOptimized()Out[5]: TrueIn [6]: %timeit res = cv2.medianBlur(img,49)10 loops, best of 3: 34.9 ms per loop# Disable itIn [7]: cv2.setUseOptimized(False)In [8]: cv2.useOptimized()Out[8]: FalseIn [9]: %timeit res = cv2.medianBlur(img,49)10 loops, best of 3: 64.1 ms per loop
本篇比較麻煩的就是位操作了,分析好久,還沒完全弄明白;有待更新。
聯(lián)系客服