예제 #1
0
파일: matlab.py 프로젝트: alkasm/cvpack
def bwdist(
    img,
    method=cv2.DIST_L2,
    dist_mask=cv2.DIST_MASK_5,
    label_type=cv2.DIST_LABEL_CCOMP,
    ravel=True,
):
    """Mimics Matlab's bwdist function, similar to OpenCV's distanceTransform()
    but with different output.

    https://www.mathworks.com/help/images/ref/bwdist.html

    Available metrics:
        https://docs.opencv.org/3.4/d7/d1b/group__imgproc__misc.html#gaa2bfbebbc5c320526897996aafa1d8eb

    Available distance masks:
        https://docs.opencv.org/3.4/d7/d1b/group__imgproc__misc.html#gaaa68392323ccf7fad87570e41259b497

    Available label types:
        https://docs.opencv.org/3.4/d7/d1b/group__imgproc__misc.html#ga3fe343d63844c40318ee627bd1c1c42f
    """
    flip = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV)[1]
    dist, labeled = cv2.distanceTransformWithLabels(flip, method, dist_mask)

    if ravel:  # return linear indices if ravel == True (default)
        idx = np.zeros(img.shape, dtype=np.intp)
        idx_func = np.flatnonzero
    else:  # return two-channel indices if ravel == False
        idx = np.zeros((*img.shape, 2), dtype=np.intp)
        idx_func = lambda masked: np.dstack(np.where(masked))

    for l in np.unique(labeled):
        mask = labeled == l
        idx[mask] = idx_func(img * mask)
    return dist, idx
예제 #2
0
def colorate_template_map_by_segm(template, segm, scale):

    texture_image = np.zeros((2048, 2048))
    map_height = np.int32(texture_image.shape[0] / scale)
    map_width = np.int32(texture_image.shape[1] / scale)
    face_indices_map = np.zeros((map_height, map_width, 3))

    text_coords = template.vt[:, :2]
    text_coords[:, 0] *= map_width
    text_coords[:, 1] = (1 - text_coords[:, 1]) * map_height
    text_coords = np.int32(text_coords)

    parts = range(1 + np.max(segm))
    for part in parts:
        indices = np.where(segm == part)[0]
        color = np.int32(np.random.rand(3) * 255)
        while np.all(color == [0, 0, 0]):
            color = np.int32(np.random.rand(3) * 255)
        for face in indices:
            points = text_coords[template.ft[face]]
            points = np.int32([points]).squeeze()
            cv2.fillConvexPoly(face_indices_map, points, color.tolist())

    b_coords_map = face_indices_map
    unmapped = (np.sum((b_coords_map.reshape(
        b_coords_map.shape[0] * b_coords_map.shape[1], 3) == -np.zeros(3)),
                       axis=1) == 3).reshape(b_coords_map.shape[0],
                                             b_coords_map.shape[1])
    dst, lab = cv2.distanceTransformWithLabels(unmapped.astype(np.uint8),
                                               distanceType=1,
                                               maskSize=3,
                                               labelType=1)
    full_texture = face_indices_map[np.logical_not(unmapped)][lab - 1]
    return full_texture
예제 #3
0
def watershed_segmentation(rgb_img, mask, distance=10):
    """Uses the watershed algorithm to detect boundary of objects. Needs a marker file which specifies area which is
       object (white), background (grey), unknown area (black).

    Inputs:
    rgb_img             = image to perform watershed on needs to be 3D (i.e. np.shape = x,y,z not np.shape = x,y)
    mask                = binary image, single channel, object in white and background black
    distance            = min_distance of local maximum

    Returns:
    analysis_images     = list of output images

    :param rgb_img: numpy.ndarray
    :param mask: numpy.ndarray
    :param distance: int
    :return analysis_images: list
    """
    params.device += 1

    # Store debug mode
    debug = params.debug
    params.debug = None

    dist_transform = cv2.distanceTransformWithLabels(mask, cv2.DIST_L2, maskSize=0)[0]

    localMax = peak_local_max(dist_transform, indices=False, min_distance=distance, labels=mask)

    markers = ndi.label(localMax, structure=np.ones((3, 3)))[0]
    dist_transform1 = -dist_transform
    labels = watershed(dist_transform1, markers, mask=mask)

    img1 = np.copy(rgb_img)

    for x in np.unique(labels):
        rand_color = color_palette(len(np.unique(labels)))
        img1[labels == x] = rand_color[x]

    img2 = apply_mask(img1, mask, 'black')

    joined = np.concatenate((img2, rgb_img), axis=1)

    estimated_object_count = len(np.unique(markers)) - 1

    # Reset debug mode
    params.debug = debug
    if params.debug == 'print':
        print_image(dist_transform, os.path.join(params.debug_outdir, str(params.device) + '_watershed_dist_img.png'))
        print_image(joined, os.path.join(params.debug_outdir, str(params.device) + '_watershed_img.png'))
    elif params.debug == 'plot':
        plot_image(dist_transform, cmap='gray')
        plot_image(joined)

    outputs.add_observation(variable='estimated_object_count', trait='estimated object count',
                            method='plantcv.plantcv.watershed', scale='none', datatype=int,
                            value=estimated_object_count, label='none')

    # Store images
    outputs.images.append([dist_transform, joined])

    return joined
예제 #4
0
def compute_sdf(mask, signed=True, with_indices=True):
    """ Returns a signed distance transform for a mask, and label indices.
    :param mask: Zero values are exterior, non-zero values are interior area
    """

    # Get interior mask by erosion, compute contour from that and invert for 0-level set
    contour, interior = compute_contour(mask)
    zero_level = (1 - contour).astype(np.uint8)

    # Build mapping between 1D contour and label index
    mask2idx = [-1]
    mask2idx.extend(np.flatnonzero(contour))

    if with_indices:
        dt, idx = cv2.distanceTransformWithLabels(
            zero_level,
            cv2.DIST_L2,
            cv2.DIST_MASK_PRECISE,
            labelType=cv2.DIST_LABEL_PIXEL)
    else:
        dt = cv2.distanceTransform(zero_level, cv2.DIST_L2,
                                   cv2.DIST_MASK_PRECISE)

    if signed:
        dt[interior.astype(bool)] *= -1  # Make interior negative

    if not with_indices:
        return dt

    # Map back from pixel label to 1D image position
    for r in range(idx.shape[0]):
        for c in range(idx.shape[1]):
            idx[r, c] = mask2idx[idx[r, c]]

    return dt, idx, contour
예제 #5
0
def watershed_segmentation(rgb_img, mask, distance=10):
    """Uses the watershed algorithm to detect boundary of objects. Needs a marker file which specifies area which is
       object (white), background (grey), unknown area (black).

    Inputs:
    rgb_img             = image to perform watershed on needs to be 3D (i.e. np.shape = x,y,z not np.shape = x,y)
    mask                = binary image, single channel, object in white and background black
    distance            = min_distance of local maximum

    Returns:
    analysis_images     = list of output images

    :param rgb_img: numpy.ndarray
    :param mask: numpy.ndarray
    :param distance: int
    :return analysis_images: list
    """
    params.device += 1
    # # Will be depricating opencv version 2
    # if cv2.__version__[0] == '2':
    #     dist_transform = cv2.distanceTransform(mask, cv2.cv.CV_DIST_L2, maskSize=0)
    # else:
    dist_transform = cv2.distanceTransformWithLabels(mask, cv2.DIST_L2, maskSize=0)[0]

    localMax = peak_local_max(dist_transform, indices=False, min_distance=distance, labels=mask)

    markers = ndi.label(localMax, structure=np.ones((3, 3)))[0]
    dist_transform1 = -dist_transform
    labels = watershed(dist_transform1, markers, mask=mask)

    img1 = np.copy(rgb_img)

    for x in np.unique(labels):
        rand_color = color_palette(len(np.unique(labels)))
        img1[labels == x] = rand_color[x]

    img2 = apply_mask(img1, mask, 'black')

    joined = np.concatenate((img2, rgb_img), axis=1)

    estimated_object_count = len(np.unique(markers)) - 1

    analysis_image = []
    analysis_image.append(joined)

    if params.debug == 'print':
        print_image(dist_transform, os.path.join(params.debug_outdir, str(params.device) + '_watershed_dist_img.png'))
        print_image(joined, os.path.join(params.debug_outdir, str(params.device) + '_watershed_img.png'))
    elif params.debug == 'plot':
        plot_image(dist_transform, cmap='gray')
        plot_image(joined)

    outputs.add_observation(variable='estimated_object_count', trait='estimated object count',
                            method='plantcv.plantcv.watershed', scale='none', datatype=int,
                            value=estimated_object_count, label='none')

    # Store images
    outputs.images.append(analysis_image)

    return analysis_image
예제 #6
0
def getRadiusCenterCircle(raw_dist):
    dist_transform, label = cv2.distanceTransformWithLabels(raw_dist,
                                                            cv2.DIST_L2,
                                                            maskSize=5)

    _, radius, _, center = cv2.minMaxLoc(dist_transform)

    points = None

    # 넘버링 여러 곳에 하는 기능 개발 중, 주석처리하였음

    # dist_transform = cv2.distanceTransform(raw_dist, cv2.DIST_L2, maskSize=5)
    # points = [list(dist_transform)[i] for i in range(0, len(dist_transform), 300) if list(dist_transform)[i] > 50]
    # print(type(list(dist_transform)[50]), list(dist_transform)[50])

    # ret, dist1 = cv2.threshold(dist_transform, 0.6*dist_transform.max(), 255, 0)

    # print(f'dist_transform : {dist_transform}')
    # print(f'label: {label}')

    # points = []
    # points = np.where(dist_transform > 10)

    # for idx in range(0, len(dist_transform)-30, 30):
    #     _, radius, _, center = cv2.minMaxLoc(dist_transform[idx:idx+30])
    #     if radius > 10:
    #         points.append((radius, center))

    # print(np.unique(np.where(label > 10)))

    # result = cv2.normalize(dist_transform, None, 255, 0, cv2.NORM_MINMAX, cv2.CV_8UC1)
    # minVal, maxVal, a, center = cv2.minMaxLoc(result)

    return radius, center, points
def nearest_point(refined_lidar):
    value_mask = np.asarray(1.0 - np.squeeze(refined_lidar) > 0.1).astype(
        np.uint8)
    dt, lbl = cv2.distanceTransformWithLabels(value_mask,
                                              cv2.DIST_L1,
                                              5,
                                              labelType=cv2.DIST_LABEL_PIXEL)
    return dt, lbl
예제 #8
0
    def loadsympascal(self, imgidx, gtidx):
        # load image and skeleton
        image = cv2.imread(
            '{}/SymPASCAL-by-KZ/{}'.format(self.data_dir, imgidx), 1)
        skeleton = cv2.imread(
            '{}/SymPASCAL-by-KZ/{}'.format(self.data_dir, gtidx), 0)
        skeleton = (skeleton > 0).astype(np.uint8)

        # normalization
        image = image.astype(np.float32)
        image -= self.mean
        image = image.transpose((2, 0, 1))

        # compute flux and dilmask
        kernel = np.ones((15, 15), np.uint8)
        dilmask = cv2.dilate(skeleton, kernel)
        rev = 1 - skeleton
        height = rev.shape[0]
        width = rev.shape[1]
        rev = (rev > 0).astype(np.uint8)
        dst, labels = cv2.distanceTransformWithLabels(
            rev,
            cv2.cv.CV_DIST_L2,
            cv2.cv.CV_DIST_MASK_PRECISE,
            labelType=cv2.DIST_LABEL_PIXEL)

        index = np.copy(labels)
        index[rev > 0] = 0
        place = np.argwhere(index > 0)

        nearCord = place[labels - 1, :]
        x = nearCord[:, :, 0]
        y = nearCord[:, :, 1]
        nearPixel = np.zeros((2, height, width))
        nearPixel[0, :, :] = x
        nearPixel[1, :, :] = y
        grid = np.indices(rev.shape)
        grid = grid.astype(float)
        diff = grid - nearPixel

        dist = np.sqrt(np.sum(diff**2, axis=0))

        direction = np.zeros((2, height, width), dtype=np.float32)
        direction[0, rev > 0] = np.divide(diff[0, rev > 0], dist[rev > 0])
        direction[1, rev > 0] = np.divide(diff[1, rev > 0], dist[rev > 0])

        direction[0] = direction[0] * (dilmask > 0)
        direction[1] = direction[1] * (dilmask > 0)

        flux = -1 * np.stack((direction[0], direction[1]))

        dilmask = (dilmask > 0).astype(np.float32)
        dilmask = dilmask[np.newaxis, ...]

        return image, flux, dilmask
예제 #9
0
def disappear_instance(labels_source,
                       instances,
                       instance_ids=None,
                       only_objects=True,
                       swap_fraction=0.5,
                       **_):
    print("Removing instances")
    if instance_ids is None:
        instance_uniq = np.unique(instances)

        if only_objects:
            instance_uniq_objects = instance_uniq[instance_uniq >= 24000]
        else:
            instance_uniq_objects = instance_uniq[instance_uniq >= 1]

        if instance_uniq_objects.__len__() == 0:
            return dict(labels_swapErr=labels_source.copy(), )

    instance_ids = np.random.choice(instance_uniq_objects,
                                    int(instance_uniq_objects.__len__() *
                                        swap_fraction),
                                    replace=False)
    disappear_mask = np.any([instances == i for i in instance_ids], axis=0)

    obj_classes = np.unique(labels_source[disappear_mask])

    forbidden_class = remove_trainIDs
    mask_forbidden_class = np.any(
        [labels_source == j for j in forbidden_class], axis=0)

    mask_no_label = mask_forbidden_class | disappear_mask
    mask_label = np.logical_not(mask_no_label)

    closest_dst, closest_labels = cv2.distanceTransformWithLabels(
        mask_no_label.astype(np.uint8),
        distanceType=cv2.DIST_L2,
        maskSize=5,
        labelType=cv2.DIST_LABEL_PIXEL,
    )

    BG_indices = closest_labels[mask_label]
    BG_labels = labels_source[mask_label]

    label_translation = np.zeros(labels_source.shape,
                                 dtype=np.uint8).reshape(-1)
    label_translation[BG_indices] = BG_labels

    label_recon = labels_source.copy()
    label_recon[disappear_mask] = label_translation[closest_labels.reshape(
        labels_source.shape)[disappear_mask]]

    result = dict(labels_swapErr=label_recon, )

    return result
 def update(dummy=None):
     global need_update
     need_update = False
     thrs = cv2.getTrackbarPos('threshold', 'distrans')
     mark = cv2.Canny(img, thrs, 3*thrs)
     dist, labels = cv2.distanceTransformWithLabels(~mark, cv.CV_DIST_L2, 5)
     if voronoi:
         vis = cm[np.uint8(labels)]
     else:
         vis = cm[np.uint8(dist*2)]
     vis[mark != 0] = 255
     cv2.imshow('distrans', vis)
예제 #11
0
파일: distrans.py 프로젝트: zx901/opencv
 def update(dummy=None):
     global need_update
     need_update = False
     thrs = cv2.getTrackbarPos('threshold', 'distrans')
     mark = cv2.Canny(img, thrs, 3 * thrs)
     dist, labels = cv2.distanceTransformWithLabels(~mark, cv2.DIST_L2, 5)
     if voronoi:
         vis = cm[np.uint8(labels)]
     else:
         vis = cm[np.uint8(dist * 2)]
     vis[mark != 0] = 255
     cv2.imshow('distrans', vis)
예제 #12
0
파일: morph.py 프로젝트: jacektl/ocrd_cis
def spread_labels(labels, maxdist=9999999):
    """Spread the given labels to the background."""
    #distances,features = morphology.distance_transform_edt(labels==0,return_distances=1,return_indices=1)
    #indexes = features[0]*labels.shape[1]+features[1]
    #spread = labels.ravel()[indexes.ravel()].reshape(*labels.shape)
    distances, indexes = cv2.distanceTransformWithLabels(
        array(labels == 0, uint8),
        cv2.DIST_L2,
        cv2.DIST_MASK_PRECISE,
        labelType=cv2.DIST_LABEL_PIXEL)
    spread = labels[where(labels > 0)][indexes - 1]
    spread *= (distances < maxdist)
    return spread
def nearest_filling(depth_map):
    zero_mask = depth_map < 1e-3
    dist, labels = cv2.distanceTransformWithLabels(
        src=zero_mask.astype(np.uint8),
        distanceType=cv2.DIST_L2,
        maskSize=cv2.DIST_MASK_3,
        labelType=cv2.DIST_LABEL_PIXEL)
    far_mask = dist > 35.
    labels = labels - 1
    depth_vec = depth_map[~zero_mask]
    intered_depth = depth_vec[labels]
    intered_depth[far_mask] = 0.
    return intered_depth
예제 #14
0
def run(controller):
    maps_initialized = False
    count = 0
    while (True):
        #sleepで更新速度を制御
        time.sleep(1)
        frame = controller.frame()
        image = frame.images[0]
        if image.is_valid:
            if not maps_initialized:
                left_coordinates, left_coefficients = convert_distortion_maps(
                    frame.images[0])
                right_coordinates, right_coefficients = convert_distortion_maps(
                    frame.images[1])
                maps_initialized = True

            undistorted_left = undistort(image, left_coordinates,
                                         left_coefficients, 400, 400)
            undistorted_right = undistort(image, right_coordinates,
                                          right_coefficients, 400, 400)

            #画像を2値化(白黒に処理)
            ret, hand = cv2.threshold(undistorted_right, 70, 255,
                                      cv2.THRESH_BINARY)

            my_hand = hand[80:320, 40:360]

            # 以下、最も広い白領域のみを残すための計算
            # まず、白領域の塊(クラスター)にラベルを振る
            img_dist, img_label = cv2.distanceTransformWithLabels(
                255 - my_hand, cv2.DIST_L2, 5)
            img_label = np.uint8(img_label) & my_hand
            # ラベル0は黒領域なので除外
            img_label_not_zero = img_label[img_label != 0]
            # 最も多く現れたラベルが最も広い白領域のラベル
            if len(img_label_not_zero) != 0:
                m = stats.mode(img_label_not_zero)[0]
            else:
                m = 0
            # 最も広い白領域のみを残す
            this_hand = np.uint8(img_label == m) * 255

            # 得られた二値化画像を画面に表示
            cv2.imshow('hand', this_hand)
            #cv2.imshow('hand', undistorted_right)
            cv2.imwrite("ImageData/5/img_five{0:03d}.png".format(count),
                        this_hand)
            count += 1

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
예제 #15
0
def run(controller):
    maps_initialized = False
    while(True):
        #sleepで更新速度を制御
        time.sleep(0.1)
        frame = controller.frame()
        image = frame.images[0]
        if image.is_valid:
            if not maps_initialized:
                left_coordinates, left_coefficients = convert_distortion_maps(frame.images[0])
                right_coordinates, right_coefficients = convert_distortion_maps(frame.images[1])
                maps_initialized = True

            undistorted_left = undistort(image, left_coordinates, left_coefficients, 400, 400)
            undistorted_right = undistort(image, right_coordinates, right_coefficients, 400, 400)

            #画像を2値化(白黒に処理)
            ret,hand = cv2.threshold(undistorted_right,80,255,cv2.THRESH_BINARY)

            my_hand = hand[80:320,40:360]

            # 以下、最も広い白領域のみを残すための計算
            # まず、白領域の塊(クラスター)にラベルを振る
            img_dist, img_label = cv2.distanceTransformWithLabels(255-my_hand, cv2.DIST_L2, 5)
            img_label = np.uint8(img_label) & my_hand
            # ラベル0は黒領域なので除外
            img_label_not_zero = img_label[img_label != 0]
            # 最も多く現れたラベルが最も広い白領域のラベル
            if len(img_label_not_zero) != 0:
                m = stats.mode(img_label_not_zero)[0]
            else:
                m = 0
            # 最も広い白領域のみを残す
            this_hand = np.uint8(img_label == m)*255

            # 最大の白領域からscikit-learnに入力するためのベクトルを取得
            hand_vector = getImageVector(this_hand)

            # 学習済のニューラルネットワークから分類結果を取得
            result = clf.predict(hand_vector)
            # 分類結果を表示
            print(janken_class[result[0]])

            # 得られた二値化画像を画面に表示
            cv2.imshow('hand', this_hand)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
예제 #16
0
def label2flux(label):
    """
    Create direction_field of corresponding label

    Returns:
        direction_field: shape (2, h, w), weight_matrix: (h, w)
    """
    label = label.copy()
    label = label.astype(np.int32)
    height, width = label.shape
    label += 1
    categories = np.unique(label)

    if 0 in categories:
        raise ValueError('invalid category')

    label = cv2.copyMakeBorder(label, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0)

    weight_matrix = np.zeros((height+2, width+2), dtype=np.float32)
    direction_field = np.zeros((2, height+2, width+2), dtype=np.float32)

    for category in categories:
        img = (label == category).astype(np.uint8)
        weight_matrix[img > 0] = 1. / np.sqrt(img.sum())

        _, labels = cv2.distanceTransformWithLabels(img, cv2.DIST_L2, cv2.DIST_MASK_PRECISE, labelType=cv2.DIST_LABEL_PIXEL)

        index = np.copy(labels)
        index[img > 0] = 0
        place =  np.argwhere(index > 0)

        nearCord = place[labels-1,:]
        x = nearCord[:, :, 0]
        y = nearCord[:, :, 1]
        nearPixel = np.zeros((2, height+2, width+2))
        nearPixel[0,:,:] = x
        nearPixel[1,:,:] = y
        grid = np.indices(img.shape)
        grid = grid.astype(float)
        diff = grid - nearPixel    

        direction_field[:, img > 0] = diff[:, img > 0]     

    weight_matrix = weight_matrix[1:-1, 1:-1]
    direction_field = direction_field[:, 1:-1, 1:-1]

    return direction_field, weight_matrix
def Distance_Transform_simple(lidar):
    # only use the nearest neighbor to fill in the depth map
    lidar = np.squeeze(lidar)
    height, width = np.shape(lidar)

    mask_lidar = lidar > 0.9

    value_mask = np.asarray(1.0 - np.squeeze(mask_lidar)).astype(np.uint8)
    dt, lbl = cv2.distanceTransformWithLabels(value_mask,
                                              cv2.DIST_L1,
                                              5,
                                              labelType=cv2.DIST_LABEL_PIXEL)

    with_value = mask_lidar

    depth_list = np.squeeze(lidar[with_value])
    label_list = np.reshape(lbl, [1, height * width])
    depth_list_all = depth_list[label_list - 1]
    depth_map = np.reshape(depth_list_all, (height, width))

    return depth_map
def fill_spherical(range_image):
    # fill in spherical image for calculating normal vector
    height, width = np.shape(range_image)[:2]
    value_mask = np.asarray(1.0 - np.squeeze(range_image) > 0.1).astype(
        np.uint8)
    dt, lbl = cv2.distanceTransformWithLabels(value_mask,
                                              cv2.DIST_L1,
                                              5,
                                              labelType=cv2.DIST_LABEL_PIXEL)

    with_value = np.squeeze(range_image) > 0.1

    depth_list = np.squeeze(range_image)[with_value]

    label_list = np.reshape(lbl, [1, height * width])
    depth_list_all = depth_list[label_list - 1]

    depth_map = np.reshape(depth_list_all, (height, width))

    depth_map = cv2.GaussianBlur(depth_map, (7, 7), 0)
    #depth_map=range_image*with_value+depth_map*(1-with_value)
    return depth_map
def watershedSegmentation(wb, dataCube, plantMaterial, lighting, destination,
                          datacubeName):
    """
    Segements the plant material with watershed segmentation algorithm, 
    saves the segmented data in CSV files.

    :param wb: (Spectronon workbench)
    :param dataCube: (numpy array) BIL Datacube
    :param plantMaterial: (numpy array) Locations of the plant material
    :param lighting: (string) Illumination description string
    :param destination: (string) Directory in which to save the data
    :param datacubeName: (string) Name of the current data cube

    :return: (None)
    """

    lines, samples, bands = dataCube.shape

    plantMaterialGray = plantMaterial
    plantMaterial = cv2.merge(
        (plantMaterialGray, plantMaterialGray, plantMaterialGray))

    # Remove noise from the image
    kernel = np.ones((3, 3), np.uint8)
    opening = cv2.morphologyEx(plantMaterialGray, cv2.MORPH_OPEN, kernel)

    # Locate the for-sure background area
    sure_bg = cv2.dilate(opening, kernel)

    # Finding the for-sure foreground area
    dist_transform, labels = cv2.distanceTransformWithLabels(
        opening, cv2.DIST_L2, 5)
    ret, sure_fg = cv2.threshold(dist_transform, 0.4 * dist_transform.max(),
                                 255, 0)
    sure_fg = cv2.morphologyEx(sure_fg, cv2.MORPH_CLOSE, kernel)

    # Finding unknown region
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)

    # Marker labelling
    ret, markers = cv2.connectedComponents(sure_fg)

    # Add one to all labels so that sure background is not 0, but 1
    markers = markers + 1

    # Now, mark the region of unknown with zero. We will have the segmented "leaves"
    # present in the image
    markers[unknown == 255] = 0
    markers = cv2.watershed(plantMaterial, markers)
    numMarkers = max(markers.flatten())

    plt.imshow(markers)
    plt.show()

    saveData = wb.postQuestion("Continue with saving the segmented data?",
                               title="Segmentation Okay?")
    if not saveData:
        return

    # Save the data for each leaf in a separate csv file (skip the background)
    for leaf in xrange(2, numMarkers):

        outputData = dataCube[markers == leaf, :]
        if len(outputData.flatten()) / 240 < 2:
            break

        filename = datacubeName + '_' + lighting + '_' + str(leaf).zfill(
            4) + ".csv"
        fileLocation = os.path.join(destination, filename)

        np.savetxt(fileLocation, outputData, delimiter=',')
예제 #20
0
def main(N):
    savefile = "result.pkl"
    # 手の認識用パラメータ(HチャンネルとSチャンネルとを二値化するための条件)
    hmin = 0
    hmax = 30  # 15-40程度にセット
    smin = 50

    janken_class = ['グー', 'チョキ', 'パー']
    class_count = [0, 0, 0]

    #savefile = sys.argv[1]
    try:
        clf = joblib.load(savefile)
    except IOError:
        print('学習済ファイル{0}を開けません'.format(savefile))
        sys.exit()

    with picamera.PiCamera() as camera:
        with picamera.array.PiRGBArray(camera) as stream:
            # カメラの解像度を320x240にセット
            camera.resolution = (320, 240)
            # カメラのフレームレートを15fpsにセット
            camera.framerate = 15
            # ホワイトバランスをfluorescent(蛍光灯)モードにセット
            camera.awb_mode = 'fluorescent'
            print('認識を開始します')
            for i in range(N * 2):
                sleep(0.5)
                # stream.arrayにBGRの順で映像データを格納
                camera.capture(stream, 'bgr', use_video_port=True)

                # 映像データをHSV形式に変換
                hsv = cv2.cvtColor(stream.array, cv2.COLOR_BGR2HSV)
                # HSV形式からHチャンネルとSチャンネルの画像を得る
                hsv_channels = cv2.split(hsv)
                h_channel = hsv_channels[0]
                s_channel = hsv_channels[1]

                # Hチャンネルを平滑化
                h_binary = cv2.GaussianBlur(h_channel, (5, 5), 0)

                # Hチャンネルの二値化画像を作成
                # hmin~hmaxの範囲を255(白)に、それ以外を0(黒)に
                ret, h_binary = cv2.threshold(h_binary, hmax, 255,
                                              cv2.THRESH_TOZERO_INV)
                ret, h_binary = cv2.threshold(h_binary, hmin, 255,
                                              cv2.THRESH_BINARY)
                # Sチャンネルの二値化画像を作成
                # smin~255の範囲を255(白)に、それ以外を0に(黒)に
                ret, s_binary = cv2.threshold(s_channel, smin, 255,
                                              cv2.THRESH_BINARY)

                # HチャンネルとSチャンネルの二値化画像のANDをとる
                # HチャンネルとSチャンネルの両方で255(白)の領域のみ白となる
                hs_and = h_binary & s_binary

                # 以下、最も広い白領域のみを残すための計算
                # まず、白領域の塊(クラスター)にラベルを振る
                img_dist, img_label = cv2.distanceTransformWithLabels(
                    255 - hs_and, cv2.cv.CV_DIST_L2, 5)
                img_label = np.uint8(img_label) & hs_and
                # ラベル0は黒領域なので除外
                img_label_not_zero = img_label[img_label != 0]
                # 最も多く現れたラベルが最も広い白領域のラベル
                if len(img_label_not_zero) != 0:
                    m = stats.mode(img_label_not_zero)[0]
                else:
                    m = 0
                # 最も広い白領域のみを残す
                hand = np.uint8(img_label == m) * 255

                # 最大の白領域からscikit-learnに入力するためのベクトルを取得
                hand_vector = getImageVector(hand)

                # 学習済のニューラルネットワークから分類結果を取得
                result = clf.predict(hand_vector)
                class_count[result[0]] += 1

                # 分類結果を表示
                print(janken_class[result[0]])

                # 得られた二値化画像を画面に表示
                #cv2.imshow('hand', hand)

                # 'q'を入力でアプリケーション終了
                #key = cv2.waitKey(1)
                #if key & 0xFF == ord('q'):
                #    break

                # streamをリセット
                stream.seek(0)
                stream.truncate()

            cv2.destroyAllWindows()
    #一番多かった手をリターン
    return class_count.index(max(class_count))
예제 #21
0
            ret, h_binary = cv2.threshold(h_binary, hmax, 255,
                                          cv2.THRESH_TOZERO_INV)
            ret, h_binary = cv2.threshold(h_binary, hmin, 255,
                                          cv2.THRESH_BINARY)
            # Sチャンネルの二値化画像を作成
            # smin~255の範囲を255(白)に、それ以外を0に(黒)に
            ret, s_binary = cv2.threshold(s_channel, smin, 255,
                                          cv2.THRESH_BINARY)

            # HチャンネルとSチャンネルの二値化画像のANDをとる
            # HチャンネルとSチャンネルの両方で255(白)の領域のみ白となる
            hs_and = h_binary & s_binary

            # 以下、最も広い白領域のみを残すための計算
            # まず、白領域の塊(クラスター)にラベルを振る
            img_dist, img_label = cv2.distanceTransformWithLabels(
                255 - hs_and, cv2.cv.CV_DIST_L2, 5)
            img_label = np.uint8(img_label) & hs_and
            # ラベル0は黒領域なので除外
            img_label_not_zero = img_label[img_label != 0]
            # 最も多く現れたラベルが最も広い白領域のラベル
            if len(img_label_not_zero) != 0:
                m = stats.mode(img_label_not_zero)[0]
            else:
                m = 0
            # 最も広い白領域のみを残す
            hand = np.uint8(img_label == m) * 255

            # 表示して動作チェックするため h_channel, s_channel, h_binary, s_binary を結合
            hs = np.concatenate((h_channel, h_binary), axis=0)
            hs_bin = np.concatenate((s_channel, s_binary), axis=0)
            hs_final = np.concatenate((hs_and, hand), axis=0)
예제 #22
0
def watershed_segmentation(device, img, mask, distance=10, filename=False, debug=None):
    """Uses the watershed algorithm to detect boundary of objects. Needs a marker file which specifies area which is
       object (white), background (grey), unknown area (black).

    Inputs:
    device              = device number. Used to count steps in the pipeline
    img                 = image to perform watershed on needs to be 3D (i.e. np.shape = x,y,z not np.shape = x,y)
    mask                = binary image, single channel, object in white and background black
    distance            = min_distance of local maximum
    filename            = if user wants to output analysis images change filenames from false
    debug               = None, print, or plot. Print = save to file, Plot = print to screen.

    Returns:
    device              = device number
    watershed_header    = shape data table headers
    watershed_data      = shape data table values
    analysis_images     = list of output images

    :param device: int
    :param img: numpy array
    :param mask: numpy array
    :param distance: int
    :param filename: str
    :param debug: str
    :return device: int
    :return watershed_header: list
    :return watershed_data: list
    :return analysis_images: list
    """

    if cv2.__version__[0] == '2':
        dist_transform = cv2.distanceTransform(mask, cv2.cv.CV_DIST_L2, maskSize=0)
    else:
        dist_transform = cv2.distanceTransformWithLabels(mask, cv2.DIST_L2, maskSize=0)[0]

    localMax = peak_local_max(dist_transform, indices=False, min_distance=distance, labels=mask)

    markers = ndi.label(localMax, structure=np.ones((3, 3)))[0]
    dist_transform1 = -dist_transform
    labels = watershed(dist_transform1, markers, mask=mask)

    img1 = np.copy(img)

    for x in np.unique(labels):
        rand_color = color_palette(len(np.unique(labels)))
        img1[labels == x] = rand_color[x]

    device, img2 = apply_mask(img1, mask, 'black', device, debug=None)

    joined = np.concatenate((img2, img), axis=1)

    estimated_object_count = len(np.unique(markers)) - 1

    analysis_images = []
    if filename != False:
        out_file = str(filename[0:-4]) + '_watershed.jpg'
        print_image(joined, out_file)
        analysis_images.append(['IMAGE', 'watershed', out_file])

    watershed_header = (
        'HEADER_WATERSHED',
        'estimated_object_count'
    )

    watershed_data = (
        'WATERSHED_DATA',
        estimated_object_count
    )

    if debug == 'print':
        print_image(dist_transform, str(device) + '_watershed_dist_img.png')
        print_image(joined, str(device) + '_watershed_img.png')
    elif debug == 'plot':
        plot_image(dist_transform, cmap='gray')
        plot_image(joined)

    return device, watershed_header, watershed_data, analysis_images
예제 #23
0
marker2.color.g = 0.0
marker2.color.b = 1.0

marker2.pose.orientation.w = 1.0
marker2.lifetime = rospy.Duration()
#########################################

print "precomputing distance field .."
t1 = time.time()

#outsource to cython
obstaclemap = util.get_obstaclemap(mapdata)

(distance_field,
 labels) = cv2.distanceTransformWithLabels(obstaclemap,
                                           cv2.DIST_L2,
                                           cv2.DIST_MASK_PRECISE,
                                           labelType=cv2.DIST_LABEL_PIXEL)
distance_field = np.array(distance_field, dtype=float)

t2 = time.time()

print "computed distance field in ", (t2 - t1)

pf = util.ParticleFilter(mapdata, distance_field, 15000)

with open(logfile) as f:

    i = 0
    for line in f:

        message = line.split(" ")[:-1]  #last element is timestamp
예제 #24
0
plt.title("Histogram_flat")
plt.plot(hist_flat)
plt.xlabel("# Bin")
plt.ylabel("% pixel")
plt.xlim([0, 512])

chans = cv2.split(image)
colors = ("b", "g", "r")
plt.figure(2)
plt.title("'Flattened' Color Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
features = []

# loop over the image channels
for (chan, color) in zip(chans, colors):
    # create a histogram for the current channel and
    # concatenate the resulting histograms for each
    # channel
    hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
    features.extend(hist)

    # plot the histogram
    plt.plot(hist, color=color)
    plt.xlim([0, 256])
print(np.array(features).flatten().shape[0])
plt.show()
cv2.waitKey()
cv2.destroyAllWindows()
cv2.distanceTransformWithLabels()
예제 #25
0
def white_black():
    from PIL import Image
    import cv2
    import numpy as np
    from scipy import stats

    hmin = 0
    hmax = 30  # 15-40程度にセット
    smin = 50

    while True:
        img_src = cv2.imread("img_tsumami000.jpg", 1)  #テストの場合はここに画像へのパス
        # 映像データをHSV形式に変換
        hsv_src = cv2.cvtColor(img_src, cv2.COLOR_BGR2HSV)
        # HSV形式からHチャンネルとSチャンネルの画像を得る
        hsv_channels = cv2.split(hsv_src)
        h_channel = hsv_channels[0]
        s_channel = hsv_channels[1]

        # Hチャンネルを平滑化
        h_binary = cv2.GaussianBlur(h_channel, (5, 5), 0)

        # Hチャンネルの二値化画像を作成
        # hmin~hmaxの範囲を255(白)に、それ以外を0(黒)に
        ret, h_binary = cv2.threshold(h_binary, hmax, 255,
                                      cv2.THRESH_TOZERO_INV)
        ret, h_binary = cv2.threshold(h_binary, hmin, 255, cv2.THRESH_BINARY)
        # Sチャンネルの二値化画像を作成
        # smin~255の範囲を255(白)に、それ以外を0に(黒)に
        ret, s_binary = cv2.threshold(s_channel, smin, 255, cv2.THRESH_BINARY)

        # HチャンネルとSチャンネルの二値化画像のANDをとる
        # HチャンネルとSチャンネルの両方で255(白)の領域のみ白となる
        hs_and = h_binary & s_binary
        path = "after2.jpg"
        cv2.imwrite(path, hs_and)
        # 以下、最も広い白領域のみを残すための計算
        # まず、白領域の塊(クラスター)にラベルを振る
        img_dist, img_label = cv2.distanceTransformWithLabels(
            255 - hs_and, cv2.DIST_L2, 5)
        img_label = np.uint8(img_label) & hs_and
        # ラベル0は黒領域なので除外
        img_label_not_zero = img_label[img_label != 0]
        # 最も多く現れたラベルが最も広い白領域のラベル
        if len(img_label_not_zero) != 0:
            m = stats.mode(img_label_not_zero)[0]
        else:
            m = 0
            # 最も広い白領域のみを残す
        hand = np.uint8(img_label == m) * 255

        key = cv2.waitKey(1) & 0xFF
        # 得られた二値化画像を画面に表示

        cv2.imshow("hand", hand)
        cv2.moveWindow('hand', 800, 200)
        cv2.imwrite("img_tsumami_test.jpg", hand)
        if key == ord('q'):
            break
    cv2.destroyAllWindows()

    return hand
예제 #26
0
def watershed_segmentation(rgb_img, mask, distance=10):
    """Uses the watershed algorithm to detect boundary of objects. Needs a marker file which specifies area which is
       object (white), background (grey), unknown area (black).

    Inputs:
    rgb_img             = image to perform watershed on needs to be 3D (i.e. np.shape = x,y,z not np.shape = x,y)
    mask                = binary image, single channel, object in white and background black
    distance            = min_distance of local maximum

    Returns:
    watershed_header    = shape data table headers
    watershed_data      = shape data table values
    analysis_images     = list of output images

    :param rgb_img: numpy.ndarray
    :param mask: numpy.ndarray
    :param distance: int
    :return watershed_header: list
    :return watershed_data: list
    :return analysis_images: list
    """
    params.device += 1
    # # Will be depricating opencv version 2
    # if cv2.__version__[0] == '2':
    #     dist_transform = cv2.distanceTransform(mask, cv2.cv.CV_DIST_L2, maskSize=0)
    # else:
    dist_transform = cv2.distanceTransformWithLabels(mask, cv2.DIST_L2, maskSize=0)[0]

    localMax = peak_local_max(dist_transform, indices=False, min_distance=distance, labels=mask)

    markers = ndi.label(localMax, structure=np.ones((3, 3)))[0]
    dist_transform1 = -dist_transform
    labels = watershed(dist_transform1, markers, mask=mask)

    img1 = np.copy(rgb_img)

    for x in np.unique(labels):
        rand_color = color_palette(len(np.unique(labels)))
        img1[labels == x] = rand_color[x]

    img2 = apply_mask(img1, mask, 'black')

    joined = np.concatenate((img2, rgb_img), axis=1)

    estimated_object_count = len(np.unique(markers)) - 1

    analysis_image = []
    analysis_image.append(joined)

    watershed_header = (
        'HEADER_WATERSHED',
        'estimated_object_count'
    )

    watershed_data = (
        'WATERSHED_DATA',
        estimated_object_count
    )

    if params.debug == 'print':
        print_image(dist_transform, os.path.join(params.debug_outdir, str(params.device) + '_watershed_dist_img.png'))
        print_image(joined, os.path.join(params.debug_outdir, str(params.device) + '_watershed_img.png'))
    elif params.debug == 'plot':
        plot_image(dist_transform, cmap='gray')
        plot_image(joined)

    # Store into global measurements
    if not 'watershed' in outputs.measurements:
        outputs.measurements['watershed'] = {}
    outputs.measurements['watershed']['estimated_object_count'] = estimated_object_count

    # Store images
    outputs.images.append(analysis_image)

    return watershed_header, watershed_data, analysis_image
예제 #27
0
    def model_fn(model,
                 data,
                 criterion,
                 perfermance=False,
                 vis=False,
                 device="cuda",
                 epoch=0,
                 num_class=4,
                 w=None):
        # imgs, gts, _ = data
        imgs, gts = data[:2]
        imgs = imgs.to(device)
        gts = torch.squeeze(gts, 1).to(device)
        gts = gts.to(device)
        loss = 0
        tb_dict = {}
        disp_dict = {}

        if not perfermance:
            net_out = model(imgs)  # [d1]
            mask = net_out[0]
            # ==============================================================================================================
            # calculate distance map
            imgs_ = imgs.cpu()
            batchsize = imgs_.shape[0]
            shape = list(imgs_.numpy().shape)
            gts_onehot = make_one_hot(gts.unsqueeze(1).long(), num_classes=4)
            shape[1] = 4
            dist_onebatch = np.zeros(shape[1:])
            dis_all = np.zeros(shape)
            for i in range(batchsize):
                for c in range(num_class):
                    dst, labels = cv2.distanceTransformWithLabels(
                        gts_onehot[i, c, ...].numpy().astype(np.uint8),
                        cv2.DIST_L2,
                        cv2.DIST_MASK_PRECISE,
                        labelType=cv2.DIST_LABEL_PIXEL)
                    # dst = list(dst)

                    dist_onebatch[c] = dst / np.max(dst) if np.max(
                        dst) else dst
                dis_all[i] = dist_onebatch
            dis_all_train = torch.from_numpy(dis_all).float().to(device)
            # print(dis_all_train.shape)
            # ==============================================================================================================

            # ==============================================================================================================
            # calculate mask gradient
            mask_ = torch.softmax(mask, dim=1)  # softmax
            mask_ = mask_.cpu()
            batchsize = mask_.detach().shape[0]
            shape = list(mask_.detach().numpy().shape)
            grad_onebatch = np.zeros(shape[1:])
            grad_all = np.zeros(shape)

            for i in range(batchsize):
                for c in range(num_class):
                    cv2.imwrite('mask_i_c.png', mask_[i, c,
                                                      ...].detach().numpy())
                    mask_i_c = cv2.imread('mask_i_c.png', 0)
                    os.remove('mask_i_c.png')
                    laplacian = cv2.Laplacian(mask_i_c, cv2.CV_64F)

                    # dst = list(dst)
                    grad_onebatch[c] = laplacian
                grad_all[i] = grad_onebatch
            grad_all_train = torch.from_numpy(grad_all).float().to(device)
            # print(dis_all_train.shape)
            # ==============================================================================================================

            ce_loss = torch.nn.CrossEntropyLoss()(mask, gts.long())

            smooth_loss = smooth_dist_loss(mask_true=make_one_hot(
                gts.unsqueeze(1).long(), num_classes=4).to(device),
                                           mask_pred=mask,
                                           grad_pred=grad_all_train,
                                           dist_true=dis_all_train)

            loss = ce_loss + smooth_loss

            tb_dict.update({"CE_loss": ce_loss.item()})
            tb_dict.update({"smooth_loss": smooth_loss.item()})
            tb_dict.update({"total_loss": loss.item()})

            disp_dict.update({"CE_loss": ce_loss.item()})
            disp_dict.update({"smooth_loss": smooth_loss.item()})
            disp_dict.update({"total_loss": loss.item()})

        if perfermance:
            gts_ = gts.unsqueeze(1)
            model.eval()
            net_out_eval = model(imgs)[0]
            net_out = F.softmax(net_out_eval, dim=1)
            _, preds = torch.max(net_out, 1)
            preds = preds.unsqueeze(1)
            cal_perfer(make_one_hot(preds, num_class),
                       make_one_hot(gts_.long(), num_class), tb_dict)
            model.train()

        return ModelReturn(loss, tb_dict, disp_dict)
예제 #28
0
import cv2
import numpy as np

# 이미지를 읽어서 바이너리 스케일로 변환
img = cv2.imread('../img/2full_body.jpg')
imggray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, biimg = cv2.threshold(imggray, 127, 255, cv2.THRESH_BINARY_INV)

# 거리 변환 ---①
dst, labels = cv2.distanceTransformWithLabels(biimg, cv2.DIST_L2, 5)
print(np.unique(labels))
# 거리 값을 0 ~ 255 범위로 정규화 ---②
dst = (dst / (dst.max() - dst.min()) * 255).astype(np.uint8)
# 거리 값에 쓰레시홀드로 완전한 뼈대 찾기 ---③
skeleton = cv2.adaptiveThreshold(dst, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                 cv2.THRESH_BINARY, 7, -3)
# 결과 출력
skeleton = cv2.cvtColor(skeleton, cv2.COLOR_GRAY2BGR)
for i in np.unique(labels):
    skeleton[labels == i] = [int(j) for j in np.random.randint(0, 255, 3)]

cv2.imshow('origin', img)
cv2.imshow('dist', dst)
cv2.imshow('skel', skeleton)
cv2.waitKey(0)
cv2.destroyAllWindows()
def imageProcessing():
    with graph.as_default():
        with picamera.PiCamera() as camera:
            with picamera.array.PiRGBArray(camera) as stream:
                # カメラの解像度を320x240にセット
                camera.resolution = (320, 240)
                # カメラのフレームレートを15fpsにセット
                camera.framerate = 15
                # ホワイトバランスをfluorescent(蛍光灯)モードにセット
                camera.awb_mode = 'fluorescent'

                while True:
                    # stream.arrayにBGRの順で映像データを格納
                    camera.capture(stream, 'bgr', use_video_port=True)

                    # 映像データをHSV形式に変換
                    hsv = cv2.cvtColor(stream.array, cv2.COLOR_BGR2HSV)
                    # HSV形式からHチャンネルとSチャンネルの画像を得る
                    hsv_channels = cv2.split(hsv)
                    h_channel = hsv_channels[0]
                    s_channel = hsv_channels[1]

                    # Hチャンネルを平滑化
                    h_binary = cv2.GaussianBlur(h_channel, (5, 5), 0)

                    # Hチャンネルの二値化画像を作成
                    # hmin~hmaxの範囲を255(白)に、それ以外を0(黒)に
                    ret, h_binary = cv2.threshold(h_binary, hmax, 255,
                                                  cv2.THRESH_TOZERO_INV)
                    ret, h_binary = cv2.threshold(h_binary, hmin, 255,
                                                  cv2.THRESH_BINARY)
                    # smin~255の範囲を255(白)に、それ以外を0に(黒)に
                    ret, s_binary = cv2.threshold(s_channel, smin, 255,
                                                  cv2.THRESH_BINARY)

                    # HチャンネルとSチャンネルの二値化画像のANDをとる
                    # HチャンネルとSチャンネルの両方で255(白)の領域のみ白となる
                    hs_and = h_binary & s_binary

                    # 以下、最も広い白領域のみを残すための計算
                    # まず、白領域の塊(クラスター)にラベルを振る
                    if CVversion == 2:
                        img_dist, img_label = cv2.distanceTransformWithLabels(
                            255 - hs_and, cv2.cv.CV_DIST_L2, 5)
                    else:
                        img_dist, img_label = cv2.distanceTransformWithLabels(
                            255 - hs_and, cv2.DIST_L2, 5)
                    img_label = np.uint8(img_label) & hs_and
                    # ラベル0は黒領域なので除外
                    img_label_not_zero = img_label[img_label != 0]
                    # 最も多く現れたラベルが最も広い白領域のラベル
                    if len(img_label_not_zero) != 0:
                        m = stats.mode(img_label_not_zero)[0]
                    else:
                        m = 0
                    # 最大の白領域のみを残す
                    hand = np.uint8(img_label == m) * 255

                    # 最大の白領域からscikit-learnに入力するためのベクトルを取得
                    hand_vector = getImageVector(hand)

                    # 学習済のニューラルネットワークから結果を取得
                    X = np.array(hand_vector)
                    if K.image_data_format() == 'channels_first':
                        X = X.reshape(X.shape[0], 1, img_rows, img_cols)
                        input_shape = (1, img_rows, img_cols)
                    else:
                        X = X.reshape(X.shape[0], img_rows, img_cols, 1)
                        input_shape = (img_rows, img_cols, 1)
                    result = model.predict_classes(X, verbose=0)

                    # 分類結果をrecognizedHandに格納
                    global recognizedHand
                    recognizedHand = result[0]

                    # 手と判定されている領域を表示
                    cv2.imshow('hand', hand)

                    # waitを入れる
                    key = cv2.waitKey(1)

                    if appliStop == True:
                        break

                    # streamをリセット
                    stream.seek(0)
                    stream.truncate()

                cv2.destroyAllWindows()
                app.jankenStop()
                app.quit()
def watershedSegmentation(wb, dataCube, plantMaterial, lighting, destination, datacubeName):
    """
    Segements the plant material with watershed segmentation algorithm, 
    saves the segmented data in CSV files.

    :param wb: (Spectronon workbench)
    :param dataCube: (numpy array) BIL Datacube
    :param plantMaterial: (numpy array) Locations of the plant material
    :param lighting: (string) Illumination description string
    :param destination: (string) Directory in which to save the data
    :param datacubeName: (string) Name of the current data cube

    :return: (None)
    """
    
    lines, samples, bands = dataCube.shape

    plantMaterialGray = plantMaterial
    plantMaterial = cv2.merge((plantMaterialGray, plantMaterialGray, plantMaterialGray))

    # Remove noise from the image
    kernel = np.ones((3,3), np.uint8)
    opening = cv2.morphologyEx(plantMaterialGray, cv2.MORPH_OPEN, kernel)

    # Locate the for-sure background area
    sure_bg = cv2.dilate(opening, kernel)
    
    # Finding the for-sure foreground area
    dist_transform, labels = cv2.distanceTransformWithLabels(opening, cv2.DIST_L2, 5)
    ret, sure_fg = cv2.threshold(dist_transform, 0.4*dist_transform.max(), 255, 0)
    sure_fg = cv2.morphologyEx(sure_fg, cv2.MORPH_CLOSE, kernel)
    
    # Finding unknown region
    sure_fg = np.uint8(sure_fg)
    unknown = cv2.subtract(sure_bg, sure_fg)

    # Marker labelling
    ret, markers = cv2.connectedComponents(sure_fg)
    
    # Add one to all labels so that sure background is not 0, but 1
    markers = markers+1
    
    # Now, mark the region of unknown with zero. We will have the segmented "leaves"
    # present in the image
    markers[unknown==255] = 0
    markers = cv2.watershed(plantMaterial, markers)
    numMarkers = max(markers.flatten())

    plt.imshow(markers)
    plt.show()

    saveData = wb.postQuestion("Continue with saving the segmented data?", 
                               title="Segmentation Okay?")
    if not saveData:
        return

    # Save the data for each leaf in a separate csv file (skip the background)
    for leaf in xrange(2, numMarkers):

        outputData = dataCube[markers==leaf, :]
        if len(outputData.flatten()) / 240 < 2:
            break

        filename = datacubeName + '_' + lighting + '_' + str(leaf).zfill(4) + ".csv"
        fileLocation = os.path.join(destination, filename)

        np.savetxt(fileLocation, outputData, delimiter=',')
예제 #31
0
    def model_fn(model,
                 data,
                 criterion,
                 perfermance=False,
                 vis=False,
                 device="cuda",
                 epoch=0,
                 num_class=4,
                 w=None):
        # imgs, gts, _ = data
        imgs, gts = data[:2]
        imgs = imgs.to(device)
        gts = torch.squeeze(gts, 1).to(device)
        gts = gts.to(device)
        loss = 0
        tb_dict = {}
        disp_dict = {}

        if not perfermance:

            net_out = model(imgs)  # [d1]
            mask = net_out[0]
            dist = net_out[1]
            # ==============================================================================================================
            # calculate distance map
            imgs_ = imgs.cpu()
            batchsize = imgs_.shape[0]
            shape = list(imgs_.numpy().shape)
            gts_onehot = make_one_hot(gts.unsqueeze(1).long(), num_classes=4)
            shape[1] = 4
            dist_onebatch = np.zeros(shape[1:])
            dis_all = np.zeros(shape)
            for i in range(batchsize):
                for c in range(num_class):
                    dst, labels = cv2.distanceTransformWithLabels(
                        gts_onehot[i, c, ...].numpy().astype(np.uint8),
                        cv2.DIST_L2,
                        cv2.DIST_MASK_PRECISE,
                        labelType=cv2.DIST_LABEL_PIXEL)
                    # dst = list(dst)
                    dist_onebatch[c] = dst
                dis_all[i] = dist_onebatch
            dis_all_train = torch.from_numpy(dis_all).float().to(device)
            # print(dis_all_train.shape)
            # ==============================================================================================================

            ce_loss = torch.nn.CrossEntropyLoss()(mask, gts.long())
            dist_loss = torch.nn.MSELoss()(
                dist, dis_all_train) / dist.flatten().size()[0]
            # dc_loss = DiceLoss()(net_out,make_one_hot(gts.unsqueeze(1).long(),num_classes=4).to(device))

            loss = ce_loss + dist_loss

            tb_dict.update({"CE_loss": ce_loss.item()})
            tb_dict.update({"dist_loss": dist_loss.item()})
            tb_dict.update({"total_loss": loss.item()})

            disp_dict.update({"CE_loss": ce_loss.item()})
            disp_dict.update({"dist_loss": dist_loss.item()})
            disp_dict.update({"total_loss": loss.item()})

        if perfermance:
            gts_ = gts.unsqueeze(1)
            model.eval()
            net_out_eval = model(imgs)[0]
            net_out = F.softmax(net_out_eval, dim=1)
            _, preds = torch.max(net_out, 1)
            preds = preds.unsqueeze(1)
            cal_perfer(make_one_hot(preds, num_class),
                       make_one_hot(gts_.long(), num_class), tb_dict)
            model.train()

        return ModelReturn(loss, tb_dict, disp_dict)
예제 #32
0
    def loadtd(self, idx):
        # generate mask
        image = cv2.imread('{}/MSRA-TD500/train/text_image/{}'.format(self.data_dir, idx), 1)
        height = image.shape[0]
        width = image.shape[1]
        mask = np.zeros(image.shape, dtype=np.uint8)
        bbox = ReadGt_td('{}/MSRA-TD500/train/gt/'.format(self.data_dir), idx[:-4])
        for bboxi in range(len(bbox)):
            pts, hard = GetPts_td(bbox, bboxi)
            if hard == 1: continue
            cv2.fillPoly(mask,[pts],(255,255,255),1)

        # random crop parameters
        attempt = 0
        min_overlap = [0.1, 0.3, 0.5, 0.7]
        while attempt < 10:
            patch_h0, patch_w0, patch_h, patch_w = random_crop(mask)
            overlap = mask[patch_h0:patch_h0+patch_h, patch_w0:patch_w0+patch_w] > 0
            random_idx = np.random.randint(0, 4)
            if np.sum(overlap) >= np.sum(mask>0)*min_overlap[random_idx]:
                break
            attempt += 1
        if attempt == 10:
            patch_h = min(height, width)
            patch_w = patch_h
            patch_h0 = (height-patch_h) // 2
            patch_w0 = (width-patch_w) // 2

        # random crop & resize
        image = image[patch_h0:patch_h0+patch_h, patch_w0:patch_w0+patch_w, :]
        image = cv2.resize(image, (self.patch_size, self.patch_size), interpolation=cv2.INTER_LINEAR)

        # random rotate
        prob = np.random.uniform(0,1)
        if prob <= 0.2:
            rtimes = 1
        elif prob >= 0.8:
            rtimes = 3
        else:
            rtimes = 0
        for rcount in range(rtimes):
            image = np.rot90(image)
        # cv2.imwrite('train_input/{}'.format(idx),image)

        # normalization
        image = image.astype(np.float32)
        image -= self.mean
        image = image.transpose((2,0,1))

        # compute vec
        accumulation = np.zeros((3, self.patch_size, self.patch_size), dtype=np.float32)
        for bboxi in range(len(bbox)):
            pts, hard = GetPts_td(bbox, bboxi)
            seg = np.zeros(mask.shape, dtype=np.uint8)
            cv2.fillPoly(seg,[pts],(255,255,255),1)
            segPatch = seg[patch_h0:patch_h0+patch_h, patch_w0:patch_w0+patch_w, :]
            for rcount in range(rtimes):
                segPatch = np.rot90(segPatch)
            if segPatch.max()!=255: continue
            segPatch = cv2.resize(segPatch, (self.patch_size, self.patch_size), interpolation=cv2.INTER_LINEAR)
            cv2.rectangle(segPatch,(0,0),(self.patch_size-1,self.patch_size-1),(0,0,0),1)

            img = cv2.cvtColor(segPatch,cv2.COLOR_BGR2GRAY)
            img = (img > 128).astype(np.uint8)
            dst, labels = cv2.distanceTransformWithLabels(img, cv2.cv.CV_DIST_L2, cv2.cv.CV_DIST_MASK_PRECISE, labelType=cv2.DIST_LABEL_PIXEL)
            index = np.copy(labels)
            index[img > 0] = 0
            place = np.argwhere(index > 0)
            nearCord = place[labels-1,:]
            x = nearCord[:, :, 0]
            y = nearCord[:, :, 1]
            nearPixel = np.zeros((2, self.patch_size, self.patch_size))
            nearPixel[0,:,:] = x
            nearPixel[1,:,:] = y
            grid = np.indices(img.shape)
            grid = grid.astype(float)
            diff = grid - nearPixel
            dist = np.sqrt(np.sum(diff**2, axis = 0))

            direction = np.zeros((3, self.patch_size, self.patch_size), dtype=np.float32)
            direction[0,img > 0] = np.divide(diff[0,img > 0], dist[img > 0])
            direction[1,img > 0] = np.divide(diff[1,img > 0], dist[img > 0])
            if hard == 0:
                direction[2,img > 0] = bboxi+1

            accumulation[0,img > 0] = 0
            accumulation[1,img > 0] = 0
            accumulation[2,img > 0] = 0
            accumulation = accumulation + direction
        vec = np.stack((accumulation[0], accumulation[1]))

        # compute weight
        weight = np.zeros((self.patch_size, self.patch_size), dtype=np.float32)
        posRegion = accumulation[2]>0
        posCount = np.sum(posRegion)
        if posCount != 0:
            bboxRemain = 0
            for bboxi in range(len(bbox)):
                overlap_bboxi = accumulation[2]==(bboxi+1)
                overlapCount_bboxi = np.sum(overlap_bboxi)
                if overlapCount_bboxi == 0: continue
                bboxRemain = bboxRemain+1
            bboxAve = float(posCount)/bboxRemain
            for bboxi in range(len(bbox)):
                overlap_bboxi = accumulation[2]==(bboxi+1)
                overlapCount_bboxi = np.sum(overlap_bboxi)
                if overlapCount_bboxi == 0: continue
                pixAve = bboxAve/overlapCount_bboxi
                weight = weight*(~overlap_bboxi) + pixAve*overlap_bboxi
        weight = weight[np.newaxis, ...]

        return image, vec, weight
예제 #33
0
marker2.color.g = 0.0
marker2.color.b = 1.0

marker2.pose.orientation.w = 1.0
marker2.lifetime = rospy.Duration()
#########################################



print "precomputing distance field .."
t1 = time.time()

#outsource to cython
obstaclemap = util.get_obstaclemap(mapdata)

(distance_field, labels) = cv2.distanceTransformWithLabels(obstaclemap, cv2.DIST_L2, cv2.DIST_MASK_PRECISE, labelType=cv2.DIST_LABEL_PIXEL)
distance_field = np.array(distance_field, dtype=float)

t2 = time.time()

print "computed distance field in ", (t2-t1)


pf = util.ParticleFilter(mapdata, distance_field, 15000)


with open(logfile) as f:

	i = 0
	for line in f:
예제 #34
0
def run(controller):
    maps_initialized = False
    count = 308
    while (True):
        #sleepで更新速度を制御
        time.sleep(0.4)
        frame = controller.frame()
        image = frame.images[0]
        if image.is_valid:
            if not maps_initialized:
                left_coordinates, left_coefficients = convert_distortion_maps(
                    frame.images[0])
                right_coordinates, right_coefficients = convert_distortion_maps(
                    frame.images[1])
                maps_initialized = True

            undistorted_left = undistort(image, left_coordinates,
                                         left_coefficients, 400, 400)
            undistorted_right = undistort(image, right_coordinates,
                                          right_coefficients, 400, 400)

            #画像を2値化(白黒に処理)
            ret, hand = cv2.threshold(undistorted_right, 70, 255,
                                      cv2.THRESH_BINARY)

            my_hand = hand[80:320, 40:360]

            # 以下、最も広い白領域のみを残すための計算
            # まず、白領域の塊(クラスター)にラベルを振る
            img_dist, img_label = cv2.distanceTransformWithLabels(
                255 - my_hand, cv2.DIST_L2, 5)
            img_label = np.uint8(img_label) & my_hand
            # ラベル0は黒領域なので除外
            img_label_not_zero = img_label[img_label != 0]
            # 最も多く現れたラベルが最も広い白領域のラベル
            if len(img_label_not_zero) != 0:
                m = stats.mode(img_label_not_zero)[0]
            else:
                m = 0
            # 最も広い白領域のみを残す
            this_hand = np.uint8(img_label == m) * 255

            #膨張
            #kernel = np.ones((5,5),np.uint8)
            #this_hand = cv2.dilate(this_hand,kernel,iterations = 1)

            #hand_color = cv2.cvtColor(this_hand,cv2.COLOR_GRAY2BGR)

            # 輪郭を抽出
            contours, hierarchy = cv2.findContours(this_hand,
                                                   cv2.RETR_EXTERNAL,
                                                   cv2.CHAIN_APPROX_SIMPLE)
            cnt = contours[0]

            #重心を求める
            mu = cv2.moments(this_hand, False)
            mx, my = int(mu["m10"] / mu["m00"]), int(mu["m01"] / mu["m00"])

            #手首の位置を求める
            frame = controller.frame()
            righthand = frame.hands.rightmost
            arm = righthand.arm

            i_box = frame.interaction_box
            normalized_tip = i_box.normalize_point(arm.wrist_position)
            app_x = 160 * normalized_tip.x + 80
            app_y = 120 * (normalized_tip.z) + 60
            app = (int(app_x), int(app_y))

            #重心と手首の位置から回転させる
            angle = 90 + math.degrees(math.atan2(my - app[1], mx - app[0]))
            trans = cv2.getRotationMatrix2D((mx, my), angle, 1.0)
            this_hand = cv2.warpAffine(this_hand, trans, (360, 240))

            # 得られた二値化画像を画面に表示
            cv2.imshow('hand', this_hand)
            #cv2.imshow('hand', undistorted_right)
            cv2.imwrite("HandImages2/img_two{0:03d}.png".format(count),
                        this_hand)
            count += 1

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break