Exemple #1
0
        #save selected template to corresponding directory
        print 'printing new template to', name, '...'
        cv2.imwrite(name, tpl)

        #add selected template do corresponding dict
        templates[TCB + LCR].append(tpl)

        #show selected template on screen
        img2show = cv2.cvtColor(input, cv2.COLOR_GRAY2BGR)
        cv2.rectangle(img2show, (x - 10, y - 10), (x + 10, y + 10),
                      templateColorMap[TCB + LCR], 2)
        cv2.imshow('output', img2show)


jasf_cv.getNewWindow('output')
jasf_cv.getNewWindow('settings')
cv2.namedWindow('output')
cv2.setMouseCallback('output', onMouseDblClk)


def doNothing(val):
    pass


TCB_track = jasf_cv.setTrackbar('Top/Bottom', 0, 1)
LCR_track = jasf_cv.setTrackbar('Left/Center/Rght', 0, 2)
jasf_cv.setTrackbar('th', 40, 500)

TCB_map = {0: 'T', 1: 'B'}
LCR_map = {0: 'L', 1: 'C', 2: 'R'}
Exemple #2
0
"""Prorgam to play with the parameters of the Canny filter"""
import sys
sys.path.insert(0, '../')
import cv2
import numpy as np
from matplotlib import pyplot as plt
from jasf import jasf_cv

from config import video2load
cam = cv2.VideoCapture(video2load)

#cam = cv2.VideoCapture(0)
output = jasf_cv.getNewWindow('Canny Output')
track_window = jasf_cv.getNewWindow('Settings')


def onChangeCallBack():
    pass


cv2.createTrackbar('th_min', track_window, 0, 255, onChangeCallBack)
cv2.setTrackbarPos('th_min', track_window, 100)

cv2.createTrackbar('th_max', track_window, 0, 255, onChangeCallBack)
cv2.setTrackbarPos('th_max', track_window, 150)

while cam.isOpened():
    ret, frame = cam.read()
    #frame = cv2.blur(frame, (7,7))
    img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
help =  "\n\n HELP \n\nThis program will test the class FloorDetectPreProcess_S_based.  This class crops the original \
video file to select the area it believes is the ground texture. The ground texture is \
captured by the value of a pixel with its surrounding. The comparsion is made in the S component of the color space.\n\n"

print help import sys sys.path.insert(0, '../') import numpy as np import cv2 from cv2 import imshow from jasf import
jasf_cv from jasf import jasf_ratFinder from devika import devika_cv from util import *

cam = cv2.VideoCapture('../../video/mp4/selected/camereMoves.mp4')
cam = cv2.VideoCapture('../../video/mp4/selected/camereMoves2.mp4')
cam = cv2.VideoCapture('../../video/avi/myFavoriteVideo.avi')

filter = FloorDetectPreProcess_S_based()

jasf_cv.getNewWindow('input')
jasf_cv.getNewWindow('s')
jasf_cv.getNewWindow('output')
jasf_cv.getNewWindow('settings')

window_trackbar = 'settings'
#create track bars to stabilish color limits
def nothing(e):
    pass
cv2.createTrackbar('window_size',window_trackbar,5,13,nothing)
cv2.createTrackbar('th_min',window_trackbar,107,255,nothing)
cv2.createTrackbar('th_max',window_trackbar,193,255,nothing)
cv2.createTrackbar('erode',window_trackbar,3,13,nothing)
cv2.createTrackbar('dilate',window_trackbar,4,13,nothing)
cv2.createTrackbar('left/right',window_trackbar,0,1,nothing)


while cam.isOpened():
Exemple #4
0
sys.path.insert(0, '../')
import numpy as np
import cv2
from cv2 import imshow
from jasf import jasf_cv
from jasf import jasf_ratFinder
from devika import devika_cv
from util import *

cam = cv2.VideoCapture('../../video/mp4/selected/camereMoves.mp4')
cam = cv2.VideoCapture('../../video/avi/myFavoriteVideo.avi')
cam = cv2.VideoCapture('../../video/mp4/selected/camereMoves2.mp4')

filter = FloorDetectPreProcess()

jasf_cv.getNewWindow('input')
jasf_cv.getNewWindow('invert')
jasf_cv.getNewWindow('cnts')
jasf_cv.getNewWindow('output')

while cam.isOpened():
    ret, frame = cam.read()
    if ret == False:
        print 'finishing due to end of video'
        break
    left, right = devika_cv.break_left_right(frame)

    output, otsu_th, invert, cnts = filter.preProcess(right[:, :, 0])

    cnts = cv2.drawContours(right.copy(), cnts, -1, (255, 0, 0), 2)
the floor is square. We apply Otsu threshold + open + dilate + coutours + contour
approximation. The filtering techinique is the same as in rat finder. However, since here
we want to segment the opposite, there is a point in which we invert the image"""
import sys
sys.path.insert(0, '../')
import numpy as np
import cv2
from jasf import jasf_cv
from devika import devika_cv
from copy import deepcopy
from cv2 import imshow


cam = cv2.VideoCapture('../../video/avi/myFavoriteVideo.avi')

window_input = jasf_cv.getNewWindow('input')
window_output = jasf_cv.getNewWindow('output')
window_settings = jasf_cv.getNewWindow('settings')

def doNothing(opt):
    pass

cv2.createTrackbar('epsilon', window_settings, 34, 100, doNothing) 
cv2.createTrackbar('jump', window_settings, 50, 400, doNothing) 

cleaning_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))

def detectFloor(input, alpha, previous_approx, previous_roi, allowed_jump):
    #leave Otsu decide the threshold
    ret, otsu_threshold = cv2.threshold(input, 0, 1, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    #opening operation to fill holes and eliminate noise
Exemple #6
0
#####################################
videoFiles = list(p for p in pathlib.Path(control_Path2VideoFiles).iterdir() if p.is_file() and p.name[0] != '.')

def printVideoFiles():
    print 'Here are the video files'
    for i,fn in enumerate(videoFiles):
        print i, fn.name
printVideoFiles()


cam = cv2.VideoCapture(videoFiles[0].absolute().as_posix())
#####################################
#set windows
#####################################
jasf_cv.getSettingsWindow()
jasf_cv.getNewWindow('settings2')
jasf_cv.getNewWindow('video', dimension = (320,240))
jasf.cv.getManyWindows(control_componentsToCapture, dim = (60,80), n=(6,6))
jasf_cv.getNewWindow('range')

#control variables
control_mode = 'run'


#####################################
#function to control video index
#####################################
def onVideoChange(index):
    """Function to be called when video trackbar is moved. It will re initializate the
    global variable cam"""
    global cam, control_mode
Exemple #7
0
def main():
    #initialize camera
    cam = cv2.VideoCapture('../../video/avi/myFavoriteVideo.avi')

    #create windows to display output and intermediate results
    window_input = jasf_cv.getNewWindow('input')
    window_otsu = jasf_cv.getNewWindow('otsu')
    window_open = jasf_cv.getNewWindow('open')
    window_small_filter = jasf_cv.getNewWindow('smallFilter')
    window_output = jasf_cv.getNewWindow('output')
    window_settings = jasf_cv.getNewWindow('settings')

    def doNothing(val):
        """function to be passed to createTrackbar"""
        pass

    #create trackbars
    cv2.createTrackbar('th', window_settings, 0, 400, doNothing)
    cv2.createTrackbar('max', window_settings, 100, 400, doNothing)
    cv2.createTrackbar('area_delta', window_settings, 4, 10, doNothing)


    #---------------------------------------------------------------------------
    #initialize required variable
    #---------------------------------------------------------------------------
    rx,ry,mouse = -1, -1,[]
    previous_roi, roi = [], []
    approx, previous_approx = [], []
    #these values were set manually to produce good results
    #alpha is amount of error tolerated when approximating a polynomial surface
    alpha = 34/1000.0
    #this controls the amount of increase in area from one iteration to the other
    allowed_floor_jump = 50
    #---------------------------------------------------------------------------
    #main loop
    #---------------------------------------------------------------------------
    while cam.isOpened():
        #read frame
        ret, frame = cam.read()
        if frame == None:
            print 'finishing due to end of video'
            break
        left, right = devika_cv.break_left_right(frame)

        #read trackbars
        #those two are actually set by the own program
        th = cv2.getTrackbarPos('th', window_settings)
        th_max = cv2.getTrackbarPos('max', window_settings)
        #this one the user can freely set
        area_delta = cv2.getTrackbarPos('area_delta', window_settings)

        #get blue component
        B = right[:,:,0]
        #-----------------------------------------------------------------
        #Step 1: localize the square box
        #-----------------------------------------------------------------
        input = B.copy()
        #we need to keep track of those two previous states
        previous_approx = deepcopy(approx)
        previous_roi = deepcopy(roi)
        #this returns a contourn to the floor region
        #this method also returns the threshold computer by Otsu's method and that should
        #be used later
        roi, approx, inver, otsu_th = jasf_ratFinder.detectFloor(input, 34/1000.0, previous_approx, previous_roi, allowed_floor_jump )
        #make the contour into a mask where ones represent pixels to consider and zeros
        #pixels to disconsider
        floor_mask = np.zeros_like(input)
        floor_mask = cv2.drawContours(floor_mask, [roi], 0, 1, -1)
        #floor_mask = cv2.dilate(floor_mask, cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)))

        #-----------------------------------------------------------------
        #Step 2: find candidates to rat contour
        #-----------------------------------------------------------------
        input = input * floor_mask
        #this will run on the first iteration to initialize the mouse position
        if (rx,ry) == (-1, -1):
            rx,ry,mouse = initialize(input, th, th_max, otsu_th)
            #computer area of the mouse and set the boundaries for the next iteration
            area = cv2.contourArea(mouse)/100
            newTh = max(area - area_delta, 9)
            newTh_max = max(area + area_delta, 16)
            cv2.setTrackbarPos('th', window_settings, int(newTh))
            cv2.setTrackbarPos('max', window_settings, int(newTh_max))

        #find candidates to rat contour
        contours, otsu_threshold, open, filterSmall = jasf_ratFinder.detectInterestingContours(input, th, th_max, otsu_th)

        #-----------------------------------------------------------------
        #Step 3: select which contour is the real mouse
        #-----------------------------------------------------------------
        rx,ry,new_mouse = filterMouse(contours, (rx, ry))
        #if mouse was found, update parameters
        if type(new_mouse) is not bool:
            mouse = new_mouse
            area = cv2.contourArea(mouse)/100
            newTh = max(area - area_delta, 9)
            newTh_max = max(area + area_delta, 16)
            cv2.setTrackbarPos('th', window_settings, int(newTh))
            cv2.setTrackbarPos('max', window_settings, int(newTh_max))

        
        #-----------------------------------------------------------------
        #Step 4: show output and some intermediate results
        #-----------------------------------------------------------------
        #draw countours
        output = input.copy()
        output = myDrawContours(output, [mouse])
        
        cv2.imshow(window_otsu, 255*otsu_threshold)
        cv2.imshow(window_open, 255*open)
        cv2.imshow(window_small_filter, 255*filterSmall)
        cv2.imshow(window_input, input) 
        cv2.imshow(window_output, output) 

        #check if execution should continue or not
        ch = cv2.waitKey(1) & 0xFF
        if ch == ord('q'):
            print 'end of execution due to user command'
            break

    cam.release()
    cv2.destroyAllWindows()
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))

        if np.abs(x2 - x1) < max:
            myLines.append(line)

    return myLines

cam = cv2.VideoCapture('../../video/mp4/myFavoriteVideo.mp4')
#cam = cv2.VideoCapture(0)
outputWindow = jasf_cv.getNewWindow('Probabilistic Hough Line')
track_window = jasf_cv.getNewWindow('settings')
cannyWindow = jasf_cv.getNewWindow('Canny')
houghSettings_window = jasf_cv.getNewWindow('HoughLineSettings')



def onControlChange(opt):
    if opt == 0:
        print 'changing mode to Probabilistic Hough Transform'
    else:
        print 'changing mode to Standard Hough Transform'
    control_hough_mode = opt

#general settings 
jasf_cv.setTrackbar('blur_size', 3, 17)
Exemple #9
0
#20 mean shift algorithm
import numpy as np
import cv2
from jasf import jasf_cv

fn = "../video/mp4/2014-07-16_08-41-11.mp4"
cap = cv2.VideoCapture()
cap.open(fn)
#cap = cv2.VideoCapture(0)

#create windows to display results
window_input = jasf_cv.getNewWindow('input')
window_output = jasf_cv.getNewWindow('output')

#window for trackbars
window_trackbar = jasf_cv.getNewWindow('settings')


def nothing(x):
    pass


#create track bars to stabilish color limits
mode = 'manual'


def onModeChange(opt):
    mode = 'manual' if opt == 0 else 'mouse'
    print 'mode changed to', mode

Exemple #10
0
import sys
sys.path.insert(0, '../')
import numpy as np
import cv2
from jasf import jasf_cv

from config import video2load
cam = cv2.VideoCapture(video2load)

Bwindow = jasf_cv.getNewWindow('Blue Component')
Gwindow = jasf_cv.getNewWindow('Green component')
Rwindow = jasf_cv.getNewWindow('Red component')

Hwindow = jasf_cv.getNewWindow('Hue Component')
Swindow = jasf_cv.getNewWindow('Saturation component')
Vwindow = jasf_cv.getNewWindow('Value component')

jasf_cv.getNewWindow('canny')
jasf_cv.getNewWindow('settings')

jasf_cv.setTrackbar('canny_min')
jasf_cv.setTrackbar('canny_max')

while cam.isOpened():

    canny_min = cv2.getTrackbarPos('canny_min', 'settings')
    canny_max = cv2.getTrackbarPos('canny_max', 'settings')

    ret, frame = cam.read()
    B, G, R = cv2.split(frame)
Exemple #11
0
import sys
sys.path.insert(0, '../')
import cv2
import jasf
from jasf import jasf_cv
from devika import devika_cv

jasf_cv.getNewWindow('settings')
jasf_cv.getNewWindow('video')

import pathlib
videoFiles = list(p for p in pathlib.Path('../../video/mp4/').iterdir() if p.is_file() and p.name[0] != '.')

def printVideoFiles():
    print 'Here are the video files'
    for i,fn in enumerate(videoFiles):
        print i, fn.name
printVideoFiles()


cam = cv2.VideoCapture(videoFiles[0].absolute().as_posix())


#control variables
control_mode = 'run'


def onVideoChange(index):
    """Function to be called when video trackbar is moved. It will re initializate the
    global variable cam"""
    global cam, control_mode
#20 mean shift algorithm
import numpy as np
import cv2
from jasf import jasf_cv

    
fn = "../video/mp4/2014-07-16_08-41-11.mp4"
cap = cv2.VideoCapture()
cap.open(fn)
#cap = cv2.VideoCapture(0)

#create windows to display results
window_input = jasf_cv.getNewWindow('input')
window_output = jasf_cv.getNewWindow('output')

#window for trackbars
window_trackbar = jasf_cv.getNewWindow('settings')


def nothing(x):
    pass

#create track bars to stabilish color limits
mode = 'manual'
def onModeChange(opt):
    mode = 'manual' if opt == 0 else 'mouse'
    print 'mode changed to', mode

cv2.createTrackbar('manual/mouse mode',window_trackbar,0,1,onModeChange)

cv2.createTrackbar('Hu',window_trackbar,0,180,nothing)
Exemple #13
0
            raise MouseNotFound('Closest center not close enough to previous position! Is this rat a ninja or what??')

    except MouseNotFound as E:
        print 'rat not found!'
        print E.reason
        return last_center[0], last_center[1], False

    return rx,ry,mouse


#initialize camera
cam = cv2.VideoCapture('../video/avi/myFavoriteVideo.avi')
#the problem with this one is that the rat merges with the back panel
#cam = cv2.VideoCapture('../video/avi/2014-07-16_10-41-14.avi')

window_input = jasf_cv.getNewWindow('input')
window_otsu = jasf_cv.getNewWindow('otsu')
window_open = jasf_cv.getNewWindow('open')
window_small_filter = jasf_cv.getNewWindow('smallFilter')
window_output = jasf_cv.getNewWindow('output')
window_settings = jasf_cv.getNewWindow('settings')

cleaning_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))

def doNothing(val):
    pass

cv2.createTrackbar('th', window_settings, 0, 400, doNothing)
cv2.createTrackbar('max', window_settings, 100, 400, doNothing)
cv2.createTrackbar('area_delta', window_settings, 4, 10, doNothing)
Exemple #14
0
def askUserForInput(frame, modeReadingFromData=False, userData=dict()):
    """Function will read the settings, find interesting contours and show then to the user so he can pick the correct
    contour """
    global control_mouse
    #read image and break into right and left
    left, right = devika_cv.break_left_right(frame)
    inputList = [left, right, frame]

    #create window to wait input
    jasf_cv.getNewWindow('user input', dimension=(160,120))

    cnts = []
    control_mouse.initialized = False

    def analyseUserInput(x,y):
        """This function will be called in two cases:
            *by the next functoin
            *when there is some userInput stored from previous run

            This piece of code was refactored in order to be used in these two cases
            """
        global control_mouse
        #compute center of current contours and their distances to the user click
        #'cnts' here will be set on the loop that is written after this function definition
        centers = [jasf_cv.getCenterOfContour(c) for c in cnts]
        distances = [np.linalg.norm(np.array(c) - np.array((x,y))) for c in centers]
        #the mouse is the one closest to the user click
        i = np.argmin(distances)
        rx,ry = centers[i]
        mouse = cnts[i] 

        #the user cannot miss badly
        if jasf.math.pointDistance((rx,ry), (x,y)) > 20:
            print 'not close enough!'
            pass
        else: 
            print 'position set!'
            control_mouse.setPosition(rx, ry, mouse)
            control_mouse.initialized = True

            #add user input to dictionary of user inputs
            userInputData[control_settings['currentVideoFileName']].append({'frame':
                readControlSetting('framesSinceStart'), 'input':(rx,ry), 'settings_state':readSettingsState()}) 


    def onUserInputDblCklick(event, x, y, flags, params):
        """ mouse callback to set the rat position. This function gets the user press
        position and compare it with the known centers, picking the closest match. It will reject the chosen position if
        it is distant from the guessed centers"""
        global control_mouse
        if event == cv2.EVENT_LBUTTONDBLCLK:
            analyseUserInput(x,y)


    if modeReadingFromData:
        rx,ry = userData['input']
        setSettingsState(userData['settings_state'])

        ##sorry for this code repetition starting here
        #read parameters from settings window
        th, th_max, delta, dilateSize, erodeSize, LRA = readSettings()
        #select which image to use
        input = inputList[LRA]
        #get blue component
        input = input[:,:,1]
        input = input.copy()
        #find contours
        contourFinder.setParams(dilateSize, erodeSize, th, th_max)
        cnts, otsu_threshold, filterSmall = contourFinder.detectInterestingContours(input)
        ##code repetition ends here

        #call analyse; hopefully this will already set control_mouse.initialized and the loop will not run
        analyseUserInput(rx,ry)

    else:
        #in this case, the loop should run
        cv2.setMouseCallback('user input', onUserInputDblCklick)
    
    #ask user to select contour
    while  control_mouse.initialized == False:
        #read parameters from settings window
        th, th_max, delta, dilateSize, erodeSize, LRA = readSettings()
        #select which image to use
        input = inputList[LRA]
        #get blue component
        input = input[:,:,1]
        input = input.copy()
        #find contours
        contourFinder.setParams(dilateSize, erodeSize, th, th_max)
        cnts, otsu_threshold, filterSmall = contourFinder.detectInterestingContours(input)
        #draw all contours
        img2show = jasf_cv.drawContours(input, cnts)
        cv2.imshow('user input', img2show)
        ch = cv2.waitKey(150) & 0xFF

    cv2.destroyWindow('user input')


    #compute area and set the new thresholds
    updateValuesOfTh(delta)
Exemple #15
0
    print 'usage: python main.py option=choice'
    print 'options:'
    print 'flowComputer: regular, rangePercent_regular, rangePercent_windowFlow, windowFlow'

#videos will be read from this path
control_Path2VideoFiles = '../videoCleanExperiment/output/fallingMouse/'
control_Path2VideoFiles = '../../video/mp4/'
control_settings = {'control_mode':'run', 'currentVideoFileName':'NONE'}
control_inputDict = jasf.parseInput(sys.argv)

videoFiles = list(p for p in pathlib.Path(control_Path2VideoFiles).iterdir() if p.is_file() and p.name[0] != '.')
videoFiles.sort(key = lambda x: x.name)
print videoFiles
cam = cv2.VideoCapture(videoFiles[0].absolute().as_posix())

jasf_cv.getNewWindow('settings')
jasf_cv.getNewWindow('settings1')
jasf.cv.getManyWindows(['B', 'otsuTh', 'clean+filter', 'tracking', 'flowImg'], n = (5,5))
#####################################
#set trackbars
#####################################
def onVideoChange(index):
    """Function to be called when video trackbar is moved. It will re initializate the
    global variable cam"""
    global cam
    setControlSetting('control_mode', 'pause')
    cam.release()
    fn = videoFiles[index].absolute().as_posix() 
    print 'opening', fn
    cam = cv2.VideoCapture(fn)
Exemple #16
0
""" Here we try to detect the floor in which the mouse lives. The main assumption is that
the floor is square. We apply Otsu threshold + open + dilate + coutours + contour
approximation. The filtering techinique is the same as in rat finder. However, since here
we want to segment the opposite, there is a point in which we invert the image"""
import sys
sys.path.insert(0, '../')
import numpy as np
import cv2
from jasf import jasf_cv
from devika import devika_cv
from copy import deepcopy
from cv2 import imshow

cam = cv2.VideoCapture('../../video/avi/myFavoriteVideo.avi')

window_input = jasf_cv.getNewWindow('input')
window_output = jasf_cv.getNewWindow('output')
window_settings = jasf_cv.getNewWindow('settings')


def doNothing(opt):
    pass


cv2.createTrackbar('epsilon', window_settings, 34, 100, doNothing)
cv2.createTrackbar('jump', window_settings, 50, 400, doNothing)

cleaning_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))


def detectFloor(input, alpha, previous_approx, previous_roi, allowed_jump):