2016年3月30日 星期三

get mouse 的使用說明


1.open    background_subtract.py   (滑鼠左鍵點兩下)

會看到下面的視窗....




重點是這一行

cap = cv2.VideoCapture('./src4.mp4')

把 src4.mp4 改成你新跑的檔案...

然後按下RUN -> Run Module ....等一下子會跳出一張圖的畫面....按下Enter

就會看到程式開始跑了....


2.   剛剛的程式跑完後...會把跑完的圖片存在   bg_subtract folder 裡面...

     接下來我們進到  bg_subtract/source folder 裡面.... 尋找第一張圖片...

      何謂第一張圖片呢? 他的意思是只有老鼠在動的第一章圖片.....不能有其他

動的東西在裡面..... 以這個例子而言...剛好是 file name 為200 的時候...

       然後打開  find_mouse.py  (滑鼠左鍵點兩下)

重點是這一行

file_index = 200


把200 改成你的數字......

        然後一樣執行它.....  Run -> Run Module 

         會出現圖片....把滑鼠移到老鼠的位置上面....點一下滑鼠左鍵....

        在剛剛的執行視窗會出現   老鼠的座標...把這個數字記下來...

        

3.   打開  contour.py   (滑鼠點擊contour.py 左鍵兩下)

重點是這幾行

file_index_start = 200 ..把200 改成剛剛第二步驟的file index

file_index_end  =  3639  把 3639  改成總共有幾張...假設有 3639張...就填 3639

                                         或則是你只想跑到那一張就停....就填那一張的file_index

mouse_sx =    216    ...把216改成 剛剛第二步驟所抓到的 (x,y) 的 x 數值

mouse_sy  =  278     .. 把278 改成 剛剛第二步驟所抓到的 (x,y) 的 y 數值  

mouse_ex  =    359   ..  是老鼠終點的x 座標

mouse_ey  =    232  ..   是老鼠終點的y 座標...

如果不想啟用 老鼠跑到終點就結束的功能的話....就不需要填...

但是需要把 mouse_done =1  改成   mouse_done = 0

大概在code 的中間部份....

if(get_mouse):
                dist_thr = dist_thr_init
    sx = int(cx)
    sy = int(cy)

                
                mouse_dist = distance(sx,sy,mouse_ex,mouse_ey)
                if(mouse_dist < mouse_end_point_dist_thr):
                        mouse_done = 1                 

這裡因為我有用tab 鍵....沒辦法用 gui 的方式去執行

所以要用 terminal window 的方式....

點擊上面黑色的螢幕的icon ....


然後  cd ./python_code/

sudo python contour.py


最後跑完的圖片的檔名是temp.jpg



dist : 4514   (pixel distance)

第一象限的frame count,第二象限的frame count,第三象限的frame count,第四象限 的 frame count,



2016年3月10日 星期四

opencv tutorial : pyimagesearch



example :1  shape_detector


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
        help="path to the input image")
args = vars(ap.parse_args())

# load the image and resize it to a smaller factor so that
# the shapes can be approximated better
image = cv2.imread(args["image"])
resized = imutils.resize(image, width=300)
ratio = image.shape[0] / float(resized.shape[0])

# convert the resized image to grayscale, blur it slightly,
# and threshold it
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

取出 thresh 的影像....

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
sd = ShapeDetector()   

取出cnts ...利用  findContours.....

然後取cnts[0]



# loop over the contours
for c in cnts:
        # compute the center of the contour, then detect the name of the
        # shape using only the contour
        M = cv2.moments(c)
        cX = int((M["m10"] / M["m00"]) * ratio)
        cY = int((M["m01"] / M["m00"]) * ratio)
        shape = sd.detect(c)

        # multiply the contour (x, y)-coordinates by the resize ratio,
        # then draw the contours and the name of the shape on the image
        c *= ratio
        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
        cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
                0.5, (255, 255, 255), 2)

        # show the output image
        cv2.imshow("Image", image)
        cv2.waitKey(0)


利用
 shape = sd.detect(c)

程式碼如下:  重點是利用approx = cv2.approxPolyDP(c, 0.04 * peri, True)

得到 approx(頂點)....然後  len(approx) 去判斷...3 就是三角形... 

        def detect(self, c):
                # initialize the shape name and approximate the contour
                shape = "unidentified"
                peri = cv2.arcLength(c, True)
                approx = cv2.approxPolyDP(c, 0.04 * peri, True)

                # if the shape is a triangle, it will have 3 vertices
                if len(approx) == 3:
                        shape = "triangle"

                # if the shape has 4 vertices, it is either a square or
                # a rectangle
                elif len(approx) == 4:
                        # compute the bounding box of the contour and use the
                        # bounding box to compute the aspect ratio
                        (x, y, w, h) = cv2.boundingRect(approx)
                        ar = w / float(h)

                        # a square will have an aspect ratio that is approximately
                        # equal to one, otherwise, the shape is a rectangle
                        shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"

                # if the shape is a pentagon, it will have 5 vertices
                elif len(approx) == 5:
                        shape = "pentagon"

                # otherwise, we assume the shape is a circle
                else:
                        shape = "circle"

                # return the name of the shape
                return shape


最後輸入command

python detect_shapes.py --image shapes_and_colors.png




Reference : http://www.pyimagesearch.com/

opencv 的中文教學網站




Reference : http://www.cmlab.csie.ntu.edu.tw/~jsyeh/wiki/doku.php?id=%E8%91%89%E6%AD%A3%E8%81%96%E8%80%81%E5%B8%AB:%E6%95%99%E7%A0%94%E7%A9%B6%E7%94%9F%E5%AD%B8opencv

opencv 的書籍


1.     OpenCV with Python Blueprints

2.    

2016年3月1日 星期二


OpenCV comes with a data file, letter-recognition.data in opencv/samples/cpp/ folder. If you open it, you will see 20000 lines which may, on first sight, look like garbage. Actually, in each row, first column is an alphabet which is our label. Next 16 numbers following it are its different features. These features are obtained from UCI Machine Learning Repository. You can find the details of these features in this page.

它先把   letter-recognition.data  讀近來

然後垂直分成兩半....上半部拿來當作training pattern

                               下半部拿來當作test pattern

第一個column 是label.....  後面16 column 是 feature point....

這兩行就是再做這件事
responses, trainData = np.hsplit(train,[1])
labels, testData = np.hsplit(test,[1])

這行就是在train pattern....  trainData   ...  responses 就是辨認的label
knn.train(trainData, cv2.ml.ROW_SAMPLE, responses)

這行就是在測試pattern....把最後辨識出來的label 放到  result 去
ret, result, neighbours, dist = knn.findNearest(testData, k=5)

這行就是在比較.....
correct = np.count_nonzero(result == labels)

source code : 如下

import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')


import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the data, converters convert the letter to a number
data= np.loadtxt('letter-recognition.data', dtype= 'float32', delimiter = ',',
                    converters= {0: lambda ch: ord(ch)-ord('A')})

# split the data to two, 10000 each for train and test
train, test = np.vsplit(data,2)

# split trainData and testData to features and responses
responses, trainData = np.hsplit(train,[1])
labels, testData = np.hsplit(test,[1])


# Initiate the kNN, classify, measure accuracy.
#knn = cv2.ml.KNearest()
knn = cv2.ml.KNearest_create()
knn.train(trainData, cv2.ml.ROW_SAMPLE, responses)
ret, result, neighbours, dist = knn.findNearest(testData, k=5)


correct = np.count_nonzero(result == labels)
accuracy = correct*100.0/10000
print accuracy


Reference : http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_ml/py_knn/py_knn_opencv/py_knn_opencv.html#knn-opencv