def create_image(model, x, y, r, z):
'''
create an image for the given latent vector z
'''
# create input vector
Z = np.repeat(z, x.shape[0]).reshape((-1,x.shape[0]))
X = np.concatenate([x, y, r, Z.T], axis=1)
pred = model.predict(X)
img = []
for k in range(pred.shape[1]):
yp = pred[:, k]
# if k == pred.shape[1]-1:
# yp = np.sin(yp)
yp = (yp - yp.min()) / (yp.max()-yp.min())
img.append(yp.reshape(y_dim, x_dim))
img = np.dstack(img)
if img.shape[-1] == 3:
from skimage.color import hsv2rgb
img = hsv2rgb(img)
return (img*255).astype(np.uint8)
python类hsv2rgb()的实例源码
def from_hsv(picture):
return color.hsv2rgb(picture)
def HSVAtoRGBA(image):
# print("image:", image)
hsv = image[:,:,:3]
# print("hsv:", hsv)
a = image[:,:,3:]
hsv = hsv.clip(0,1)
# print("hsvclip:", hsv)
a = a.clip(0,1)
# print("aclip:", hsv)
rgb = color.hsv2rgb(hsv)
a = a
rgba = np.concatenate((rgb,a),axis=2) * 255.0
# print("rgba:", hsv)
rgba = rgba.clip(0,255).astype(np.uint8)
return rgba
def add_hsv_noise(rgb, hue_offset, saturation_offset, value_offset, proba=0.5):
mask = np.all(rgb != 0, axis=2)
hsv = rgb2hsv(rgb)
if random.uniform(0, 1) > proba:
hsv[:, :, 0] = (hsv[:, :, 0] + random.uniform(-hue_offset, hue_offset)) % 1
if random.uniform(0, 1) > proba-0.1:
hsv[:, :, 1] = (hsv[:, :, 1] + random.uniform(-saturation_offset, saturation_offset)) % 1
if random.uniform(0, 1) > proba-0.1:
hsv[:, :, 2] = (hsv[:, :, 2] + random.uniform(-value_offset, value_offset)) % 1
rgb = hsv2rgb(hsv) * 255
return rgb.astype(np.uint8) * mask[:, :, np.newaxis]
def trans(self, img1, img2, img3):
rst = np.array((img1.T, img2.T, img3.T), dtype=np.float64)
rst /= 255.0
rst = color.hsv2rgb(rst.T)
rst *= 255
return rst.astype(np.uint8)
# ============= RGB - CIE ============
def get_overlayed_image(x, c, gray_factor_bg = 0.3):
'''
For an image x and a relevance vector c, overlay the image with the
relevance vector to visualise the influence of the image pixels.
'''
imDim = x.shape[0]
if np.ndim(c)==1:
c = c.reshape((imDim,imDim))
if np.ndim(x)==2: # this happens with the MNIST Data
x = 1-np.dstack((x, x, x))*gray_factor_bg # make it a bit grayish
if np.ndim(x)==3: # this is what happens with cifar data
x = color.rgb2gray(x)
x = 1-(1-x)*0.5
x = np.dstack((x,x,x))
alpha = 0.8
# Construct a colour image to superimpose
im = plt.imshow(c, cmap = cm.seismic, vmin=-np.max(np.abs(c)), vmax=np.max(np.abs(c)), interpolation='nearest')
color_mask = im.to_rgba(c)[:,:,[0,1,2]]
# Convert the input image and color mask to Hue Saturation Value (HSV) colorspace
img_hsv = color.rgb2hsv(x)
color_mask_hsv = color.rgb2hsv(color_mask)
# Replace the hue and saturation of the original image
# with that of the color mask
img_hsv[..., 0] = color_mask_hsv[..., 0]
img_hsv[..., 1] = color_mask_hsv[..., 1] * alpha
img_masked = color.hsv2rgb(img_hsv)
return img_masked
def TF_shift_hue(x, shift=0.0):
assert len(x.shape) == 3
h, w, nc = x.shape
hsv = rgb2hsv(x)
hsv[:,:,0] += shift
return hsv2rgb(hsv)
def _change_brightness(self, X):
X = color.rgb2hsv(X.transpose(1, 2, 0)/255.0)
diff = np.random.uniform(-1 * self._brightness, self._brightness)
X[:,:,2] = np.clip((X[:,:,2] + diff), 0.0, 1.0)
X = color.hsv2rgb(X).transpose(2, 0, 1) * 255.0
return X.astype(np.int32)