def extract_rays(seq, sub, skip=2, max_frame=None, imtype='png'): edge_dir = os.path.join(seq, 'edges', '*.'+imtype) paths = glob.glob(edge_dir) paths.sort() F = len(paths) cam = IO.read_cam_param( os.path.join(seq, 'cam.yaml') ) Rt_path = os.path.join(seq, 'tracking.csv') R, t, times, valid = IO.read_tracking(Rt_path) valid_bool = zeros(len(R), bool) valid_bool[valid] = True cloud = RayCloud.RayCloud() cloud.set_cam(cam) cloud.set_poses(R, t) fig = pynutmeg.figure('segments', 'figs/segments.qml') if max_frame is None: max_frame = F print("Estimating bounding box volume from labels") frames, boxes = IO.read_rects( os.path.join(seq, 'rects.yaml') ) cloud.apply_bounding_boxes(frames, boxes) print("Extracting Edge Rays") for f in range(0, max_frame, skip): if not valid_bool[f]: continue print("\r\tFrame: {} of {}".format(f, F), end=' '*16, flush=True) E = IO.imread( paths[f] ) pts, labels = Contours.find_contours_edge(E, low=20, high=35, min_length=30) im = np.empty_like(E) im[:] = 255 im[pts[:,1], pts[:,0]] = 0 # TODO: Maybe put the next 2 into the one function. This is fine for now though pts, labels = Contours.simplify_labeled(pts, labels, eps=1.5) tangents, labels = Contours.segment_tangents(pts, labels, thresh=35.) # fig.set('ax.im', binary=255-E) fig.set('ax.im', binary=im) cloud.add_pixels(pts, tangents, f, labels) print("\nSaving ray cloud...") cloud.save( os.path.join(seq, sub) )
def visualize_segments(seq): edge_dir = os.path.join(seq, 'edges', '*.jpg') paths = glob.glob(edge_dir) fig = pynutmeg.figure('segments', 'figs/segments.qml') fig.set_gui('figs/segments_gui.qml') fig.set('ax.im', xOffset=-0.5, yOffset=-0.5) nextframe = fig.parameter('nextframe') nextframe.wait_changed() prv = fig.parameter('prev') nxt = fig.parameter('next') for p in paths[::50]: print("\nReading in {}".format(p)) E = IO.imread(p) pts, labels = Contours.find_contours_edge(E, low=30, high=50, min_length=10) J, I = pts.T show = np.empty_like(E) show[:] = 255 show[I, J] = 255 - E[I, J] fig.set('ax.im', binary=show) label = 0 x = empty(0) y = empty(0) while True: if nextframe.changed: nextframe.read() break label_changed = nxt.changed or prv.changed if nxt.changed: label += 1 elif prv.changed: label = max(0, label - 1) if label_changed: print("Calc for label {}".format(label)) prv.read() nxt.read() sel = np.where(labels == label)[0] x = J[sel].astype(float) y = I[sel].astype(float) fig.set('ax.P0', x=x, y=y) fig.set('ax.P1', x=x[0:1], y=y[0:1]) time.sleep(0.005)
def myMain(): pics = '/Users/seandoyle/git/AndromedaVision/WPIPics' myTarget = createSampleTarget() for filename in os.listdir(pics): fullFileName = os.path.join(pics, filename) image = cv2.imread(fullFileName) boundingBoxesOfTargetCandidates = [] normalImageDebugger = ImageDebugger(image, boundingBoxesOfTargetCandidates, "Normal Image") normalImageDebugger.displayImage() colorFilter = Color(myTarget.hSV, image) hSVFilteredImage = colorFilter.run() hSVFilteredImageDebugger = ImageDebugger(hSVFilteredImage, boundingBoxesOfTargetCandidates, "HSV") hSVFilteredImageDebugger.displayImage() contourFilter = Contours(myTarget.numberOfContours, hSVFilteredImage) boundingBoxesOfTargetCandidates = contourFilter.run() contourFilterDebugger = ImageDebugger(image, boundingBoxesOfTargetCandidates, "Contour") contourFilterDebugger.displayImageWithBoundingBoxes() sizeFilter = Size(myTarget.size, boundingBoxesOfTargetCandidates) boundingBoxesOfTargetCandidates = sizeFilter.run() sizeFilterDebugger = ImageDebugger(image, boundingBoxesOfTargetCandidates, "Size") sizeFilterDebugger.displayImageWithBoundingBoxes() lengthToWidthRatioFilter = LengthToWidthRatio(myTarget.lengthToWidthRatio, boundingBoxesOfTargetCandidates) boundingBoxesOfTargetCandidates = lengthToWidthRatioFilter.run() lengthToWidthRatioDebugger = ImageDebugger(image, boundingBoxesOfTargetCandidates, "LengthToWidth") lengthToWidthRatioDebugger.displayImageWithBoundingBoxes() percentWhitePixelsFilter = PercentWhitePixels(myTarget.percentWhitePixels, boundingBoxesOfTargetCandidates, hSVFilteredImage) boundingBoxesOfTargetCandidates = percentWhitePixelsFilter.run() percentWhitePixelsDebugger = ImageDebugger(hSVFilteredImage, boundingBoxesOfTargetCandidates, "percentWhitePixels") percentWhitePixelsDebugger.displayImageWithBoundingBoxes()
def imageWriter(images,seen,args,fout,classification,stats,fft,lap,edge): for imgfile in images: if imgfile in seen: continue if os.path.isdir(os.path.join(args.directory, imgfile)): temp = os.listdir(os.path.join(args.directory, imgfile)) for f in temp: images.append(os.path.join(imgfile, f)) continue #rewrite of above if statement #if os.path.isdir(os.path.join(args.directory,imgfile)): # temp = os.listdir(os.path.join(args.directory,imgfile)) # pool = Pool() # pool.map(images.append, os.path.join(imgfile, f)) # pool.close() # pool.join() # continue if not args.all and imgfile not in classification: continue # silently skip the bin files that have the gps data if imgfile.endswith('bin'): continue # alert to other files that were skipped if not (imgfile.endswith('png') | imgfile.endswith('jpg')): sys.stderr.write("Skipped file: " + imgfile + "\n") continue if args.verbose: sys.stderr.write("Parsing " + imgfile + "\n") fout.write( imgfile + "\t" ) if imgfile in classification: fout.write( classification[imgfile] + "\t") else: fout.write( "unknown\t" ) img = ImageIO.cv2read(os.path.join(args.directory, imgfile)) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) fout.write( ('\t'.join(map(str, [stats.min(gray), stats.max(gray), stats.median(gray), stats.mean(gray)]))) + "\t" ) ngray = Normalization.equalizeHistograms(gray) # apply a gaussian blur to remove edge effects ngray = cv2.GaussianBlur(ngray, (3,3), 0) fout.write( ('\t'.join(map(str, [stats.min(ngray), stats.max(ngray), stats.median(ngray), stats.mean(ngray)]))) + "\t") for i in range(3): imp = img[:,:,i] fout.write( ('\t'.join(map(str, [stats.min(imp), stats.max(imp), stats.median(imp), stats.mean(imp)]))) + "\t" ) fout.write( str(fft.energy(gray)) + "\t" + str(fft.energy(ngray)) + "\t") if args.features: feats.detect_kp_ORB(ngray) fout.write( str(feats.numberKeyPoints()) + "\t" + str(feats.medianKeyPointSize()) + "\t" + str(feats.meanKeyPointSize()) + "\t") for i in range(15): fout.write( str(feats.numKeyPoints(i*10)) + "\t") else: fout.write("0\t0\t0\t"); for i in range(15): fout.write("0\t") for i in range(15): k=2*i+1 fout.write( str(lap.sum(ngray, k)) + "\t") for i in range(25): t2 = 10*i fout.write( str(edge.sumCanny(ngray, 1, t2)) + "\t") #edge.sumCanny(gray) # Contour detection ctr = Contours.contours(ngray) for i in range(5): threshold=50*i ctr.withCanny(1, threshold) if ctr.numberOfContours() == 0: fout.write( "0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t" ) else: try: fout.write( "\t".join(map(str, [ctr.numberOfContours(), ctr.numberOfClosedContours(), ctr.numberOfOpenContours(), ctr.totalContourArea(), cv2.contourArea(ctr.largestContourByArea()), ctr.totalPerimeterLength()])) + "\t") ctr.linelengths() fout.write( "\t".join(map(str, [ctr.maxLineLength(), ctr.meanLineLength(), ctr.medianLineLength(), ctr.modeLineLength()])) + "\t") except Exception as e: sys.stderr.write("There was an error calculating the contours for " + imgfile +": " + e.message + "\n") fout.write( "0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t" ) fout.write("\n")
pxcoords = [item[1][1] for item in lines] xyzcoords = [item[0][1] for item in lines] # Print start frame map, used for Design 4 artifact creation Utilities.plotLineXYZ(xyzcoords[0], cam.getDEM(), show=False, save=destination + 'Tracked_xyz_' + start_frame_name) # Write line coordinates to txt file # FileHandler.writeLineCoords(pxcoords, xyzcoords, imn, # destination + 'TESTuvcoord.txt', # destination + 'TESTxyzcoords.txt') # ----------------------- Create Contour From Line ------------------------- line = Contours.line_from_coords(pxcoords) cameraMatrix = cam.getCamMatrixCV2() distortP = cam.getDistortCoeffsCV2() current_imgset = terminus._imageSet image = current_imgset[0].getImageCorr(cameraMatrix, distortP) # ----------------------- Set Snake Parameters ------------------------- ends = 'free' alpha = 0.01 # Lower than 1 is ideal beta = 0.5 # Lower than 1 is ideal w_line = -10 # Must be negative w_edge = 20 # Must be positive gamma = 0.01 # Time Stepping, no reason to change convergence = 0.01 # Lower than 1 is ideal
for i in range(3): imp = img[:,:,i] print ('\t'.join(map(str, [stats.min(imp), stats.max(imp), stats.median(imp), stats.mean(imp)]))), "\t", print fft.energy(gray), "\t", fft.energy(ngray), "\t", for i in range(15): k=2*i+1 print lap.sum(ngray, k), "\t", for i in range(25): t2 = 10*i print edge.sumCanny(ngray, 1, t2), "\t", #edge.sumCanny(gray) # Contour detection ctr = Contours.contours(ngray) for i in range(5): threshold=50*i ctr.withCanny(1, threshold) if ctr.numberOfContours() == 0: print "0\t0\t0\t0\t0\t0\t0\t0\t0\t0" else: try: print "\t".join(map(str, [ctr.numberOfContours(), ctr.numberOfClosedContours(), ctr.numberOfOpenContours(), ctr.totalContourArea(), cv2.contourArea(ctr.largestContourByArea()), ctr.totalPerimeterLength()])), "\t", ctr.linelengths() print "\t".join(map(str, [ctr.maxLineLength(), ctr.meanLineLength(), ctr.medianLineLength(), ctr.modeLineLength()])), "\t", except ValueError as e: sys.stderr.write("There was an error calculating the contours for " + imgfile +": " + e.message + "\n") break
import sys sys.path.append('../Modules/') import Contours import cv2 im = cv2.imread('test.jpg') g = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) c = Contours.contours(g) print "Threshold\t# contours\tClosed\tOpen\tTotal area\tLargest\tPerimeter" for i in range(5): thresh = 50*i c.withCanny(1, thresh) # print "\t".join(map(str, [thresh, c.numberOfContours(), c.numberOfClosedContours(), # c.numberOfOpenContours(), c.totalContourArea(), cv2.contourArea(c.largestContourByArea()), # c.totalPerimeterLength()])) print thresh, "\t", print c.numberOfContours(), "\t", print c.numberOfClosedContours(), "\t", print c.numberOfOpenContours(), "\t", print c.totalContourArea(), "\t", print cv2.contourArea(c.largestContourByArea()), "\t", print c.totalPerimeterLength()