2015年11月12日 星期四

How to Install OpenCV3.0 for Mint17 or Ubuntu15.10

如何在 Mint17 或 ubuntu15安裝 OpenCV3.0

Mint17:

1.install files
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
sudo apt-get update
sudo apt-get install libopencv-dev build-essential checkinstall cmake pkg-config yasm libtiff4-dev libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg 


2.downlods
cd ~/
mkdir OpenCV3_Install
cd OpenCV3_Install
sudo apt-get install git
git clone https://github.com/Itseez/opencv.git

3.make
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j4
sudo make install

4.patch

open this file
sudo gedit /etc/ld.so.conf.d/opencv.conf
add
"/usr/local/lib"
and run "sudo ldconfig "

open this file
sudo gedit /etc/bash.bashrc
add
"PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig"
"export PKG_CONFIG_PA"

Finish


=====================================================================
unubtu15.10

# 安裝需要的套件
sudo apt-get install libopencv-dev build-essential checkinstall cmake pkg-config yasm libtiff5-dev libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg

# 下載/OpenCV
cd ~/
mkdir OpenCV3_Install
cd OpenCV3_Install
sudo apt-get install git
git clone https://github.com/Itseez/opencv.git

# 準備編譯
cd opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

# 開始編譯
make -j4
sudo make install




sudo apt-get install gedit
clear
echo "================================="
echo "Now Add to file"
echo "================================="
echo "/usr/local/lib"
echo "================================="
sudo gedit /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
clear
echo "================================="
echo "Now Add to file"
echo "================================="
echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig"
echo "export PKG_CONFIG_PATH"
echo "================================="
sudo gedit /etc/bash.bashrc
echo "OpenCV version is"
pkg-config --modversion opencv

=====================================================================
TestFile
# -*- 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.LINE_AA)
while(1):
    cv2.imshow('Dring',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

Press "Esc" Exit

2015年8月18日 星期二

WebCamera_Show

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()

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剛出新版本可能需要等一下。
如果你只是要可以執行opencv並不挑版本的初學者,可以直接執行就好。
python -m  pip install OpenCV-Python

測試執行範例

載下來之後,預設狀態直接用點兩下執行就好了,如果你把它修改成用編譯器打開這邊你在用自己的方式執行程式即可。
另外程序有設置按ESC才能關閉,直接X關閉會在跳出來。

手動安裝 OpenCV

上面執行沒問題這邊就不用做了噢~給需要換版本的人看的。
OpenCV:https://opencv.org/releases/
載下來的 OpenCV 可以直接右鍵使用 rar 打開或是點兩下直接安裝到桌面即可。
然後找到這個位置
opencv-4.1.0-vc14_vc15.exe\opencv\build\python\cv2\python-2.7
裡面有一個檔案 cv2.pyd 這個是官方已經預先編譯好的檔案,直接拿過來就可以了。
把這個檔案複製到下面這個位置,如果你有改python安裝位置這邊要自己找到。
C:\Python27\Lib\site-packages
至此就完成安裝了,其實也沒多複雜拉XDD

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()

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()

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()

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()