예제 #1
0
def run(image, mask=None, smoothing=False, show=False, show_now=True):
    if mask is None:
        mask = np.ones_like(image)
        im_orig = image.copy()
    else:
        image, mask = tools.crop_to_bbox(image, mask)
        im_orig = image.copy()
        mean_v = int(image[np.nonzero(mask)].mean())
        image = np.where(mask, image, mean_v)
    mask = mask.astype(np.uint8)

    if smoothing:
        image = tools.smoothing(image)

    # defaultne hleda svetle oblasti
    image = np.invert(image)

    rgb_image = cv2.cvtColor(image, cv2.COLOR_BAYER_GR2RGB).astype(np.float32)

    # print 'Calculate saliency map for test image:'
    orgb_image = SightSpotUtil.eval_orgb_image(rgb_image)

    # start = time.clock()
    saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, 1.0, 40.0, 'auto')
    # print 'Saliency map extracted in', time.clock() - start, 'sec.'

    # start = time.clock()
    # heatmap_image = SightSpotUtil.eval_heatmap(saliency_map)
    # heatmap_image = np.asarray(heatmap_image)
    # heatmap_image.setflags(write=True)
    # print 'Heatmap extracted in', time.clock() - start, 'sec.'

    saliency_map *= mask
    # heatmap_image *= np.dstack((mask, mask, mask))
    im_orig *= mask

    saliency_map = skiexp.rescale_intensity(saliency_map, out_range=(0, 1))

    if show:
        if smoothing:
            plt.subplot(131), plt.imshow(im_orig, 'gray', interpolation='nearest'), plt.title('input')
            plt.subplot(132), plt.imshow(image, 'gray', interpolation='nearest'), plt.title('smoothed')
            plt.subplot(133), plt.imshow(saliency_map, 'gray', interpolation='nearest'), plt.title('saliency')
        else:
            plt.subplot(121), plt.imshow(im_orig, 'gray', interpolation='nearest'), plt.title('input')
            plt.subplot(122), plt.imshow(saliency_map, 'gray', interpolation='nearest'), plt.title('saliency')
        if show_now:
            plt.show()

    return im_orig, image, saliency_map
예제 #2
0
 def _get_orgb_image(self):
     if self._orgb_image is None:
         self._orgb_image = SightSpotUtil.eval_orgb_image(self._rgb_image)
     return self._orgb_image
예제 #3
0
        cluster_size = numpy.sum(cluster_idx)
        if cluster_size == 0:
            palette_items.append((0, 0, 0))
        else:
            avg = numpy.sum(rgb_image[cluster_idx], axis=0) / cluster_size
            palette_items.append(tuple(avg))
    palette = numpy.array(palette_items, dtype='uint8')
    visualization = palette[segmentation_map + 1]
    return Image.fromarray(visualization)


if __name__ == '__main__':
    print 'Segmentation of test image:'
    image = Image.open('flower.jpg')
    rgb_image = numpy.asarray(image, dtype='float32')
    orgb_image = SightSpotUtil.eval_orgb_image(rgb_image)

    start = time.clock()
    segmentation_map = SightSpotUtil.eval_slic_map(orgb_image, 32.0, 0.25, 4)
    print 'Segmentation map extracted in', time.clock() - start, 'sec.'
    print 'Segment number:', numpy.max(segmentation_map) + 1

    _visualize_clusters(segmentation_map).show('Clusters in random colors')
    _visualize_contours(rgb_image, segmentation_map,
                        (255, 255, 0)).show('Cluster contours')
    _visualize_averaging(rgb_image, segmentation_map).show('Averaging')

    print 'Use segmentation to improve clustering...'
    saliency_map = SightSpotUtil.eval_saliency_map(orgb_image, 3.0, 60.0,
                                                   'auto')
    precise_saliency_map = SightSpotUtil.combine_saliency_and_segmentation(
예제 #4
0
def _rgb2orgb(r, g, b):
    rgb_image = numpy.array([[[255.0 * r, 255.0 * g, 255.0 * b]]], dtype='float32')
    orgb = SightSpotUtil.eval_orgb_image(rgb_image)[0, 0]
    return tuple(orgb)
예제 #5
0
    print 'Test some basic oRGB color conversions:'
    print "RGB (0, 0, 0) is oRGB (%f, %f, %f) for BLACK   " % _rgb2orgb(0, 0, 0) # Black.
    print "RGB (1, 0, 0) is oRGB (%f, %f, %f) for RED     " % _rgb2orgb(1, 0, 0) # Red.
    print "RGB (0, 1, 0) is oRGB (%f, %f, %f) for GREEN   " % _rgb2orgb(0, 1, 0) # Green.
    print "RGB (0, 0, 1) is oRGB (%f, %f, %f) for BLUE    " % _rgb2orgb(0, 0, 1) # Blue.
    print "RGB (1, 1, 0) is oRGB (%f, %f, %f) for YELLOW  " % _rgb2orgb(1, 1, 0) # Yellow.
    print "RGB (0, 1, 1) is oRGB (%f, %f, %f) for SKY-BLUE" % _rgb2orgb(0, 1, 1) # Sky-blue.
    print "RGB (1, 0, 1) is oRGB (%f, %f, %f) for MAGENTA " % _rgb2orgb(1, 0, 1) # Magenta.
    print "RGB (1, 1, 1) is oRGB (%f, %f, %f) for WHITE   " % _rgb2orgb(1, 1, 1) # White.

    print 'Show channels of test image:'
    image = Image.open('flower.jpg')
    rgb_image = numpy.asarray(image, dtype='float32')

    start = time.clock()
    orgb_image = SightSpotUtil.eval_orgb_image(rgb_image)
    print 'ORGB image extracted in', time.clock() - start, 'sec.'

    print 'Colorizing...'
    lu_channel = orgb_image[:,:,0]
    rg_channel = orgb_image[:,:,1]
    yb_channel = orgb_image[:,:,2]
    lu_image = Image.fromarray(lu_channel * 255 + 0.5)
    rg_image = _colorize_channel(rg_channel, (255, 0, 0), (0, 255, 0))
    yb_image = _colorize_channel(yb_channel, (255, 255, 0), (0, 0, 255))
    image.show('Source image')
    lu_image.show('Luminance channel')
    rg_image.show('Red-green channel')
    yb_image.show('Yellow-blue channel')

예제 #6
0
def _rgb2orgb(r, g, b):
    rgb_image = numpy.array([[[255.0 * r, 255.0 * g, 255.0 * b]]],
                            dtype='float32')
    orgb = SightSpotUtil.eval_orgb_image(rgb_image)[0, 0]
    return tuple(orgb)
예제 #7
0
 def _get_orgb_image(self):
     if self._orgb_image is None:
         self._orgb_image = SightSpotUtil.eval_orgb_image(self._rgb_image)
     return self._orgb_image