How to cut image into red, green and blue channels using misc.imread

I am trying to crop an image in RGB and I have a problem with building these images. I get all the images from a specific folder using this function:

def get_images(path, image_type):
image_list = []
for filename in glob.glob(path + '/*'+ image_type):
    im=misc.imread(filename, mode='RGB')
    image_list.append(im)
return image_list

This function creates a 4d array (30, 1536, 2048, 3), and I'm sure that the first value represents the number of images, the second and third represent dimensions, and the third represent RGB values.

After I got all the images, I saved them as a numpy array

image_list = get_images('C:\HDR\images', '.jpg')
temp = np.array(image_list)

After that, I tried using simple slicing to get specific colors from these images:

red_images = temp[:,:,:,0]
green_images = temp[:,:,:,1]
blue_images = temp[:,:,:,2]

When I print out the values, everything seems beautiful.

print(temp[11,125,311,:])
print(red_images[11,125,311])
print(green_images[11,125,311])
print(blue_images[11,125,311])

And I get the following:

[105  97  76]
105
97
76

, , . matplotlib.pyplot.imshow , :

The image of the red channel

, :

 plt.imshow(temp[29,:,:,0])

, :

plt.imshow(temp[29,:,:,2])

:

Image error channel

. ?

+4
2

, matplotlib (.. ) " ".

imshow, , , , :

plt.imshow(image_slice, cmap=plt.cm.gray)

@mrGreenBrown , , misc.imread scipy, .. scipy.misc.imread. matplotlib.pyplot.

- . . , RGB, "" ( ) , . .

, Matplotlib , , "".

JPEG, 3 , R, G B . , , PGM, . , , 3 , , , .

plt.cm.gray cmap imshow " " . , (, ) , "" "".

, 3- 0.

, "":

# Assuming I is numpy array with 3 channels in RGB order
I_red = image.copy()  # Duplicate image
I_red[:, :, 1] = 0    # Zero out contribution from green
I_red[:, :, 2] = 0    # Zero out contribution from blue

stackoverflow .

+9

, RGB- ...

import matplotlib.pyplot as plt
from matplotlib.cbook import get_sample_data

image = plt.imread(get_sample_data('grace_hopper.jpg'))

titles = ['Grace Hopper', 'Red channel', 'Green channel', 'Blue channel']
cmaps = [None, plt.cm.Reds_r, plt.cm.Greens_r, plt.cm.Blues_r]

fig, axes = plt.subplots(1, 4, figsize=(13,3))
objs = zip(axes, (image, *image.transpose(2,0,1)), titles, cmaps)

for ax, channel, title, cmap in objs:
    ax.imshow(channel, cmap=cmap)
    ax.set_title(title)
    ax.set_xticks(())
    ax.set_yticks(())

plt.savefig('RGB1.png')

enter image description here , , , ...

- , . , ,

...
from numpy import array, zeros_like
def channel(image, color):
    if color not in (0, 1, 2): return image
    c = image[..., color]
    z = zeros_like(c)
    return array([(c, z, z), (z, c, z), (z, z, c)][color]).transpose(1,2,0)

, , ...

colors = range(-1, 3)
fig, axes = plt.subplots(1, 4, figsize=(13,3))
objs = zip(axes, titles, colors)
for ax, title, color in objs:
    ax.imshow(channel(image, color))
    ax.set_title(title)
    ax.set_xticks(())
    ax.set_yticks(())

plt.savefig('RGB2.png')

enter image description here , , , ( , ), ...

+2

All Articles