def load_image(image_name, camimgs, cam, invmask, hgback, hgmax, hgqual, hgmind, hgminf, scale, sigma): try: camimgs += image_name # Set up Homography object homog = Velocity.Homography(camimgs, cam, invmask, calibFlag=True, band='L', equal=True) # Calculate homography hg = homog.calcHomographyPairs(hgback, hgmax, hgqual, hgmind, hgminf) homogmatrix = [item[0] for item in hg] new_terminus = Line(camimgs, cam, homogmatrix) current_imgset = new_terminus._imageSet imn = new_terminus.getImageNames() cameraMatrix = cam.getCamMatrixCV2() distortP = cam.getDistortCoeffsCV2() # Create New Snake image = current_imgset[0].getImageCorr(cameraMatrix, distortP) processed_image = setup_image(image, scale, sigma) return processed_image except: return [[False]]
def findFeature(cat, feat): if cat == "time": return [f for f in tds.posFeatures().keys() if f in feat] if cat == "freq": return [f for f in fds.posFeatures().keys() if f in feat] if cat == "vel": return [f for f in vel.posFeatures().keys() if f in feat] if cat == "peak": return [f for f in ps.posFeatures().keys() if f in feat]
def findCols(cat, el): if cat == "time": return [col for col in tds.posCols() if col in el] if cat == "freq": return [col for col in fds.posCols() if col in el] if cat == "vel": return [col for col in vel.posCols() if col in el] if cat == "peak": parts = el.split("_") if len(parts) == 2: return [(parts[0], None)] if len(parts) == 3: return [(parts[0], parts[1], parts[2] == "cor")] return []
def getAllFeatures(): part = { 'time': { 'cols': tds.posCols(), 'features': tds.posFeatures().keys() }, 'freq': { 'cols': fds.posCols(), 'features': fds.posFeatures().keys() }, 'vel': { 'cols': vs.posCols(), 'features': vs.posFeatures().keys() }, 'peak': { 'cols': ps.posPeaks(), 'features': ps.posFeatures().keys() } } return { 'ankle': part, 'hip': part }
def extractBodyPart(data, bodyPart, requiredFeatures=None): requiredFeatures = checkRequiredFeatures(requiredFeatures, {'time': None, 'freq': None, 'peak': None, 'vel': None}) features = dict() features.update(fds.getSimpleFreqDomainFeatures(data, requiredFeatures['freq'])) features.update(tds.getSimpleTimeDomainFeatures(data, requiredFeatures['time'])) features.update(ps.getSimplePeakFeatures(data, requiredFeatures['peak'])) features.update(vs.getVelocityFeatures(data, requiredFeatures['vel'])) features = dict((bodyPart + '.' + key, features[key]) for key in features.keys()) return features
def calcManualArea(img, imn, hmatrix=None, pxplot=None, invprojvars=None): '''Manually define an area in a given image. User input is facilitated through an interactive plot to click around the area of interest. XYZ areas are calculated if a set of inverse projection variables are given. Args img (arr): Image array (for plotting the image). imn (str): Image name pxplot (list): Plotting extent for manual area definition invprojvars (list): Inverse projection variables Returns xyzarea (list): Sum of total detected areas (xyz) xyzpts (list): XYZ coordinates of detected areas pxextent (list): Sum of total detected areas (px) pxpts (list): UV coordinates of detected areas ''' #Initialise figure window and plot image fig=plt.gcf() fig.canvas.set_window_title(imn + ': Click around region. Press enter ' 'to record points.') plt.imshow(img, origin='upper', cmap='gray') #Set plotting extent if required if pxplot is not None: plt.axis([pxplot[0],pxplot[1], pxplot[2],pxplot[3]]) #Manual input of points from clicking on plot using pyplot.ginput rawpx = plt.ginput(n=0, timeout=0, show_clicks=True, mouse_add=1, mouse_pop=3, mouse_stop=2) print('\n' + imn + ': you clicked ' + str(len(rawpx)) + ' points') #Show plot plt.show() plt.close() #Convert coordinates to array pxpts=[] for i in rawpx: pxpts.append([[i[0],i[1]]]) pxpts.append([[rawpx[0][0],rawpx[0][1]]]) pxpts=np.asarray(pxpts) #Calculate homography-corrected pts if desired if hmatrix is not None: print('Correcting for camera motion') pxpts = Velocity.apply_persp_homographyPts(pxpts, hmatrix, inverse=True) #Create polygon if area has been recorded try: #Create geometry pxpoly=getOGRArea(pxpts.squeeze()) #Calculate area of polygon area pxextent = pxpoly.Area() #Create zero object if no polygon has been recorded except: pxextent = 0 print('Total extent: ' + str(pxextent) + ' px (out of ' + str(img.shape[0]*img.shape[1]) + ' px)') #Convert pts list to array pxpts = np.array(pxpts) pxpts = np.squeeze(pxpts) if invprojvars is not None: #Get xyz coordinates with inverse projection xyzpts=projectUV(pxpts, invprojvars) #Calculate area of xyz polygon xyzarea = getOGRArea(xyzpts) xyzarea=xyzarea.GetArea() #Return XYZ and pixel areas print('Total area: ' + str(xyzarea) + ' m') return [[[xyzarea], [xyzpts]], [[pxextent], [pxpts]]] #Return pixel areas only else: return [[None, None], [pxextent, pxpts]]
def calcAutoArea(img, imn, colourrange, hmatrix=None, threshold=None, invprojvars=None): '''Detects areas of interest from a given image, and returns pixel and xyz areas along with polygon coordinates. Detection is performed from the image using a predefined RBG colour range. The colour range is then used to extract pixels within that range using the OpenCV function inRange. If a threshold has been set (using the setThreshold function) then only nth polygons will be retained. XYZ areas and polygon coordinates are only calculated when a set of inverse projection variables are provided. Args img (arr): Image array imn (str): Image name colourrange (list): RBG colour range for areas to be detected from threshold (int): Threshold number of detected areas to retain invprojvars (list): Inverse projection variables Returns xyzarea (list): Sum of total detected areas (xyz) xyzpts (list): XYZ coordinates of detected areas pxextent (list): Sum of total detected areas (px) pxpts (list): UV coordinates of detected areas ''' #Get upper and lower RBG boundaries from colour range upper_boundary = colourrange[0] lower_boundary = colourrange[1] #Transform RBG range to array upper_boundary = np.array(upper_boundary, dtype='uint8') lower_boundary = np.array(lower_boundary, dtype='uint8') #Extract extent based on RBG range mask = cv2.inRange(img, lower_boundary, upper_boundary) # #Speckle filter to remove noise - needs fixing # mask = cv2.filterSpeckles(mask, 1, 30, 2) #Polygonize extents using OpenCV findContours function polyimg, line, hier = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) print('\nDetected ' + str(len(line)) + ' regions in ' + str(imn)) #Append all polygons from the polys list that have more than #a given number of points rawpx = [] for c in line: if len(c) >= 40: rawpx.append(c) #If threshold has been set, only keep the nth longest polygons if threshold is not None: if len(rawpx) >= threshold: rawpx.sort(key=len) rawpx = rawpx[-(threshold):] print('Kept ' + str(len(rawpx)) + ' regions') #Calculate homography-corrected pts if desired if hmatrix is not None: print('Correcting for camera motion') pxpts=[] for i in rawpx: corr = Velocity.apply_persp_homographyPts(i, hmatrix, inverse=True) pxpts.append(corr) else: pxpts=rawpx #Calculate areas pxextent=[] for p in range(len(pxpts)): try: #Create geometry pxpoly=getOGRArea(pxpts[p].squeeze()) #Calculate area of polygon area pxextent.append(pxpoly.Area()) #Create zero object if no polygon has been recorded except: pxextent = 0 print ('Total extent: ' + str(sum(pxextent)) + ' px (out of ' + str(img.shape[0]*img.shape[1]) + ' px)') #Get xyz coordinates with inverse projection if invprojvars is not None: xyzpts=[] xyzarea=[] for i in pxpts: #Inverse project points proj=projectUV(i, invprojvars) xyzpts.append(proj) #Get areas for xyz polygons ogrpol = getOGRArea(proj) xyzarea.append(ogrpol.GetArea()) print('Total area: ' + str(sum(xyzarea)) + ' m') #Return XYZ and pixel areas return [[xyzarea, xyzpts], [pxextent, pxpts]] else: #Return pixel areas only return [[None, None], [pxextent, pxpts]]
from IK import * import multiprocessing from threading import * from tkinter import * from Paths import * from Plot import * from Velocity import * D = Derivatives() FK = ForwardKinematics() IK = InverseKinematics() P = Parameters() Plot = Plot() T = Paths() V = Velocity() q = np.zeros(P.links().shape[0]) signal = np.zeros(P.links().shape[0]) p = np.zeros(3) o = np.zeros(1) a = np.zeros(1) v = np.zeros(3) w = np.zeros(P.links().shape[0]) time = 0 dt = 0 counter = 0 class trunk: @staticmethod def algorithm(u, z):
def calcManualLine(img, imn, hmatrix=None, invprojvars=None): """Manually define a line in a given image to produce XYZ and UV line length and corresponding coordinates. Lines are defined through user input by clicking in the interactive image plot. This primarily operates via the pyplot.ginput function which allows users to define coordinates through plot interaction. If inverse projection variables are given, XYZ lines and coordinates are also calculated. :param img: Image array for plotting. :type img: arr :param imn: Image name :type imn: str :param hmatrix: Homography matrix, default to None :type hmatrix: arr, optional :param invprojvars: Inverse projection variables [X,Y,Z,uv0], default to None :type invprojvars: list, optional :returns: Four list elements containing: line length in xyz (list), xyz coordinates of lines (list), line length in pixels (list), and uvcoordinates of lines (list) :rtype: list """ #Initialise figure window fig = plt.gcf() fig.canvas.set_window_title(imn + ': Define line. ' 'Press enter to record points.') #Plot image plt.imshow(img, origin='upper', cmap='gray') rawpx = plt.ginput(n=0, timeout=0, show_clicks=True, mouse_add=1, mouse_pop=3, mouse_stop=2) print('\nYou clicked ' + str(len(rawpx)) + ' points in image ' + str(imn)) #Show plot plt.show() plt.close() #Convert coordinates to array pxpts = [] for i in rawpx: pxpts.append([[i[0], i[1]]]) pxpts = np.asarray(pxpts) #Calculate homography-corrected pts if desired if hmatrix is not None: print('Correcting for camera motion') pxpts = Velocity.apply_persp_homographyPts(pxpts, hmatrix, inverse=True) #Re-format pixel point coordinates pxpts = np.squeeze(pxpts) #Create OGR pixl line object and extract length pxline = getOGRLine(pxpts) print('Line contains ' + str(pxline.GetPointCount()) + ' points') pxline = pxline.Length() print('Line length: ' + str(pxline) + ' px') if invprojvars is not None: #Get xyz coordinates with inverse projection xyzpts = projectUV(pxpts, invprojvars) #Create ogr line object xyzline = getOGRLine(xyzpts) xyzline = xyzline.Length() print('Line length: ' + str(xyzline) + ' m') return [[xyzline, xyzpts], [pxline, pxpts]] else: #Return pixel coordinates only return [[None, None], [pxline, pxpts]]
distort = np.hstack([ radcorr1[0][0], radcorr1[0][1], tancorr1[0][0], tancorr1[0][1], radcorr1[0][2] ]) new_invprojvars = setProjection(dem, camloc1, campose1, radcorr1, tancorr1, focal1, camcen1, refimagePath) campars = [dem, new_projvars, new_invprojvars] residuals = computeResidualsXYZ(new_invprojvars, GCPxyz, GCPuv, dem) print('\nLOADING MASKS') print('Defining velocity mask') vmask1 = FileHandler.readMask(None, vmaskPath_s) vmask2 = Velocity.readDEMmask(dem, im1, new_invprojvars, vmaskPath_d) print('Defining homography mask') hmask = FileHandler.readMask(None, hmaskPath) #-------------------- Plot camera environment info ------------------------ print('\nPLOTTING CAMERA ENVIRONMENT INFO') #Load reference image refimg = FileHandler.readImg(refimagePath) imn = Path(refimagePath).name #Show Prinicpal Point in image Utilities.plotPrincipalPoint(camcen1, refimg, imn)