def colorization(img, marked_img, k): H, W, C = img.shape # convert to YUV color space img_yuv = rgb2yuv(img) marked_yuv = rgb2yuv(marked_img) Y = np.array(img_yuv[:, :, 0]) # find the color position and update sparse matrix diff = np.where(marked_yuv != img_yuv) colored_coord_idx = np.unique(diff[0] * W + diff[1]) weight_matrix = build_weights_matrix(Y, colored_coord_idx, k) # compute the least-square solution, by computing the pseudo-inverse LU = splu(weight_matrix) b1 = (marked_yuv[:, :, 1]).flatten() b2 = (marked_yuv[:, :, 2]).flatten() U = LU.solve(b1) V = LU.solve(b2) sol = np.zeros_like(img) sol[:, :, 0] = Y sol[:, :, 1] = U.reshape((H, W)) sol[:, :, 2] = V.reshape((H, W)) return sol
def ssim(ims1, ims2, scale): mean_ssim = 0.0 for im1, im2 in zip(ims1, ims2): im1 = color.rgb2yuv(im1[scale:-scale, scale:-scale])[..., 0] im2 = color.rgb2yuv(im2[scale:-scale, scale:-scale])[..., 0] mean_ssim += measure.compare_ssim(im1, im2) / len(ims1) return mean_ssim
def SSIM(img1, img2): [h, w, c] = img1.shape if c > 2: img1 = color.rgb2yuv(img1) img1 = img1[:, :, 0] img2 = color.rgb2yuv(img2) img2 = img2[:, :, 0] score = ssim(img1, img2) return score
def rmse(ims1, ims2, scale): mean_rmse = 0.0 for im1, im2 in zip(ims1, ims2): im1 = color.rgb2yuv(im1[scale:-scale, scale:-scale])[..., 0] im2 = color.rgb2yuv(im2[scale:-scale, scale:-scale])[..., 0] mean_rmse += np.sum(np.square(im2 - im1)) / len(ims1) mean_rmse = np.sqrt(mean_rmse) return mean_rmse
def test_yuv(self): rgb = np.array([[[1.0, 1.0, 1.0]]]) assert_array_almost_equal(rgb2yuv(rgb), np.array([[[1, 0, 0]]])) assert_array_almost_equal(rgb2yiq(rgb), np.array([[[1, 0, 0]]])) assert_array_almost_equal(rgb2ypbpr(rgb), np.array([[[1, 0, 0]]])) assert_array_almost_equal(rgb2ycbcr(rgb), np.array([[[235, 128, 128]]])) rgb = np.array([[[0.0, 1.0, 0.0]]]) assert_array_almost_equal(rgb2yuv(rgb), np.array([[[0.587, -0.28886916, -0.51496512]]])) assert_array_almost_equal(rgb2yiq(rgb), np.array([[[0.587, -0.27455667, -0.52273617]]])) assert_array_almost_equal(rgb2ypbpr(rgb), np.array([[[0.587, -0.331264, -0.418688]]])) assert_array_almost_equal(rgb2ycbcr(rgb), np.array([[[144.553, 53.797, 34.214]]]))
def test_yuv_roundtrip(self): img_rgb = img_as_float(self.img_rgb)[::16, ::16] assert_array_almost_equal(yuv2rgb(rgb2yuv(img_rgb)), img_rgb) assert_array_almost_equal(yiq2rgb(rgb2yiq(img_rgb)), img_rgb) assert_array_almost_equal(ypbpr2rgb(rgb2ypbpr(img_rgb)), img_rgb) assert_array_almost_equal(ycbcr2rgb(rgb2ycbcr(img_rgb)), img_rgb) assert_array_almost_equal(ydbdr2rgb(rgb2ydbdr(img_rgb)), img_rgb)
def gen_pencil_drawing(img, kernel_size, stroke_width=0, num_of_directions=8, smooth_kernel="gauss", gradient_method=0, rgb=False, w_group=0, pencil_texture_path="", stroke_darkness=1, tone_darkness=1): if not rgb: # Grayscale image: im = img else: # RGB image: yuv_img = color.rgb2yuv(img) im = yuv_img[:,:,0] # Generate the Stroke Map: S = gen_stroke_map(im, kernel_size, stroke_width=stroke_width, num_of_directions=num_of_directions, smooth_kernel=smooth_kernel, gradient_method=gradient_method) S = np.power(S, stroke_darkness) # Generate the Tone Map: J = gen_tone_map(im, w_group=w_group) # Read the pencil texture: if not pencil_texture_path: pencil_texture = io.imread('./pencils/pencil0.jpg', as_gray=True) else: pencil_texture = io.imread(pencil_texture_path, as_gray=True) # Generate the Pencil Texture Map: T = gen_pencil_texture(im, pencil_texture, J) T = np.power(T, tone_darkness) # The final Y channel: R = np.multiply(S, T) if not rgb: return R else: yuv_img[:,:,0] = R return exposure.rescale_intensity(color.yuv2rgb(yuv_img), in_range=(0, 1))
def loadSeq(self, cam,person): seqImgs = self.getSeqImgFiles(os.path.join(self.opt.dir_RGB,cam,person)) seqLen = len(seqImgs) for i in range(0,seqLen): filename = seqImgs[i] img = sio.imread(os.path.join(self.opt.dir_RGB,cam,person,filename)) * 1.0 img = rgb2yuv(resize(img, (64, 48), mode='reflect')) imgof = sio.imread(os.path.join(self.opt.dir_OF,cam,person,filename)) * 1.0 imgof = resize(imgof, (64, 48), mode='reflect') if i == 0: s = img.shape imagePixelData = torch.zeros((seqLen, 5, s[0], s[1]),dtype=torch.float) for c in range(0, 3): d = torch.from_numpy(img[:, :, c]) m = torch.mean(d, axis=(0, 1)) v = torch.std(d, axis=(0, 1)) d = d - m d = d / v imagePixelData[i, c, :, :] = d for c in range(0, 2): d = torch.from_numpy(imgof[:, :, c]) m = torch.mean(d, axis=(0, 1)) v = torch.std(d, axis=(0, 1)) d = d - m d = d / v imagePixelData[i, c + 3, :, :] = d return imagePixelData
def preprocess_image(x): if __GLOBAL_PARAMS__['NORMALIZATION']: x = (x - 128.0) / 128.0 if __GLOBAL_PARAMS__['YUV']: x = np.array([rgb2yuv(x.reshape((1,180,180,3)))]) x = x.reshape((3,180,180)) return x
def convert(colorspace, image): # Normalize data. image = image.astype('float32') / 255 #conver to HSL if colorspace == "HSL": image = convertHSL(image) #conver to HSV if colorspace == "HSV": image = color.rgb2hsv(image) #conver to XYZ if colorspace == "XYZ": image = color.rgb2xyz(image) #conver to LUV if colorspace == "LUV": image = color.rgb2luv(image) #conver to LAB if colorspace == "LAB": image = color.rgb2lab(image) #conver to YUV if colorspace == "YUV": image = color.rgb2yuv(image) print("finish") from scipy.misc import imsave imsave('test.png', image) return image
def moments(colln_imgs): ''' Function to compute color moments of list of images Input: List of length N, where N is the number of images Output: Numpy Array of size (N, M), where N is the number of images and M = 1728 ''' win_h, win_w = 100, 100 colln_mnts = [] for img in colln_imgs: img = rgb2yuv(img) img_h, img_w = img.shape[0], img.shape[1] mean, std_dev, skwn, mnts = [], [], [], [] for h in range(0, img_h-win_h+1, win_h): for w in range(0, img_w-win_w+1, win_w): win = img[h:h+win_h, w:w+win_w, :] mean.append([np.mean(win[:,:,0]), np.mean(win[:,:,1]), np.mean(win[:,:,2])]) std_dev.append([np.std(win[:,:,0]), np.std(win[:,:,1]), np.std(win[:,:,2])]) skwn.append([skew(win[:,:,0], axis=None), skew(win[:,:,1], axis=None), skew(win[:,:,2], axis=None)]) mnts += [mean, std_dev, skwn] colln_mnts.append(mnts) colln_mnts = np.array(colln_mnts) w,x,y,z = colln_mnts.shape colln_mnts = colln_mnts.reshape((w,x*y*z)) return colln_mnts
def loadSequenceImages(self, cameraDir, opticalflowDir, filesList): nImgs = len(filesList) for i, file in enumerate(filesList): filename = cameraDir + '/' + file filenameOF = opticalflowDir + '/' + file img = sio.imread(filename) * 1.0 img = rgb2yuv(resize(img, (64, 48), mode='reflect')) imgof = sio.imread(filenameOF) * 1.0 imgof = resize(imgof, (64, 48), mode='reflect') if i == 0: s = img.shape imagePixelData = np.zeros((nImgs, 5, s[0], s[1]), dtype=np.float64) for c in xrange(0, 3): d = img[:, :, c] d = d m = np.mean(d, axis=(0, 1)) v = np.std(d, axis=(0, 1)) d = d - m d = d / v imagePixelData[i, c, :, :] = d for c in xrange(0, 2): d = imgof[:, :, c] d = d m = np.mean(d, axis=(0, 1)) v = np.std(d, axis=(0, 1)) d = d - m d = d / v imagePixelData[i, c + 3, :, :] = d return imagePixelData
def preproc(data, normalize=False, flip=False, mean_image=None, outType='YUV'): data_size = data.shape[0] img_size = int(data.shape[1] / 3) if normalize: if mean_image is None: mean_image = np.mean(data) mean_image = mean_image / np.float32(255) data = (data - mean_image) / np.float32(255) data_RGB = np.dstack((data[:, :img_size], data[:, img_size:2 * img_size], data[:, 2 * img_size:])) data_RGB = data_RGB.reshape((data_size, int(np.sqrt(img_size)), int(np.sqrt(img_size)), 3)) if flip: data_RGB = data_RGB[0:data_size, :, :, :] data_RGB_flip = data_RGB[:, :, :, ::-1] data_RGB = np.concatenate((data_RGB, data_RGB_flip), axis=0) if outType == 'YUV': data_out = color.rgb2yuv(data_RGB) return data_out, data_RGB # returns YUV as 4D tensor and RGB as 4D tensor elif outType == 'LAB': data_out = color.rgb2lab(data_RGB) data_gray = color.rgb2gray(data_RGB)[:, :, :, None] return data_out, data_gray # returns LAB and grayscale as 4D tensor
def find_setups(init_img, show=False): """ Finds the 6 frames in an image by using the YUV colorspace Args: init_img: filename of first image show: boolean flag for plotting of identified frames Returns: List of frame corners coordinates """ setups = [] img = mpimg.imread(f'{init_img}') img = color.rgb2yuv(img)[:, :, 1] img = filters.gaussian(img, 10) t = filters.threshold_otsu(img) img = img > t label = measure.label(img) regions = measure.regionprops(label) avg = [] # detection might add small non-panel areas and these must be removed for r in regions: avg.append(r.area) avg = np.array(avg).mean() for r in regions: if r.area > 0.9*avg: setups.append([r.bbox[0]+10, r.bbox[2]-10, r.bbox[1]+10, r.bbox[3]-10]) if show: fig, ax = plt.subplots(figsize=(6, 6)) ax.imshow(img[r.bbox[0]+10:r.bbox[2]-10, r.bbox[1]+10:r.bbox[3]-10]) ax.grid(False) plt.show() return setups
def convert_from_rgb2yuv(x): device = x.device x = x.cpu() x = x.permute(0, 2, 3, 1).numpy() x = rgb2yuv(x) x = torch.from_numpy(x).float().permute(0, 3, 1, 2) x = x.to(device) return x
def preprocess_image(image): # image = tf.image.decode_png(image, channels=3) # image = tf.image.resize(image, [FLAGS.height, FLAGS.width]) # image_float = tf.image.convert_image_dtype(image, tf.float32) # image_yuv = tf.image.rgb_to_yuv(image_float) image = resize(image, (FLAGS.height, FLAGS.width)) image_yuv = rgb2yuv(image) return image_yuv
def __getitem__(self, index): img = Image.open(self.files[index]) yuv = rgb2yuv(img) y = yuv[..., 0] - 0.5 u_t = yuv[..., 1] / 0.43601035 v_t = yuv[..., 2] / 0.61497538 return torch.Tensor(np.expand_dims(y, axis=0)), torch.Tensor( np.stack([u_t, v_t], axis=0))
def __init__(self, img, criterion=None, min_nrows=4, min_ncols=4): self.img = img self.criterion = criterion if criterion is not None else lambda img: img.max() - img.min() > 0.25 self.root = None self.ychan = rgb2yuv(img)[:, :, 0] self.ychan = (self.ychan - self.ychan.min()) / (self.ychan.max() - self.ychan.min()) self.min_nrows = min_nrows self.min_ncols = min_ncols
def load_extra_data(outType='YUV'): names = np.array(glob.glob('extra/*.jpg')) files = np.array([imread(f) for f in names]) if outType == 'YUV': return color.rgb2yuv(files), files elif outType == 'LAB': return color.rgb2lab(files), color.rgb2gray(files)[:, :, :, None]
def scale_image(x): # Extract the Y component after converting to YUV x_yuv = rgb2yuv(x) x_y = x_yuv[:, :, 0] # Scale to input size for convnet temp = resize(x_y, (84, 84), interpolation=INTER_LINEAR) scipy.misc.toimage(temp).save('outfile.jpg') return resize(x_y, (84, 84), interpolation=INTER_LINEAR)
def preprocess(self, input): input = np.asarray(input) if time.time() - self.time > self.fps: self.time = time.time() self.vis.image(input.transpose((2, 0, 1)), win=0) input = resize(input, (self.args.im_size, self.args.im_size), mode='reflect') input = rgb2yuv(input)[:,:,0] return input
def get_data(vimagedata): from sklearn.feature_extraction.image import extract_patches vdataname = vimagedata.pop() data = img_as_float(imread(vdataname)) data = rgb2yuv(data)[:, :, 0:1] data_ik = extract_patches(data, (high_dim, high_dim, 1), (high_dim // 2, high_dim // 2, 1)).reshape([-1] + list((high_dim, high_dim, 1))) return data_ik
def pencil_draw_color(I, texture): if len(I.shape) == 2: return pencil_draw(I, texture) else: I_yuv = color.rgb2yuv(I) Y_gray = pencil_draw(I_yuv[:, :, 0], texture) I_yuv[:, :, 0] = Y_gray I_after = color.yuv2rgb(I_yuv) I_after = np.maximum(I_after, 0) I_after = np.minimum(I_after, 1) #I_ruv[:, :, 0] = 0.5 return I_after
def alterYUV(img): img = color.rgb2yuv(img) params = [] #Y img[:, :, 0] += randUnifC(-0.05, 0.05, params=params) #U img[:, :, 1] += randUnifC(-0.02, 0.02, params=params) #V img[:, :, 2] += randUnifC(-0.02, 0.02, params=params) # U & V channels can have negative values; clip only Y img[:, :, 0] = np.clip(img[:, :, 0], 0, 1.0) img = color.yuv2rgb(img) img = np.clip(img, 0, 1.0) return img
def inference(G, in_path, out_path): p = Image.open(in_path).convert('RGB') W, H = p.size dest_yuv = rgb2yuv(p) dest_img = np.expand_dims(np.expand_dims(dest_yuv[..., 0], axis=0), axis=0) p.thumbnail((args.res, args.res)) img_yuv = rgb2yuv(p) #H,W,_ = img_yuv.shape print('size:' + str((p.size))) infimg = np.expand_dims(np.expand_dims(img_yuv[..., 0], axis=0), axis=0) img_variable = Variable(torch.Tensor(infimg - 0.5)) if args.gpu >= 0: img_variable = img_variable.cuda(args.gpu) res = G(img_variable) uv = res.cpu().detach().numpy() uv[:, 0, :, :] *= 0.436 uv[:, 1, :, :] *= 0.615 (_, _, H1, W1) = uv.shape print('size out:' + str((W1, H1))) uv = zoom(uv, (1, 1, H / H1, W / W1)) yuv = np.concatenate([dest_img, uv], axis=1)[0] rgb = yuv2rgb(yuv.transpose(1, 2, 0)) cv2.imwrite(out_path, (rgb.clip(min=0, max=1) * 256)[:, :, [2, 1, 0]])
def dir_data_generator(dir, batch_size, data_range=(0, 0), outType='YUV'): names = np.array(glob.glob(dir + '/*.jpg')) if data_range != (0, 0): names = names[data_range[0]:data_range[1]] batch_count = len(names) // batch_size while True: for i in range(0, batch_count): files = np.array([imread(f) for f in names[i * batch_size:i * batch_size + batch_size]]) if outType == 'YUV': yield color.rgb2yuv(files), files elif outType == 'LAB': yield color.rgb2lab(files), color.rgb2gray(files)[:, :, :, None]
def hog_feature(img, feature_vectore=True): """Return the HOG features based on YUV channels of the given image""" img = img_as_ubyte(color.rgb2yuv(img)) # parameters to tune orient = 9 pix_per_cell = 8 cell_per_block = 2 features = [feature.hog(img[:, :, channel], orientations=orient, pixels_per_cell=(pix_per_cell, pix_per_cell), cells_per_block=(cell_per_block, cell_per_block), feature_vector=feature_vectore) for channel in range(3)] if feature_vectore: return np.concatenate(features) else: return features
def test_yuv_roundtrip(self, channel_axis): img_rgb = img_as_float(self.img_rgb)[::16, ::16] img_rgb = np.moveaxis(img_rgb, source=-1, destination=channel_axis) assert_array_almost_equal( yuv2rgb(rgb2yuv(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb) assert_array_almost_equal( yiq2rgb(rgb2yiq(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb) assert_array_almost_equal( ypbpr2rgb(rgb2ypbpr(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb) assert_array_almost_equal( ycbcr2rgb(rgb2ycbcr(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb) assert_array_almost_equal( ydbdr2rgb(rgb2ydbdr(img_rgb, channel_axis=channel_axis), channel_axis=channel_axis), img_rgb)
def convolver_rgb(image, kernel, iterations=1): img_yuv = rgb2yuv(image) img_yuv[:, :, 0] = multi_convolver(img_yuv[:, :, 0], kernel, iterations) final_image = yuv2rgb(img_yuv) fig, ax = plt.subplots(1, 2, figsize=(17, 10)) ax[0].imshow(image) ax[0].set_title(f'Original', fontsize=20) ax[1].imshow(final_image) ax[1].set_title(f'YUV Adjusted, Iterations = {iterations}', fontsize=20) [axi.set_axis_off() for axi in ax.ravel()] fig.tight_layout() return final_image
def generate_layers_simple(config, back_list, objects_all, num_objects_list): max_objects = max(num_objects_list) num_objects = np.random.choice(num_objects_list) idx_back = np.random.randint(0, len(back_list)) back = back_list[idx_back] image_shape = back.shape obj_layers = np.zeros((max_objects, *image_shape), dtype=np.uint8) occ_masks = np.zeros((num_objects, *image_shape[:-1]), dtype=np.float) indices = np.random.randint(len(objects_all), size=num_objects) scales = np.random.uniform(config['range_scale'][0], config['range_scale'][1], size=num_objects) obj_images = [ rescale_image(objects_all[idx][0], scale) for idx, scale in zip(indices, scales) ] if 'range_intensity' in config: y_scales = np.random.uniform(config['range_intensity'][0], config['range_intensity'][1], size=num_objects) yuv_images = [rgb2yuv(image[..., :-1]) for image in obj_images] for image, y_scale in zip(yuv_images, y_scales): image[..., 0] *= y_scale rgb_images = [(np.clip(yuv2rgb(image), 0, 1) * 255).astype(np.uint8) for image in yuv_images] obj_images = [ np.concatenate([rgb, obj[..., -1:]], axis=-1) for rgb, obj in zip(rgb_images, obj_images) ] obj_classes = np.array([objects_all[idx][1] for idx in indices]) for idx, image in enumerate(obj_images): row1 = np.random.randint(image_shape[0] - image.shape[0] + 1) row2 = row1 + image.shape[0] col1 = np.random.randint(image_shape[1] - image.shape[1] + 1) col2 = col1 + image.shape[1] obj_layers[idx, row1:row2, col1:col2] = image occ_masks[idx, row1:row2, col1:col2] = 1 if config['overlap_bbox'] else image[..., -1] / 255 layers = np.concatenate([back[None], obj_layers]) layers = np.rollaxis(layers, -1, -3) classes = np.full(max_objects + 1, -1, dtype=np.int8) classes[1:obj_classes.shape[0] + 1] = obj_classes return layers, classes, occ_masks