文章目录
- OpenCV的色彩空间——RGB与BGR
- OpenCV的色彩空间——HSV与HSL
- HSV主要用于OpenCV中
- HSL
- OpenCV色彩空间转换
- YUV主要用于视频中
- 题目
- 图像操作的基石Numpy【基础操作】
- np.array
- np.zeros
- np.ones
- np.full
- np.identity
- np.eye
- Numpy基本操作之矩阵的检索与赋值
- Numpy基本操作三——ROI(Region of Image)
- OpenCV的重要结构体Mat
- Mat属性
- Mat的深拷贝与浅拷贝
- Mat浅拷贝
- Mat深拷贝
- 图像的多种属性
- 通道的分割与合并
OpenCV的色彩空间——RGB与BGR
主要用于物理硬件显示器中
OpenCV的色彩空间——HSV与HSL
HSV主要用于OpenCV中
HSL
OpenCV色彩空间转换
YUV主要用于视频中
详情见官网手册
cvtColor函数
# -*- coding: utf-8 -*-
import cv2def 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')# 颜色空间转换APIcvt_img = cv2.cvtColor(img, colorspaces[index])cv2.imshow('color', cvt_img)key = cv2.waitKey(10)if key & 0xff == ord('q'):breakcv2.destroyAllWindows()
题目
A
图像操作的基石Numpy【基础操作】
np.array
np.zeros
np.ones
np.full
np.identity
np.eye
Numpy基本操作之矩阵的检索与赋值
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = np.zeros((480, 640), np.uint8)cnt = 0
while cnt < 200:img[cnt, 100] = 255cnt += 1cv2.imshow('img', img)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = np.zeros((480, 640, 3), np.uint8)cnt = 0
while cnt < 200:# BGRimg[cnt, 100, 0] = 255cnt += 1cv2.imshow('img', img)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = np.zeros((480, 640, 3), np.uint8)cnt = 0
while cnt < 200:img[cnt, 100] = [0, 0, 255]cnt += 1cv2.imshow('img', img)
key = cv2.waitKey(0)
if key & 0xff == ord('q'):cv2.destroyAllWindows()
Numpy基本操作三——ROI(Region of Image)
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = 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 npimg = 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()
OpenCV的重要结构体Mat
Mat属性
Mat的深拷贝与浅拷贝
红色是我们原先创建的mat,蓝色是我们之后创建的mat,我们把A赋给B,默认浅拷贝
Mat浅拷贝
拷贝的是header,并没有拷贝data
Mat深拷贝
让Mat B独立,拷贝了一份data,完全和Mat A独立开来
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = 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 npimg = cv2.imread('E://pic//2.jpg')
# shape属性中包含三个信息
# 高度、长度、通道数
print(img.shape)# 图像占用多大空间
# 高度 * 长度 * 通道数
print(img.size)# 图像中每个元素的位深
print(img.dtype)
通道的分割与合并
变成一个颜色的二维数组就没有颜色了,就只有黑白色之分了
# -*- coding: utf-8 -*-
import cv2
import numpy as npimg = 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] = 255cv2.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 npimg = 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] = 255img2 = 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()
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!