opencv例項練習

2021-10-03 04:12:24 字數 1258 閱讀 6922

利用滑鼠畫矩形或者線段

import cv2 as cv

import numpy as np

drawing = false # 如果 mode為true繪製矩形。按下 'm』變成繪製曲線。 預設是false 滑鼠按下是true

mode = true

ix, iy = -1,-1

def draw_circle(event,x, y, flags, param):

global ix, iy, drawing, mode

if event == cv.event_mbuttondown: # cv.event_mbuttondown = , 中鍵按下

drawing = true

ix, iy = x, y

elif event == cv.event_mousemove:  #  cv.event_mousemove , 滑鼠移動

if drawing == true:

if mode == true:

cv.rectangle(img, (ix, iy), (x, y), (0, 255 ,0), -1)

else:

cv.circle(img,(x, y),10,(0, 0, 255), -1)

elif event == cv.event_lbuttonup: # cv.event_lbuttonup , 左鍵釋放

drawing = false

if mode == true:

cv.rectangle(img,(ix, iy), (x, y),(0, 255, 0), -1)

else:

cv.circle(img,(x, y), 10,(0, 0, 255), -1)

img = np.zeros((512, 512, 3), np.uint8) #黑色

cv.namedwindow(『image』)

cv.setmousecallback(『image』,draw_circle)

while(1):

cv.imshow(『image』, img)

k = cv.waitkey(1)&0xff

if k == ord(『m』):

mode = not mode

elif k == 27:

break

輸出結果:

opencv 例項演示

1.腐蝕膨脹 main.cpp include opencv2 imgproc imgproc.hpp include opencv2 highgui highgui.hpp include highgui.h include include erosion腐蝕 dilation膨脹 滑動條用來控制...

opencv程式設計例項

void imagestitch int index,mat image1,mat image2,const mat comframe premaxloc.x 5 偏移量x屬於0 20,x取任意位置均可以。保持第一張寬度為800 120 5 685 premaxloc.y 10 偏移量y屬於0 20...

編譯練習opencv

a b 1.安裝依賴 gcc 4.4.x or later cmake 2.8.7 or higher gitgtk 2.x or higher,including headers libgtk2.0 dev pkg config 可選 python 2.6 or later and numpy 1...