def remove_noise_with_hsv(self, img):
     # Use number of occurrences to find the standard h, s, v
     # Convert to int so we can sort the colors
     # noinspection PyTypeChecker
     img_int = np.dot(np.rint(img * 255), np.power(256, np.arange(3)))
     color_array = sort_by_occurrence(img_int.flatten())
     # standard color is the 2nd most frequent color
     std_color = color_array[1]
     std_b, mod = divmod(std_color, 256 ** 2)
     std_g, std_r = divmod(mod, 256)
     # noinspection PyTypeChecker
     std_h, std_s, std_v = colors.rgb_to_hsv(np.array([std_r, std_g, std_b]) / 255)
     # print(std_h * 360, std_s * 100, std_v * 100)
     height, width, _ = img.shape
     img_hsv = colors.rgb_to_hsv(img)
     h, s, v = img_hsv[:, :, 0], img_hsv[:, :, 1], img_hsv[:, :, 2]
     h_mask = np.abs(h - std_h) > self.h_tolerance
     s_mask = np.abs(s - std_s) > self.s_tolerance
     delta_v = np.abs(v - std_v)
     v_mask = delta_v > self.v_tolerance
     hsv_mask = np.logical_or(np.logical_or(h_mask, s_mask), v_mask)
     new_img = 1 - delta_v
     new_img[hsv_mask] = 0
     # Three types of grayscale colors in new_img:
     # Type A: 1. Outside noise, or inside point.
     # Type B: between 0 and 1. Outside noise, or contour point.
     # Type C: 0. Inside noise, or background.
     return new_img
Esempio n. 2
0
 def generate_color_range(self):
     # color and marker range:
     self.colorrange = []
     self.markerrange = []
     mr2 = []
     # first color range:
     cc0 = plt.cm.gist_rainbow(np.linspace(0.0, 1.0, 8.0))
     # shuffle it:
     for k in range((len(cc0) + 1) // 2):
         self.colorrange.extend(cc0[k::(len(cc0) + 1) // 2])
     self.markerrange.extend(len(cc0) * 'o')
     mr2.extend(len(cc0) * 'v')
     # second darker color range:
     cc1 = plt.cm.gist_rainbow(np.linspace(0.33 / 7.0, 1.0, 7.0))
     cc1 = mc.hsv_to_rgb(mc.rgb_to_hsv(np.array([cc1])) * np.array([1.0, 0.9, 0.7, 0.0]))[0]
     cc1[:, 3] = 1.0
     # shuffle it:
     for k in range((len(cc1) + 1) // 2):
         self.colorrange.extend(cc1[k::(len(cc1) + 1) // 2])
     self.markerrange.extend(len(cc1) * '^')
     mr2.extend(len(cc1) * '*')
     # third lighter color range:
     cc2 = plt.cm.gist_rainbow(np.linspace(0.67 / 6.0, 1.0, 6.0))
     cc2 = mc.hsv_to_rgb(mc.rgb_to_hsv(np.array([cc2])) * np.array([1.0, 0.5, 1.0, 0.0]))[0]
     cc2[:, 3] = 1.0
     # shuffle it:
     for k in range((len(cc2) + 1) // 2):
         self.colorrange.extend(cc2[k::(len(cc2) + 1) // 2])
     self.markerrange.extend(len(cc2) * 'D')
     mr2.extend(len(cc2) * 'x')
     self.markerrange.extend(mr2)
Esempio n. 3
0
def test_rgb_hsv_round_trip():
    for a_shape in [(500, 500, 3), (500, 3), (1, 3), (3,)]:
        np.random.seed(0)
        tt = np.random.random(a_shape)
        assert_array_almost_equal(tt,
            mcolors.hsv_to_rgb(mcolors.rgb_to_hsv(tt)))
        assert_array_almost_equal(tt,
            mcolors.rgb_to_hsv(mcolors.hsv_to_rgb(tt)))
Esempio n. 4
0
def test2(dicLoc):
    itemCollection = pickle.load(open('itemCollection',"rb"))
    bagTypes = itemCollection.getBagTypes()
    
    for image in bagTypes['silver']:
        img = scipy.misc.imread(image.getFullImageLocation())
        array=np.asarray(img)
        arr=(array.astype(float))/255.0
        img_hsv = colors.rgb_to_hsv(arr[...,:3])
        
        lu1=img_hsv[...,0].flatten()
        plt.subplot(1,3,1)
        plt.hist(lu1*360,bins=10,range=(0.0,360.0),histtype='stepfilled', color='r', label='Hue')
        plt.title("Hue")
        plt.xlabel("Value")
        plt.ylabel("Frequency")
        plt.legend()
        
        lu2=img_hsv[...,1].flatten()
        plt.subplot(1,3,2)                  
        plt.hist(lu2,bins=10,range=(0.0,1.0),histtype='stepfilled', color='g', label='Saturation')
        plt.title("Saturation")   
        plt.xlabel("Value")    
        plt.ylabel("Frequency")
        plt.legend()
        
        lu3=img_hsv[...,2].flatten()
        plt.subplot(1,3,3)                  
        plt.hist(lu3*255,bins=10,range=(0.0,255.0),histtype='stepfilled', color='b', label='Intesity')
        plt.title("Intensity")   
        plt.xlabel("Value")    
        plt.ylabel("Frequency")
        plt.legend()
        plt.show()
Esempio n. 5
0
def get_color_range(n):
    colors = mcolors.TABLEAU_COLORS
    by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
                    for name, color in colors.items())
    sorted_names = [name for hsv, name in by_hsv]
    delta = int(len(sorted_names)/n)
    return sorted_names[::delta]
Esempio n. 6
0
    def abrir_imagen(self, widget):
        img = None
        fc = gtk.FileChooserDialog(title='Abrir imagen...', parent=None, action=gtk.FILE_CHOOSER_ACTION_OPEN, 
                                   buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
        fc.set_default_response(gtk.RESPONSE_OK)
        respuesta = fc.run()
        if respuesta == gtk.RESPONSE_OK:
            self.nombre_archivo = fc.get_filename()
            ext = self.nombre_archivo[-4:]

            if ext == ".jpg":
                img = Image.open(self.nombre_archivo)
                self.imagen = numpy.asarray(img,dtype=numpy.float64)
                if len(self.imagen.shape) == 3: # si es a color
                    self.imagen_color = c.rgb_to_hsv(self.imagen)
                    self.imagen = self.imagen_color[:,:,2]
                    self.imagen = self.imagen.astype(numpy.uint8)
                    self.color = True
                else:
                    self.color = False
            else:
                self.imagen = self.switch_ext(ext)

            self.dibujar_img(self.imagen)
            self.historial = []
            self.historial_color = []
            self.indice_historial = -1
            self.actualiza_historial()
        fc.destroy()
Esempio n. 7
0
def main():
    from scipy import ndimage

    from matplotlib import colors

    image = ndimage.imread('../../../heavy/raspberry.jpg')
    image = colors.rgb_to_hsv(image)

    height = image.shape[0]
    width = image.shape[1]

    hue = image[:, :, 0]
    saturation = image[:, :, 1]
    value = image[:, :, 2]

    analyzer = Analyzer(width, height)

    from PIL import Image, ImageEnhance

    output = Image.open('../../../heavy/raspberry.jpg')
    converter = ImageEnhance.Color(output)
    output = converter.enhance(0)

    prepared = analyzer.prepare(saturation, value)

    for target in HUES:
        x, y, angle, r = analyzer.analyze(hue, saturation, value, target, prepared)
        if r != 0:
            render(output, target, x, y, angle, r)

    output.save('output.png')
Esempio n. 8
0
def sorted_color_maps():
    '''List of color name and their hex values sorted by HSV.

    This code is taken from:
        http://matplotlib.org/examples/color/named_colors.html
    '''
    colors_ = list(six.iteritems(colors.cnames))

    # Add the single letter colors.
    for name, rgb in six.iteritems(colors.ColorConverter.colors):
        hex_ = colors.rgb2hex(rgb)
        colors_.append((name, hex_))

    # Transform to hex color values.
    hex_ = [color[1] for color in colors_]
    # Get the rgb equivalent.
    rgb = [colors.hex2color(color) for color in hex_]
    # Get the hsv equivalent.
    hsv = [colors.rgb_to_hsv(color) for color in rgb]

    # Split the hsv values to sort.
    hue = [color[0] for color in hsv]
    sat = [color[1] for color in hsv]
    val = [color[2] for color in hsv]

    # Sort by hue, saturation and value.
    ind = np.lexsort((val, sat, hue))
    sorted_colors = [colors_[i] for i in ind]
    sorted_colors = [
        c_1
        for (c_1, c_2) in zip(sorted_colors[:-1], sorted_colors[1:])
        if c_1[1] != c_2[1]]
    return sorted_colors
Esempio n. 9
0
def shadow_filter(image, dpi):
    """This filter creates a metallic look on patches.

    image : the image of the patch
    dpi   : the resultion of the patch"""
    # Get the shape of the image
    nx, ny, depth = image.shape
    # Create a mash grid
    xx, yy = np.mgrid[0:nx, 0:ny]
    # Draw a circular "shadow"
    circle = (xx + nx * 4) ** 2 + (yy + ny) ** 2
    # Normalize
    circle -= circle.min()
    circle = circle / circle.max()
    # Steepness
    value = circle.clip(0.3, 0.6) + 0.4
    saturation = 1 - circle.clip(0.7, 0.8)
    # Normalize
    saturation -= saturation.min() - 0.1
    saturation = saturation / saturation.max()
    # Convert the rgb part (without alpha) to hsv
    hsv = mc.rgb_to_hsv(image[:, :, :3])
    # Multiply the value of hsv image with the shadow
    hsv[:, :, 2] = hsv[:, :, 2] * value
    # Highlights with saturation
    hsv[:, :, 1] = hsv[:, :, 1] * saturation
    # Copy the hsv back into the image (we haven't touched alpha)
    image[:, :, :3] = mc.hsv_to_rgb(hsv)
    # the return values are: new_image, offset_x, offset_y
    return image, 0, 0
def rgb_to_greyscale(dataset_rgb):
    """
    Convert each image in the given dataset to greyscale.

    The dataset used in the model uses this specific shape:
        [channels, hight, width]
    it has to be changed as the rgb_to_hsv function needs this shape:
        [hight, width, channels]
    The new greyscale image is stored in a new array only using the last
    value of the hsv array.
    This new array has to be reshaped to meet the original criteria
    of the model.
    """
    dataset_grey = np.zeros((dataset_rgb.shape[0], 1,
                             dataset_rgb.shape[2], dataset_rgb.shape[3]))

    for i in range(len(dataset_rgb)):
        img_rgb = np.swapaxes(dataset_rgb[i], 0, 2)
        img_hsv = colors.rgb_to_hsv(img_rgb)
        img_grey = np.zeros((img_hsv.shape[0], img_hsv.shape[1]))
        for x in range(len(img_hsv)):
            for y in range(len(img_hsv[x])):
                img_grey[x][y] = img_hsv[x][y][2:]
        # plt.imshow(img_grey, cmap=cm.Greys_r)
        # plt.show()
        img_grey = img_grey.reshape(32, 32, 1)
        img_grey = np.swapaxes(img_grey, 2, 0)
        dataset_grey[i] = img_grey

    return dataset_grey
def Detect_laser(image):
    hsv= col.rgb_to_hsv((image/255.0))
    laser = np.zeros(image.shape[:2])
    for i in range(150,450):
        for j in range(260,640):
            if  hsv[i,j,1]<0.25 and hsv[i,j,2]>0.85:
                laser[i,j] = 1
    return laser
Esempio n. 12
0
 def __init__(self, imageRGB):
     self.imageRGB = imageRGB.copy()
     self.imageHSV = colors.rgb_to_hsv(self.imageRGB)
     self.imageHeight = self.imageRGB.shape[0]
     self.imageWidth = self.imageRGB.shape[1]
     self.segmentedRGB = None
     self.segmentedHSV = None
     self.computationTime = None
Esempio n. 13
0
def rgb_to_hsv_tuple(rgb_tuple):
    """
    Convert 3 tuple that represents a RGB color (values between 0..1) 
    to a 3 tuple in HSV color space.
    If you have an array of color values use: ``matplotlib.colors.rgb_to_hsv``.
    """ 
    colarr = rgb_to_hsv(np.array([[rgb_tuple]]))
    return tuple(colarr[0, 0, :])
Esempio n. 14
0
def extract_hue(infile):
	"""
		Returns a hue greyscale image from an rgb image.
	"""
	rgb_image = imread(infile)
	hsv_image = rgb_to_hsv(rgb_image)
	hue_image = -hsv_image[:,:,0]
	imsave(fname='%s-hue.png' %(infile.split('.')[0]),arr=hue_image,cmap='gray')
Esempio n. 15
0
def Detect_laser(image):
    hsv= col.rgb_to_hsv((image/255.0))
    laser = np.zeros(image.shape[:2])
    for i in range(150,450):
        for j in range(260,640):
            if  hsv[i,j,1]<0.25 and hsv[i,j,2]>0.85:
                laser[i,j] = 1
    open_laser = cv2.morphologyEx(laser, cv2.MORPH_OPEN, disk(2))
    return open_laser
Esempio n. 16
0
def Detect_laser(image):
    hsv= col.rgb_to_hsv((image/255.0))

    laser = np.zeros(image.shape[:2])
    for i in range(laser.shape[0]):
        for j in range(laser.shape[1]):
            if hsv[i,j,0] < 0.5 and hsv[i,j,2]> 0.6:
                laser[i,j] = 1
    open_laser = laser
    return open_laser
Esempio n. 17
0
def open_hsv(fname, size=(256,256)):
	img = Image.open(fname)
	img.thumbnail(size)
	ary = np.asarray(img)/255.0
	# xform to hsv
	if just_rgb:
		hsv = ary
	else: 
		hsv = rgb_to_hsv(ary)
	return hsv
Esempio n. 18
0
File: _color.py Progetto: ablot/phy
def _apply_color_masks(color, masks=None, alpha=None):
    alpha = alpha or .5
    hsv = rgb_to_hsv(color[:, :3])
    # Change the saturation and value as a function of the mask.
    if masks is not None:
        hsv[:, 1] *= masks
        hsv[:, 2] *= .5 * (1. + masks)
    color = hsv_to_rgb(hsv)
    n = color.shape[0]
    color = np.c_[color, alpha * np.ones((n, 1))]
    return color
Esempio n. 19
0
def get_color_scheme(base_color, num=4, spread=1.):
    """ Distributes num colors around the color wheel starting with a base
    color and converting the fraction `spread` of the circle """
    base_rgb = mclr.colorConverter.to_rgb(base_color)
    base_rgb = np.reshape(np.array(base_rgb), (1, 1, 3))
    base_hsv = mclr.rgb_to_hsv(base_rgb)[0, 0]
    res_hsv = np.array([[
        ((base_hsv[0] + dh) % 1., base_hsv[1], base_hsv[2])
        for dh in np.linspace(-0.5*spread, 0.5*spread, num, endpoint=False)
    ]])
    return mclr.hsv_to_rgb(res_hsv)[0]
def desaturate(image):
    """
    Desaturate, or Greyscale a color image
    :param image: 3D ndarray
    :return: 2D array
    """
    greyscale = colors.rgb_to_hsv(image)
    greyscale[:, :, 1] = 0  # Desaturate the image
    greyscale = colors.hsv_to_rgb(greyscale)
    greyscale = greyscale[:, :, 0]  # 3D array to 2D
    return greyscale
Esempio n. 21
0
 def create_brightness_colormap(self,principal_rgb_color, scale_size):
     '''
     Create brightness colormap based on one principal RGB color
     '''
     if np.any(principal_rgb_color > 1):
         raise Exception('principal_rgb_color values should  be in range [0,1]')
     hsv_color = colors.rgb_to_hsv(principal_rgb_color)
     hsv_colormap = np.concatenate((np.tile(hsv_color[:-1][None, :], (scale_size, 1))[:],
                                    np.linspace(0, 1, scale_size)[:, None]),
                                   axis=1)
     self.colormap=self.array2cmap(colors.hsv_to_rgb(hsv_colormap))
Esempio n. 22
0
def Detect_laser(image):
    hsv= col.rgb_to_hsv((image/255.0))

    laser = np.zeros(image.shape[:2])
    target=0.29 #green
    seuil=0.05 
    for i in range(laser.shape[0]):
        for j in range(laser.shape[1]):
            if abs(hsv[i,j,0] - target) < seuil and hsv[i,j,1]>0.2 and hsv[i,j,2]>0.1:
                laser[i,j] = 1
    return laser
def read_data_sets(data_dir, distortion=True, dtype=np.float32):
    train_images = np.array(np.load(os.path.join(data_dir, "cifar10TrainingData.npy")).reshape(50000, 3, 32, 32), dtype=dtype)
    train_images = np.rollaxis(train_images, 1, 4)
    train_images = rgb_to_hsv(train_images)
    train_images = np.rollaxis(train_images, 3, 1)

    train_labels = np.load(os.path.join(data_dir, "cifar10TrainingDataLabel.npy"))

    train_images_list = []
    train_labels_list = []

    for i in range(10):
        train_images_list.append(train_images[train_labels == i][:400])
        train_labels_list += [i] * 400
    index = np.arange(4000)
    np.random.shuffle(index)
    train_images = np.vstack(train_images_list)[index]
    train_labels = np.array(train_labels_list, dtype = np.int32)[index]

    train_images = train_images[index]
    train_labels = train_labels[index]
    mean_images = np.mean(train_images, axis = 0)
    #train_images = train_images - mean_images

    print(train_images.shape)
    print(train_labels.shape)

    test_images = np.array(np.load(os.path.join(data_dir, "cifar10TestingData.npy")).reshape(10000, 3, 32, 32), dtype=dtype)
    test_images = np.rollaxis(test_images, 1, 4)
    test_images = rgb_to_hsv(test_images)
    test_images = np.rollaxis(test_images, 3, 1)
    #test_images = test_images - mean_images
    print(test_images.shape)
    test_labels = np.load(os.path.join(data_dir, "cifar10TestingDataLabel.npy"))

    train = DataSet(train_images, train_labels, distortion=distortion)
    test = DataSet(test_images, test_labels, test=True)

    Datasets = collections.namedtuple('Datasets', ['train', 'test'])

    return Datasets(train = train, test = test)
def Detect_laser(image):
    hsv= col.rgb_to_hsv((image/255.0))

    laser = np.zeros(image.shape[:2])
#    target=0.29 #green
#    seuil=0.05 
    for i in range(laser.shape[0]):
        for j in range(laser.shape[1]):
            if  hsv[i,j,1]<0.25 and hsv[i,j,2]>0.85:
                laser[i,j] = 1
    open_laser = cv2.morphologyEx(laser, cv2.MORPH_OPEN, disk(2))
    return open_laser
Esempio n. 25
0
def _hover_effects(style):
    r, g, b = mpl_colors.colorConverter.to_rgb(style['color'])
    h, s, v = mpl_colors.rgb_to_hsv((r, g, b))
    s = (s + 0) / 1.5
    v = (v + 0) / 1.025
    return dict(
        style,
        color=mpl_colors.hsv_to_rgb((h, s, v)),
        path_effects=[
            pe.withStroke(linewidth=1, foreground='#000000', alpha=1.0),
        ],
    )
def q4(img,k):
    # data = reshape(img,(img.shape[0]*img.shape[1],img.shape[2]))
    img_hsv = colors.rgb_to_hsv(img*1.0/IMG_MAX_VALUE)
    H = img_hsv[:,:,0]
    H_data = reshape(H,(H.shape[0]*H.shape[1],1))

    H_centroids,_ = kmeans(H_data,k) # k colors will be found
    H_qnt,_ = vq(H_data,H_centroids)
    H_centers_idx = reshape(H_qnt,(H.shape[0],H.shape[1]))
    H_clustered = H_centroids[H_centers_idx]

    return H_data,H_clustered,H_centroids
Esempio n. 27
0
def plot_colortable(colors, title, sort_colors=True, emptycols=0):

    cell_width = 212
    cell_height = 22
    swatch_width = 48
    margin = 12
    topmargin = 40

    # Sort colors by hue, saturation, value and name.
    if sort_colors is True:
        by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))),
                         name)
                        for name, color in colors.items())
        names = [name for hsv, name in by_hsv]
    else:
        names = list(colors)

    n = len(names)
    ncols = 4 - emptycols
    nrows = n // ncols + int(n % ncols > 0)

    width = cell_width * 4 + 2 * margin
    height = cell_height * nrows + margin + topmargin
    dpi = 72

    fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi)
    fig.subplots_adjust(margin/width, margin/height,
                        (width-margin)/width, (height-topmargin)/height)
    ax.set_xlim(0, cell_width * 4)
    ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)
    ax.yaxis.set_visible(False)
    ax.xaxis.set_visible(False)
    ax.set_axis_off()
    ax.set_title(title, fontsize=24, loc="left", pad=10)

    for i, name in enumerate(names):
        row = i % nrows
        col = i // nrows
        y = row * cell_height

        swatch_start_x = cell_width * col
        swatch_end_x = cell_width * col + swatch_width
        text_pos_x = cell_width * col + swatch_width + 7

        ax.text(text_pos_x, y, name, fontsize=14,
                horizontalalignment='left',
                verticalalignment='center')

        ax.hlines(y, swatch_start_x, swatch_end_x,
                  color=colors[name], linewidth=18)

    return fig
Esempio n. 28
0
def tint(col, factor=1.0):
    """Tint a color (make it darker), returning a new RGB array
    """
    # this method is more complicated than it need be to
    # support matplotlib-1.x.
    # for matplotlib-2.x this would just be
    #     h, s, v = colors.rgb_to_hsv(colors.to_rgb(c))
    #     v *= factor
    #     return colors.hsv_to_rgb((h, s, v))
    rgb = numpy.array(to_rgb(col), ndmin=3)
    hsv = colors.rgb_to_hsv(rgb)
    hsv[-1][-1][2] *= factor
    return colors.hsv_to_rgb(hsv)[-1][-1]
Esempio n. 29
0
def applyCube(ary, cube):
	cubeh,cubes,cubev = cube
	sh = ary.shape[0:2]
	hsv = rgb_to_hsv(ary)
	idx = ((hsv*N*0.9999). astype(np.int)) . reshape(-1,3)

	# output hsv matrix MxMx3
	outh = cubeh[idx[:,0],idx[:,1],idx[:,2]]
	outs = cubes[idx[:,0],idx[:,1],idx[:,2]]
	outv = cubev[idx[:,0],idx[:,1],idx[:,2]]
	out_hsv = np.dstack((outh.reshape(sh), outs.reshape(sh), outv.reshape(sh)))
	out = hsv_to_rgb(out_hsv)

	return out
Esempio n. 30
0
def _resize_image(img, size = (128,128)):
    #takes an image (length x width x 3 with rgb colors)
    #and a tuple of size in pixels
    #returns the image normalized in hsv coloring
    #which cuts out whitespace and centers the image
    #returns false if something doesn't work
    if img.shape[-1] != 3:
        return False
    hsv = colors.rgb_to_hsv(img)
    w = np.nonzero((hsv[:,:,2] < 160) & (hsv[:,:,1] < 160))
    min_x, min_y = np.min(w,axis=1)
    max_x, max_y = np.max(w,axis=1)
    ims = misc.imresize(img[min_x:max_x,min_y:max_y,:],size)/256.0
    return ims
    def get_random_data(self,
                        annotation_line,
                        input_shape,
                        jitter=.3,
                        hue=.1,
                        sat=1.5,
                        val=1.5):
        '''r实时数据增强的随机预处理'''
        line = annotation_line.split()
        image = Image.open(line[0])
        iw, ih = image.size
        h, w = input_shape
        box = np.array(
            [np.array(list(map(int, box.split(',')))) for box in line[1:]])

        # resize image
        new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
            1 - jitter, 1 + jitter)
        scale = rand(.25, 2)
        if new_ar < 1:
            nh = int(scale * h)
            nw = int(nh * new_ar)
        else:
            nw = int(scale * w)
            nh = int(nw / new_ar)
        image = image.resize((nw, nh), Image.BICUBIC)

        # place image
        dx = int(rand(0, w - nw))
        dy = int(rand(0, h - nh))
        new_image = Image.new('RGB', (w, h), (128, 128, 128))
        new_image.paste(image, (dx, dy))
        image = new_image

        # flip image or not
        flip = rand() < .5
        if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

        # distort image
        hue = rand(-hue, hue)
        sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
        val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
        x = rgb_to_hsv(np.array(image) / 255.)
        x[..., 0] += hue
        x[..., 0][x[..., 0] > 1] -= 1
        x[..., 0][x[..., 0] < 0] += 1
        x[..., 1] *= sat
        x[..., 2] *= val
        x[x > 1] = 1
        x[x < 0] = 0
        image_data = hsv_to_rgb(x) * 255  # numpy array, 0 to 1

        # correct boxes
        box_data = np.zeros((len(box), 5))
        if len(box) > 0:
            np.random.shuffle(box)
            box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
            box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
            if flip: box[:, [0, 2]] = w - box[:, [2, 0]]
            box[:, 0:2][box[:, 0:2] < 0] = 0
            box[:, 2][box[:, 2] > w] = w
            box[:, 3][box[:, 3] > h] = h
            box_w = box[:, 2] - box[:, 0]
            box_h = box[:, 3] - box[:, 1]
            box = box[np.logical_and(box_w > 1,
                                     box_h > 1)]  # discard invalid box
            box_data = np.zeros((len(box), 5))
            box_data[:len(box)] = box
        if len(box) == 0:
            return image_data, []

        if (box_data[:, :4] > 0).any():
            return image_data, box_data
        else:
            return image_data, []
Esempio n. 32
0
def get_random_data(annotation_line, input_shape, random=True, max_boxes=20, jitter=.3, hue=.1, sat=1.5, val=1.5, proc_img=True):
    '''random preprocessing for real-time data augmentation
        input_shape: 网络输入的宽高
        random: True为打开随机多尺度训练,
                提示:当打开随机多尺度训练时,前面设置的网络输入尺寸width和height其实就不起作用了,width
                             会在320到608之间随机取值,且width=height,没10轮随机改变一次,一般建议可以根据自己需要修改
                             随机尺度训练的范围,这样可以增大batch
        max_boxes: 每张图片最多可标记的box的数量
        jitter: 
        hue: 数据增强参数,通过调整色调来生成更多训练样本
        sat: 数据增强参数,通过调整饱和度来生成更多训练样本
    '''
    line = annotation_line.split()
    image = Image.open(line[0])
    iw, ih = image.size
    h, w = input_shape
    box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])

    if not random:
        # resize image
        scale = min(w/iw, h/ih)
        nw = int(iw*scale)
        nh = int(ih*scale)
        dx = (w-nw)//2
        dy = (h-nh)//2
        image_data=0
        if proc_img:
            image = image.resize((nw,nh), Image.BICUBIC)
            new_image = Image.new('RGB', (w,h), (128,128,128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image)/255.

        # correct boxes
        box_data = np.zeros((max_boxes,5))
        if len(box)>0:
            np.random.shuffle(box)
            if len(box)>max_boxes: box = box[:max_boxes]
            box[:, [0,2]] = box[:, [0,2]]*scale + dx
            box[:, [1,3]] = box[:, [1,3]]*scale + dy
            box_data[:len(box)] = box

        return image_data, box_data

    # resize image
    new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)
    scale = rand(.25, 2)
    if new_ar < 1:
        nh = int(scale*h)
        nw = int(nh*new_ar)
    else:
        nw = int(scale*w)
        nh = int(nw/new_ar)
    image = image.resize((nw,nh), Image.BICUBIC)
    # imgs = np.asarray(image)
    # cv2.imwrite("temp_image/resize_image.jpg", imgs)

    # place image
    # 平移变换
    dx = int(rand(0, w-nw))
    dy = int(rand(0, h-nh))
    new_image = Image.new('RGB', (w,h), (128,128,128))
    new_image.paste(image, (dx, dy))
    image = new_image
    # imgs = np.asarray(image)
    # cv2.imwrite("temp_image/place_image.jpg", imgs)

    # flip image or not
    # 翻转图片
    flip = rand()<.5
    if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)
    
    # imgs = np.asarray(image)
    # cv2.imwrite("temp_image/flip_image.jpg", imgs)

    # distort image
    # HSV 抖动
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand()<.5 else 1/rand(1, sat)
    val = rand(1, val) if rand()<.5 else 1/rand(1, val)
    # 归一化处理
    # 内部函数,通过公式转化。具体函数不介绍。
    x = rgb_to_hsv(np.array(image)/255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0]>1] -= 1
    x[..., 0][x[..., 0]<0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    # 避免S/V CHANNEL越界
    x[x>1] = 1
    x[x<0] = 0
    image_data = hsv_to_rgb(x) # numpy array, 0 to 1

    # imgs = np.asarray(image)
    # cv2.imwrite("temp_image/distort_image.jpg", imgs)

    # correct boxes
    box_data = np.zeros((max_boxes,5))
    if len(box)>0:
        np.random.shuffle(box)
        box[:, [0,2]] = box[:, [0,2]]*nw/iw + dx
        box[:, [1,3]] = box[:, [1,3]]*nh/ih + dy
        if flip: box[:, [0,2]] = w - box[:, [2,0]]
        box[:, 0:2][box[:, 0:2]<0] = 0
        box[:, 2][box[:, 2]>w] = w
        box[:, 3][box[:, 3]>h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w>1, box_h>1)] # discard invalid box
        if len(box)>max_boxes: box = box[:max_boxes]
        box_data[:len(box)] = box

    return image_data, box_data
Esempio n. 33
0
def get_random_data(images_path,
                    annotation,
                    input_shape,
                    max_boxes=20,
                    hue=.1,
                    sat=1.5,
                    val=1.5):
    """random pre-processing for real-time data augmentation"""
    image = None  # , image_cv = None, None
    # get the random image by name
    for filename in glob.iglob(images_path + '/**/' + annotation.name,
                               recursive=True):
        image = cv2.imread(filename)
        break
    assert image is not None

    # annotation of the image
    boxes = np.array([
        np.array(
            list(
                map(int, [
                    label.box.x1, label.box.y1, label.box.x2, label.box.y2,
                    yolov3_classes.get(label.category)
                ]))) for label in annotation.labels
    ])

    # print('n_boxes: ' + str(len(boxes)))
    # print(boxes)

    # font = cv2.FONT_HERSHEY_SIMPLEX
    # for box in boxes:
    #     cv2.rectangle(image_cv, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 255, 0), 3)
    #     cv2.putText(image_cv, str(box[4]), (int(box[0]), int(box[1])), font, 1, (255, 255, 255), 2, cv2.LINE_AA)

    # cv2.imshow('gt', image_cv)
    # cv2.waitKey(0)

    # original size
    ih, iw, _ = image.shape
    # target size
    w, h = input_shape
    # scale for target size
    scale_x = w / iw
    scale_y = h / ih

    # resize image
    image = cv2.resize(image, (w, h), interpolation=cv2.INTER_CUBIC)

    # flip image or not
    flip = rand() < .5
    if flip:
        image = cv2.flip(image, 0)  # horizontal flip

    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
    x = rgb_to_hsv(np.array(image) / 255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

    # correct boxes
    box_data = np.zeros((max_boxes, 5))

    if len(boxes) > 0:
        np.random.shuffle(boxes)

        # only use first 'max_boxes' number of boxes
        if len(boxes) > max_boxes:
            boxes = boxes[:max_boxes]

        # rescale boxes
        boxes[:, [0, 2]] = boxes[:, [0, 2]] * scale_x
        boxes[:, [1, 3]] = boxes[:, [1, 3]] * scale_y

        # flip boxes
        if flip:
            boxes[:, [0, 2]] = w - boxes[:, [2, 0]]

        # x and y coordinates should be between [0..w] and [0..h]
        boxes[:, 0:2][boxes[:, 0:2] < 0] = 0
        boxes[:, 2][boxes[:, 2] > w] = w
        boxes[:, 3][boxes[:, 3] > h] = h

        # boxes should be at least >1 pixel wide and long
        box_w = boxes[:, 2] - boxes[:, 0]
        box_h = boxes[:, 3] - boxes[:, 1]
        boxes = boxes[np.logical_and(box_w > 1, box_h > 1)]

        box_data[:len(boxes)] = boxes

    return image_data, box_data
Esempio n. 34
0
def _create_colors(
    base_color: Union[str, Tuple[float, float, float]],
    n: int,
    hue_range: Optional[Tuple[float, float]] = (-0.1, 0.1),
    saturation_range: Optional[Tuple[float, float]] = (-0.3, 0.3),
    value_range: Optional[Tuple[float, float]] = (-0.3, 0.3),
    convert_to_rgb: bool = True,
    as_hex: bool = True,
) -> List[Any]:
    """
    Create variations of colors from base color.

    Parameters
    ----------
    base_color
        Base color which serves as a starting point.
    n
        Number of colors to create.
    hue_range
        Minimum and maximum value to add to the base color's hue.
        If `None`, don't adjust the hue.
    saturation_range
        Minimum and maximum value to add to the base color's saturation.
        If `None`, don't adjust the saturation.
    value_range
        Minimum and maximum value to add to the base color's value.
        If `None`, don't adjust the value.
    convert_to_rgb
        Whether to convert colors from HSV to RGB.
    as_hex:
        Whether to return colors as hex string.

    Returns
    -------
    :class:`list`
        List of colors, either as a hex string or an RGB array.
    """

    if not mcolors.is_color_like(base_color):
        raise ValueError("Base color is not color-like.")
    if n <= 0:
        raise ValueError(f"Number of colors must be > 0, found `{n}`.")

    base_color = mcolors.rgb_to_hsv(mcolors.to_rgb(base_color))

    if n == 1:
        colors = [base_color]
    else:
        n *= 2  # sometimes the colors are too similar, we take every 2nd one
        colors = np.repeat(base_color[..., np.newaxis], n, axis=1).T

        for i, r in enumerate((hue_range, saturation_range, value_range)):
            if r is None:
                continue
            r_low, r_high = sorted(r)
            c = base_color[i]

            colors[:, i] = np.linspace(max(c + r_low, 0), min(c + r_high, 1),
                                       n)

    if convert_to_rgb:
        colors = map(mcolors.hsv_to_rgb, colors)
    if as_hex:
        colors = map(mcolors.to_hex, colors)

    return list(
        colors)[::2]  # we've created twice as much colors, select every other
                which='both',
                right=False,
                left=False,
                labelleft=False)
# ax.axis('off')
lim = 2.8
plt.xlim(-lim, lim)
plt.ylim(-lim, lim)
# plt.savefig('output/data.png')

# x1 = np.linspace(-lim, lim, num=100)
# x2 = np.linspace(-lim, lim, num=100)
cmap_ = [[1, 0.498039215686275, 0.0549019607843137],
         [0.12156862745098, 0.466666666666667, 0.705882352941177]]
cmap = hsv_to_rgb(
    interp1d([0., 1.], rgb_to_hsv(cmap_),
             axis=0)(link_fn(np.linspace(-3.5, 3.5, num=64))))
newcmp = ListedColormap(cmap)

plt.figure(2)
im = plt.imshow(link_fn(mu).T,
                cmap=newcmp,
                extent=[-lim, lim, -lim, lim],
                origin='lower')
cb = plt.colorbar(im)
cb.set_ticks([cb.vmin, 0, cb.vmax])
cb.set_ticklabels([-1, 0, 1])
plt.contour(Xtest, Rtest, mu, levels=[.0], colors='k', linewidths=1.5)
# plt.axis('equal')
for label in [1, 0]:
    ind = Y[:, 0] == label
def Clustering(images):
    i = 0
    data_bsas = []
    for img in images:
        i += 1
        #img = NormalizeHist(img)
        img = cv2.medianBlur(img, 5)

        array = np.asarray(img)

        temp = []
        for subarray in array:
            for element in subarray:
                if (element[0] + element[1] + element[2]) / 3 > 10:
                    temp.append(element)

        temp = np.asarray(temp)
        arr = (temp.astype(float)) / 255.0
        img_hsv = colors.rgb_to_hsv(arr[..., :3])
        lu1 = img_hsv[..., 0].flatten()
        list = plt.hist(lu1 * 360, bins=360, range=(0.0, 360.0))

        toshow = []

        sum = 0
        for element in list[0]:
            sum += element

        #"""
        for  element in list[0]:
            if 100 * element / sum > 0:
                toshow.append(1)
            else:
                toshow.append(0)
        #"""

        #for  element in list[0]:
            #toshow.append(100 * element / sum)

        #for element in list[0]:
            #toshow.append(int(element))

        name = 'C:/results/' + str(i) + ' plot'
        plt.savefig(name)

        #plt.show()
        plt.cla()

        data_bsas.append(toshow)

    ComputeDistance(data_bsas)

    #"""
    from pyclustering.cluster.bsas import bsas, bsas_visualizer
    from pyclustering.utils import read_sample
    from pyclustering.samples.definitions import SIMPLE_SAMPLES

    max_clusters = 3
    threshold = 1.0

    bsas_instance = bsas(data_bsas, max_clusters, threshold, 'euclidean')
    bsas_instance.process()

    clusters = bsas_instance.get_clusters()
    representatives = bsas_instance.get_representatives()

    print (clusters)
Esempio n. 37
0
def get_hatch_color(rgb_color):
    hatch_color_hsv = mplcolors.rgb_to_hsv(rgb_color)
    hatch_color_hsv[2] = hatch_color_hsv[2] + 0.2 if color_is_dark(
        rgb_color) else hatch_color_hsv[2] - 0.2
    return mplcolors.hsv_to_rgb(hatch_color_hsv)
Esempio n. 38
0
def color_is_dark(rgb_color):
    hatch_color_hsv = mplcolors.rgb_to_hsv(rgb_color)
    return hatch_color_hsv[2] < 0.5
pos_dict_cul = {pos: [] for pos in ref_position_list}
for start, end in time_list:
    T.append(end)
    pos_en = X_en_cul[('2019-12-24', end)]
    for pos, en in pos_en:
        pos_dict_cul[pos].append(en)
import matplotlib.colors as mcolors
CSS4_COLORS = mcolors.CSS4_COLORS
num_colors = len(ref_position_list)

bad_colors = set(['seashell', 'linen', 'ivory', 'oldlace','snow','beige','whitesmoke', 'white',
                      'floralwhite', 'lightyellow', 'lightgoldenrodyellow', 'honeydew', 'mintcream', 'azure', 'lightcyan',
                      'aliceblue', 'ghostwhite', 'lavenderblush'
                     ])

by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))),
                             name) for name, color in CSS4_COLORS.items())
names = [name for hsv, name in by_hsv if name not in bad_colors]

ind_list = np.linspace(0, len(names), num_colors, dtype = int, endpoint=False).tolist()
color_names = [names[ind] for ind in ind_list]
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize = (12, 8))
dt = 0
for idx, pos in enumerate(ref_position_list):
    plt.plot(np.arange(len(pos_dict_cul[pos])), pos_dict_cul[pos], '-o', color = color_names[idx], label = '{}: {}'.format(pos, annotaion_dict[pos]))

plt.xticks(np.arange(len(pos_dict_cul[pos])), T, rotation = 90)
plt.legend(bbox_to_anchor=(1., 1))
plt.xlim([-0.5, len(T) + 1.8])
Esempio n. 40
0
def plot_loo_pit(
    ax,
    figsize,
    ecdf,
    loo_pit,
    loo_pit_ecdf,
    unif_ecdf,
    p975,
    p025,
    fill_kwargs,
    ecdf_fill,
    use_hdi,
    x_vals,
    hdi_kwargs,
    hdi_odds,
    n_unif,
    unif,
    plot_unif_kwargs,
    loo_pit_kde,
    legend,
    labeller,
    y_hat,
    y,
    color,
    textsize,
    hdi_prob,
    plot_kwargs,
    backend_kwargs,
    show,
):
    """Matplotlib loo pit plot."""
    if backend_kwargs is None:
        backend_kwargs = {}

    backend_kwargs = {
        **backend_kwarg_defaults(),
        **backend_kwargs,
    }

    (figsize, _, _, xt_labelsize, linewidth, _) = _scale_fig_size(figsize, textsize, 1, 1)
    backend_kwargs.setdefault("figsize", figsize)
    backend_kwargs["squeeze"] = True

    if ax is None:
        _, ax = create_axes_grid(1, backend_kwargs=backend_kwargs)

    plot_kwargs = matplotlib_kwarg_dealiaser(plot_kwargs, "plot")
    plot_kwargs["color"] = to_hex(color)
    plot_kwargs.setdefault("linewidth", linewidth * 1.4)
    if isinstance(y, str):
        label = "LOO-PIT ECDF" if ecdf else "LOO-PIT"
        xlabel = y
    elif isinstance(y, DataArray) and y.name is not None:
        label = "LOO-PIT ECDF" if ecdf else "LOO-PIT"
        xlabel = y.name
    elif isinstance(y_hat, str):
        label = "LOO-PIT ECDF" if ecdf else "LOO-PIT"
        xlabel = y_hat
    elif isinstance(y_hat, DataArray) and y_hat.name is not None:
        label = "LOO-PIT ECDF" if ecdf else "LOO-PIT"
        xlabel = y_hat.name
    else:
        label = "LOO-PIT ECDF" if ecdf else "LOO-PIT"
        xlabel = ""
    xlabel = labeller.var_name_to_str(y)

    plot_kwargs.setdefault("label", label)
    plot_kwargs.setdefault("zorder", 5)

    plot_unif_kwargs = matplotlib_kwarg_dealiaser(plot_unif_kwargs, "plot")
    light_color = rgb_to_hsv(to_rgb(plot_kwargs.get("color")))
    light_color[1] /= 2  # pylint: disable=unsupported-assignment-operation
    light_color[2] += (1 - light_color[2]) / 2  # pylint: disable=unsupported-assignment-operation
    plot_unif_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color)))
    plot_unif_kwargs.setdefault("alpha", 0.5)
    plot_unif_kwargs.setdefault("linewidth", 0.6 * linewidth)

    if ecdf:
        n_data_points = loo_pit.size
        plot_kwargs.setdefault("drawstyle", "steps-mid" if n_data_points < 100 else "default")
        plot_unif_kwargs.setdefault("drawstyle", "steps-mid" if n_data_points < 100 else "default")

        if ecdf_fill:
            if fill_kwargs is None:
                fill_kwargs = {}
            fill_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color)))
            fill_kwargs.setdefault("alpha", 0.5)
            fill_kwargs.setdefault(
                "step", "mid" if plot_kwargs["drawstyle"] == "steps-mid" else None
            )
            fill_kwargs.setdefault("label", f"{hdi_prob * 100:.3g}% credible interval")
    elif use_hdi:
        if hdi_kwargs is None:
            hdi_kwargs = {}
        hdi_kwargs.setdefault("color", to_hex(hsv_to_rgb(light_color)))
        hdi_kwargs.setdefault("alpha", 0.35)
        hdi_kwargs.setdefault("label", "Uniform HDI")

    if ecdf:
        ax.plot(
            np.hstack((0, loo_pit, 1)), np.hstack((0, loo_pit - loo_pit_ecdf, 0)), **plot_kwargs
        )

        if ecdf_fill:
            ax.fill_between(unif_ecdf, p975 - unif_ecdf, p025 - unif_ecdf, **fill_kwargs)
        else:
            ax.plot(unif_ecdf, p975 - unif_ecdf, unif_ecdf, p025 - unif_ecdf, **plot_unif_kwargs)
    else:
        x_ss = np.empty((n_unif, len(loo_pit_kde)))
        u_dens = np.empty((n_unif, len(loo_pit_kde)))
        if use_hdi:
            ax.axhspan(*hdi_odds, **hdi_kwargs)

            # Adds horizontal reference line
            ax.axhline(1, color="w", zorder=1)
        else:
            for idx in range(n_unif):
                x_s, unif_density = kde(unif[idx, :])
                x_ss[idx] = x_s
                u_dens[idx] = unif_density
            ax.plot(x_ss.T, u_dens.T, **plot_unif_kwargs)
        ax.plot(x_vals, loo_pit_kde, **plot_kwargs)
        ax.set_xlim(0, 1)
        ax.set_ylim(0, None)
    ax.set_xlabel(xlabel)
    ax.tick_params(labelsize=xt_labelsize)
    if legend:
        if not (use_hdi or (ecdf and ecdf_fill)):
            label = f"{hdi_prob * 100:.3g}% credible interval" if ecdf else "Uniform"
            ax.plot([], label=label, **plot_unif_kwargs)
        ax.legend()

    if backend_show(show):
        plt.show()

    return ax
Esempio n. 41
0
def get_random_data(annotation_line,
                    input_shape,
                    random=True,
                    max_boxes=20,
                    jitter=.3,
                    hue=.1,
                    sat=1.5,
                    val=1.5,
                    proc_img=True):
    '''random preprocessing for real-time data augmentation'''
    line = annotation_line.split()
    image = Image.open(line[0])  # PIL打开图片
    iw, ih = image.size  # image_width, image_height, 图片原生宽度、高度
    h, w = input_shape  # 416, 416
    box = np.array([
        np.array(list(map(int, box.split(',')))) for box in line[1:]
    ])  # 标签中的 l, t, r, b

    if not random:
        # resize image
        scale = min(w / iw, h / ih)
        nw = int(iw * scale)
        nh = int(ih * scale)
        dx = (w - nw) // 2
        dy = (h - nh) // 2
        image_data = 0
        if proc_img:
            image = image.resize((nw, nh), Image.BICUBIC)
            new_image = Image.new('RGB', (w, h), (128, 128, 128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image) / 255.

        # correct boxes
        box_data = np.zeros((max_boxes, 5))
        if len(box) > 0:
            np.random.shuffle(box)
            if len(box) > max_boxes: box = box[:max_boxes]
            box[:, [0, 2]] = box[:, [0, 2]] * scale + dx
            box[:, [1, 3]] = box[:, [1, 3]] * scale + dy
            box_data[:len(box)] = box

        return image_data, box_data

    # resize image
    new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
        1 - jitter, 1 + jitter)
    scale = rand(.25, 2)
    if new_ar < 1:
        nh = int(scale * h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * w)
        nh = int(nw / new_ar)
    image = image.resize((nw, nh), Image.BICUBIC)

    # place image
    dx = int(rand(0, w - nw))
    dy = int(rand(0, h - nh))
    new_image = Image.new('RGB', (w, h), (128, 128, 128))
    new_image.paste(image, (dx, dy))
    image = new_image

    # flip image or not
    flip = rand() < .5
    if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
    x = rgb_to_hsv(np.array(image) / 255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

    # correct boxes
    box_data = np.zeros((max_boxes, 5))
    if len(box) > 0:
        np.random.shuffle(box)
        box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
        box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
        if flip: box[:, [0, 2]] = w - box[:, [2, 0]]
        box[:, 0:2][box[:, 0:2] < 0] = 0
        box[:, 2][box[:, 2] > w] = w
        box[:, 3][box[:, 3] > h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w > 1, box_h > 1)]  # discard invalid box
        if len(box) > max_boxes: box = box[:max_boxes]
        box_data[:len(box)] = box

    return image_data, box_data
print(kmeans.labels_)
print(kmeans.inertia_)

from sklearn.decomposition import PCA
pca = PCA(3)
pca.fit(zoo_data)

pca_data = pd.DataFrame(pca.transform(zoo_data))
print(pca_data.head())

from matplotlib import colors as mcolors
import math
''' Generating different colors in ascending order  
                                of their hsv values '''
colors = list(
    zip(*sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
                for name, color in dict(mcolors.BASE_COLORS, **
                                        mcolors.CSS4_COLORS).items())))[1]

# number of steps to taken generate n(clusters) colors
skips = math.floor(len(colors[5:-5]) / clusters)
cluster_colors = colors[5:-5:skips]

# plot reduced dimension data
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(pca_data[0],
           pca_data[1],
Esempio n. 43
0
def get_random_data(annotation_line,
                    input_shape,
                    max_boxes=50,
                    jitter=.3,
                    hue=.1,
                    sat=1.5,
                    val=1.5):
    '''random preprocessing for real-time data augmentation'''
    line = annotation_line.split()
    image = Image.open(line[0])
    iw, ih = image.size
    h, w = input_shape
    box = np.array(
        [np.array(list(map(int, box.split(',')))) for box in line[1:]])

    # 对图像进行缩放并且进行长和宽的扭曲
    new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
        1 - jitter, 1 + jitter)
    scale = rand(.25, 2)
    if new_ar < 1:
        nh = int(scale * h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * w)
        nh = int(nw / new_ar)
    image = image.resize((nw, nh), Image.BICUBIC)

    # 将图像多余的部分加上灰条
    dx = int(rand(0, w - nw))
    dy = int(rand(0, h - nh))
    new_image = Image.new('RGB', (w, h), (128, 128, 128))
    new_image.paste(image, (dx, dy))
    image = new_image

    # 翻转图像
    flip = rand() < .5
    if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

    # 色域扭曲
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
    x = rgb_to_hsv(np.array(image) / 255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

    # 将box进行调整
    box_data = np.zeros((max_boxes, 5))
    if len(box) > 0:
        np.random.shuffle(box)
        box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
        box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
        if flip: box[:, [0, 2]] = w - box[:, [2, 0]]
        box[:, 0:2][box[:, 0:2] < 0] = 0
        box[:, 2][box[:, 2] > w] = w
        box[:, 3][box[:, 3] > h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w > 1, box_h > 1)]  # discard invalid box
        if len(box) > max_boxes: box = box[:max_boxes]
        box_data[:len(box)] = box

    return image_data, box_data
Esempio n. 44
0
def get_random_mosaic_data(annotations,
                           input_shape,
                           max_boxes=CONFIG.AUG.MAX_BOXES,
                           hue=.1,
                           sat=1.5,
                           val=1.5,
                           jitter=0.3):
    """mosaic augment V1
    mosaic augment V1 can make every image be cropped to be suitable for four regions.
    But this lose variety of image scale because every image need to suitable for their region.

    Args:
        annotations: ['img_path x1, y1, x2, y2 class', 'img_path x1, y1, x2, y2 class', ...]
                     merge four images one time
        input_shape: (416, 416) or others
        max_boxes:   50 or bigger
        hue:         random hue transformation
        sat:         random sat transformation
        val:         random val transformation
        jitter

    Returns:
        augment data with mosaic

    """
    h, w = input_shape
    min_x = 0.4
    min_y = 0.4
    scale_min = 1 - min(min_x, min_y)
    # scale_max = scale_min + 0.2
    scale_max = scale_min + 0.6

    place_x = [0, int(min_x * w), 0, int(min_x * w)]
    place_y = [0, 0, int(min_y * h), int(min_y * h)]

    imgs = []
    boxes_data = []
    index = 0

    for line in annotations:
        contents = line.split()
        img = Image.open(contents[0])
        img = img.convert('RGB')

        iw, ih = img.size

        boxes = np.array(
            [np.array(list(map(int, box.split(',')))) for box in contents[1:]])

        # 1.resize
        new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
            1 - jitter, 1 + jitter)
        scale = rand(scale_min, scale_max)
        if new_ar < 1:
            new_h = int(scale * h)
            new_w = int(new_h * new_ar)
        else:
            new_w = int(scale * w)
            new_h = int(new_w / new_ar)
        img = img.resize((new_w, new_h), Image.BICUBIC)

        # 2.flip
        flip = rand() < 0.5
        if flip:
            img = img.transpose(Image.FLIP_LEFT_RIGHT)
            # don't translate boxes coordination here, because len(boxes) may be equal zero.
            # boxes[..., [0, 2]] = new_w - boxes[..., [0, 2]]

        # 3.hsv transform
        hue = rand(-hue, hue)
        sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
        val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
        x = rgb_to_hsv(np.array(img) / 255.)
        x[..., 0] += hue
        x[..., 0][x[..., 0] > 1] -= 1
        x[..., 0][x[..., 0] < 0] += 1
        x[..., 1] *= sat
        x[..., 2] *= val
        x[x > 1] = 1
        x[x < 0] = 0
        img = hsv_to_rgb(x)
        img = Image.fromarray((img * 255).astype(np.uint8))

        # 4.place img
        new_img = Image.new('RGB', (w, h), (128, 128, 128))
        dx = place_x[index]
        dy = place_y[index]
        new_img.paste(img, (dx, dy))
        new_img = np.array(new_img) / 255
        imgs.append(new_img)
        index += 1

        # 5.correct box
        if len(boxes) > 0:
            # correct resize
            boxes[..., [0, 2]] = boxes[..., [0, 2]] * new_w / iw
            boxes[..., [1, 3]] = boxes[..., [1, 3]] * new_h / ih
            # correct flip
            if flip:
                boxes[..., [2, 0]] = new_w - boxes[..., [0, 2]]
            # correct place
            boxes[..., [0, 2]] = boxes[..., [0, 2]] + dx
            boxes[..., [1, 3]] = boxes[..., [1, 3]] + dy
            # pick valid boxes
            boxes[..., [0, 1]][boxes[..., [0, 1]] < 0] = 0
            boxes[..., 2][boxes[..., 2] > w] = w
            boxes[..., 3][boxes[..., 3] > h] = h
            boxes_w = boxes[..., 2] - boxes[..., 0]
            boxes_y = boxes[..., 3] - boxes[..., 1]
            boxes = boxes[np.logical_and(boxes_w > 1, boxes_y > 1)]

            boxes_data.append(boxes)
        else:
            # TODO: ######################################################################################
            # TODO: have fixed bug:
            # TODO: if len(boxes) <= 0, boxes not appended into boxes_data,
            # TODO: so, when cropping the boxes, it will encounter errors because i use i in for loop
            # TODO: ######################################################################################
            boxes_data.append([])

    # 6.crop imgs
    cropx = np.random.randint(int(w * min_x), int(w * (1 - min_x)))
    cropy = np.random.randint(int(h * min_y), int(h * (1 - min_y)))
    merge_img = np.zeros((h, w, 3))
    merge_img[:cropy, :cropx, :] = imgs[0][:cropy, :cropx, :]
    merge_img[:cropy, cropx:, :] = imgs[1][:cropy, cropx:, :]
    merge_img[cropy:, :cropx, :] = imgs[2][cropy:, :cropx, :]
    merge_img[cropy:, cropx:, :] = imgs[3][cropy:, cropx:, :]

    boxes = np.zeros(shape=(max_boxes, 5))

    new_boxes = crop_boxes(boxes_data, cropx, cropy)
    num_boxes = len(new_boxes)
    if num_boxes <= max_boxes and num_boxes > 0:
        boxes[0:num_boxes] = new_boxes
    elif num_boxes > max_boxes:
        boxes = new_boxes[:max_boxes]

    return merge_img, boxes
Esempio n. 45
0
def get_random_data(annotation_line,
                    input_shape,
                    random=True,
                    max_boxes=80,
                    jitter=.1,
                    hue=.1,
                    sat=1.5,
                    val=1.5,
                    proc_img=True):
    '''random preprocessing for real-time data augmentation'''
    line = annotation_line

    image = Image.open(line[0])
    iw, ih = image.size
    h, w = input_shape
    box = np.array(
        [np.array(list(map(float, box.split(',')))) for box in line[1:]])

    if not random:
        # resize image
        scale = min(w / iw, h / ih)
        nw = int(iw * scale)
        nh = int(ih * scale)
        dx = (w - nw) // 2
        dy = (h - nh) // 2
        image_data = 0
        if proc_img:
            image = image.resize((nw, nh), Image.BICUBIC)
            new_image = Image.new('RGB', (w, h), (128, 128, 128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image) / 255.

        # correct boxes
        box_data = np.zeros((max_boxes, 5 + NUM_ANGLES3))
        if len(box) > 0:
            np.random.shuffle(box)
            if len(box) > max_boxes: box = box[:max_boxes]
            box[:, [0, 2]] = box[:, [0, 2]] * scale + dx
            box[:, [1, 3]] = box[:, [1, 3]] * scale + dy
            box_data[:len(box), 0:5] = box[:, 0:5]

            for b in range(0, len(box)):
                for i in range(5, MAX_VERTICES * 2, 2):
                    if box[b, i] == 0 and box[b, i + 1] == 0:
                        continue
                    box[b, i] = box[b, i] * scale + dx
                    box[b, i + 1] = box[b, i + 1] * scale + dy

            box_data[:, i:NUM_ANGLES3 + 5] = 0

            for i in range(0, len(box)):
                boxes_xy = (box[i, 0:2] + box[i, 2:4]) // 2
                boxes_wh = (box[i, 2:4] - box[i, 0:2])

                for ver in range(5, MAX_VERTICES * 2, 2):
                    if box[i, ver] == 0 and box[i, ver + 1] == 0:
                        break
                    dist_x = boxes_xy[0] - box[i, ver]
                    dist_y = boxes_xy[1] - box[i, ver + 1]
                    dist = np.sqrt(np.power(dist_x, 2) + np.power(dist_y, 2))
                    if (dist < 1): dist = 1

                    angle = np.degrees(np.arctan2(dist_y, dist_x))
                    if (angle < 0): angle += 360
                    iangle = int(angle) // ANGLE_STEP
                    relative_angle = (angle -
                                      (iangle * int(ANGLE_STEP))) / ANGLE_STEP

                    if dist > box_data[i, 5 + iangle * 3]:
                        box_data[i, 5 + iangle * 3] = dist
                        box_data[i, 5 + iangle * 3 + 1] = relative_angle
                        box_data[i, 5 + iangle * 3 + 2] = 1
        return image_data, box_data

    # resize image
    new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
        1 - jitter, 1 + jitter)
    scale = rand(.6, 1.8)
    if new_ar < 1:
        nh = int(scale * h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * w)
        nh = int(nw / new_ar)
    image = image.resize((nw, nh), Image.BICUBIC)

    nwiw = nw / iw
    nhih = nh / ih

    # place image
    dx = int(rand(0, w - nw))
    dy = int(rand(0, h - nh))
    new_image = Image.new('RGB', (w, h), (128, 128, 128))
    new_image.paste(image, (dx, dy))
    image = new_image

    # flip image or not
    flip = rand() < .5
    if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
    x = rgb_to_hsv(np.array(image) / 255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

    # correct boxes
    box_data = np.zeros((max_boxes, 5 + NUM_ANGLES3))
    if len(box) > 0:
        np.random.shuffle(box)
        box[:, [0, 2]] = box[:, [0, 2]] * nwiw + dx
        box[:, [1, 3]] = box[:, [1, 3]] * nhih + dy
        if flip: box[:, [0, 2]] = (w - 1) - box[:, [2, 0]]

        for b in range(0, len(box)):
            for i in range(5, MAX_VERTICES * 2, 2):
                if box[b, i] == 0 and box[b, i + 1] == 0:
                    continue
                box[b, i] = np.clip(box[b, i] * nwiw + dx, 0, w - 1)
                box[b, i + 1] = np.clip(box[b, i + 1] * nhih + dy, 0, h - 1)
                if flip:
                    box[b, i] = (w - 1) - box[b, i]

        box[:, 0:2][box[:, 0:2] < 0] = 0
        box[:, 2][box[:, 2] >= w] = w - 1
        box[:, 3][box[:, 3] >= h] = h - 1
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w > 1, box_h > 1)]  # discard invalid box
        if len(box) > max_boxes: box = box[:max_boxes]
        box_data[:len(box), 0:5] = box[:, 0:5]

        box_data[:, i:NUM_ANGLES3 + 5] = 0

        for i in range(0, len(box)):
            boxes_xy = (box[i, 0:2] + box[i, 2:4]) // 2

            for ver in range(5, MAX_VERTICES * 2, 2):
                if box[i, ver] == 0 and box[i, ver + 1] == 0:
                    break
                dist_x = boxes_xy[0] - box[i, ver]
                dist_y = boxes_xy[1] - box[i, ver + 1]
                dist = np.sqrt(np.power(dist_x, 2) + np.power(dist_y, 2))
                if (dist < 1): dist = 1

                angle = np.degrees(np.arctan2(dist_y, dist_x))
                if (angle < 0): angle += 360
                iangle = int(angle) // ANGLE_STEP
                if iangle == NUM_ANGLES: iangle = 0

                if dist > box_data[i, 5 + iangle * 3]:
                    box_data[i, 5 + iangle * 3] = dist
                    box_data[i, 5 + iangle * 3 +
                             1] = (angle - (iangle * int(ANGLE_STEP))
                                   ) / ANGLE_STEP  #relative angle
                    box_data[i, 5 + iangle * 3 + 2] = 1

    return image_data, box_data
Esempio n. 46
0
import matplotlib.pyplot as plt
import matplotlib.colors as cl
from scipy import misc

#load some images
f = misc.face()
f = misc.imread("unnamed.png")

dims = f.shape
for i in range(0, dims[2]):
    plt.imshow(f[:, :, i], 'gray')
    plt.show()

f = misc.imread("SmilingCat.jpg")

dims = f.shape
for i in range(0, dims[2]):
    plt.imshow(f[:, :, i], 'gray')
    plt.show()

#To Hue Saturation Value Room
hsv = cl.rgb_to_hsv(f)

plt.imshow(hsv)
plt.show()

#Histogram
H = hsv[:, :, 1]
n, bins, patches = plt.hist(H)
#May take a while
Esempio n. 47
0
#   Author :        fengge
#   date:          2019/3/31
#   Description :
#-------------------------------------------------
from __future__ import division

import matplotlib.pyplot as plt
from matplotlib import colors as mcolors

if __name__ == "__main__":

    colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)

    # Sort colors by hue, saturation, value and name.
    by_hsv = sorted(
        (tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
        for name, color in colors.items())
    sorted_names = [name for hsv, name in by_hsv]

    n = len(sorted_names)
    ncols = 4
    nrows = n // ncols + 1

    fig, ax = plt.subplots(figsize=(8, 5))

    # Get height and width
    X, Y = fig.get_dpi() * fig.get_size_inches()
    h = Y / (nrows + 1)
    w = X / ncols

    for i, name in enumerate(sorted_names):
Esempio n. 48
0
def get_random_data(annotation_line,
                    input_shape,
                    max_boxes=CONFIG.AUG.MAX_BOXES,
                    jitter=.3,
                    hue=.1,
                    sat=1.5,
                    val=1.5):
    """online data augment

    Args:
        annotation_line: 'img_path x1, y1, x2, y2 class'
        input_shape:     [h, w]
        max_boxes:       max boxes that can be trained
        jitter:          random aspect ratio
        hue:             random hue transformation
        sat:             random sat transformation
        val:             random val transformation

    Returns:
        image_data
        box_data

    """
    line = annotation_line.split()
    image = Image.open(line[0])
    iw, ih = image.size
    h, w = input_shape
    box = np.array(
        [np.array(list(map(int, box.split(',')))) for box in line[1:]])

    # resize image
    new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
        1 - jitter, 1 + jitter)
    scale = rand(.25, 2)
    if new_ar < 1:
        nh = int(scale * h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * w)
        nh = int(nw / new_ar)
    image = image.resize((nw, nh), Image.BICUBIC)

    # place image
    dx = int(rand(0, w - nw))
    dy = int(rand(0, h - nh))
    new_image = Image.new('RGB', (w, h), (128, 128, 128))
    new_image.paste(image, (dx, dy))
    image = new_image

    # flip image or not
    flip = rand() < .5
    if flip:
        image = image.transpose(Image.FLIP_LEFT_RIGHT)

    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
    x = rgb_to_hsv(np.array(image) / 255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

    # correct boxes
    box_data = np.zeros((max_boxes, 5))
    if len(box) > 0:
        np.random.shuffle(box)
        box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
        box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
        if flip:
            box[:, [0, 2]] = w - box[:, [2, 0]]
        box[:, 0:2][box[:, 0:2] < 0] = 0
        box[:, 2][box[:, 2] > w] = w
        box[:, 3][box[:, 3] > h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w > 1, box_h > 1)]  # discard invalid box
        if len(box) > max_boxes:
            box = box[:max_boxes]
        box_data[:len(box)] = box

    return image_data, box_data
Esempio n. 49
0
def get_random_data(annotation_line, input_shape, random=True, max_boxes=20, jitter=.3, hue=.1, sat=1.5, val=1.5,
                    proc_img=True):
    '''random preprocessing for real-time data augmentation
    Resizes the image and its bounding box to the target size

    '''
    line = annotation_line.split()
    image = Image.open(line[0])
    iw, ih = image.size
    # target input image shape
    h, w = input_shape
    # each box contains x1, y1, x2, y2, class_label
    box = np.array([np.array(list(map(int, box.split(',')))) for box in line[1:]])

    if not random:
        # resize image
        scale = min(w / iw, h / ih)
        nw = int(iw * scale)
        nh = int(ih * scale)
        dx = (w - nw) // 2
        dy = (h - nh) // 2
        image_data = 0
        if proc_img:
            image = image.resize((nw, nh), Image.BICUBIC)
            new_image = Image.new('RGB', (w, h), (128, 128, 128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image) / 255.

        # correct boxes
        box_data = np.zeros((max_boxes, 5))
        # if a box coordinates is available in annotation file for this line
        if len(box) > 0:
            np.random.shuffle(box)
            if len(box) > max_boxes: box = box[:max_boxes]
            # resize the x-coords of the corners
            box[:, [0, 2]] = box[:, [0, 2]] * scale + dx
            # resize the y-coords of the corners
            box[:, [1, 3]] = box[:, [1, 3]] * scale + dy
            box_data[:len(box)] = box

        return image_data, box_data

    # resize image
    new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(1 - jitter, 1 + jitter)
    scale = rand(.25, 2)
    if new_ar < 1:
        nh = int(scale * h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * w)
        nh = int(nw / new_ar)
    image = image.resize((nw, nh), Image.BICUBIC)

    # place image
    dx = int(rand(0, w - nw))
    dy = int(rand(0, h - nh))
    new_image = Image.new('RGB', (w, h), (128, 128, 128))
    new_image.paste(image, (dx, dy))
    image = new_image

    # flip image or not
    flip = rand() < .5
    if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
    x = rgb_to_hsv(np.array(image) / 255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

    # make gray
    gray = rand() < .2
    if gray:
        image_gray = np.dot(image_data, [0.299, 0.587, 0.114])
        # a gray RGB image is GGG
        image_data = np.moveaxis(np.stack([image_gray, image_gray, image_gray]), 0, -1)

    # invert colors
    invert = rand() < .1
    if invert:
        image_data = 1. - image_data

    # correct boxes
    box_data = np.zeros((max_boxes, 5))
    if len(box) > 0:
        np.random.shuffle(box)
        box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
        box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
        if flip: box[:, [0, 2]] = w - box[:, [2, 0]]
        box[:, 0:2][box[:, 0:2] < 0] = 0
        box[:, 2][box[:, 2] > w] = w
        box[:, 3][box[:, 3] > h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w > 1, box_h > 1)]  # discard invalid box
        if len(box) > max_boxes: box = box[:max_boxes]
        box_data[:len(box)] = box

    return image_data, box_data
Esempio n. 50
0
    plt.ylim(Ny, 0)

    plt.savefig('output_houghLine.png')


if __name__ == '__main__':
    ###################
    start_time = time.time()

    # Load the image
    image = plt.imread('Lines.jpg')

    Ny, Nx, _ = image.shape

    # Get value Channel (intensity)
    hsvImage = color.rgb_to_hsv(image)
    valImage = hsvImage[..., 2]

    # Detect edges using canny
    edgeImage = feature.canny(valImage,
                              sigma=1.4,
                              low_threshold=40,
                              high_threshold=150)

    # Show original image
    plt.figure('Original Image')
    plt.imshow(image)
    plt.set_cmap('gray')

    # Show edge image
    plt.figure('Edge Image')
Esempio n. 51
0
def get_random_data(annotation_line,
                    input_shape,
                    random=True,
                    max_boxes=200,
                    jitter=.3,
                    hue=.1,
                    sat=1.5,
                    val=1.5,
                    proc_img=True,
                    mixup=False,
                    merge_line=None,
                    split_s=' '):
    line = annotation_line.split(split_s)
    image = Image.open(line[0])
    iw, ih = image.size
    h, w = input_shape
    if split_s == ' ':
        box = np.array(
            [np.array(list(map(int, box.split(',')))) for box in line[1:]])
    elif split_s == '\t':
        box = np.array(
            [np.array(list(map(int, box.split(',')))) for box in line[1:-1]])

    if not random:
        # resize image
        scale = min(w / iw, h / ih)
        nw = int(iw * scale)
        nh = int(ih * scale)
        dx = (w - nw) // 2
        dy = (h - nh) // 2
        image_data = 0
        if proc_img:
            image = image.resize((nw, nh), Image.BICUBIC)
            new_image = Image.new('RGB', (w, h), (128, 128, 128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image) / 255.

        # correct boxes
        box_data = np.zeros((max_boxes, 5))
        if len(box) > 0:
            np.random.shuffle(box)
            if len(box) > max_boxes: box = box[:max_boxes]
            box[:, [0, 2]] = box[:, [0, 2]] * scale + dx
            box[:, [1, 3]] = box[:, [1, 3]] * scale + dy
            box_data[:len(box)] = box
            box_data = np.concatenate([box_data, np.ones(len(box))])

        return image_data, box_data

    sample = get_mixup_image(image, box, mixup, merge_line)
    image = Image.fromarray(sample['image'])
    box = np.concatenate(
        (sample['gt_bbox'], np.expand_dims(
            sample['gt_class'], 1), np.expand_dims(sample['gt_score'], 1)),
        axis=1)
    iw, ih = sample['w'], sample['h']

    # resize image
    new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
        1 - jitter, 1 + jitter)
    scale = rand(.25, 2)
    if new_ar < 1:
        nh = int(scale * h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * w)
        nh = int(nw / new_ar)
    image = image.resize((nw, nh), Image.BICUBIC)

    # place image
    dx = int(rand(0, w - nw))
    dy = int(rand(0, h - nh))
    new_image = Image.new('RGB', (w, h), (128, 128, 128))
    new_image.paste(image, (dx, dy))
    image = new_image

    # flip image or not
    flip = rand() < .5
    if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
    x = rgb_to_hsv(np.array(image) / 255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

    # correct boxes
    box_data = np.zeros((max_boxes, 6))
    if len(box) > 0:
        np.random.shuffle(box)
        box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
        box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
        if flip: box[:, [0, 2]] = w - box[:, [2, 0]]
        box[:, 0:2][box[:, 0:2] < 0] = 0
        box[:, 2][box[:, 2] > w] = w
        box[:, 3][box[:, 3] > h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w > 1, box_h > 1)]  # discard invalid box
        if len(box) > max_boxes: box = box[:max_boxes]
        box_data[:len(box)] = box
    # import cv2
    # tmp = (image_data * 255).astype(np.uint8)
    # for s in box_data:
    #     tmp = cv2.rectangle(tmp, (int(s[0]), int(s[1])),
    #                         (int(s[2]), int(s[3])), (0, 0, 255), 2)
    # cv2.imwrite('tmp.jpg', tmp[:,:,::-1])

    return image_data, box_data
Esempio n. 52
0
def get_random_data_with_Mosaic(annotation_line, input_shape, max_boxes=50, hue=.1, sat=1.5, val=1.5):
    '''random preprocessing for real-time data augmentation'''
    h, w = input_shape
    min_offset_x = 0.4
    min_offset_y = 0.4
    scale_low = 1 - min(min_offset_x, min_offset_y)
    scale_high = scale_low + 0.2

    image_datas = []
    box_datas = []
    index = 0

    place_x = [0, 0, int(w * min_offset_x), int(w * min_offset_x)]
    place_y = [0, int(h * min_offset_y), int(w * min_offset_y), 0]
    for line in annotation_line:
        # 每一行进行分割
        line_content = line.split()
        # 打开图片
        image = Image.open(line_content[0])
        image = image.convert("RGB")
        # 图片的大小
        iw, ih = image.size
        # 保存框的位置
        box = np.array([np.array(list(map(int, box.split(',')))) for box in line_content[1:]])

        # 是否翻转图片
        flip = rand() < .5
        if flip and len(box) > 0:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)
            box[:, [0, 2]] = iw - box[:, [2, 0]]

        # 对输入进来的图片进行缩放
        new_ar = w / h
        scale = rand(scale_low, scale_high)
        if new_ar < 1:
            nh = int(scale * h)
            nw = int(nh * new_ar)
        else:
            nw = int(scale * w)
            nh = int(nw / new_ar)
        image = image.resize((nw, nh), Image.BICUBIC)

        # 进行色域变换
        hue = rand(-hue, hue)
        sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
        val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
        x = rgb_to_hsv(np.array(image) / 255.)
        x[..., 0] += hue
        x[..., 0][x[..., 0] > 1] -= 1
        x[..., 0][x[..., 0] < 0] += 1
        x[..., 1] *= sat
        x[..., 2] *= val
        x[x > 1] = 1
        x[x < 0] = 0
        image = hsv_to_rgb(x)

        image = Image.fromarray((image * 255).astype(np.uint8))
        # 将图片进行放置,分别对应四张分割图片的位置
        dx = place_x[index]
        dy = place_y[index]
        new_image = Image.new('RGB', (w, h), (128, 128, 128))
        new_image.paste(image, (dx, dy))
        image_data = np.array(new_image) / 255

        index = index + 1
        box_data = []
        # 对box进行重新处理
        if len(box) > 0:
            np.random.shuffle(box)
            box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
            box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
            box[:, 0:2][box[:, 0:2] < 0] = 0
            box[:, 2][box[:, 2] > w] = w
            box[:, 3][box[:, 3] > h] = h
            box_w = box[:, 2] - box[:, 0]
            box_h = box[:, 3] - box[:, 1]
            box = box[np.logical_and(box_w > 1, box_h > 1)]
            box_data = np.zeros((len(box), 5))
            box_data[:len(box)] = box

        image_datas.append(image_data)
        box_datas.append(box_data)

    # 将图片分割,放在一起
    cutx = np.random.randint(int(w * min_offset_x), int(w * (1 - min_offset_x)))
    cuty = np.random.randint(int(h * min_offset_y), int(h * (1 - min_offset_y)))

    new_image = np.zeros([h, w, 3])
    new_image[:cuty, :cutx, :] = image_datas[0][:cuty, :cutx, :]
    new_image[cuty:, :cutx, :] = image_datas[1][cuty:, :cutx, :]
    new_image[cuty:, cutx:, :] = image_datas[2][cuty:, cutx:, :]
    new_image[:cuty, cutx:, :] = image_datas[3][:cuty, cutx:, :]

    # 对框进行进一步的处理
    new_boxes = merge_bboxes(box_datas, cutx, cuty)

    # 将box进行调整
    box_data = np.zeros((max_boxes, 5))
    if len(new_boxes) > 0:
        if len(new_boxes) > max_boxes: new_boxes = new_boxes[:max_boxes]
        box_data[:len(new_boxes)] = new_boxes
    return new_image, box_data
Esempio n. 53
0
def rgb_hsv(color: RGB) -> HSV:
    return HSV(*colors.rgb_to_hsv((color.R, color.G, color.B)))
Esempio n. 54
0
import matplotlib.pyplot as plt
from matplotlib import colors

colors_ = list(six.iteritems(colors.cnames))

# Add the single letter colors.
for name, rgb in six.iteritems(colors.ColorConverter.colors):
    hex_ = colors.rgb2hex(rgb)
    colors_.append((name, hex_))

# Transform to hex color values.
hex_ = [color[1] for color in colors_]
# Get the rgb equivalent.
rgb = [colors.hex2color(color) for color in hex_]
# Get the hsv equivalent.
hsv = [colors.rgb_to_hsv(color) for color in rgb]

# Split the hsv values to sort.
hue = [color[0] for color in hsv]
sat = [color[1] for color in hsv]
val = [color[2] for color in hsv]

# Sort by hue, saturation and value.
ind = np.lexsort((val, sat, hue))
sorted_colors = [colors_[i] for i in ind]

n = len(sorted_colors)
ncols = 4
nrows = int(np.ceil(1. * n / ncols))

fig, ax = plt.subplots()
Esempio n. 55
0
    def _data_aug(image,
                  box,
                  is_training,
                  jitter=0.3,
                  hue=0.1,
                  sat=1.5,
                  val=1.5,
                  image_size=(352, 640)):
        """Data augmentation function."""
        if not isinstance(image, Image.Image):
            image = Image.fromarray(image)

        iw, ih = image.size
        ori_image_shape = np.array([ih, iw], np.int32)
        h, w = image_size

        if not is_training:
            return _infer_data(image, image_size, box)

        flip = _rand() < .5
        # correct boxes
        box_data = np.zeros((max_boxes, 5))
        while True:
            # Prevent the situation that all boxes are eliminated
            new_ar = float(w) / float(h) * _rand(1 - jitter, 1 + jitter) / \
                     _rand(1 - jitter, 1 + jitter)
            scale = _rand(0.25, 2)

            if new_ar < 1:
                nh = int(scale * h)
                nw = int(nh * new_ar)
            else:
                nw = int(scale * w)
                nh = int(nw / new_ar)

            dx = int(_rand(0, w - nw))
            dy = int(_rand(0, h - nh))

            if len(box) >= 1:
                t_box = box.copy()
                np.random.shuffle(t_box)
                t_box[:,
                      [0, 2]] = t_box[:, [0, 2]] * float(nw) / float(iw) + dx
                t_box[:,
                      [1, 3]] = t_box[:, [1, 3]] * float(nh) / float(ih) + dy
                if flip:
                    t_box[:, [0, 2]] = w - t_box[:, [2, 0]]
                t_box[:, 0:2][t_box[:, 0:2] < 0] = 0
                t_box[:, 2][t_box[:, 2] > w] = w
                t_box[:, 3][t_box[:, 3] > h] = h
                box_w = t_box[:, 2] - t_box[:, 0]
                box_h = t_box[:, 3] - t_box[:, 1]
                t_box = t_box[np.logical_and(box_w > 1,
                                             box_h > 1)]  # discard invalid box

            if len(t_box) >= 1:
                box = t_box
                break

        box_data[:len(box)] = box
        # resize image
        image = image.resize((nw, nh), Image.BICUBIC)
        # place image
        new_image = Image.new('RGB', (w, h), (128, 128, 128))
        new_image.paste(image, (dx, dy))
        image = new_image

        # flip image or not
        if flip:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)

        # convert image to gray or not
        gray = _rand() < .25
        if gray:
            image = image.convert('L').convert('RGB')

        # when the channels of image is 1
        image = np.array(image)
        if len(image.shape) == 2:
            image = np.expand_dims(image, axis=-1)
            image = np.concatenate([image, image, image], axis=-1)

        # distort image
        hue = _rand(-hue, hue)
        sat = _rand(1, sat) if _rand() < .5 else 1 / _rand(1, sat)
        val = _rand(1, val) if _rand() < .5 else 1 / _rand(1, val)
        image_data = image / 255.
        if do_hsv:
            x = rgb_to_hsv(image_data)
            x[..., 0] += hue
            x[..., 0][x[..., 0] > 1] -= 1
            x[..., 0][x[..., 0] < 0] += 1
            x[..., 1] *= sat
            x[..., 2] *= val
            x[x > 1] = 1
            x[x < 0] = 0
            image_data = hsv_to_rgb(x)  # numpy array, 0 to 1
        image_data = image_data.astype(np.float32)

        # preprocess bounding boxes
        bbox_true_1, bbox_true_2, bbox_true_3, gt_box1, gt_box2, gt_box3 = \
            _preprocess_true_boxes(box_data, anchors, image_size)

        return image_data, bbox_true_1, bbox_true_2, bbox_true_3, \
               ori_image_shape, gt_box1, gt_box2, gt_box3
Esempio n. 56
0
def get_random_mosaic_data_v2(annotations,
                              input_shape,
                              max_boxes=CONFIG.AUG.MAX_BOXES,
                              jitter=0.2,
                              scale_min=0.5,
                              scale_max=2,
                              hue=.1,
                              sat=1.5,
                              val=1.5,
                              min_x=0.4,
                              min_y=0.4):
    """mosaic augment V2
    V2 can keep the variety of size in data augment
    But, V2 may produce some gray region that don't have background or foreground.

    Args:
        annotations: ['img_path x1, y1, x2, y2 class', ...]
        input_shape: h, w
        max_boxes:   a scalar
        jitter:      aspect ratio
        scale_min:   min scale ratio
        scale_max:   max scale ratio
        hue:         a float
        sat:         a float
        val:         a float
        min_x:       random cut ratio of x is random(min_x, 1-min_x)
        min_y:       random cut ratio of y is random(min_y, 1-min_y)

    Returns:
        merge_img
        boxes

    """
    min_x = min_x
    min_y = min_y
    h, w = input_shape

    imgs = []
    boxes_data = []

    for line in annotations:
        line = line.split()
        image = Image.open(line[0])
        iw, ih = image.size
        box = np.array(
            [np.array(list(map(int, box.split(',')))) for box in line[1:]])

        # 1.resize image
        new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
            1 - jitter, 1 + jitter)
        scale = rand(scale_min, scale_max)
        if new_ar < 1:
            nh = int(scale * h)
            nw = int(nh * new_ar)
        else:
            nw = int(scale * w)
            nh = int(nw / new_ar)
        image = image.resize((nw, nh), Image.BICUBIC)

        # 2.generate new image
        dx = int(rand(0, w - nw))
        dy = int(rand(0, h - nh))
        new_image = Image.new('RGB', (w, h), (128, 128, 128))
        new_image.paste(image, (dx, dy))
        image = new_image

        # 3.flip image
        flip = rand() < .5
        if flip:
            image = image.transpose(Image.FLIP_LEFT_RIGHT)

        # 4.hsv transformation
        hue = rand(-hue, hue)
        sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
        val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
        x = rgb_to_hsv(np.array(image) / 255.)
        x[..., 0] += hue
        x[..., 0][x[..., 0] > 1] -= 1
        x[..., 0][x[..., 0] < 0] += 1
        x[..., 1] *= sat
        x[..., 2] *= val
        x[x > 1] = 1
        x[x < 0] = 0
        image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

        # 5.correct box
        box_data = np.zeros((max_boxes, 5))
        if len(box) > 0:
            np.random.shuffle(box)
            box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
            box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
            if flip:
                box[:, [0, 2]] = w - box[:, [2, 0]]
            box[:, 0:2][box[:, 0:2] < 0] = 0
            box[:, 2][box[:, 2] > w] = w
            box[:, 3][box[:, 3] > h] = h
            box_w = box[:, 2] - box[:, 0]
            box_h = box[:, 3] - box[:, 1]
            box = box[np.logical_and(box_w > 1,
                                     box_h > 1)]  # discard invalid box
            if len(box) > max_boxes:
                box = box[:max_boxes]
            box_data[:len(box)] = box

        boxes_data.append(box_data)
        imgs.append(image_data)

    # 6.crop image
    merge_img = np.zeros(shape=(h, w, 3))
    cut_x = int(w * rand(a=min_x, b=1 - min_x))
    cut_y = int(h * rand(a=min_y, b=1 - min_y))
    # left-top
    y1 = int(rand(0.0, h - cut_y))
    x1 = int(rand(0.0, h - cut_x))
    merge_img[:cut_y, :cut_x, :] = imgs[0][y1:y1 + cut_y, x1:x1 + cut_x, :]
    # right-top
    x2 = int(rand(0.0, cut_x))
    y2 = int(rand(0.0, h - cut_y))
    merge_img[:cut_y, cut_x:, :] = imgs[1][y2:y2 + cut_y, x2:x2 + w - cut_x, :]
    # left-bottom
    x3 = int(rand(0.0, h - cut_x))
    y3 = int(rand(0.0, cut_y))
    merge_img[cut_y:, :cut_x, :] = imgs[2][y3:y3 + h - cut_y, x3:x3 + cut_x, :]
    # right-bottom
    x4 = int(rand(0.0, cut_x))
    y4 = int(rand(0.0, cut_y))
    merge_img[cut_y:, cut_x:, :] = imgs[3][y4:y4 + h - cut_y,
                                           x4:x4 + w - cut_x]

    # 7.correct boxes
    valid_box = []
    box_data_1 = boxes_data[0]
    if len(box_data_1) > 0:
        point_11 = (0, 0)
        point_12 = (x1, y1)
        point_13 = (x1 + cut_x, y1 + cut_y)
        valid_box_1 = crop_boxes_v2(box_data_1, point_11, point_12, point_13)
        valid_box.append(valid_box_1)

    box_data_2 = boxes_data[1]
    if len(box_data_2) > 0:
        point_21 = (cut_x, 0)
        point_22 = (x2, y2)
        point_23 = (x2 + w - cut_x, y2 + cut_y)
        valid_box_2 = crop_boxes_v2(box_data_2, point_21, point_22, point_23)
        valid_box.append(valid_box_2)

    box_data_3 = boxes_data[2]
    if len(box_data_3) > 0:
        point_31 = (0, cut_y)
        point_32 = (x3, y3)
        point_33 = (x3 + cut_x, y3 + h - cut_y)
        valid_box_3 = crop_boxes_v2(box_data_3, point_31, point_32, point_33)
        valid_box.append(valid_box_3)

    box_data_4 = boxes_data[3]
    if len(box_data_4) > 0:
        point_41 = (cut_x, cut_y)
        point_42 = (x4, y4)
        point_43 = (x4 + w - cut_x, y4 + h - cut_y)
        valid_box_4 = crop_boxes_v2(box_data_4, point_41, point_42, point_43)
        valid_box.append(valid_box_4)

    # TODO: fix this bug
    # TODO: if valid_box=[], np.concatenate(valid_box, axis=0) will have errors
    valid_box = np.concatenate(valid_box, axis=0)

    boxes = np.zeros(shape=(max_boxes, 5), dtype='float32')
    num_boxes = len(valid_box)
    if num_boxes <= max_boxes:
        boxes[0:num_boxes] = valid_box
    else:
        boxes = valid_box[:max_boxes]

    return merge_img, boxes
def rgb_to_hsv(image):
    new_image = colors.rgb_to_hsv(image)
    return new_image
Esempio n. 58
0
def get_random_data(annotation_line,
                    input_shape,
                    random=True,
                    max_boxes=20,
                    jitter=.3,
                    hue=.1,
                    sat=1.5,
                    val=1.5,
                    proc_img=True):
    '''random preprocessing for real-time data augmentation'''
    line = annotation_line.split()
    image = Image.open(line[0])
    iw, ih = image.size  #原图大小
    h, w = input_shape  #416,416
    box = np.array([
        np.array(list(map(int, box.split(',')))) for box in line[1:]
    ])  #(目标个数,5)

    if not random:  #如果random==False,则按照长边到416的原则等比例缩放,然后放到416*416的灰度图中央
        # resize image
        scale = min(w / iw, h / ih)
        nw = int(iw * scale)
        nh = int(ih * scale)
        dx = (w - nw) // 2
        dy = (h - nh) // 2
        image_data = 0
        if proc_img:
            image = image.resize((nw, nh), Image.BICUBIC)
            new_image = Image.new('RGB', (w, h), (128, 128, 128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image) / 255.

        # correct boxes
        box_data = np.zeros((max_boxes, 5))
        if len(box) > 0:
            np.random.shuffle(box)
            if len(box) > max_boxes: box = box[:max_boxes]
            box[:, [0, 2]] = box[:, [0, 2]] * scale + dx
            box[:, [1, 3]] = box[:, [1, 3]] * scale + dy
            box_data[:len(box)] = box

        return image_data, box_data

    # resize image
    new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(
        1 - jitter, 1 + jitter)  #定义新的宽高比,rand返回最大为1+jitter,最小为1-jitter
    scale = rand(.25, 2)
    if new_ar < 1:
        nh = int(scale * h)  #在416高度上放大或缩小,比例0.25到2之间
        nw = int(nh * new_ar)  #根据新的宽高比缩放宽度
    else:
        nw = int(scale * w)
        nh = int(nw / new_ar)
    image = image.resize((nw, nh), Image.BICUBIC)

    # place image
    dx = int(rand(0, w - nw))  #有可能为负数
    dy = int(rand(0, h - nh))
    new_image = Image.new('RGB', (w, h), (128, 128, 128))  #生成灰度图像
    new_image.paste(
        image,
        (dx, dy))  #将resize图像,随机放在灰度图像某个位置,有可能小于416,则整图贴,有可能新图像大于416,导致只显示原图一部分
    image = new_image

    # flip image or not
    flip = rand() < .5
    if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)

    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
    #rgb转hsv加深颜色信息
    x = rgb_to_hsv(np.array(image) / 255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1
    # correct boxes,对label数据根据图像的size变化进行修改
    box_data = np.zeros((max_boxes, 5))
    if len(box) > 0:
        np.random.shuffle(box)
        box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
        box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
        if flip: box[:, [0, 2]] = w - box[:, [2, 0]]
        box[:, 0:2][box[:, 0:2] < 0] = 0
        box[:, 2][box[:, 2] > w] = w
        box[:, 3][box[:, 3] > h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w > 1, box_h > 1)]  # discard invalid box
        if len(box) > max_boxes: box = box[:max_boxes]
        box_data[:len(box)] = box

    return image_data, box_data
Esempio n. 59
0
def get_random_data(image_dir,
                    target_dir,
                    image_name,
                    input_shape=(320, 320),
                    max_boxes=20,
                    jitter=.3,
                    hue=.1,
                    sat=1.5,
                    val=1.5,
                    proc_img=True):
    '''random preprocessing for real-time data augmentation'''
    for i in range(50):
        image = Image.open(image_dir)
        # image.show()
        iw, ih = image.size
        h, w = input_shape

        # # 读取基于原图大小的box
        # # 随机变化图像大小
        # # resize image
        # # w/h比例随机变化
        # new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)
        # # 以input为基础,输入框的尺度随机变化
        # scale = rand(.25, 2)
        # # 注意,下式首先变化长或宽,之后再以此为基础,再以新的w/h得到另一边的长度
        # if new_ar < 1:
        #     nh = int(scale*h)
        #     nw = int(nh*new_ar)
        # else:
        #     nw = int(scale*w)
        #     nh = int(nw/new_ar)
        # # 将图像resize到新的大小
        # print('nw:', nw, ' nh:', nh)
        # image = image.resize((nw,nh), Image.BICUBIC)
        # # image.show()
        #
        # # place image
        # # 随机寻找一点,作为图像crop的起点,保证crop的图像不会越界
        # dx = int(rand(0, w-nw))
        # dy = int(rand(0, h-nh))
        # print('dx:', dx, ' dy:', dy)
        # # Image.new最后一个参数是对三个通道进行赋值,作为初始化
        # new_image = Image.new('RGB', (w,h), (128,128,128))
        # # 只截取图像的一部分,和上面代码结合相当于随机缩放图像,(dx, dy)定义左上点
        # new_image.paste(image, (dx, dy))
        # image = new_image
        # # image.show()
        #
        # image.save(os.path.join(target_dir, 'scale_' + str(i) + '_' + image_name))

        hue = rand(-hue, hue)
        sat = rand(1, sat) if rand() < .5 else 1 / rand(1, sat)
        val = rand(1, val) if rand() < .5 else 1 / rand(1, val)
        # 这里归一化到[0,1]!!!!!!!!!!!!!!!!!!!!!
        x = rgb_to_hsv(np.array(image) / 255.)
        x[..., 0] += hue
        x[..., 0][x[..., 0] > 1] -= 1  ### 还有这种操作!!!!!!!!!!!!!!!
        x[..., 0][x[..., 0] < 0] += 1
        x[..., 1] *= sat
        x[..., 2] *= val
        x[x > 1] = 1  ### 还有这种操作!!!!!!!!!!!!!!!
        x[x < 0] = 0
        image_data = hsv_to_rgb(x)
        image_data *= 255.
        image = Image.fromarray(image_data.astype('uint8')).convert('RGB')
        image.save(os.path.join(target_dir,
                                'hsv_' + str(i) + '_' + image_name))
Esempio n. 60
0
def get_random_data(
    annotation_line,
    input_shape,
    random=True,
    max_boxes=20,
    jitter=0.3,
    hue=0.1,
    sat=1.5,
    val=1.5,
    proc_img=True,
):
    """random preprocessing for real-time data augmentation"""

    # This type of splitting makes sure that it is compatible with spaces in folder names
    # We split at the first space that is followed by a number
    tmp_split = re.split("( \d)", annotation_line, maxsplit=1)
    if len(tmp_split) > 2:
        line = tmp_split[0], tmp_split[1] + tmp_split[2]
    else:
        line = tmp_split
    # line[0] contains the filename
    image = Image.open(line[0])
    # The rest of the line includes bounding boxes
    line = line[1].split(" ")
    iw, ih = image.size
    h, w = input_shape
    box = np.array([np.array(list(map(int, box.split(",")))) for box in line[1:]])

    if not random:
        # resize image
        scale = min(w / iw, h / ih)
        nw = int(iw * scale)
        nh = int(ih * scale)
        dx = (w - nw) // 2
        dy = (h - nh) // 2
        image_data = 0
        if proc_img:
            image = image.resize((nw, nh), Image.BICUBIC)
            new_image = Image.new("RGB", (w, h), (128, 128, 128))
            new_image.paste(image, (dx, dy))
            image_data = np.array(new_image) / 255.0

        # correct boxes
        box_data = np.zeros((max_boxes, 5))
        if len(box) > 0:
            np.random.shuffle(box)
            if len(box) > max_boxes:
                box = box[:max_boxes]
            box[:, [0, 2]] = box[:, [0, 2]] * scale + dx
            box[:, [1, 3]] = box[:, [1, 3]] * scale + dy
            box_data[: len(box)] = box

        return image_data, box_data

    # resize image
    new_ar = w / h * rand(1 - jitter, 1 + jitter) / rand(1 - jitter, 1 + jitter)
    scale = rand(0.25, 2)
    if new_ar < 1:
        nh = int(scale * h)
        nw = int(nh * new_ar)
    else:
        nw = int(scale * w)
        nh = int(nw / new_ar)
    image = image.resize((nw, nh), Image.BICUBIC)

    # place image
    dx = int(rand(0, w - nw))
    dy = int(rand(0, h - nh))
    new_image = Image.new("RGB", (w, h), (128, 128, 128))
    new_image.paste(image, (dx, dy))
    image = new_image

    # flip image or not
    #flip = rand() < 0.5
    #if flip:
    #    image = image.transpose(Image.FLIP_LEFT_RIGHT)

    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand() < 0.5 else 1 / rand(1, sat)
    val = rand(1, val) if rand() < 0.5 else 1 / rand(1, val)
    x = rgb_to_hsv(np.array(image) / 255.0)
    x[..., 0] += hue
    x[..., 0][x[..., 0] > 1] -= 1
    x[..., 0][x[..., 0] < 0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x > 1] = 1
    x[x < 0] = 0
    image_data = hsv_to_rgb(x)  # numpy array, 0 to 1

    # make gray
    gray = rand() < 0.2
    if gray:
        image_gray = np.dot(image_data, [0.299, 0.587, 0.114])
        # a gray RGB image is GGG
        image_data = np.moveaxis(np.stack([image_gray, image_gray, image_gray]), 0, -1)

    # invert colors
    invert = rand() < 0.1
    if invert:
        image_data = 1.0 - image_data

    # correct boxes
    box_data = np.zeros((max_boxes, 5))
    if len(box) > 0:
        np.random.shuffle(box)
        box[:, [0, 2]] = box[:, [0, 2]] * nw / iw + dx
        box[:, [1, 3]] = box[:, [1, 3]] * nh / ih + dy
        #if flip:
        #    box[:, [0, 2]] = w - box[:, [2, 0]]
        box[:, 0:2][box[:, 0:2] < 0] = 0
        box[:, 2][box[:, 2] > w] = w
        box[:, 3][box[:, 3] > h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w > 1, box_h > 1)]  # discard invalid box
        if len(box) > max_boxes:
            box = box[:max_boxes]
        box_data[: len(box)] = box

    return image_data, box_data