cur_arr = cur_arr_3[:, :, 0] from src.utils import preprocessing import cv2 from src.utils.canny import decide_sigma for sigma in [5]: for neighbor in [30]: blur = cv2.bilateralFilter(cur_arr.copy(), neighbor, sigma, sigma) showimage_pil(blur) cur_arr = blur sigma = 0.0 r = 1 threshold = 0.025 while r > threshold: print "sigma: " + str(sigma) ret = my_canny(cur_arr, sigma=sigma, save=False, show=False) s, r = decide_sigma(ret, ret.size, threshold=threshold,show=True) if s is False: sigma += 0.5 else: if sigma == 0.0 and r < 0.01: sharpen = preprocessing.sharpen(blur, sigma=2) cur_arr= sharpen showimage_pil(sharpen) r = 1 continue break showimage_pil(ret)
parent_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory xray_dir = os.path.dirname(parent_dir) os.chdir(xray_dir) sys.path.append(parent_dir) result_dir ='/Users/ruhansa/Desktop/result/xray/' filenum = '1' rawfn = xray_dir+ '/data/' + filenum + '.jpg' img =Image.open(rawfn) width = img.size[0] height = img.size[1] arr = np.array(img.getdata(), dtype=np.uint8).reshape(height, width, 3) ## 2. do canny with ROI (TODO: Probabilisticly solve this problem) from src.utils.canny import my_canny fn = result_dir + filenum +'canny' ret = my_canny(arr[:, :, 0], fn, sigma=2, save=DEBUG, show=DEBUG) y1, x1 = 200, 150 y2, x2 = 500, 350 arr2 = np.array(ret[y1:y2, x1:x2], dtype=np.int16) if DEBUG is True: temp = Image.fromarray(255*arr2.astype(np.uint8)) temp.show() ## 3. using hough_transform to find initial horizontal line from src.utils.hough import hough_horizontal fn = result_dir + filenum + 'canny_hough' lines_init = hough_horizontal(arr2, fn, hough_line_len=30, save=True, show=True, raw=img, xdiff=150, ydiff=200) ## 4. Extend line using smaller sigma (TODO: Probabilistically choose sigma) from src.utils.util import extend_line_canny_points
max_orientation, max_magnitude, max_frequency = gabor_feature_real(arr, sigmax=sigmax, sigmay=sigmay) from src.utils.gabor import normalize normal_gabor = normalize(max_magnitude) fn = '/Users/ruhansa/Desktop/4.jpg' saveimage_pil(normal_gabor, fn) arr = filename2arr(fn) from src.utils.canny import decide_sigma sigma = 0.0 for i in range(5): print "sigma: " + str(sigma) ret = my_canny(arr, sigma=sigma, save=False, show=True) s = decide_sigma(ret, ret.size) if s is False: sigma += 0.5 else: break raw = Image.fromarray(arr) w, h = raw.size from src.utils.hough import my_hough_2 import math v_hough_end = math.pi / 9 v_hough_start = - math.pi / 9 # horizontal h_hough_end = math.pi / 2 + math.pi / 9 h_hough_start = math.pi / 2 - math.pi / 9
ROI = arr[box[1]:box[3], box[0]:box[2]] edges, sigma, cur_ROI= best_edges(ROI, threshold=0.02) # showimage_pil(edges) ### step3: hough from src.utils.hough import hough_horizontal fn = result_dir + '/' + testn + 'canny_hough' lines_init, img3 = hough_horizontal(edges, fn=None, hough_line_len=40, line_gap=50, save=False, show=True, raw=img3, xdiff=box[0], ydiff=box[1]) if debug is True: raw_input("line detect from bounding box") ### 4. Extend line using smaller sigma from src.utils.util import extend_line_points from src.utils.canny import my_canny canny1 = my_canny(cur_ROI, sigma=(sigma-2), save=False, show=False) from src.utils.util import line_struct xdiff=box[0] ydiff=box[1] for line in lines_init: l = line_struct() l.line = [[line[0][0] + xdiff, line[0][1] + ydiff], [line[1][0] + xdiff, line[1][1] + ydiff]] left, right = extend_line_points(line, edges=canny1) if left is not -1: if len(left) is not 0: l.extend_pts_left = [[left[i][0] + xdiff, left[i][1] + ydiff] for i in range(len(left))] if len(right) is not 0: l.extend_pts_right = [[right[i][0] + xdiff, right[i][1] + ydiff] for i in range(len(right))] if l.getLen() <= mean and l.getLen() > mean/5:
fn = result_folder + testn + '_' + clfn + '/boxes.pkl' boxes = pickle.load(open(fn, "rb")) """ ############################### detect edges based on boxes ############################### """ from src.utils.hough import hough_horizontal, hough_vertical from src.utils.canny import my_canny from src.utils.preprocessing import normalize box = boxes[2] window = arr[box[1]+5: box[3]-5, box[0]+5: box[2]-5] # to eliminate the edge scenario width = window.shape[1] height = window.shape[0] window = normalize(window) ret = my_canny(window, sigma=0.0, save=False, show=DEBUG) raw = Image.fromarray(np.zeros((height, width))) lineh, raw = hough_horizontal(ret, hough_line_len=30,line_gap=50, save=False, show=True, raw=raw) linev, raw = hough_vertical(ret,hough_line_len=30,line_gap=50, save=False, show=True, raw=raw ) raw.show() f = np.array(raw.convert('L').getdata()).reshape(height, width)/255. from src.utils.canny import decide_sigma decide_sigma(f, area=width*height) from src.utils.GVF import GVF, normalize_GVF_external_force u, v = GVF(f, 0.2, 80) px,py = normalize_GVF_external_force(u, v) from src.utils.io import show_vector line, plt = show_vector(px, 0-py, skip=6, holdon=True) from src.utils.snake import snake_disp