# -*- coding: utf-8 -*-
import cv2
def callback():
pass
# 创建窗口
cv2.namedWindow('color', cv2.WINDOW_NORMAL)
img = cv2.imread('E://pic//love_girl_guy_beach_sea_peter_smile_couple_5760x3840.jpg')
colorspaces = [cv2.COLOR_BGR2RGBA, cv2.COLOR_BGR2BGRA,
cv2.COLOR_BGR2GRAY, cv2.COLOR_BGR2HSV,
cv2.COLOR_BGR2YUV]
cv2.createTrackbar('curcolor', 'color', 0, len(colorspaces), callback)
while True:
index = cv2.getTrackbarPos('curcolor', 'color')
# 颜色空间转换API
cvt_img = cv2.cvtColor(img, colorspaces[index])
cv2.imshow('color', cvt_img)
key = cv2.waitKey(10)
if key & 0xff == ord('q'):
break
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((480, 640), np.uint8)
cnt = 0
while cnt < 200:
img[cnt, 100] = 255
cnt += 1
cv2.imshow('img', img)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((480, 640, 3), np.uint8)
cnt = 0
while cnt < 200:
# BGR
img[cnt, 100, 0] = 255
cnt += 1
cv2.imshow('img', img)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((480, 640, 3), np.uint8)
cnt = 0
while cnt < 200:
img[cnt, 100] = [0, 0, 255]
cnt += 1
cv2.imshow('img', img)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((480, 640, 3), np.uint8)
# cnt = 0
# while cnt < 200:
# img[cnt, 100] = [0, 0, 255]
# cnt += 1
roi = img[100:400, 200:600]
roi[:] = [0, 255, 0]
cv2.imshow('img', roi)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = np.zeros((480, 640, 3), np.uint8)
# cnt = 0
# while cnt < 200:
# img[cnt, 100] = [0, 0, 255]
# cnt += 1
roi = img[100:400, 100:600]
roi[:] = [0, 255, 0]
roi[:, 100] = [0, 0, 0]
roi[10:200, 10:200] = [255, 0, 0]
cv2.imshow('img', roi)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):
cv2.destroyAllWindows()
红色是我们原先创建的mat,蓝色是我们之后创建的mat,我们把A赋给B,默认浅拷贝
让Mat B独立,拷贝了一份data,完全和Mat A独立开来
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//2.jpg')
# 默认浅拷贝
img2 = img
# 深拷贝
img3 = img.copy()
img[10:100, 10:100] = [0, 255, 66]
cv2.imshow('img', img)
cv2.imshow('img2', img2)
cv2.imshow('img3', img3)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//2.jpg')
# shape属性中包含三个信息
# 高度、长度、通道数
print(img.shape)
# 图像占用多大空间
# 高度 * 长度 * 通道数
print(img.size)
# 图像中每个元素的位深
print(img.dtype)
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//2.jpg')
b, g, r = cv2.split(img)
b[10:100, 10:100] = 255
g[10:100, 10:100] = 255
r[10:100, 10:100] = 255
cv2.imshow('img', img)
cv2.imshow('b', b)
cv2.imshow('g', g)
cv2.imshow('r', r)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as np
img = cv2.imread('E://pic//2.jpg')
b, g, r = cv2.split(img)
b[10:100, 10:100] = 255
g[10:100, 10:100] = 255
# r[10:100, 10:100] = 255
img2 = cv2.merge((b, g, r))
cv2.imshow('img', img)
cv2.imshow('b', b)
cv2.imshow('g', g)
cv2.imshow('r', r)
cv2.imshow('img2', img2)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):
cv2.destroyAllWindows()
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!