读取、图像、保存图像
import cv2
im = cv2.imread("../dataset/data/Linus.png", 1)
im_gray = cv2.imread("../dataset/data/Linus.png", 0)
cv2.imshow("test", im)
cv2.imshow("gray",im_gray)
print(type(im))
print(type(im_gray))
print(im.shape)
print(im_gray.shape)
cv2.imwrite("../dataset/Linus_2.png", im)
cv2.waitKey()
cv2.destroyAllWindows()
色彩通道操作
import cv2
im = cv2.imread("../dataset/data/Linus.png", 1)
cv2.imshow("RGB", im)
img_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imshow("Gray", img_gray)
cv2.waitKey()
cv2.destroyAllWindows()
import numpy as np
import cv2
im = cv2.imread("../data/opencv2.png")
print(im.shape)
cv2.imshow("im", im)
b = im[:, :, 0]
cv2.imshow("b", b)
im[:, :, 0] = 0
cv2.imshow("im-b0", im)
im[:, :, 1] = 0
cv2.imshow("im-b0g0", im)
cv2.waitKey()
cv2.destroyAllWindows()
直方图均衡化
import numpy as np
import cv2
from matplotlib import pyplot as plt
im = cv2.imread("../dataset/data/sunrise.jpg", 0)
cv2.imshow("orig", im)
im_equ = cv2.equalizeHist(im)
cv2.imshow("equ1", im_equ)
print(im.ravel())
plt.subplot(2, 1, 1)
plt.hist(im.ravel(),
256, [0, 256], label="orig")
plt.legend()
plt.subplot(2, 1, 2)
plt.hist(im_equ.ravel(), 256, [0, 256], label="equalize")
plt.legend()
plt.show()
cv2.waitKey()
cv2.destroyAllWindows()
彩色亮度直方图均衡化
import cv2
original = cv2.imread('../dataset/data/sunrise.jpg')
cv2.imshow('Original', original)
yuv = cv2.cvtColor(original, cv2.COLOR_BGR2YUV)
print("yuv.shape:", yuv.shape)
yuv[..., 0] = cv2.equalizeHist(yuv[..., 0])
equalized_color = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR)
cv2.imshow('Equalized Color', equalized_color)
cv2.waitKey()
cv2.destroyAllWindows()
二值化与反二值化
import cv2 as cv
img = cv.imread("../dataset/data/lena.jpg", 0)
cv.imshow("img", img)
t, rst = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
cv.imshow("rst", rst)
t, rst2 = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)
cv.imshow("rst2", rst2)
cv.waitKey()
cv.destroyAllWindows()
图像翻转
import numpy as np
import cv2
im = cv2.imread("../data/Linus.png")
cv2.imshow("src", im)
im_flip0 = cv2.flip(im, 0)
cv2.imshow("im_flip0", im_flip0)
im_flip1 = cv2.flip(im, 1)
cv2.imshow("im_flip1", im_flip1)
cv2.waitKey()
cv2.destroyAllWindows()
图像仿射变换
import numpy as np
import cv2
def translate(img, x, y):
"""
坐标平移变换
:param img: 原始图像数据
:param x:平移的x坐标
:param y:平移的y坐标
:return:返回平移后的图像
"""
h, w = img.shape[:2]
M = np.float32([[1, 0, x],
[0, 1, y]])
shifted = cv2.warpAffine(img, M, (w, h))
return shifted
def rotate(img, angle, center=None, scale=1.0):
"""
图像旋转变换
:param img: 原始图像数据
:param angle: 旋转角度
:param center: 旋转中心,如果为None则以原图中心为旋转中心
:param scale: 缩放比例,默认为1,1.0就是不进行缩放
:return: 返回旋转后的图像
"""
h, w = img.shape[:2]
if center is None:
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(img, M, (w, h))
return rotated
if __name__ == "__main__":
im = cv2.imread("../dataset/data/Linus.png")
cv2.imshow("SrcImg", im)
shifted = translate(im, 0, 50)
cv2.imshow("Shifted1", shifted)
shifted = translate(im, -40, 40)
cv2.imshow("Shifted2", shifted)
rotated = rotate(im, 45)
cv2.imshow("rotated1", rotated)
rotated = rotate(im, -90)
cv2.imshow("rorated2", rotated)
cv2.waitKey()
cv2.destroyAllWindows()
图像的缩放
import numpy as np
import cv2
im = cv2.imread("../dataset/data/Linus.png")
cv2.imshow("src", im)
h, w = im.shape[:2]
dst_size = (int(w/2), int(h/2))
resized = cv2.resize(im, dst_size)
cv2.imshow("reduce", resized)
dst_size = (200, 300)
method = cv2.INTER_NEAREST
resized = cv2.resize(im, dst_size, interpolation=method)
cv2.imshow("NEAREST", resized)
dst_size = (200, 300)
method = cv2.INTER_LINEAR
resized = cv2.resize(im, dst_size, interpolation=method)
cv2.imshow("LINEAR", resized)
cv2.waitKey()
cv2.destroyAllWindows()
图像的裁剪
import numpy as np
import cv2
def random_crop(im, w, h):
start_x = np.random.randint(0, im.shape[1])
start_y = np.random.randint(0, im.shape[0])
new_img = im[start_y:start_y + h, start_x: start_x + w]
return new_img
def center_crop(im, w, h):
start_x = int(im.shape[1] / 2) - int(w / 2)
start_y = int(im.shape[0] / 2) - int(h / 2)
new_img = im[start_y:start_y + h, start_x: start_x + w]
return new_img
im = cv2.imread("../dataset/data/banana_1.png", 1)
new_img = random_crop(im, 200, 200)
new_img2 = center_crop(im, 200, 200)
cv2.imshow("orig", im)
cv2.imshow("random_crop", new_img)
cv2.imshow("center_crop", new_img2)
cv2.waitKey()
cv2.destroyAllWindows()
图像的相加
import cv2
a = cv2.imread("../dataset/data/lena.jpg", 0)
b = cv2.imread("../dataset/data/lily_square.png", 0)
dst1 = cv2.add(a, b)
dst2 = cv2.addWeighted(a, 0.6, b, 0.4, 0)
cv2.imshow("a", a)
cv2.imshow("b", b)
cv2.imshow("dst1", dst1)
cv2.imshow("dst2", dst2)
cv2.waitKey()
cv2.destroyAllWindows()
图像的相减
import cv2
a = cv2.imread("../dataset/data/3.png", 0)
b = cv2.imread("../dataset/data/4.png", 0)
dst = cv2.subtract(a, b)
cv2.imshow("a", a)
cv2.imshow("b", b)
cv2.imshow("dst1", dst)
cv2.waitKey()
cv2.destroyAllWindows()
透视变化
import cv2
import numpy as np
img = cv2.imread('../dataset/data/pers.png')
rows, cols = img.shape[:2]
print(rows, cols)
pts1 = np.float32([[58, 2], [167, 9], [8, 196], [126, 196]])
pts2 = np.float32([[16, 2], [167, 8], [8, 196], [169, 196]])
M = cv2.getPerspectiveTransform(pts1,
pts2)
print(M.shape)
dst = cv2.warpPerspective(img,
M,
(cols, rows))
M = cv2.getPerspectiveTransform(pts2,
pts1)
dst2 = cv2.warpPerspective(dst,
M,
(cols, rows))
cv2.imshow("img", img)
cv2.imshow("dst", dst)
cv2.imshow("dst2", dst2)
cv2.waitKey()
cv2.destroyAllWindows()
测试所用图片链接
图片链接