图像处理入门100题(一)
本篇记录了自己写的GIthub上的ImageProcessing 100 Wen的问题1-10的答案,注释里包含了一些自己的感悟。为了方便,注释是用英文写的。
问题序号 | 题目 |
---|---|
1 | 通道交换 |
2 | 灰度化 |
3 | 二值化 |
4 | 大津二值化算法(最佳阈值) |
5 | HSV与RGB的变换 |
6 | 减色处理 |
7 | 平均池化 |
8 | 最大池化 |
9 | 高斯滤波 |
10 | 中值滤波 |
Python答案如下,结尾包含了问题的验证。
import cv2.cv2
import numpy as np
# Question One
# change channels
# from RGB to BGR
# please note that the images are stored by BGR
# Answer
def RGB2BGR(img):
out = img.copy()
out[:, :] = out[:, :, (2, 1, 0)] # from 0:B 1:G 2:R <= 2:R 1:G 0:B
return out
# Question Two
# gray scale of images
# Y = 0.2126 * R + 0.7152 * G + 0.0722 * B
# Answer
def RGB2GRAY(img):
out = 0.2126 * img[:, :, 2] + 0.7152 * img[:, :, 1] + 0.0722 * img[:, :, 0]
out = out.astype(np.uint8)
return out
# Question Three
# Binary image
# threshold: if y < t then y := 0 else y := 1
# Answer
def BINARY(img, th=128):
out = RGB2GRAY(img)
out[out < th] = 0 # please not this usage, out < th return a matrix which has the same dimension with out
out[out >= th] = 255 # out < th is consisted of True and False
return out
# Question Four
# find best threshold for binary image
# Otsu's method:
# there are two classes: class0 that is less than threshold and class1 that is greater and equal than threshold
# w0: the rate of number of pixels of class0, the same as w1
# M0: the mean value of pixel value of class0, the same as M1
# objective function: make Sb^2 = w0 * w1 * (M0 - M1)^2 biggest
# Answer
def BESTTH(img):
best_t = 0
max_sb = 0
H = img.shape[0]
W = img.shape[1]
gray = RGB2GRAY(img)
for t in range(1, 255):
class0 = gray[gray < t]
class1 = gray[gray >= t]
w0 = len(class0) / (H * W)
w1 = len(class1) / (H * W)
M0 = np.mean(class0) if len(class0) > 0 else 0
M1 = np.mean(class1) if len(class1) > 0 else 0
sb = w0 * w1 * np.square(M0 - M1)
if sb > max_sb:
best_t = t
max_sb = sb
return best_t
# Question Five
# HSV to RGB, RGB to HSV
# Answer
def RGB2HSV(img):
hsv = np.zeros_like(img, dtype=np.float)
img = img / 255.0 # RGB in [0, 1]
max = np.max(img, axis=2) # max = max(R, G, B)
min = np.min