import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
# Video reverse =0
# frame = cv2.flip(frame,0)
frame = cv2.flip(frame,1)
# 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()
本站建議搭配 Hover Zoom+ chrome 擴充套件,可自動放大圖片
( 站內文介紹: https://goo.gl/BnBSGa )
網站搜索不是很好用建議使用google底下括號內是範例,整串打進google搜索內;
[ site:https://charlottehong.blogspot.tw/ 你要搜索的內容 ]
2015年8月18日 星期二
2015年8月17日 星期一
Python2.7 如何在 Win10 上安裝 OpenCV 函式庫
Python2.7 如何在 Win10 上安裝 OpenCV 函式庫
安裝
再來安裝Numpy,這個其實不是OpenCV必備的,沒有也可以執行,只是這個太常用了,幾乎可以是說是python必裝的,很容易在OpenCV教學的範例代碼上見到。就直接安裝省事吧~~
按下 Win+X 在按 A 打開終端機,然後輸入
python -m pip install numpy
安裝 OpenCV
先說最簡單的方式,跟上面一樣直接執行 pip 安裝。
這個方式會直接安裝pip裡面的opencv,缺點是不能選版本。我這邊測試目前是直接幫我安裝最新版本 OpenCV4.1.0 了,如果OpenCV剛出新版本可能需要等一下。
這個方式會直接安裝pip裡面的opencv,缺點是不能選版本。我這邊測試目前是直接幫我安裝最新版本 OpenCV4.1.0 了,如果OpenCV剛出新版本可能需要等一下。
如果你只是要可以執行opencv並不挑版本的初學者,可以直接執行就好。
python -m pip install OpenCV-Python
測試執行範例
測試用的程式 Drawing.py
https://gist.github.com/hunandy14/c67ee781b99322a2d1cf
https://gist.github.com/hunandy14/c67ee781b99322a2d1cf
載下來之後,預設狀態直接用點兩下執行就好了,如果你把它修改成用編譯器打開這邊你在用自己的方式執行程式即可。
手動安裝 OpenCV
上面執行沒問題這邊就不用做了噢~給需要換版本的人看的。
OpenCV:https://opencv.org/releases/
載下來的 OpenCV 可以直接右鍵使用 rar 打開或是點兩下直接安裝到桌面即可。
載下來的 OpenCV 可以直接右鍵使用 rar 打開或是點兩下直接安裝到桌面即可。
然後找到這個位置
opencv-4.1.0-vc14_vc15.exe\opencv\build\python\cv2\python-2.7
裡面有一個檔案
把這個檔案複製到下面這個位置,如果你有改python安裝位置這邊要自己找到。
cv2.pyd
這個是官方已經預先編譯好的檔案,直接拿過來就可以了。把這個檔案複製到下面這個位置,如果你有改python安裝位置這邊要自己找到。
C:\Python27\Lib\site-packages
至此就完成安裝了,其實也沒多複雜拉XDD
標籤:
OpenCV
2015年8月11日 星期二
opencv 檢視灰階影像
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
opencv 滑鼠點擊繪圖
import cv2
import numpy as np
drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1
# mouse callback function
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
elif event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xFF
if k == ord('m'):
mode = not mode
elif k == 27:
break
cv2.destroyAllWindows()
import numpy as np
drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1
# mouse callback function
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,0,0),-1)
elif event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xFF
if k == ord('m'):
mode = not mode
elif k == 27:
break
cv2.destroyAllWindows()
opencv 錄影
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
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()
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
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()
Opencv 繪圖
繪製各種形狀的圖形
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# 畫布大小
img = np.zeros((720,1280,3), np.uint8)
# (位置),大小,(顏色)粗度
cv2.circle(img,(200,200), 100, (0,0,255), 2)
# (第一個座標)(第二個座標)(顏色),粗度
cv2.rectangle(img,(350,100),(550,300),(0,255,0),3)
# (第一個座標)(第二個座標)(顏色),粗度
cv2.line(img,(600,100),(800,300),(255,0,0),10)
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))
font = cv2.FONT_HERSHEY_SIMPLEX
# (座標),大小,(顏色),粗度
cv2.putText(img,'Charlotte.HonG',(0,500), font, 5,(255,255,255),10,cv2.CV_AA)
while(1):
cv2.imshow('Dring',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# 畫布大小
img = np.zeros((720,1280,3), np.uint8)
# (位置),大小,(顏色)粗度
cv2.circle(img,(200,200), 100, (0,0,255), 2)
# (第一個座標)(第二個座標)(顏色),粗度
cv2.rectangle(img,(350,100),(550,300),(0,255,0),3)
# (第一個座標)(第二個座標)(顏色),粗度
cv2.line(img,(600,100),(800,300),(255,0,0),10)
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))
font = cv2.FONT_HERSHEY_SIMPLEX
# (座標),大小,(顏色),粗度
cv2.putText(img,'Charlotte.HonG',(0,500), font, 5,(255,255,255),10,cv2.CV_AA)
while(1):
cv2.imshow('Dring',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
2015年6月3日 星期三
UART(RX TX) RaspberryPI 與 Arduino 互傳字串
將Arduino與RasveryPi版上的RX與TX互接(PiRX=ArduiTX)
Pi版上請參考該技術手冊,Arduino則由程式碼內可得知
執行:ps ax | fgrep AMA
你會看到
2039 ? Ss+ 0:00 /sbin/getty -L ttyAMA0 115200 vt100
2055 pts/0 S+ 0:00 fgrep --color=auto AMA
確認無誤之後,繼續輸入以下指令:
sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt
將cmdline.txt備份
sudo vim /boot/cmdline.txt
cmdline.txt如下:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
將它改為:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
訂閱:
文章 (Atom)