Python+Opencv3 圖像基本操作


對最近學習的openCV相關的圖像操作進行整理,尤其是對圖像的讀取、視頻的讀寫等,詳細如下:

  • 1. 圖像的讀取顯示
import cv2
img = cv2.imread('/home/zxl/Downloads/timg.jpeg')
cv2.namedWindow('Image')
cv2.imshow('Image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 2. 攝像頭視頻實時顯示並保存
import cv2

cap = cv2.VideoCapture(1)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# 定義一個視頻存儲對象,以及視頻編碼方式,幀率,視頻大小格式,最后一項設定灰度圖
out = cv2.VideoWriter('/home/zxl/PycharmProjects/learn/output.avi',fourcc, 20.0, (1280,720))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
	#此處讀出的圖像是反的,需要將圖像鏡像
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
 
         

運行結果如下:

1548903993(1)

注:程序中用到圖像鏡像函數,函數原型如下:

flip

Flips a 2D array around vertical, horizontal, or both axes.

Python: cv2.flip(src, flipCode[, dst]) → dst

 Python: cv.Flip(src, dst=None, flipMode=0) → None

Parameters:

  • src – input array.
  • dst – output array of the same size and type as src.
  • flipCode – a flag to specify how to flip the array; 0 means flipping around the x-axis and positive value (for example, 1) means flipping around y-axis. Negative value (for example, -1) means flipping around both axes (see the discussion below for the formulas).

src

Anno

0

垂直翻轉

1

水平翻轉

-1

水平垂直翻轉

  • 3. 使用opencv函數庫畫各種圖形
import cv2
import numpy as np

#craet 512*512 black image
img = np.zeros((512,512,3),np.uint8)

#draw line
cv2.line(img,(0,0),(512,512),(0,0,255),1,cv2.LINE_AA)
#put txt
cv2.putText(img,'Draw OpenCV Example',(64,500),cv2.FONT_HERSHEY_COMPLEX,1,(125,125,125),1,cv2.LINE_AA)
#draw rect
cv2.rectangle(img,(64,476),(512-64,506),(125,0,125),4,cv2.LINE_4)
#draw circle
cv2.circle(img,(256,256),64,(0,256,0),2,cv2.LINE_AA)

cv2.rectangle(img,(256-64-2,256-64-2),(256+64+2,256+64+2),(125,0,256),2,cv2.LINE_AA)

#draw triangle
triangles = np.array([
    [(256-2, 0), (0, 512-64-4), (512-4, 512-64-4)]])
cv2.polylines(img,triangles,True,(255,0,0),2,cv2.LINE_AA)
#use cv2 display
cv2.imshow('image',img)
k = cv2.waitKey(0)
# wait for ESC key to exit
if k == 27:
    cv2.destroyAllWindows()
# wait for 's' key to save and exit
elif k == ord('s'):
    cv2.imwrite('black.png',img)
    cv2.destroyAllWindows()

運行結果如下:black

其中程序中用到畫三角形,搞了我半天,因為opencv的函數庫里邊沒有找到畫三角形的,參考網上,先定義三個頂點,使用polylines鏈接起來。LINE_AA:線條抗鋸齒。

  • 4.回調函數實現坐標獲取、當前像素點像素值獲取,在當前像素點處畫框/畫圓
import cv2
import numpy as np
#Draw cilcle at where you double clicked
def draw_circle(event,x,y,flags,param):
    if event==cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),100,(255,0,0),-1)
#get coordinate and get current RGB
def get_position(event,x,y,flags,param):
    if event == cv2.EVENT_FLAG_LBUTTON:
        print('coordinate:x=%d,y=%d'%(x,y))
        print('RGB=', img[x, y])
img=cv2.imread('/home/zxl/Downloads/timg.jpeg')
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
#cv2.setMouseCallback('image',get_position)  #備用
while 1:
    cv2.imshow('image',img)
    if cv2.waitKey(20)&0xFF==27:
        break
cv2.destroyAllWindows()

運行結果如下:

1548906621(1)

其中有個比較有意思的地方,為了實現獲取當前鼠標點擊處的坐標和該坐標處的像素點值,做了一個回調,會對后續圖像的研究有幫助。

  • 5. 在視頻中添加OSD

   如何在攝像頭預覽中添加自己的OSD,並且做到部分透明的效果,研究了圖片合並后采用Mask和ROI的方式,對圖像進行與操作,可以給每一幀圖像添加OSD,進而達到給視頻添加OSD的目的。

import cv2
import numpy as np

def get_position(event,x,y,flags,param):
    if event == cv2.EVENT_FLAG_LBUTTON:
        print('coordinate:x=%d,y=%d'%(x,y))
        print('RGB=',hsv[x,y])

cap = cv2.VideoCapture(0)
cv2.namedWindow('ROI')
cv2.setMouseCallback('ROI',get_position)

img_roi = cv2.imread('black_1.png')
r,c,channel =img_roi.shape
img_roi_1 = cv2.resize(img_roi,(int(r/2),int(c/2)))

img_roi_1_gray = cv2.cvtColor(img_roi_1,cv2.COLOR_RGB2GRAY)
ret,mask = cv2.threshold(img_roi_1_gray,25,255,cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

img_bg = cv2.bitwise_and(img_roi_1,img_roi_1,mask=mask)

rows,cols,channels =img_roi_1.shape


while cap.isOpened():
    ret, frame = cap.read()
    if ret == 1:
        frame = cv2.flip(frame,0)
        roi = frame[0:rows, 0:cols]
        img_qg = cv2.bitwise_and(roi,roi,mask=mask_inv)
        dst = cv2.add(img_qg,img_bg)
        frame[0:rows, 0:cols] = dst

        cv2.imshow('ROI',frame)

    if cv2.waitKey(2) & 0xFF == 27:
        break
cap.release()

cv2.destroyAllWindows()

black1548928215(1)

       到此基本把最近了解的opencv的圖像基本操作搞了一邊。中間涉及到Open live Write、github、ubuntu下環境的配置,來來回回折騰了有半個月,總算把這塊暫時匯總了一下,年后集中注意力搞識別類的更加深入的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM