Scikit-Image Tutorial:画像はnumpyアレイの最終課題に挑戦

その買うを、もっとハッピーに。|ハピタス

このサイトにあるexercise(課題)に挑戦してみる。試しに、問題文をグーグル翻訳で訳してみたが、かなり完璧な訳になっているので驚かされた。グーグル翻訳は、AI効果によって、日々進歩しているような感じを受けた。

スポンサーリンク

課題:文字Hを描画する

Define a function that takes as input an RGB image and a pair of coordinates (row, column), and returns a copy with a green letter H overlaid at those coordinates. The coordinates point to the top-left corner of the H.
RGBイメージと座標のペア(行、列)を入力として受け取り、それらの座標に緑の文字Hを重ねたコピーを返す関数を定義せよ。 座標は、Hの左上隅を指す。

The arms and strut of the H should have a width of 3 pixels, and the H itself should have a height of 24 pixels and width of 20 pixels.
Hのアームとストラットの幅は3ピクセルで、H自体の高さは24ピクセル、幅は20ピクセルでなければならない。

Start with the following template:
次のテンプレートから始めること。

cd skimage-tutorials/lectures
/home/workspace/git/skimage-tutorials/lectures
plt.rcParams['figure.figsize'] = 20, 8
plt.rcParams["font.size"] = "17"
def draw_H(image, coords, color=(0, 128, 0)):
    out = image.copy()
    
    canvas = out[coords[0]:coords[0] + 24,
                 coords[1]:coords[1] + 20]
    
    canvas[:, :3] = color
    canvas[:, -3:] = color
    canvas[11:14] = color
    return out

Test your function like so:
次のように関数をテストする

from skimage import data
import matplotlib.pyplot as plt

cat = data.chelsea()
cat_H = draw_H(cat, (50, -50))
plt.imshow(cat_H);
スポンサーリンク

課題:RGBチャンネルを視覚化する

Display the different color channels of the image along (each as a gray-scale image). Start with the following template:
画像のカラーチャネルを個別に(それぞれグレースケール画像として)表示せよ。次のテンプレートから始めること。

# --- read in the image ---

image = plt.imread('../images/Bells-Beach.jpg')

# --- assign each color channel to a different variable ---

r = image[..., 0]
g = image[..., 1]
b = image[..., 2]

# --- display the image and r, g, b channels ---

f, axes = plt.subplots(1, 4, figsize=(16, 5))

for ax in axes:
    ax.axis('off')

(ax_r, ax_g, ax_b, ax_color) = axes
    
ax_r.imshow(r, cmap='gray')
ax_r.set_title('red channel')

ax_g.imshow(g, cmap='gray')
ax_g.set_title('green channel')

ax_b.imshow(b, cmap='gray')
ax_b.set_title('blue channel')

# --- Here, we stack the R, G, and B layers again
#     to form a color image ---
ax_color.imshow(np.stack([r, g, b], axis=2))
ax_color.set_title('all channels');

Now, take a look at the following R, G, and B channels. How would their combination look? (Write some code to confirm your intuition.)
ここで、次のR、G、およびBチャンネルを見てみましょう。 それらの組み合わせはどのように見えますか?(直観を確認するためのコードを何か書いてください。)

from skimage import draw
import numpy as np

red = np.zeros((300, 300))
green = np.zeros((300, 300))
blue = np.zeros((300, 300))

r, c = draw.circle(100, 100, 100)
red[r, c] = 1

r, c = draw.circle(100, 200, 100)
green[r, c] = 1

r, c = draw.circle(200, 150, 100)
blue[r, c] = 1

f, axes = plt.subplots(1, 3)
for (ax, channel) in zip(axes, [red, green, blue]):
    ax.imshow(channel, cmap='gray')
    ax.axis('off')
plt.imshow(np.stack([red, green, blue], axis=2));
スポンサーリンク

課題:グレースケールへの変換

The relative luminance of an image is the intensity of light coming from each point. Different colors contribute differently to the luminance: it’s very hard to have a bright, pure blue, for example. So, starting from an RGB image, the luminance is given by:
画像の相対輝度は、各点から来る光の強度で,色によって輝度に異なる影響を与える。例えば、明るい純粋な青を作成するのは非常に難しく、従って、RGBイメージから開始する場合、輝度は以下の式によって与えられる

$$
Y = 0.2126R + 0.7152G + 0.0722B
$$

Use Python 3.5’s matrix multiplication, @, to convert an RGB image to a grayscale luminance image according to the formula above.
Python 3.5の行列乗算@を使用して、上記の式に従ってRGB画像をグレースケールの輝度画像に変換します。

Compare your results to that obtained with skimage.color.rgb2gray.
結果をskimage.color.rgb2grayで取得した結果と比較します。

Change the coefficients to 1/3 (i.e., take the mean of the red, green, and blue channels, to see how that approach compares with rgb2gray).
係数を1/3に変更する(つまり、そのやり方がrgb2grayとどのように比較されるかを見るために赤、緑、青のチャネルの平均を取る)。

from skimage import io, color, img_as_float

image = img_as_float(io.imread('../images/balloon.jpg'))

gray = color.rgb2gray(image)
my_gray = image @ [0.2126, 0.7152, 0.0722]

# --- display the results ---

f, (ax0, ax1) = plt.subplots(1, 2, figsize=(10, 6))

ax0.imshow(gray, cmap='gray')
ax0.set_title('skimage.color.rgb2gray')

ax1.imshow(my_gray, cmap='gray')
ax1.set_title('my rgb2gray');
# Let's calculate the mean intensity error, 
# as compared to scikit-image's color.rgb2gray
# scikit-imageのcolor.rgb2grayと比べた時の、平均強度
# エラーを計算してみましょう

import numpy as np
np.mean(np.abs(gray - my_gray))
2.2436926652161234e-05
スポンサーリンク
スポンサーリンク