Example #1
0
    ### step2: get edges
    if wholes is not None:
        edges = []
        lls = []  # store line structure with extended points and original lines, eliminate extreme lines
        houghs = []
        for box in wholes:

            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]]
## 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
canny1 = my_canny(arr[:, :, 0], fn, sigma=1, save=True, show=True)
from src.utils.util import line_struct
lines = [] # store line structure with extended points and original lines, eliminate extreme lines
for line in lines_init:
    l = line_struct()
    l.line = line
    left, right = extend_line_canny_points(line, canny1)
    if left is not -1:
        l.extend_pts_left = left
        l.extend_pts_right = right
    lines.append(l)
Example #3
0
"""
###############################
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
from src.utils.snake import init_rect
x, y = init_rect(px.shape[0], px.shape[1])