opencv 相关
Posted shepherd2015
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv 相关相关的知识,希望对你有一定的参考价值。
主要是用opencv的python接口,偶尔用用opencv的C++接口
一,响应鼠标事件,画框。参考的是opencv sample里头的 grabCut.py
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 """ 4 Created on Sat Apr 21 09:45:16 2018 5 6 @author: scj 7 """ 8 9 import numpy as np 10 import cv2 11 import sys 12 13 filename = ‘./1.jpg‘ 14 15 img = cv2.imread(filename) 16 rows = img.shape[0] 17 cols = img.shape[1] 18 size = (int(cols*0.5), int(rows*0.5)) 19 img = cv2.resize(img, size, interpolation=cv2.INTER_AREA) 20 img2 = img.copy() 21 22 23 BLUE = [255, 0, 0] # rectangle color 24 25 def onmouse(event,x,y,flags,param): 26 global img,img2,drawing,value,mask,rectangle,rect,rect_or_mask,ix,iy,rect_over 27 28 # Draw Rectangle 29 if event == cv2.EVENT_LBUTTONDOWN: 30 rectangle = True 31 ix,iy = x,y 32 33 elif event == cv2.EVENT_MOUSEMOVE: 34 if rectangle == True: 35 img = img2.copy() 36 cv2.rectangle(img,(ix,iy),(x,y),BLUE,2) 37 rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y)) 38 # rect_or_mask = 0 39 40 elif event == cv2.EVENT_LBUTTONUP: 41 rectangle = False 42 rect_over = True 43 cv2.rectangle(img,(ix,iy),(x,y),BLUE,2) 44 rect = (min(ix,x),min(iy,y),abs(ix-x),abs(iy-y)) 45 # rect_or_mask = 0 46 # print(" Now press the key ‘n‘ a few times until no further change \n") 47 print("press any key to stop") 48 print("get a rectangle from (", str(ix), ", ", str(iy), ") to (" , str(x), ", ", str(y) ,") \n") 49 50 51 cv2.namedWindow(‘input‘) 52 cv2.setMouseCallback(‘input‘,onmouse) 53 54 while(1): 55 cv2.imshow(‘input‘,img) 56 k = cv2.waitKey(1) 57 58 if(k>0): 59 cv2.destroyAllWindows() 60 break;
二,C++中保存Mat成xml,然后在python里头读取
稍后再贴代码
以上是关于opencv 相关的主要内容,如果未能解决你的问题,请参考以下文章
pyhton—opencv直线检测(HoughLines)找到最长的一条线
pyhton—opencv直线检测(HoughLines)找到最长的一条线