lenna小姐姐作为图像领域中的hello world,20世纪的跨界达人,几代人的战斗对象。对于入门Python的你如果了解了lenna小姐姐,那可相当于半根脚趾头踏入了图像处理的大门,所以今天从下面几个方面给大家介绍一下lenna小姐姐
- lenna是谁?
- 为什么选这张图?
- 都对lenna做了什么?
- lenna近照
lenna是谁?
莱娜·瑟德贝里(瑞典文:Lena Soderberg),1951年3月31日出生于瑞典,在1972年11月期的《花花公子》杂志中,她化名为莱娜·舍布洛姆,成为了当期的玩伴女郎。
她的照片(即莱娜图)后来被数字图像处理领域所广泛使用。
为什么选这张图?
主要有三个原因
- 这张图适度的混合了细节、平滑区域、阴影和纹理,从而能很好的测试各种图像处理算法
- 这张图的Lena是个美女,对于图像处理界的研究者(大部分都是男性)来说,美女图可以有效的吸引他们来做研究
- 该照片的全图实在太吸引眼球,广为流传的图仅为全图的1/3,(全图这里实在放不了,一放就封号)
都对lenna做了什么?
通过cv2包,科研人员对这张图无所不用其极,绘图显示、切分、滤镜、校正、旋转、变换,行哥这里将源码和结果一一附上
1.灰度显示
代码语言:javascript复制img_gray = cv2.imread('lenna.jpg',0)
cv2.imshow('lenna', img_gray)
# 判断键盘按键
key = cv2.waitKey()
if key == 27:
cv2.destroyAllWindows()
2.截图显示
代码语言:javascript复制img_crop = img[0:100, 0:200]
cv2.imshow('lena_crop',img_crop)
# 判断键盘按键 如果是27 esc 则退出游戏
key = cv2.waitKey()
if key == 27:
cv2.destroyAllWindows()
3.RGB转换
代码语言:javascript复制B,G,R = cv2.split(img)
cv2.imshow('B',B)
cv2.imshow('G',R)
cv2.imshow('R',R)
key = cv2.waitKey()
if key == 27:
cv2.destroyAllWindows()
4.加入滤镜
代码语言:javascript复制def random_light_color(img):
# brightness
B, G, R = cv2.split(img)
b_rand = random.randint(-50, 50)
if b_rand == 0:
pass
elif b_rand > 0:
lim = 255 - b_rand
B[B > lim] = 255
B[B <= lim] = (b_rand B[B <= lim]).astype(img.dtype)
elif b_rand < 0:
lim = 0 - b_rand
B[B < lim] = 0
B[B >= lim] = (b_rand B[B >= lim]).astype(img.dtype)
g_rand = random.randint(-50, 50)
if g_rand == 0:
pass
elif g_rand > 0:
lim = 255 - g_rand
G[G > lim] = 255
G[G <= lim] = (g_rand G[G <= lim]).astype(img.dtype)
elif g_rand < 0:
lim = 0 - g_rand
G[G < lim] = 0
G[G >= lim] = (g_rand G[G >= lim]).astype(img.dtype)
r_rand = random.randint(-50, 50)
if r_rand == 0:
pass
elif r_rand > 0:
lim = 255 - r_rand
R[R > lim] = 255
R[R <= lim] = (r_rand R[R <= lim]).astype(img.dtype)
elif r_rand < 0:
lim = 0 - r_rand
R[R < lim] = 0
R[R >= lim] = (r_rand R[R >= lim]).astype(img.dtype)
img_merge = cv2.merge((B, G, R))
#img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return img_merge
img_random_color = random_light_color(img)
cv2.imshow('img_random_color', img_random_color)
key = cv2.waitKey()
if key == 27:
cv2.destroyAllWindows()
5.gamma校正
代码语言:javascript复制def adjust_gamma(image, gamma=1.0):
invGamma = 1.0 / gamma
table = []
for i in range(256):
table.append(((i / 255.0) ** invGamma) * 255)
table = np.array(table).astype("uint8")
return cv2.LUT(img_dark, table)
img_dark = cv2.imread('material/lenna.jpg')
cv2.imshow('img_dark', img_dark)
img_brighter = adjust_gamma(img_dark, 2)
cv2.imshow('img_dark', img_dark)
cv2.imshow('img_brighter', img_brighter)
key = cv2.waitKey()
if key == 27:
cv2.destroyAllWindows()
6.图像旋转
代码语言:javascript复制M = cv2.getRotationMatrix2D((img.shape[1] / 2, img.shape[0] / 2), 30, 1) # center, angle, scale
img_rotate = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
cv2.imshow('rotated lenna', img_rotate)
key = cv2.waitKey(0)
if key == 27:
cv2.destroyAllWindows()
7.仿射变换
代码语言:javascript复制rows, cols, ch = img.shape
pts1 = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]])
pts2 = np.float32([[cols * 0.2, rows * 0.1], [cols * 0.9, rows * 0.2], [cols * 0.1, rows * 0.9]])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow('affine lenna', dst)
key = cv2.waitKey(0)
if key == 27:
cv2.destroyAllWindows()
8.透视变化
代码语言:javascript复制def random_warp(img, row, col):
height, width, channels = img.shape
# warp:
random_margin = 60
x1 = random.randint(-random_margin, random_margin)
y1 = random.randint(-random_margin, random_margin)
x2 = random.randint(width - random_margin - 1, width - 1)
y2 = random.randint(-random_margin, random_margin)
x3 = random.randint(width - random_margin - 1, width - 1)
y3 = random.randint(height - random_margin - 1, height - 1)
x4 = random.randint(-random_margin, random_margin)
y4 = random.randint(height - random_margin - 1, height - 1)
dx1 = random.randint(-random_margin, random_margin)
dy1 = random.randint(-random_margin, random_margin)
dx2 = random.randint(width - random_margin - 1, width - 1)
dy2 = random.randint(-random_margin, random_margin)
dx3 = random.randint(width - random_margin - 1, width - 1)
dy3 = random.randint(height - random_margin - 1, height - 1)
dx4 = random.randint(-random_margin, random_margin)
dy4 = random.randint(height - random_margin - 1, height - 1)
pts1 = np.float32([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
pts2 = np.float32([[dx1, dy1], [dx2, dy2], [dx3, dy3], [dx4, dy4]])
M_warp = cv2.getPerspectiveTransform(pts1, pts2)
img_warp = cv2.warpPerspective(img, M_warp, (width, height))
return M_warp, img_warp
M_warp, img_warp = random_warp(img, img.shape[0], img.shape[1])
cv2.imshow('lenna_warp', img_warp)
key = cv2.waitKey(0)
if key == 27:
cv2.destroyAllWindows()
lenna近照
lenna近照
再厉害的图片处理,也抵挡不过时间的威力,将近七旬的“小姐姐”以另一种方式为图片处理领域作出了自己的贡献
致敬