2.截图显示
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转换
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.加入滤镜
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校正
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.图像旋转
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.仿射变换
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.透视变化
def random_warp(img, row, col):
height, width, channels = img.shape
# warp:
random_margin = 60
感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:
① 2000多本Python电子书(主流和经典的书籍应该都有了)
② Python标准库资料(最全中文版)
③ 项目源码(四五十个有趣且经典的练手项目及源码)
④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)
⑤ Python学习路线图(告别不入流的学习)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!