图像操作
import numpy as np
import cv2
def color_space(image):
"""色彩空间转换"""
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
h1 = np.hstack((hsv, hls))
h2 = np.hstack((ycrcb, yuv))
stack = np.vstack((h1, h2))
return stack
# 读取图片
img = cv2.imread("girl.png")
img = cv2.resize(img, (320, 274))
# 不同色彩空间
stack = color_space(img)
# 图片展示
cv2.imshow("stack", stack)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
def add(image1, image2):
"""相加"""
image1 = cv2.resize(image1, (640, 554))
image2 = cv2.resize(image2, (640, 554))
result = cv2.add(image1, image2)
cv2.imshow("add_demo", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
return result
if __name__ == "__main__":
# 读取图片
img1 = cv2.imread("girl.png")
img2 = cv2.imread("cat.png")
# 获得相加的图片
add = add(img1, img2)
# 保存图片
cv2.imwrite("add.png", add)
def subtract(image1, image2):
"""相减"""
image1 = cv2.resize(image1, (640, 554))
image2 = cv2.resize(image2, (640, 554))
result = cv2.subtract(image1, image2)
cv2.imshow("add_demo", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
return result
if __name__ == "__main__":
# 读取图片
img1 = cv2.imread("girl.png")
img2 = cv2.imread("cat.png")
# 获得相加的图片
# add = add(img1, img2)
subtract=subtract(img1,img2)
def multiply(image1, image2):
"""相成"""
image1 = cv2.resize(image1, (640, 554))
image2 = cv2.resize(image2, (640, 554))
result = cv2.multiply(image1, image2)
cv2.imshow("add_demo", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
return result
if __name__ == "__main__":
# 读取图片
img1 = cv2.imread("girl.png")
img2 = cv2.imread("cat.png")
# 获得相加的图片
# add = add(img1, img2)
# subtract=subtract(img1,img2)
multiply=multiply(img1,img2)
def divide(image1, image2):
"""相除"""
image1 = cv2.resize(image1, (640, 554))
image2 = cv2.resize(image2, (640, 554))
result = cv2.divide(image1, image2)
cv2.imshow("add_demo", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
return result
if __name__ == "__main__":
# 读取图片
img1 = cv2.imread("girl.png")
img2 = cv2.imread("cat.png")
# 获得相加的图片
# add = add(img1, img2)
# subtract=subtract(img1,img2)
#multiply=multiply(img1,img2)
divide=divide(img1,img2)
def logic_and(image1, image2):
"""逻辑与"""
image1 = cv2.resize(image1, (640, 554))
image2 = cv2.resize(image2, (640, 554))
result = cv2.bitwise_and(image1, image2)
# 结果展示
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
return result
if __name__ == "__main__":
# 读取图片
img1 = cv2.imread("girl.png")
img2 = cv2.imread("cat.png")
# 获取逻辑与后的图片
logic_and = logic_and(img1, img2)
# 保存图片
cv2.imwrite("logic_and.png", logic_and)
def logic_or(image1, image2):
"""逻辑或"""
image1 = cv2.resize(image1, (640, 554))
image2 = cv2.resize(image2, (640, 554))
result = cv2.bitwise_or(image1, image2)
# 结果展示
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
return result
if __name__ == "__main__":
# 读取图片
img1 = cv2.imread("girl.png")
img2 = cv2.imread("cat.png")
# 获取逻辑与后的图片
logic_or = logic_or(img1, img2)
def logic_not(img):
"""逻辑非"""
result = cv2.bitwise_not(img)
# 合并
result = np.hstack((img, result))
# 结果展示
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
return result
if __name__ == "__main__":
# 读取图片
img1 = cv2.imread("girl.png")
img2 = cv2.imread("cat.png")
# 获取逻辑与后的图片
logic_not = logic_not(img1)