def adjust_image(filename, debug): image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) if image.shape[1] > image.shape[0]: image = numpy.rot90(image, 3) imblur = cv2.blur(image, (50, 50)) thresh, imthresh = cv2.threshold(imblur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) contours, heirarchy = cv2.findContours(imthresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cont_im = image.copy() cv2.drawContours(cont_im, contours, -1, 255, 3) if debug == 2: opencv_fft.dbg_save("/tmp/bw.png", image) opencv_fft.dbg_save("/tmp/blur.png", imblur) opencv_fft.dbg_save("/tmp/thresh.png", imthresh) opencv_fft.dbg_save("/tmp/contour.png", cont_im) return image, imblur, thresh, imthresh, contours, heirarchy, cont_im
def adjust_image(filename,debug): image = cv2.imread(filename,cv2.IMREAD_GRAYSCALE) if image.shape[1] > image.shape[0]: image = numpy.rot90(image,3) imblur = cv2.blur(image,(50,50)) thresh, imthresh = cv2.threshold(imblur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) contours, heirarchy = cv2.findContours(imthresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) cont_im = image.copy() cv2.drawContours(cont_im,contours,-1,255,3) if debug == 2: opencv_fft.dbg_save('/tmp/bw.png',image) opencv_fft.dbg_save('/tmp/blur.png',imblur) opencv_fft.dbg_save('/tmp/thresh.png',imthresh) opencv_fft.dbg_save('/tmp/contour.png',cont_im) return image, imblur,thresh,imthresh,contours,heirarchy,cont_im
def box_light_fft(filename, debug): # Form Contours of lights to be encompassed image, imblur, thresh, imthresh, contours, heirarchy, cont_im = adjust_image(filename, debug) center_im = image.copy() centers = [] radii = [] # compute boxes around found transmitters using Bounding Box method rects, boxs, centers, number_of_transmitters = find_first_transmitters(contours) if debug == 2: box_im = image.copy() for box in boxs: cv2.drawContours(box_im, [box], 0, (255, 255, 0), 10) opencv_fft.dbg_save("/tmp/box.png", box_im) if number_of_transmitters == 0: return exit_box_algorithm(image.shape) estimated_frequencies = [] windowed_size = 100 average_window = 50 avg_threshold = 2 # what the hell are these expanded_trans_list = [] final_trans_list = [] new_centers = [] new_boxes = [] # expand list of transmitters, breaking blobs of light using center line for i in xrange(number_of_transmitters): slope1, slope2 = find_slope(boxs, i) image_col = image[:, centers[i][1]] avg_threshold = [] top_boundary, bottom_boundary = find_horizontal_bound( image_col, centers, i, average_window, avg_threshold, image ) if top_boundary == 1: top_boundary = boxs[i][2][1] bottom_boundary = boxs[i][0][1] # Run Check for angle of box if abs(slope1) < 0.2: # i can move this figure after more testing expanded_trans_list, new_centers, new_boxes = side_fft( expanded_trans_list, new_centers, new_boxes, centers, i, top_boundary, bottom_boundary, image, average_window, boxs, imthresh, ) print("Sideways box") continue avg_threshold = 2 # can play! layers = add_layers(centers[i], top_boundary, bottom_boundary) temp_trans_list = [] cord = [] # Find frequencies along layered points of the transmitter in question for k in range(len(layers)): point = layers[k] image_row = image[point[0]] left_boundary, right_boundary = find_vertical_bound( image_row, point, i, average_window, avg_threshold, image ) j = left_boundary span = 100 freq_list = [] freq_list = freq_in_row(j, span, image_row, left_boundary, right_boundary, freq_list) val = len(expanded_trans_list) expanded_trans_list, cord = seperate_transmitters( freq_list, expanded_trans_list, left_boundary, point, i, cord, k ) best_diff = 100000 best_b = 0 b = [] # if there were two boxes found, Split box in two. otherwise keep original. if len(cord) >= 3: b, best_b = find_intercept(slope1, slope2, cord, layers, boxs, best_diff, i) top_cord, bottom_cord = find_box_intercepts(slope1, slope2, b[best_b], boxs, i) box1 = create_box(bottom_cord, boxs[i][1], boxs[i][2], top_cord) box2 = create_box(boxs[i][0], bottom_cord, top_cord, boxs[i][3]) # create new algorithm for center of mass calculation. what type of box am i new_boxes.append(box1) new_boxes.append(box2) slidx1, slidy1, slidx2, slidy2 = shift_out(box1, box2, slope2) slidx3, slidy3 = shift_vert(boxs, slope1, i) mid_topx, mid_topy, mid_botx, mid_boty = find_box_mids(boxs[i]) sign = find_surroundings(mid_topx, mid_topy, mid_botx, mid_boty, imthresh, image, slope1) # modify the individual box centers taking account of surroundings add_new_centers(new_centers, slidx1, slidy1, slidx2, slidy2, slidx3, slidy3, box1, box2, sign) else: # If single box, add original center and box new_boxes.append(boxs[i]) new_centers.append(centers[i]) print("New Centers: " + str(new_centers)) finbox_im = image.copy() if True: for box in new_boxes: box = numpy.array(box) cv2.drawContours(finbox_im, [box], 0, (255, 255, 0), 10) cv2.imwrite("/home/noah/lab/final_box.png", finbox_im) opencv_fft.dbg_save("/temp/final_box.png", finbox_im) freqs = [] new_trans_list = [] # Edit the bounds of each center found, for 2nd fft run for i in range(len(new_centers)): # this could be made more accurate expanded_trans_list[i].x = new_centers[i][1] expanded_trans_list[i].y = new_centers[i][0] edit_light_bounds(expanded_trans_list[i], new_centers[i]) final_trans_list = [] # Determine Frequencies of all Transmitters spot = 0 for i in xrange(len(expanded_trans_list)): image_row = image[expanded_trans_list[i].y] if expanded_trans_list[i].right - expanded_trans_list[i].left > 100: y = image_row[expanded_trans_list[i].left + 50 : expanded_trans_list[i].right] else: y = image_row[expanded_trans_list[i].left : expanded_trans_list[i].right] continue peak_freq = find_peak(y) flag = False noise = 300 # 300 is very noise, can i tone this down? # erase transmitters if same frequency as another for j in xrange(len(estimated_frequencies)): if estimated_frequencies[j] < peak_freq + noise and estimated_frequencies[j] > peak_freq - noise: print("Multiple signals with same frequency") flag = True if flag == False: final_trans_list.append(expanded_trans_list[i]) estimated_frequencies.append(peak_freq) spot = spot + 1 if len(final_trans_list) < 3: return exit_box_algorithm(image.shape) print("Estimated Frequencies: " + str(estimated_frequencies)) center_list = [] radii_list = [] for i in xrange(len(final_trans_list)): point = [] point.append(final_trans_list[i].y) point.append(final_trans_list[i].x) center_list.append(point) # the radii list is meaningless right now. what is it used for? radii_list.append((final_trans_list[i].right - final_trans_list[i].left) / 2) circle_im = finbox_im.copy() for i in xrange(len(final_trans_list)): cv2.circle(circle_im, (final_trans_list[i].x, final_trans_list[i].y), 10, (255, 255, 0), -1, 8) opencv_fft.dbg_save("/home/noah/lab/circle.png", circle_im) centers = numpy.array(center_list) radii = numpy.array(radii_list) estimated_frequencies = numpy.array(estimated_frequencies) return (centers, radii, estimated_frequencies, image.shape, True)
def box_light_fft(filename,debug): #Form Contours of lights to be encompassed image, imblur,thresh,imthresh,contours,heirarchy,cont_im = adjust_image(filename,debug) center_im = image.copy() centers = [] radii = [] #compute boxes around found transmitters using Bounding Box method rects ,boxs, centers, number_of_transmitters = find_first_transmitters(contours) if debug == 2: box_im = image.copy() for box in boxs: cv2.drawContours(box_im,[box],0,(255,255,0),10) opencv_fft.dbg_save('/tmp/box.png',box_im) if number_of_transmitters == 0: return exit_box_algorithm(image.shape) estimated_frequencies = [] windowed_size = 100 average_window = 50 avg_threshold = 2 #what the hell are these expanded_trans_list = [] final_trans_list = [] new_centers = [] new_boxes = [] #expand list of transmitters, breaking blobs of light using center line for i in xrange(number_of_transmitters): slope1,slope2 = find_slope(boxs,i) image_col = image[:,centers[i][1]] avg_threshold = [] top_boundary, bottom_boundary = find_horizontal_bound(image_col,centers,i,average_window,avg_threshold,image) if top_boundary == 1: top_boundary = boxs[i][2][1] bottom_boundary = boxs[i][0][1] #Run Check for angle of box if abs(slope1) < .2:#i can move this figure after more testing expanded_trans_list, new_centers, new_boxes = side_fft(expanded_trans_list,new_centers,new_boxes, centers,i,top_boundary,bottom_boundary,image,average_window,boxs,imthresh) print ("Sideways box") continue avg_threshold = 2#can play! layers = add_layers(centers[i],top_boundary,bottom_boundary) temp_trans_list = [] cord = [] #Find frequencies along layered points of the transmitter in question for k in range(len(layers)): point = layers[k] image_row = image[point[0]] left_boundary, right_boundary = find_vertical_bound(image_row,point,i,average_window,avg_threshold,image) j = left_boundary span = 100 freq_list = [] freq_list = freq_in_row(j,span,image_row,left_boundary,right_boundary,freq_list) val = len(expanded_trans_list) expanded_trans_list,cord = seperate_transmitters(freq_list,expanded_trans_list,left_boundary,point,i,cord,k) best_diff = 100000 best_b = 0 b = [] #if there were two boxes found, Split box in two. otherwise keep original. if len(cord) >= 3: b, best_b = find_intercept(slope1,slope2,cord,layers,boxs,best_diff,i) top_cord, bottom_cord = find_box_intercepts(slope1,slope2,b[best_b],boxs,i) box1 = create_box(bottom_cord,boxs[i][1],boxs[i][2],top_cord) box2 = create_box(boxs[i][0],bottom_cord,top_cord,boxs[i][3]) #create new algorithm for center of mass calculation. what type of box am i new_boxes.append(box1) new_boxes.append(box2) slidx1,slidy1,slidx2,slidy2 = shift_out(box1,box2,slope2) slidx3, slidy3 = shift_vert(boxs,slope1,i) mid_topx,mid_topy,mid_botx,mid_boty = find_box_mids(boxs[i]) sign = find_surroundings(mid_topx,mid_topy,mid_botx,mid_boty,imthresh,image,slope1) #modify the individual box centers taking account of surroundings add_new_centers(new_centers,slidx1,slidy1,slidx2,slidy2,slidx3,slidy3,box1,box2,sign) else: #If single box, add original center and box new_boxes.append(boxs[i]) new_centers.append(centers[i]) print ("New Centers: " + str(new_centers)) finbox_im = image.copy() if True: for box in new_boxes: box = numpy.array(box) cv2.drawContours(finbox_im,[box],0,(255,255,0),10) cv2.imwrite('/home/noah/lab/final_box.png',finbox_im) opencv_fft.dbg_save('/temp/final_box.png',finbox_im) freqs = [] new_trans_list = [] #Edit the bounds of each center found, for 2nd fft run for i in range(len(new_centers)):#this could be made more accurate expanded_trans_list[i].x = new_centers[i][1] expanded_trans_list[i].y = new_centers[i][0] edit_light_bounds(expanded_trans_list[i],new_centers[i]) final_trans_list = [] #Determine Frequencies of all Transmitters spot = 0 for i in xrange(len(expanded_trans_list)): image_row = image[expanded_trans_list[i].y] if expanded_trans_list[i].right - expanded_trans_list[i].left > 100: y = image_row[expanded_trans_list[i].left + 50:expanded_trans_list[i].right] else: y = image_row[expanded_trans_list[i].left:expanded_trans_list[i].right] continue peak_freq = find_peak(y) flag = False noise = 300 #300 is very noise, can i tone this down? #erase transmitters if same frequency as another for j in xrange(len(estimated_frequencies)): if estimated_frequencies[j] < peak_freq + noise and estimated_frequencies[j] > peak_freq - noise: print ("Multiple signals with same frequency") flag = True if flag == False: final_trans_list.append(expanded_trans_list[i]) estimated_frequencies.append(peak_freq) spot = spot + 1 if len(final_trans_list) < 3: return exit_box_algorithm(image.shape) print ("Estimated Frequencies: " + str(estimated_frequencies)) center_list = [] radii_list = [] for i in xrange(len(final_trans_list)): point= [] point.append(final_trans_list[i].y) point.append(final_trans_list[i].x) center_list.append(point) #the radii list is meaningless right now. what is it used for? radii_list.append((final_trans_list[i].right - final_trans_list[i].left)/2) circle_im = finbox_im.copy() for i in xrange(len(final_trans_list)): cv2.circle(circle_im, (final_trans_list[i].x,final_trans_list[i].y),10,(255,255,0),-1,8) opencv_fft.dbg_save('/home/noah/lab/circle.png',circle_im) centers = numpy.array(center_list) radii = numpy.array(radii_list) estimated_frequencies = numpy.array(estimated_frequencies) return (centers,radii,estimated_frequencies, image.shape,True)