1、什幺是图像？

## 文末有惊喜哦！！学习资料文末点击拿走~

#### 2、NumPy和Skimage的图像基础

```from skimage.io import imread  # pip install scikit-image
>>> type(image)
numpy.ndarray```

NumPy数组带来灵活性、速度和力量。图像处理也不例外。

Ndarrays可以轻松检索图像的一般详细信息，例如图像的尺寸：

```>>> image.shape
(853, 1280, 3)
>>> image.ndim
3
# The number of pixels
>>> image.size  # 853 * 1280 * 3
3275520```

```red = image[:, :, 0]
compare(image, red, "Red Channel of the Image", cmap_type="Reds_r")```

```green = image[:, :, 1]
compare(image, green, "Green Channel of the Image", "Greens_r")```

```blue = image[:, :, 2]
compare(image, blue, "Blue Channel of the Image", "Blues_r")```

0表示红色，1表示绿色，2表示蓝色通道-非常简单。

`>>> show(image, axis=True)`

3、常见转换

```from skimage.color import rgb2gray
# Convert image to grayscale
gray = rgb2gray(image)
compare(image, gray, "Grayscale Image")```

```>>> gray.shape
(853, 1280)```

```kitten = imread("images/horizontal_flip.jpg")
horizontal_flipped = np.fliplr(kitten)
compare(kitten, horizontal_flipped, "Horizontally Flipped Image")```

```ball = imread("images/upside_down.jpg")
vertically_flipped = np.flipud(ball)
compare(ball, vertically_flipped, "Vertically Flipped Image")```

4、颜色通道直方图

```def plot_with_hist_channel(image, channel):
channels = ["red", "green", "blue"]
channel_idx = channels.index(channel)
color = channels[channel_idx]
extracted_channel = image[:, :, channel_idx]

fig, (ax1, ax2) = plt.subplots(
ncols=2, figsize=(18, 6)
)
ax1.imshow(image)
ax1.axis("off")

ax2.hist(extracted_channel.ravel(), bins=256, color=color)

ax2.set_title(f"{channels[channel_idx]} histogram")```

bin数量应该是256个，每个像素值对应一个-0表示黑色，255表示完全白色。

```colorful_scenery = imread("images/colorful_scenery.jpg")
plot_with_hist_channel(colorful_scenery, "red")```

`>>> plot_with_hist_channel(colorful_scenery, "green")`

`>>> plot_with_hist_channel(colorful_scenery, "blue")`

```gray_color_scenery = rgb2gray(colorful_scenery)
plt.hist(gray_color_scenery.ravel(), bins=256);```

### 过滤器

1、手动阈值

```stag = imread("images/binary_example.jpg")
>>> show(stag)```

```# Convert to graysacle
stag_gray = rgb2gray(stag)
>>> show(stag_gray)```

```>>> stag_gray.mean()
0.20056262759859955```

```# Set threshold
threshold = 0.35
# Binarize
binary_image = stag_gray > threshold
compare(stag, binary_image, "Binary image")```

```inverted_binary = stag_gray <= threshold
>>> compare(stag, inverted_binary, "Binary image inverted")```

2、阈值-全局

```flower = imread("images/global_threshold_ex.jpg")
flower_gray = rgb2gray(flower)
compare(flower, flower_gray)```

```from skimage.filters import try_all_threshold
fig, ax = try_all_threshold(
flower_gray, figsize=(10, 8), verbose=False
)```

`>>> show(flower)`

`>>> plt.hist(flower_gray.ravel(), bins=256);`

```spiral = imread("images/otsu_example.jpg")
spiral_gray = rgb2gray(spiral)
compare(spiral, spiral_gray)```

```from skimage.filters import threshold_otsu
# Find optimal threshold with `threshold_otsu`
threshold = threshold_otsu(spiral_gray)
# Binarize
binary_spiral = spiral_gray > threshold
compare(spiral, binary_spiral, "Binarized Image w. Otsu Thresholding")```

3、阈值-局部

```from skimage.filters import threshold_local
local_thresh = threshold_local(flower_gray, block_size=3, offset=0.0002)
binary_flower = flower_gray > local_thresh
compare(flower, binary_flower, "Tresholded flower image")```

```from skimage.filters import threshold_local
handwriting_gray = rgb2gray(handwriting)
# Find optimal threshold using local
local_thresh = threshold_local(handwriting_gray, offset=0.0003)
# Binarize
binary_handwriting = handwriting_gray > local_thresh
compare(handwriting, binary_handwriting,
"Binarized image with local thresholding")```

4、边缘检测

```from skimage.filters import sobel
coins_gray = rgb2gray(coins)
coins_edge = sobel(coins_gray)
compare(coins, coins_edge, "Images of coins with edges detected")```

sobel很直截了当；你只需在灰色图像上调用它即可获得如上所述的输出。我们将在后面的部分中看到Sobel的更复杂版本。

5、平滑

```chickens = imread("images/chickens.jpg")
>>> show(chickens)```

```from skimage.filters import gaussian
smoothed = gaussian(chickens, multichannel=True, sigma=2)
compare(chickens, smoothed, "An image smoothed with Gaussian smoothing")```

6、对比度增强

```xray = imread("images/xray.jpg")
xray_gray = rgb2gray(xray)
compare(xray, xray_gray)```

```>>> xray.max() - xray.min()
255```

```from skimage.exposure import equalize_hist
enhanced = equalize_hist(xray_gray)
>>> compare(xray, enhanced)```

```from skimage.exposure import equalize_hist
enhanced = equalize_hist(xray_gray)
>>> compare(xray, enhanced)```

```from skimage.exposure import equalize_adapthist
compare(xray, enhanced_adaptive, "Image with contrast enhancement")```

7、变换

```from skimage.transform import rotate
clockwise = rotate(clock, angle=-60)
compare(clock, clockwise, "Clockwise rotated image, use negative angles")```

```anti_clockwise = rotate(clock, angle=33)
compare(clock, anti_clockwise, "Anticlockwise rotated image, use positive angles")```

```butterflies = imread("images/butterflies.jpg")
>>> butterflies.shape
(720, 1280, 3)```

```from skimage.transform import rescale
scaled_butterflies = rescale(butterflies, scale=3 / 4, multichannel=True)
compare(
butterflies,
scaled_butterflies,
"Butterflies scaled down by a factor of 3/4",
axis=True,
)```

https://gist.github.com/f7ae272b6eb1bce408189d8de2b71656

```from skimage.transform import resize
# Also possible to set anti_aliasing
puppies_600_800 = resize(puppies, output_shape=(600, 800))
compare(puppies, puppies_600_800,
"Puppies image resized 600x800 (height, width)")```

### 图像恢复和增强

1、修补

```whale_image = imread("images/00206a224e68de.jpg")
>>> show(whale_image)```

```>>> whale_image.shape
(428, 1916, 3)```

```def make_mask(image):
"""Create a mask to artificially defect the image."""
# Apply the defect mask on the whale_image
image_defect = whale_image * ~mask[..., np.newaxis]
compare(whale_image, image_defect, "Artifically damaged image of a whale")```

```from skimage.restoration import inpaint
restored_image = inpaint.inpaint_biharmonic(
)
compare(
image_defect,
restored_image,
"Restored image after defects",
title_original="Faulty Image",
)```

2、噪声

```from skimage.util import random_noise
noisy_pup = random_noise(pup)
compare(pup, noisy_pup, "Noise puppy image")```

3、降噪-去噪

TV滤波器

```from skimage.restoration import denoise_tv_chambolle
denoised_pup_tv = denoise_tv_chambolle(noisy_pup, weight=0.2, multichannel=True)
compare(
noisy_pup,
denoised_pup_tv,
"Total Variation Filter denoising applied",
title_original="Noisy pup",
)```

```from skimage.restoration import denoise_bilateral
denoised_pup_bilateral = denoise_bilateral(noisy_pup, multichannel=True)
compare(noisy_pup, denoised_pup_bilateral, "Bilateral denoising applied image")```

```compare(
denoised_pup_tv,
denoised_pup_bilateral,
"Bilateral filtering",
title_original="TV filtering",
)```

4、分割

```from skimage import data
coffee = data.coffee()
>>> show(coffee)```

```from skimage.segmentation import slic
segments = slic(coffee)
>>> show(segments)```

```from skimage.color import label2rgb
final_image = label2rgb(segments, coffee, kind="avg")
>>> show(final_image)```

```from skimage.color import label2rgb
from skimage.segmentation import slic
def segment(image, n_segments=100):
# Obtain superpixels / segments
superpixels = slic(coffee, n_segments=n_segments)
# Put the groups on top of the original image
segmented_image = label2rgb(superpixels, image, kind="avg")
return segmented_image

# Find 500 segments
coffee_segmented_2 = segment(coffee, n_segments=500)
compare(coffee, coffee_segmented_2, "With 500 segments")```

5、等高线

```dominoes = imread("images/dominoes.jpg")
>>> show(dominoes)```

```from skimage.measure import find_contours
# Convert to grayscale
dominoes_gray = rgb2gray(dominoes)
# Find optimal threshold with treshold_otsu
thresh = threshold_otsu(dominoes_gray)
# Binarize
dominoes_binary = dominoes_gray > thresh
domino_contours = find_contours(dominoes_binary)```

```for contour in domino_contours[:5]:
print(contour.shape)
[OUT]:
(371, 2)
(376, 2)
(4226, 2)
(177, 2)
(11, 2)```

```from skimage.filters import threshold_otsu
from skimage.measure import find_contours
def mark_contours(image):
"""A function to find contours from an image"""
gray_image = rgb2gray(image)
# Find optimal threshold
thresh = threshold_otsu(gray_image)
binary_image = gray_image > thresh
contours = find_contours(binary_image)
return contours```

```def plot_image_contours(image):
fig, ax = plt.subplots()
ax.imshow(image, cmap=plt.cm.gray)
for contour in mark_contours(image):
ax.plot(contour[:, 1], contour[:, 0], linewidth=2, color="red")
ax.axis("off")
>>> plot_image_contours(dominoes)```

```dominoes_denoised = denoise_tv_chambolle(dominoes, multichannel=True)
plot_image_contours(dominoes_denoised)```

### 高级操作

1、边缘检测

```coins_3 = imread("images/coins_3.jpg")
# Convert to gray
coins_3_gray = rgb2gray(coins_3)
compare(coins_3, coins_3_gray)```

```from skimage.feature import canny
# Find edges with canny
canny_edges = canny(coins_3_gray)
compare(coins_3, canny_edges, "Edges detected with Canny algorithm")```

```canny_edges_sigma_2 = canny(coins_3_gray, sigma=2.5)
compare(coins_3, canny_edges_sigma_2, "Edges detected with less intensity")```

2、角点检测

```windows = imread("images/windows.jpg")
windows_gray = rgb2gray(windows)
compare(windows, windows_gray)```

```from skimage.feature import corner_harris
measured_image = corner_harris(windows_gray)
>>> show(measured_image)```

```from skimage.feature import corner_peaks
corner_coords = corner_peaks(measured_image, min_distance=50)
>>> len(corner_coords)
79```

```def find_corner_coords(image, min_distance=50):

# Convert to gray

gray_image = rgb2gray(image)

# Produce a measure image

measure_image = corner_harris(gray_image)
# Find coords

coords = corner_peaks(measure_image, min_distance=min_distance)
return coords```

```def show_image_cornered(image):
# Find coords
coords = find_corner_coords(image)
# Plot them on top of the image
plt.imshow(image, cmap="gray")
plt.plot(coords[:, 1], coords[:, 0], "+b", markersize=15)
plt.axis("off")
show_image_cornered(windows)```

```windows_denoised = denoise_tv_chambolle(windows, multichannel=True, weight=0.3)
show_image_cornered(windows_denoised)```