Beispiel #1
0
def mix_brock():
    img_src1 = cv.imread("./img/1.png", 1)
    img_src2 = cv.imread("./img/2.png", 1)
    img_src3 = cv.imread("./img/3.png", 1)

    try:
        pal = cv.getTrackbarPos('hoge', winName)
    except:
        pal = 150

    img_ave = img_src1 * (1 / 3) + img_src2 * (1 / 3) + img_src3 * (
        1 / 3) + pal - 150
    cv.imwrite("./img/mix.png", img_ave)
    img = Image.open('./img/mix.png')

    to_pil(cca.grey_world(from_pil(to_pil(cca.stretch(
        from_pil(img)))))).save('./img/block.png')
    try:
        pal2 = cv.getTrackbarPos('hige', winName)
    except:
        pal2 = 0

    if pal2 == 0:
        to_pil(cca.grey_world(from_pil(to_pil(cca.stretch(
            from_pil(img)))))).save('./img/block.png')
    elif pal2 == 1:
        to_pil(cca.retinex_with_adjust(cca.retinex(
            from_pil(img)))).save('./img/block.png')
    elif pal2 == 2:
        to_pil(cca.stretch(from_pil(img))).save('./img/block.png')
Beispiel #2
0
    def grey_world(self, img):
        # convert to pil format
        img_pil = self.opencv_to_pil(img)
        img_gw_pil = to_pil(cca.grey_world(from_pil(img_pil)))
        img_gw_opencv = cv2.cvtColor(np.array(img_gw_pil), cv2.COLOR_RGB2BGR)

        return img_gw_opencv
Beispiel #3
0
 def test_all(self):
     to_pil(stretch(from_pil(self.img)))
     to_pil(grey_world(from_pil(self.img)))
     to_pil(retinex(from_pil(self.img)))
     to_pil(max_white(from_pil(self.img)))
     to_pil(retinex_with_adjust(retinex(from_pil(self.img))))
     to_pil(
         standard_deviation_weighted_grey_world(from_pil(self.img), 20, 20))
     to_pil(
         standard_deviation_and_luminance_weighted_gray_world(
             from_pil(self.img), 20, 20))
     to_pil(luminance_weighted_gray_world(from_pil(self.img), 20, 20))
     to_pil(automatic_color_equalization(from_pil(self.img)))
Beispiel #4
0
 def add_white_microscope(self, image):
     image = cca.grey_world(from_pil(image))  # array np.uint8
     circle = cv2.circle((np.ones(image.shape) * 255).astype(np.uint8),
                         (image.shape[0] // 2, image.shape[1] // 2),
                         random.randint(image.shape[0] // 2 - 10,
                                        image.shape[0] // 2 + 10),
                         (0, 0, 0), -1)
     mask = circle - 255
     image[mask == 0] = 255
     image = skimage.filters.gaussian(image, sigma=random.random())
     image = cv2.normalize(image, None, 0, 255, cv2.NORM_MINMAX)
     image = PIL.Image.fromarray(image.astype(np.uint8))
     return image
def crop(filename, input_dir, output_dir, size=256, colorcorrect=False):
    filename = Path(filename)
    name = filename.name.replace(filename.suffix, '')

    path_in = Path(input_dir) / filename
    path_out_rgb_dir = Path(output_dir) / 'RGB'
    path_out_nir_dir = Path(output_dir) / 'NIR'

    ds = gdal.Open(str(path_in))
    data_matrix = ds.ReadAsArray()

    rgb = np.transpose(data_matrix[:3, :, :], (1, 2, 0))
    nir = data_matrix[3, :, :]

    height, width = nir.shape
    hnum = height // size
    wnum = width // size

    num = 0
    rejected = 0
    for h in range(hnum):
        for w in range(wnum):
            crop_rgb = rgb[size * h:size * h + size, size * w:size * w + size]
            crop_nir = nir[size * h:size * h + size, size * w:size * w + size]

            if not check_nodata(crop_rgb, crop_nir):
                rejected += 1
                continue

            if colorcorrect:
                crop_rgb = cca.stretch(cca.grey_world(crop_rgb))

            path_out_rgb = path_out_rgb_dir / '{}_{:06d}.png'.format(name, num)
            path_out_nir = path_out_nir_dir / '{}_{:06d}.png'.format(name, num)
            num += 1

            Image.fromarray(crop_rgb).save(path_out_rgb)
            Image.fromarray(crop_nir).save(path_out_nir)

    print('{} images rejected'.format(rejected))
    print('{} images generated'.format(num))
Beispiel #6
0
    def color_balance(self, imgLabel, n_algorithm, image, displayed_image):
        # Check if images exists
        if not self.__images_exists():
            return
        # Choose algorithm and save original image
        orig_image = copy.deepcopy(image)
        if n_algorithm == 0:
            image.paste(ccu.to_pil(cca.max_white(ccu.from_pil(image))))
        elif n_algorithm == 1:
            image.paste(ccu.to_pil(cca.grey_world(ccu.from_pil(image))))
        elif n_algorithm == 2:
            image.paste(
                ccu.to_pil(
                    cca.automatic_color_equalization(ccu.from_pil(image))))

        self.__paste_image__(image, displayed_image, imgLabel)
        answer = tk.messagebox.askyesno("Changes prompt", "Apply changes?")
        # If no, restore image
        if not answer:
            image.paste(orig_image.copy())
            self.__paste_image__(image, displayed_image, imgLabel)
Beispiel #7
0
import colorcorrect.algorithm as cca
import numpy as np
import sys


def from_pil(pimg):
    pimg = pimg.convert(mode='RGB')
    nimg = np.array(pimg)[:]
    # nimg.flags.writeable = True
    return nimg


def to_pil(nimg):
    return Image.fromarray(np.uint8(nimg))


if __name__ == "__main__":
    img = Image.open(sys.argv[1])
    # img.show()
    to_pil(cca.stretch(from_pil(img)))
    to_pil(cca.grey_world(from_pil(img)))
    to_pil(cca.retinex(from_pil(img)))
    to_pil(cca.max_white(from_pil(img)))
    to_pil(cca.retinex_with_adjust(cca.retinex(from_pil(img))))
    to_pil(cca.standard_deviation_weighted_grey_world(from_pil(img), 20, 20))
    to_pil(
        cca.standard_deviation_and_luminance_weighted_gray_world(
            from_pil(img), 20, 20))
    to_pil(cca.luminance_weighted_gray_world(from_pil(img), 20, 20))
    to_pil(cca.automatic_color_equalization(from_pil(img)))
def collect_false(
        bbs_dt_dict,
        bbs_gt_dict,
        data_path,
        write_path,
        num_neg_to_pick=0,
        prob_threshold=0.,
        overlap_threshold=0.5,
        data_set_name='train',
        ind_round=0,
        flag_rescale=True,
        target_width=32,
        target_height=32,
        flag_rgb=True,
        flag_trans_aug=False,
        dist_trans_list=(-2, 0, 2),
):
    '''
    Take detected bounding boxes and ground truth bounding boxes and output
    global statistics including:
        false positive per image
        missing rate
        precision at moth level
        recall at moth level

    '''

    neg_examples = []
    pos_examples = []

    # change prob_threshold, if num_neg_to_pick is specified
    if num_neg_to_pick > 0:
        prob_threshold = get_neg_to_pick_thresh(bbs_dt_dict, num_neg_to_pick)
        print "selected {} examples".format(num_neg_to_pick)
        print "thresh set to {}".format(prob_threshold)

    for img_name in bbs_dt_dict:

        bbs_dt = bbs_dt_dict[img_name]
        bbs_gt = bbs_gt_dict[img_name]

        if prob_threshold > 0.:
            bbs_dt = np.array(
                [item for item in bbs_dt if item[4] > prob_threshold])

        matches, unmatched_dt, unmatched_gt = match_bbs(
            bbs_dt, bbs_gt, overlap_threshold)

        try:
            im = scipy.misc.imread(os.path.join(data_path, img_name))
        except IOError:
            logger.warn("There was a problem reading the jpg: %s." % img_name)
            continue

        im = grey_world(im)

        if flag_rgb:
            im_take = im
        else:
            im_y, im_i, im_q = colorsys.rgb_to_yiq(
                *np.rollaxis(im[..., :3], axis=-1))
            # the rollaxis command rolls the last (-1) axis back
            # until the start
            # do a colourspace conversion

            im_take = im_y

        def augment_bbs_by_trans_wrap(bbs_orig):
            bbs_totake = np.cast['int'](bbs_orig)[:, :4]
            if flag_trans_aug:
                bbs_totake = x1y1x2y2_to_x1y1wh_batch(bbs_totake)
                bbs_totake = augment_bbs_by_trans(bbs_totake, dist_trans_list)
                bbs_totake = x1y1wh_to_x1y1x2y2_batch(bbs_totake)
            return bbs_totake

        def coord_valid(x1,
                        y1,
                        x2,
                        y2,
                        min_x=0,
                        min_y=0,
                        max_x=im_take.shape[1],
                        max_y=im_take.shape[0]):
            return x1 < min_x or y1 < min_y or x2 > max_x or y2 > max_y

        # gather negative examples from unmatched bb_dt
        if len(unmatched_dt) > 0:
            for x1, y1, x2, y2 in \
                    augment_bbs_by_trans_wrap(bbs_dt[unmatched_dt]):
                if coord_valid(x1, y1, x2, y2):
                    continue

                if y2 - y1 != target_height or x2 - x1 != target_width:
                    neg_examples.append(
                        scipy.misc.imresize(im_take[y1:y2, x1:x2],
                                            (target_height, target_width)))
                else:
                    neg_examples.append(im_take[y1:y2, x1:x2])

        # gather positive examples from unmatched bb_gt
        if len(unmatched_gt) > 0:
            for x1, y1, x2, y2 in \
                    augment_bbs_by_trans_wrap(bbs_gt[unmatched_gt]):
                if coord_valid(x1, y1, x2, y2):
                    continue

                # !!! here the target sizes should take parameters instead
                # of fixed 32 size.
                # determine if the xy, width, height are postive and within
                # range
                values_with_in_range = x2 - x1 > 0 and y2 - y1 > 0 \
                    and y1 >= 0 and y2 < im_take.shape[0] \
                    and x1 >= 0 and x2 < im_take.shape[1]

                if not values_with_in_range:
                    print "Bad boundingbox, ignored"
                    print x1, y1, x2 - x1, y2 - y1
                    continue

                if flag_rescale:
                    im_p = crop_and_rescale(im_take, (x1, y1),
                                            x2 - x1,
                                            y2 - y1,
                                            target_width=target_width,
                                            target_height=target_height)
                else:
                    im_p = crop_centered_box(im_take, (x1, y1),
                                             x2 - x1,
                                             y2 - y1,
                                             target_width=target_width,
                                             target_height=target_height)
                pos_examples.append(im_p)

    return pos_examples, neg_examples
# -*- coding: utf-8 -*-
import sys
from PIL import Image
from colorcorrect.algorithm import stretch, grey_world, retinex, retinex_with_adjust, max_white
from colorcorrect.algorithm import standard_deviation_weighted_grey_world
from colorcorrect.algorithm import standard_deviation_and_luminance_weighted_gray_world
from colorcorrect.algorithm import automatic_color_equalization
from colorcorrect.algorithm import luminance_weighted_gray_world
from colorcorrect.util import from_pil, to_pil

if __name__ == "__main__":
    img = Image.open(sys.argv[1])
    # img.show()
    to_pil(stretch(from_pil(img)))
    to_pil(grey_world(from_pil(img)))
    to_pil(retinex(from_pil(img)))
    to_pil(max_white(from_pil(img)))
    to_pil(retinex_with_adjust(retinex(from_pil(img))))
    to_pil(standard_deviation_weighted_grey_world(from_pil(img), 20, 20))
    to_pil(
        standard_deviation_and_luminance_weighted_gray_world(
            from_pil(img), 20, 20))
    to_pil(luminance_weighted_gray_world(from_pil(img), 20, 20))
    to_pil(automatic_color_equalization(from_pil(img)))
Beispiel #10
0
    temp_list[-1] = np.concatenate(temp_list[-1], axis=1)
    return np.concatenate(temp_list, axis=0)


raw_dir = os.path.expanduser('~/Work/automoth/gen_fig/raw')
proc_dir = os.path.expanduser('~/Work/automoth/gen_fig/proc')
fig_dir = os.path.expanduser('~/Dropbox/automoth_paper/figs')

name_list = [name for name in os.listdir(raw_dir)]
path_list = [os.path.join(raw_dir, name) for name in name_list]
img_list = map(imread, path_list)

img_proc_list = []
# process images
for img, name in zip(img_list, name_list):
    img_proc_list.append(grey_world(img))
    imsave(os.path.join(proc_dir, name), img_proc_list[-1])

# concatenate and down sample for output
multi_size = (4, 4)
orig_size = img_list[0].shape[:2]

img_raw_cat = np.zeros(
    tuple(np.array(multi_size) * np.array(orig_size)) + (3, ))

subsample = 2  # 2 or 4

imsave(os.path.join(fig_dir, 'colorcorrect_raw.png'),
       concatenate_images(img_list)[::subsample, ::subsample, :])
imsave(os.path.join(fig_dir, 'colorcorrect_proc.png'),
       concatenate_images(img_proc_list)[::subsample, ::subsample, :])
Beispiel #11
0
        ))

for i, file in enumerate(files_glob):
    img = Image.open(file)

    if args.negative is True:
        print("Invert colors.")
        img = ImageOps.invert(img)

    if args.colorautoadjust is True:
        print("Using automatic color equalization.")
        img = to_pil(cca.automatic_color_equalization(from_pil(img)))

    if args.colorstretch is True:
        print("Using gray world color equalization.")
        img = to_pil(cca.stretch(cca.grey_world(from_pil(img))))

    if img.mode == "RGBA":
        img.load()
        background = Image.new("RGB", img.size, (255, 255, 255))
        background.paste(img, mask=img.split()[3])
        img = background

    if args.monochrome is True:
        if img.mode == "L" or img.mode == "LA":
            pass

        if img.mode != "RGB":
            img = img.convert("RGB")
        rgb = np.array(img, dtype="float32")
def get_neg(
        data_path,
        target_height,
        target_width,
        flag_rescale=True,
        flag_rgb=True,
        num_appr=2500,
        flag_trans_aug=False,
        dist_trans_list=(-2, 0, 2),
):
    '''
    generate negative training examples, which does not contain moths
    '''

    jpg_train = [f for f in os.listdir(data_path) if f.find('.jpg') > 0]

    blobs = []
    blob_resized_list = []

    for i, j in enumerate(jpg_train):
        try:
            im = scipy.misc.imread(os.path.join(data_path, j))
        except IOError:
            logger.warn("There was a problem reading the jpg: %s." % j)
            continue

        im = grey_world(im)

        # negative patches are extracted on color input image
        contours, c_area = get_contours(im)
        c_idx = np.argsort(c_area)[::-1]  # largest first
        contours = contours[c_idx]
        c_area = c_area[c_idx]

        # fig1, subs1 = plt.subplots(nrows=1, ncols=1)
        # cv2.drawContours(im, contours[:2], -1, (0, 255, 0), -1)

        # for c in xrange(2):
        # boundingRect returns top left corner xy
        # width, and height
        #     bx,by,bw,bh = cv2.boundingRect(contours[c])
        # cv2.rectangle(im,(bx,by),(bx+bw,by+bh),(255,0,0),3) # draw rectangle in blue color)
        # subs1.imshow(im, **imargs)
        # subs1.set_title(j)

        # plt.tight_layout()

        if not flag_rgb:
            # im will be assigned to the new gray image

            # the rollaxis command rolls the last (-1) axis back until the start
            # do a colourspace conversion
            im, im_i, im_q = colorsys.rgb_to_yiq(
                *np.rollaxis(im[..., :3], axis=-1))

        # get certain amount of bbs from this image based on the approximate
        # wanted number
        num_per_image = num_appr / len(jpg_train)
        if flag_trans_aug:
            num_per_image = num_per_image / (len(dist_trans_list)**2)

        bbs = []
        for c in contours[:num_per_image]:
            # boundingRect returns top left corner xy width, and height
            bx, by, bw, bh = cv2.boundingRect(c)
            xy = (bx, by)
            bbs.append([xy, bx, by])

        if flag_trans_aug:
            bbs = augment_bbs_by_trans(bbs, dist_trans_list)

        for (bx, by), bw, bh in bbs:
            # remember y is indexed first in image
            blob = im[by:(by + bh), bx:(bx + bw)]
            blobs.append(blob)
            # print moth.shape

            if flag_rescale:
                blob_resized = crop_and_rescale(im, xy, bw, bh, target_width,
                                                target_height)
            else:
                blob_resized = crop_centered_box(im, xy, bw, bh, target_width,
                                                 target_height)

            blob_resized_list.append(blob_resized)
    return blob_resized_list
def get_pos(
        data_path,
        target_height,
        target_width,
        flag_rescale=False,
        flag_multiscale=False,
        flag_rgb=True,
        detect_width_list=[8, 16, 32, 64],
        detect_height_list=[8, 16, 32, 64],
        flag_trans_aug=False,
        dist_trans_list=(-2, 0, 2),
):
    """ Get positive training examples
    examples are rescaled to target_height and target_width

    With the assumption that the annotation file have the same name with 
    the image but with no extension

    flag_trans_aug: if do translation augmentation
    """

    jpg_train = [f for f in os.listdir(data_path) if f.find('.jpg') > 0]

    # moths = []
    moth_resized_list = []

    for i, j in enumerate(jpg_train):
        try:
            im = scipy.misc.imread(os.path.join(data_path, j))
        except IOError:
            logger.warn("There was a problem reading the jpg: %s." % j)
            continue

        im = grey_world(im)

        if not flag_rgb:
            # im will be assigned to the new gray image

            # the rollaxis command rolls the last (-1) axis back until the start
            # do a colourspace conversion
            im, im_i, im_q = colorsys.rgb_to_yiq(
                *np.rollaxis(im[..., :3], axis=-1))

        ann_file = j.split('.')[0]
        ann_path = os.path.join(data_path, ann_file)
        annotation = get_annotation(ann_path)

        # get all bbs for this image
        bbs = get_bbs(annotation)

        if flag_trans_aug:
            bbs = augment_bbs_by_trans(bbs, dist_trans_list)

        for xy, width, height in bbs:

            x, y = xy

            # determine if the xy, width, height are postive and within range
            values_with_in_range = width > 0 and height > 0 \
                and y >= 0 and y + height < im.shape[0] \
                and x >= 0 and x + width < im.shape[1]

            if not values_with_in_range:
                print "Bad boundingbox, ignored"
                print xy, width, height
                continue

            # remember y is indexed first in image
            # moth = im[y:(y + height), x:(x + width)]
            # moths.append(moth)
            # print moth.shape

            if flag_multiscale:

                moth_resized = crop_and_rescale_nearest(
                    im,
                    xy,
                    width,
                    height,
                    target_width,
                    target_height,
                    detect_width_list=detect_width_list,
                    detect_height_list=detect_height_list)
            elif flag_rescale:

                moth_resized = crop_and_rescale(im, xy, width, height,
                                                target_width, target_height)
            else:
                moth_resized = crop_centered_box(im, xy, width, height,
                                                 target_width, target_height)
            moth_resized_list.append(moth_resized)

    return moth_resized_list
Beispiel #14
0
def get_sliding_patch_features(data_path,
                               write_path,
                               target_width,
                               target_height,
                               x_stride,
                               y_stride,
                               detect_width=None,
                               detect_height=None,
                               n_images=-1,
                               flag_rgb=True,
                               img_ext='.jpg',
                               rot_kind=0):

    # detect_width and detect_height are for the directly snipped patches from
    # images.
    # target_width and target_height are the sizes of classifier input

    # determines if need to rescale and report
    flag_rescaling_test = False
    # determine if needed to rescale patches
    if detect_height == target_height and detect_width == target_width:
        print "Snipping patch sizes are equal to classifier input size."
    elif detect_height is None or detect_width is None:
        print "Patch sizes are not assigned. Assign them as classifier input size."
        detect_height, detect_width = target_height, target_width
    else:
        flag_rescaling_test = True
        print "Need to rescale patches to feed them into classifier."

    if not flag_rescaling_test:
        print "No need to rescale at test time."

    img_train = [
        img_f for img_f in os.listdir(data_path) if img_f.find(img_ext) > 0
    ]

    # if n_images is specified, then only look at the first n_images
    if n_images > 1:
        img_train = img_train[:min(len(img_train), n_images)]

    patch_feature_dict = {}
    for ind_img, img_name in enumerate(img_train):
        try:
            im = imread(os.path.join(data_path, img_name))
        except IOError:
            logger.warn("There was a problem reading the image: %s." %
                        img_name)
            continue

        if im.ndim == 3:
            im = grey_world(im)

        # logger.debug("processing %s" % img_name)

        # print "processing %s" % img_name

        new_im_width = np.int(im.shape[1] * (1. * target_width / detect_width))
        new_im_height = np.int(im.shape[0] *
                               (1. * target_width / detect_width))

        if flag_rgb:
            assert im.shape[2] == 3

            im = imresize(im, (new_im_height, new_im_width))

            # below is a slow implementation can remove
            # windows_list = []
            # for ind_rgb in range(3):
            # shape (65, 88, 28, 28)
            #     windows = sliding_window(im[:, :, ind_rgb],
            #                              win_height=target_height,
            #                              win_width=target_width,
            #                              x_stride=x_stride,
            #                              y_stride=y_stride)
            #     windows_list.append(windows)
            # shape (3, 65, 88, 28, 28)
            # windows = np.asarray(windows_list)

            # shape (65, 88, 28, 28, 3)
            windows = sliding_window(im,
                                     win_height=target_height,
                                     win_width=target_width,
                                     x_stride=x_stride,
                                     y_stride=y_stride)

            # shape (65, 88, 3, 28, 28)
            windows = np.rollaxis(windows, 4, 2)

            windows = rot_img_array(windows, kind=rot_kind)

            # shape (65, 88, 2352)
            windows = windows.reshape(
                (windows.shape[0], windows.shape[1],
                 windows.shape[2] * windows.shape[3] * windows.shape[4]))

        else:
            # the rollaxis command rolls the last (-1) axis back until the start
            # do a colourspace conversion

            if im.ndim == 3:
                im_y, im_i, im_q = colorsys.rgb_to_yiq(
                    *np.rollaxis(im[..., :3], axis=-1))
            else:
                im_y = im

            im_y = imresize(im_y, (new_im_height, new_im_width))

            windows = sliding_window(im_y,
                                     win_height=target_height,
                                     win_width=target_width,
                                     x_stride=x_stride,
                                     y_stride=y_stride)
            # print windows.shape

            windows = rot_img_array(windows, kind=rot_kind)

            windows = windows.reshape((windows.shape[0], windows.shape[1],
                                       windows.shape[2] * windows.shape[3]))

        # shape (2352 or 784, 65, 88)
        windows = np.rollaxis(windows, 2)

        # n_d number of feature dimension,
        # n_r number sliding windows in row direction
        # n_c number sliding windows in column direction
        n_d, n_r, n_c = windows.shape

        # shape (2352 or 784, 65 * 88)
        windows = windows.reshape(
            (windows.shape[0], windows.shape[1] * windows.shape[2]))

        # shape (65 * 88, 2352 or 784)
        X = windows.T

        patch_feature_dict[img_name] = X

        # above is sliding window

    return patch_feature_dict, n_r, n_c