morphology_utils.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:Shoe-Shape-Classifier 作者: jrzaurin 项目源码 文件源码
def shape_df(img, axis, nsteps):
    """
    Returns a data frame with the initial and end points enclosing the product
    in the image, across the x/y axis. Why a dataframe and not tuples? just for
    convenience.
    """

    is_color = len(img.shape) == 3
    is_grey  = len(img.shape) == 2

    if is_color:
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    elif is_grey:
        gray = img.copy()

    edges = bounding_box(gray)
    gray_c = gray[edges[2]:edges[3]+1, edges[0]:edges[1]+1]
    thr = threshold_value(gray_c)

    if axis == 'x' :
        cuts = np.rint(np.linspace(5, gray_c.shape[1]-1, nsteps, endpoint=True)).astype(int)

        init = np.apply_along_axis(get_edges, 0, arr = gray_c, thresh = thr)[0,:][cuts]
        end  = np.apply_along_axis(get_edges, 0, arr = gray_c, thresh = thr)[1,:][cuts]

        df = pd.DataFrame(data = {'coord' : cuts, 'init' : init, 'end' : end},
                          columns=['coord', 'init', 'end'])

    elif axis == 'y':
        cuts = np.round(np.linspace(4, gray_c.shape[0]-1, nsteps, endpoint=True)).astype(int)

        init = np.apply_along_axis(get_edges, 1, arr = gray_c, thresh = thr)[:,0][cuts]
        end  = np.apply_along_axis(get_edges, 1, arr = gray_c, thresh = thr)[:,1][cuts]

        df = pd.DataFrame(data = {'coord' : cuts, 'init' : init, 'end' : end},
                          columns=['coord', 'init', 'end'])

    return df
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号