本文整理汇总了Python中skimage.color.gray2rgb函数的典型用法代码示例。如果您正苦于以下问题:Python gray2rgb函数的具体用法?Python gray2rgb怎么用?Python gray2rgb使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了gray2rgb函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_image
def set_image(self, params, images, background):
""" creates a strip with the cell in different images
images is a list of rgb images
background is a grayscale image to use for the masks
"""
x0, y0, x1, y1 = self.box
img = color.gray2rgb(np.zeros((x1 - x0 + 1, (len(images) + 4) * (y1 - y0 + 1))))
bx0 = 0
bx1 = x1 - x0 + 1
by0 = 0
by1 = y1 - y0 + 1
for im in images:
img[bx0:bx1, by0:by1] = im[x0 : x1 + 1, y0 : y1 + 1]
by0 = by0 + y1 - y0 + 1
by1 = by1 + y1 - y0 + 1
perim = self.perim_mask
axial = self.sept_mask
cyto = self.cyto_mask
img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * self.cell_mask)
by0 = by0 + y1 - y0 + 1
by1 = by1 + y1 - y0 + 1
img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * perim)
by0 = by0 + y1 - y0 + 1
by1 = by1 + y1 - y0 + 1
img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * cyto)
if params.find_septum:
by0 = by0 + y1 - y0 + 1
by1 = by1 + y1 - y0 + 1
img[bx0:bx1, by0:by1] = color.gray2rgb(background[x0 : x1 + 1, y0 : y1 + 1] * axial)
self.image = img_as_int(img)
示例2: loadPictures
def loadPictures(data):
N_hieros = len(data)
repertory, file = data['anchor'][0].split("_")
label=str(data['label'][0])
picture="/Users/fgimbert/Documents/Dataset/Manual/Preprocessed/"+str(repertory)+"/"+str(file)+"_"+label+".png"
#im = Image.open(picture)
#img_x=im.size[0]
#img_y=im.size[1]
img_x=50
img_y=75
anchor, positive, negative = np.zeros((N_hieros,img_x*img_y,3)),np.zeros((N_hieros,img_x*img_y,3)),np.zeros((N_hieros,img_x*img_y,3))
labels_true = []
labels_wrong= []
for index, row in data.iterrows():
repertory, file = row['anchor'].split("_")
label = row['label']
picture = path + str(repertory) + "/" + str(
file) + "_" + str(label) + ".png"
labels_true.append(label)
img_gray = mpimg.imread(picture)
img_rgb=gray2rgb(img_gray)
anchor[index]=img_rgb.reshape(1,img_x*img_y,3)
repertory, file = row['positive'].split("_")
picture = path + str(repertory) + "/" + str(
file) + "_" + str(label) + ".png"
img_gray = mpimg.imread(picture)
img_rgb = gray2rgb(img_gray)
positive[index] = img_rgb.reshape(1, img_x * img_y, 3)
repertory, file = row['negative'].split("_")
label = row['neg_label']
picture = path + str(repertory) + "/" + str(
file) + "_" + str(label) + ".png"
labels_wrong.append(label)
img_gray = mpimg.imread(picture)
img_rgb = gray2rgb(img_gray)
negative[index] = img_rgb.reshape(1, img_x * img_y, 3)
return [anchor,positive,negative],labels_true,labels_wrong
示例3: _apply
def _apply(self, imgmsg, maskmsg):
bridge = cv_bridge.CvBridge()
img = bridge.imgmsg_to_cv2(imgmsg)
if img.ndim == 2:
img = gray2rgb(img)
mask = bridge.imgmsg_to_cv2(maskmsg, desired_encoding='mono8')
mask = mask.reshape(mask.shape[:2])
mask = gray2rgb(mask)
# compute label
roi = closed_mask_roi(mask)
roi_labels = masked_slic(img=img[roi], mask=mask[roi],
n_segments=20, compactness=30)
if roi_labels is None:
return
labels = np.zeros(mask.shape, dtype=np.int32)
# labels.fill(-1) # set bg_label
labels[roi] = roi_labels
if self.is_debugging:
# publish debug slic label
slic_labelmsg = bridge.cv2_to_imgmsg(labels)
slic_labelmsg.header = imgmsg.header
self.pub_slic.publish(slic_labelmsg)
# compute rag
g = rag_solidity(labels, connectivity=2)
if self.is_debugging:
# publish debug rag drawn image
rag_img = draw_rag(labels, g, img)
rag_img = img_as_uint(rag_img)
rag_imgmsg = bridge.cv2_to_imgmsg(
rag_img.astype(np.uint8), encoding='rgb8')
rag_imgmsg.header = imgmsg.header
self.pub_rag.publish(rag_imgmsg)
# merge rag with solidity
merged_labels = merge_hierarchical(
labels, g, thresh=1, rag_copy=False,
in_place_merge=True,
merge_func=_solidity_merge_func,
weight_func=_solidity_weight_func)
merged_labels += 1
merged_labels[mask == 0] = 0
merged_labelmsg = bridge.cv2_to_imgmsg(merged_labels.astype(np.int32))
merged_labelmsg.header = imgmsg.header
self.pub.publish(merged_labelmsg)
if self.is_debugging:
out = label2rgb(merged_labels, img)
out = (out * 255).astype(np.uint8)
out_msg = bridge.cv2_to_imgmsg(out, encoding='rgb8')
out_msg.header = imgmsg.header
self.pub_label.publish(out_msg)
示例4: overlay_cells
def overlay_cells(cells, image, colors):
"Overlay the edges of each individual cell in the provided image"
tmp = color.gray2rgb(image)
for k in cells.keys():
c = cells[k]
if c.selection_state == 1:
col = colors[c.color_i][:3]
for px in c.outline:
x, y = px
tmp[x, y] = col
if c.sept_mask is not None:
try:
x0, y0, x1, y1 = c.box
tmp[x0:x1, y0:y1] = mark_boundaries(tmp[x0:x1, y0:y1],
img_as_int(
c.sept_mask),
color=col)
except IndexError:
c.selection_state = -1
return tmp
示例5: any2rgb
def any2rgb(array, name=''):
""" Returns a normalized float 3-channel array regardless of original
dtype and channel. All valid pyparty images must pass this
name : str
Name of array which will be referenced in logger messages"""
if not isinstance(array, np.ndarray):
return to_normrgb(array)
# *****
# Quick way to convert to float (don't use img_as_float becase we want
# to enforce that upperlimit of 255 is checked
array = array / 1.0
# Returns scalar for 1-channel OR 3-channel
if array.max() > 1:
# For 8-bit, divide by 255!
if array.max() > COLORTYPE[1]:
raise ColorError("Only 8bit ints are supported for now")
array = array / COLORTYPE[1]
if array.ndim == 3:
# If RGBA
if array.shape[2] == 4:
array = array[..., 0:3]
logger.warn("4-channel RGBA recieved; ignoring A channel")
return array
elif array.ndim == 2:
logger.warn('%s color has been converted (1-channel to 3-channel RGB)'
% name)
return skcol.gray2rgb(array)
raise ColorError('%s must be 2 or 3 dimensional array!' % name )
示例6: main
def main(args):
"""
Entry point.
"""
# load the image
img = imread(args.input)
if img.ndim == 2:
img = gray2rgb(img)
elif img.shape[2] == 4:
img = img[:, :, :3]
upper_dim = max(img.shape[:2])
if upper_dim > args.max_dim:
img = rescale(img, args.max_dim/float(upper_dim), order=3)
# compute saliency
start = timeit.default_timer()
img_sal = compute_saliency(img)
runtime = timeit.default_timer() - start
print("Took {0} seconds.".format(runtime))
# save image
(fname, ext) = os.path.splitext(args.input)
out_path = fname + "_saliency" + ext
imsave(out_path, img_sal)
示例7: prep_image
def prep_image(im, IMAGE_W, IMAGE_H, BGR=BGR, bw=False):
if len(im.shape) == 2:
im = im[:, :, np.newaxis]
im = np.repeat(im, 3, axis=2)
h, w, _ = im.shape
if h*IMAGE_W < w*IMAGE_H:
im = skimage.transform.resize(im, (IMAGE_H, w*IMAGE_H//h), preserve_range=True)
else:
im = skimage.transform.resize(im, (h*IMAGE_W//w, IMAGE_W), preserve_range=True)
# Central crop
h, w, _ = im.shape
im = im[h//2-IMAGE_H//2:h//2+IMAGE_H-IMAGE_H//2, w//2-IMAGE_W//2: w//2-IMAGE_W//2 +IMAGE_W]
rawim = im.astype('uint8')
# Shuffle axes to c01
if bw:
if BGR:
im = im[:,:,::-1]
bwim = gray2rgb(rgb2gray(im))
im = (bwim*bw+im.astype("float64")*(1.-bw))
if BGR:
im = im[:,:,::-1]
im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1)
# Convert RGB to BGR
if not BGR:
im = im[::-1, :, :]
im = im - MEAN_VALUES
return rawim, floatX(im[np.newaxis])
示例8: add_rectangle
def add_rectangle(img, y0, x0, y1, x1, color="r", width=1):
"""Colors: 'r', 'g', 'b', 'w', 'k'"""
im = np.copy(img)
if im.ndim == 2:
im = gray2rgb(im)
max_val = 1
if np.max(img) > 1:
max_val = 255
channel = 3 # Bogus value when color = 'w' or 'k'
if color == "r":
channel = 0
if color == "g":
channel = 1
if color == "b":
channel = 2
for i in range(width):
yy0 = y0 + i
xx0 = x0 + i
yy1 = y1 - i
xx1 = x1 - i
rr, cc = line(yy0, xx0, yy1, xx0) # left
im = paint_line(im, rr, cc, color, channel, max_val)
rr, cc = line(yy1, xx0, yy1, xx1) # bottom
im = paint_line(im, rr, cc, color, channel, max_val)
rr, cc = line(yy1, xx1, yy0, xx1) # right
im = paint_line(im, rr, cc, color, channel, max_val)
rr, cc = line(yy0, xx1, yy0, xx0) # top
im = paint_line(im, rr, cc, color, channel, max_val)
return im
示例9: vis_col_im
def vis_col_im(im, gt):
indices_0 = np.where(gt == 0) # nothing
indices_1 = np.where(gt == 1) # necrosis
indices_2 = np.where(gt == 2) # edema
indices_3 = np.where(gt == 3) # non-enhancing tumor
indices_4 = np.where(gt == 4) # enhancing tumor
im = np.asarray(im, dtype='float32')
im = im*1./im.max()
rgb_image = color.gray2rgb(im)
m0 = [1., 1., 1.]
m1 = [1., 0., 0.]
m2 = [0.2, 1., 0.2]
m3 = [1., 1., 0.2]
m4 = [1., 0.6, 0.2]
im = rgb_image.copy()
im[indices_0[0], indices_0[1], :] *= m0
im[indices_1[0], indices_1[1], :] *= m1
im[indices_2[0], indices_2[1], :] *= m2
im[indices_3[0], indices_3[1], :] *= m3
im[indices_4[0], indices_4[1], :] *= m4
plt.imshow(im)
plt.show()
plt.close()
示例10: process_image
def process_image(sub_folder, image, img_rows, img_cols):
'''Process individual images'''
# Using the mean pixel values used in VGG16 Model
mean_pixel = [103.939, 116.779, 123.68]
image_path = os.path.join(sub_folder, image)
image_file = imread(image_path)
# If the image has 4 channels, change it to 3 channels
if len(image_file.shape) > 2 and image_file.shape[2] == 4:
image_file = image_file[:,:,:-1]
# If the image is in grey scale, change it to RGB
if len(image_file.shape) < 3:
image_file = gray2rgb(image_file)
image_file = image_file.astype(np.float32, copy=False)
# There are some images where the actual image we are interested is in
# the right side. For such images remove the left half
if image_file.shape[1] > image_file.shape[0]:
new_shape = image_file.shape[1] / 2
image_file = image_file[:,new_shape:,:]
# one more image pattern
elif image_file.shape[0] == 1000 & image_file.shape[1] == 677:
image_file = image_file[:,:455,:]
image_resized = imresize(image_file, (img_rows, img_cols))
# normalize the image
for c in xrange(3):
image_resized[:, :, c] = image_resized[:, :, c] - mean_pixel[c]
image_res = image_resized.transpose((2,0,1))
return image_res
示例11: markPath
def markPath(mat, path, mark_as='red'):
assert mark_as in ['red','green','blue','black','white']
if len(mat.shape) == 2:
mat = color.gray2rgb(mat)
ret = np.zeros(mat.shape)
ret[:,:,:] = mat[:,:,:]
# Preprocess image
if np.max(ret) < 1.1 or np.max(ret) > 256: # matrix is in float numbers
ret -= np.min(ret)
ret /= np.max(ret)
ret *= 256
# Determinate components
if mark_as == 'red':
r,g,b = 255,0,0
elif mark_as == 'green':
r,g,b = 0,255,0
elif mark_as == 'blue':
r,g,b = 0,0,255
elif mark_as == 'white':
r,g,b = 255,255,255
elif mark_as == 'black':
r,b,b = 0,0,0
# Place R,G,B
for i in path:
ret[i[0],i[1],0] = r
ret[i[0],i[1],1] = g
ret[i[0],i[1],2] = b
return ret.astype('uint8')
示例12: load_images
def load_images(path):
print 'reading file names ... '
names = [d for d in os.listdir (path) if d.endswith('.jpg')]
names = natsorted(names)
num_rows = len(names)
print names
print 'making dataset ... '
test_image = np.zeros((num_rows, num_features), dtype = float)
label = np.zeros((num_rows, 1), dtype = int)
file_names = []
i = 0
for n in names:
print n.split('.')[0]
image = imread(os.path.join(path, n))
if len(image.shape) == 3 and image.shape[2] == 3:
image = image.transpose(2, 0, 1)
test_image[i, 0:num_features] = np.reshape(image, (1, num_features))
label[i] = n.split('.')[0]
i += 1
else:
image = gray2rgb(image)
image = image.transpose(2, 0, 1)
test_image[i, 0:num_features] = np.reshape(image, (1, num_features))
label[i] = n.split('.')[0]
i += 1
return test_image, label
示例13: imreadconvert
def imreadconvert(Xname):
X=imread(Xname).astype(np.float32)
if len(X.shape)==3:
X=X.transpose(2,0,1)
return X
else:
return gray2rgb(X).transpose(2,0,1)
示例14: get_image
def get_image(fname):
arr = io.imread(fname)
if arr.ndim == 2:
arr = color.gray2rgb(arr)
arr = util.img_as_float(arr)
assert arr.ndim == 3
return arr
示例15: slics_3D
def slics_3D(im, pseudo_3D=True, n_segments=100, get_slicewise=False):
if im.ndim != 3:
raise Exception('3D image is needed.')
if not pseudo_3D:
# need to convert to RGB image
im_rgb = np.zeros((im.shape[0], im.shape[1], im.shape[2], 3))
im_rgb[:,:,:,0] = im
im_rgb[:,:,:,1] = im
im_rgb[:,:,:,2] = im
suppxls = skiseg.slic(im_rgb, n_segments=n_segments, spacing=(2,1,1))
else:
suppxls = np.zeros(im.shape)
if get_slicewise:
suppxls_slicewise = np.zeros(im.shape)
offset = 0
for i in range(im.shape[0]):
# suppxl = skiseg.slic(cv2.cvtColor(im[i,:,:], cv2.COLOR_GRAY2RGB), n_segments=n_segments)
suppxl = skiseg.slic(skicol.gray2rgb(im[i,:,:]), n_segments=n_segments)
suppxls[i,:,:] = suppxl + offset
if get_slicewise:
suppxls_slicewise[i,:,:] = suppxl
offset = suppxls.max() + 1
if get_slicewise:
return suppxls, suppxls_slicewise
else:
return suppxls