Opencv之图像色彩空间转换及几何变换函数总结

第一部分:Changing Colorspaces

1、Changing Color-space(颜色空间变换)

函数:cv2.cvtColor()

参数说明:第一个参数为图片,第二个参数为图片转换的类型,opencv提供了150中转换方法,其中常用的有BGR ↔ Gray 和 BGR ↔ HSV.

函数:cv2.inRange()

参数说明:Checks if array elements lie between the elements of two other arrays.

flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
       print( flags )
# ['COLOR_BAYER_BG2BGR', 'COLOR_BAYER_BG2BGR_EA', 'COLOR_BAYER_BG2BGR_VNG', 'COLOR_BAYER_BG2GRAY',
# 'COLOR_BAYER_BG2RGB', 'COLOR_BAYER_BG2RGB_EA', 'COLOR_BAYER_BG2RGB_VNG', 'COLOR_BAYER_GB2BGR',
# 'COLOR_BAYER_GB2BGR_EA', 'COLOR_BAYER_GB2BGR_VNG', 'COLOR_BAYER_GB2GRAY', 'COLOR_BAYER_GB2RGB',
# 'COLOR_BAYER_GB2RGB_EA', 'COLOR_BAYER_GB2RGB_VNG', 'COLOR_BAYER_GR2BGR', 'COLOR_BAYER_GR2BGR_EA',
# 'COLOR_BAYER_GR2BGR_VNG', 'COLOR_BAYER_GR2GRAY', 'COLOR_BAYER_GR2RGB', 'COLOR_BAYER_GR2RGB_EA',
# 'COLOR_BAYER_GR2RGB_VNG', 'COLOR_BAYER_RG2BGR', 'COLOR_BAYER_RG2BGR_EA', 'COLOR_BAYER_RG2BGR_VNG',
# 'COLOR_BAYER_RG2GRAY', 'COLOR_BAYER_RG2RGB', 'COLOR_BAYER_RG2RGB_EA', 'COLOR_BAYER_RG2RGB_VNG',
# 'COLOR_BGR2BGR555', 'COLOR_BGR2BGR565', 'COLOR_BGR2BGRA', 'COLOR_BGR2GRAY', 'COLOR_BGR2HLS',
# 'COLOR_BGR2HLS_FULL', 'COLOR_BGR2HSV', 'COLOR_BGR2HSV_FULL', 'COLOR_BGR2LAB', 'COLOR_BGR2LUV',
# 'COLOR_BGR2Lab', 'COLOR_BGR2Luv', 'COLOR_BGR2RGB', 'COLOR_BGR2RGBA', 'COLOR_BGR2XYZ', 'COLOR_BGR2YCR_CB',
# 'COLOR_BGR2YCrCb', 'COLOR_BGR2YUV', 'COLOR_BGR2YUV_I420', 'COLOR_BGR2YUV_IYUV', 'COLOR_BGR2YUV_YV12',
# 'COLOR_BGR5552BGR', 'COLOR_BGR5552BGRA', 'COLOR_BGR5552GRAY', 'COLOR_BGR5552RGB', 'COLOR_BGR5552RGBA',
# 'COLOR_BGR5652BGR', 'COLOR_BGR5652BGRA', 'COLOR_BGR5652GRAY', 'COLOR_BGR5652RGB', 'COLOR_BGR5652RGBA',
# 'COLOR_BGRA2BGR', 'COLOR_BGRA2BGR555', 'COLOR_BGRA2BGR565', 'COLOR_BGRA2GRAY', 'COLOR_BGRA2RGB',
# 'COLOR_BGRA2RGBA', 'COLOR_BGRA2YUV_I420', 'COLOR_BGRA2YUV_IYUV', 'COLOR_BGRA2YUV_YV12', 'COLOR_BayerBG2BGR',
# 'COLOR_BayerBG2BGR_EA', 'COLOR_BayerBG2BGR_VNG', 'COLOR_BayerBG2GRAY', 'COLOR_BayerBG2RGB',
# 'COLOR_BayerBG2RGB_EA', 'COLOR_BayerBG2RGB_VNG', 'COLOR_BayerGB2BGR', 'COLOR_BayerGB2BGR_EA',
# 'COLOR_BayerGB2BGR_VNG', 'COLOR_BayerGB2GRAY', 'COLOR_BayerGB2RGB', 'COLOR_BayerGB2RGB_EA',
# 'COLOR_BayerGB2RGB_VNG', 'COLOR_BayerGR2BGR', 'COLOR_BayerGR2BGR_EA', 'COLOR_BayerGR2BGR_VNG',
# 'COLOR_BayerGR2GRAY', 'COLOR_BayerGR2RGB', 'COLOR_BayerGR2RGB_EA', 'COLOR_BayerGR2RGB_VNG',
# 'COLOR_BayerRG2BGR', 'COLOR_BayerRG2BGR_EA', 'COLOR_BayerRG2BGR_VNG', 'COLOR_BayerRG2GRAY',
# 'COLOR_BayerRG2RGB', 'COLOR_BayerRG2RGB_EA', 'COLOR_BayerRG2RGB_VNG', 'COLOR_COLORCVT_MAX',
# 'COLOR_GRAY2BGR', 'COLOR_GRAY2BGR555', 'COLOR_GRAY2BGR565', 'COLOR_GRAY2BGRA', 'COLOR_GRAY2RGB',
#  'COLOR_GRAY2RGBA', 'COLOR_HLS2BGR', 'COLOR_HLS2BGR_FULL', 'COLOR_HLS2RGB', 'COLOR_HLS2RGB_FULL',
# 'COLOR_HSV2BGR', 'COLOR_HSV2BGR_FULL', 'COLOR_HSV2RGB', 'COLOR_HSV2RGB_FULL', 'COLOR_LAB2BGR',
#  'COLOR_LAB2LBGR', 'COLOR_LAB2LRGB', 'COLOR_LAB2RGB', 'COLOR_LBGR2LAB', 'COLOR_LBGR2LUV',
#  'COLOR_LBGR2Lab', 'COLOR_LBGR2Luv', 'COLOR_LRGB2LAB', 'COLOR_LRGB2LUV', 'COLOR_LRGB2Lab',
# 'COLOR_LRGB2Luv', 'COLOR_LUV2BGR', 'COLOR_LUV2LBGR', 'COLOR_LUV2LRGB', 'COLOR_LUV2RGB',
# 'COLOR_Lab2BGR', 'COLOR_Lab2LBGR', 'COLOR_Lab2LRGB', 'COLOR_Lab2RGB', 'COLOR_Luv2BGR',
# 'COLOR_Luv2LBGR', 'COLOR_Luv2LRGB', 'COLOR_Luv2RGB', 'COLOR_M_RGBA2RGBA', 'COLOR_RGB2BGR',
# 'COLOR_RGB2BGR555', 'COLOR_RGB2BGR565', 'COLOR_RGB2BGRA', 'COLOR_RGB2GRAY', 'COLOR_RGB2HLS',
# 'COLOR_RGB2HLS_FULL', 'COLOR_RGB2HSV', 'COLOR_RGB2HSV_FULL', 'COLOR_RGB2LAB', 'COLOR_RGB2LUV',
#  'COLOR_RGB2Lab', 'COLOR_RGB2Luv', 'COLOR_RGB2RGBA', 'COLOR_RGB2XYZ', 'COLOR_RGB2YCR_CB',
#  'COLOR_RGB2YCrCb', 'COLOR_RGB2YUV', 'COLOR_RGB2YUV_I420', 'COLOR_RGB2YUV_IYUV', 'COLOR_RGB2YUV_YV12',
#  'COLOR_RGBA2BGR', 'COLOR_RGBA2BGR555', 'COLOR_RGBA2BGR565', 'COLOR_RGBA2BGRA', 'COLOR_RGBA2GRAY',
# 'COLOR_RGBA2M_RGBA', 'COLOR_RGBA2RGB', 'COLOR_RGBA2YUV_I420', 'COLOR_RGBA2YUV_IYUV', 'COLOR_RGBA2YUV_YV12',
#  'COLOR_RGBA2mRGBA', 'COLOR_XYZ2BGR', 'COLOR_XYZ2RGB', 'COLOR_YCR_CB2BGR', 'COLOR_YCR_CB2RGB', 'COLOR_YCrCb2BGR',
#  'COLOR_YCrCb2RGB', 'COLOR_YUV2BGR', 'COLOR_YUV2BGRA_I420', 'COLOR_YUV2BGRA_IYUV', 'COLOR_YUV2BGRA_NV12',
#  'COLOR_YUV2BGRA_NV21', 'COLOR_YUV2BGRA_UYNV', 'COLOR_YUV2BGRA_UYVY', 'COLOR_YUV2BGRA_Y422', 'COLOR_YUV2BGRA_YUNV',
#  'COLOR_YUV2BGRA_YUY2', 'COLOR_YUV2BGRA_YUYV', 'COLOR_YUV2BGRA_YV12', 'COLOR_YUV2BGRA_YVYU', 'COLOR_YUV2BGR_I420',
#  'COLOR_YUV2BGR_IYUV', 'COLOR_YUV2BGR_NV12', 'COLOR_YUV2BGR_NV21', 'COLOR_YUV2BGR_UYNV', 'COLOR_YUV2BGR_UYVY',
#  'COLOR_YUV2BGR_Y422', 'COLOR_YUV2BGR_YUNV', 'COLOR_YUV2BGR_YUY2', 'COLOR_YUV2BGR_YUYV', 'COLOR_YUV2BGR_YV12',
#  'COLOR_YUV2BGR_YVYU', 'COLOR_YUV2GRAY_420', 'COLOR_YUV2GRAY_I420', 'COLOR_YUV2GRAY_IYUV', 'COLOR_YUV2GRAY_NV12',
# 'COLOR_YUV2GRAY_NV21', 'COLOR_YUV2GRAY_UYNV', 'COLOR_YUV2GRAY_UYVY', 'COLOR_YUV2GRAY_Y422', 'COLOR_YUV2GRAY_YUNV',
#  'COLOR_YUV2GRAY_YUY2', 'COLOR_YUV2GRAY_YUYV', 'COLOR_YUV2GRAY_YV12', 'COLOR_YUV2GRAY_YVYU', 'COLOR_YUV2RGB',
#  'COLOR_YUV2RGBA_I420', 'COLOR_YUV2RGBA_IYUV', 'COLOR_YUV2RGBA_NV12', 'COLOR_YUV2RGBA_NV21', 'COLOR_YUV2RGBA_UYNV',
#  'COLOR_YUV2RGBA_UYVY', 'COLOR_YUV2RGBA_Y422', 'COLOR_YUV2RGBA_YUNV', 'COLOR_YUV2RGBA_YUY2', 'COLOR_YUV2RGBA_YUYV',
# 'COLOR_YUV2RGBA_YV12', 'COLOR_YUV2RGBA_YVYU', 'COLOR_YUV2RGB_I420', 'COLOR_YUV2RGB_IYUV', 'COLOR_YUV2RGB_NV12',
#  'COLOR_YUV2RGB_NV21', 'COLOR_YUV2RGB_UYNV', 'COLOR_YUV2RGB_UYVY', 'COLOR_YUV2RGB_Y422', 'COLOR_YUV2RGB_YUNV',
# 'COLOR_YUV2RGB_YUY2', 'COLOR_YUV2RGB_YUYV', 'COLOR_YUV2RGB_YV12', 'COLOR_YUV2RGB_YVYU', 'COLOR_YUV420P2BGR',
# 'COLOR_YUV420P2BGRA', 'COLOR_YUV420P2GRAY', 'COLOR_YUV420P2RGB', 'COLOR_YUV420P2RGBA', 'COLOR_YUV420SP2BGR',
# 'COLOR_YUV420SP2BGRA', 'COLOR_YUV420SP2GRAY', 'COLOR_YUV420SP2RGB', 'COLOR_YUV420SP2RGBA', 'COLOR_YUV420p2BGR',
# 'COLOR_YUV420p2BGRA', 'COLOR_YUV420p2GRAY', 'COLOR_YUV420p2RGB', 'COLOR_YUV420p2RGBA', 'COLOR_YUV420sp2BGR',
#  'COLOR_YUV420sp2BGRA', 'COLOR_YUV420sp2GRAY', 'COLOR_YUV420sp2RGB', 'COLOR_YUV420sp2RGBA', 'COLOR_mRGBA2RGBA']

 2、How to find HSV values to track?

just pass the BGR values you want. For example, to find the HSV value of Green, try following commands in Python terminal:

green = np.uint8([[[0,255,0 ]]])
hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
       print( hsv_green )  # [[[ 60 255 255]]]

Now you take [H-10, 100,100] and [H+10, 255, 255] as lower bound and upper bound respectively. Apart from this method, you can use any image editing tools like GIMP or any online converters to find these values, but don't forget to adjust the HSV ranges.

3、A demo of Object Tracking by HSV ranges

In our application, we will try to extract a blue colored object. So here is the method:

  • Take each frame of the video
  • Convert from BGR to HSV color-space
  • We threshold the HSV image for a range of blue color
  • Now extract the blue object alone, we can do whatever on that image we want.

# -*- coding: utf-8 -*-
# http://docs.opencv.org/3.3.0/df/d9d/tutorial_py_colorspaces.html
import cv2
import numpy as np
cap = cv2.VideoCapture(0)   # 开启摄像头抓取对象
while(1):
# Take each frame
_, frame = cap.read()   # 读取视频信息
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  # Convert from BGR to HSV color-space
# define range of blue color in HSV
lower_blue = np.array([110,50,50])     # #6E3232
upper_blue = np.array([130,255,255])   # #82FFFF
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)   #
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()

4、Reference Materials

资料1:http://docs.opencv.org/3.3.0/df/d9d/tutorial_py_colorspaces.html

资料2(inRange函数):

http://docs.opencv.org/3.3.0/d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981

资料3(cvtColor函数):

http://docs.opencv.org/3.3.0/d7/d1b/group__imgproc__misc.html#ga397ae87e1288a81d2363b61574eb8cab

 

第二部分:Geometric Transformations of Images(图像的几何转换)

1、Transformations(转换)

函数:cv2.resize()

常用的转换方法:cv2.INTER_AREA     shrinking

                            cv2.INTER_CUBIC    slow

                            cv2.INTER_LINEAR   zooming

# -*- coding: utf-8 -*-
# http://docs.opencv.org/3.3.0/da/d6e/tutorial_py_geometric_transformations.html

import cv2
img = cv2.imread('../1.jpg')
res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)  # 转换
#OR
height, width = img.shape[:2]
res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)  # 转换

2、Translation(平移)

函数:cv2.warpAffine()

If you know the shift in (x,y) direction, let it be (tx,ty), you can create the transformation matrix M as follows: M = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \end{bmatrix}

import cv2
import numpy as np
img = cv2.imread('../1.jpg',0)
rows,cols = img.shape           # 将图片生成行列
M = np.float32([[1,0,100],[0,1,50]])   # 生成转换矩阵
dst = cv2.warpAffine(img,M,(cols,rows))  # 生成转换后的图像
cv2.imshow('img',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

3、Rotation(旋转)

函数:cv2.getRotationMatrix2D

# -*- coding: utf-8 -*-
# http://docs.opencv.org/3.3.0/da/d6e/tutorial_py_geometric_transformations.html

import cv2
img = cv2.imread('../1.jpg',0)
rows,cols = img.shape       # 将图片生成行列
M = cv2.getRotationMatrix2D((cols/2,rows/2),270,1)  # 生成旋转矩阵
dst = cv2.warpAffine(img,M,(cols,rows))   # 生成旋转后的图像
cv2.imshow('img',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

4、Affine Transformation(仿射转换

函数1:cv2.getAffineTransform

函数2:cv2.warpAffine

# -*- coding: utf-8 -*-
# http://docs.opencv.org/3.3.0/df/d9d/tutorial_py_colorspaces.html
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('../image/drawing2.png')
rows,cols,ch = img.shape  # 将一张图片转变成行列和通道
pts1 = np.float32([[50,50],[200,50],[50,200]])     # 输入图像的三个点
pts2 = np.float32([[10,100],[200,50],[100,250]])   # 输出图像的三个点
M = cv2.getAffineTransform(pts1,pts2)              # 生成仿射变换矩阵
dst = cv2.warpAffine(img,M,(cols,rows))            # 生成新的放射变换输出图像
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

5、Perspective Transformation(透视转换)

函数1:cv2.getPerspectiveTransform

函数2:cv2.warpPerspective

# -*- coding: utf-8 -*-
# http://docs.opencv.org/3.3.0/df/d9d/tutorial_py_colorspaces.html
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('../image/drawing3.png')
rows,cols,ch = img.shape        # 将一张图片转变成行列和通道
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])  # 找四个点,其中三点不能共线,基于输入图像中找
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])    # 找四个点,图片输出的地点
M = cv2.getPerspectiveTransform(pts1,pts2)     # 生成透视转换矩阵
dst = cv2.warpPerspective(img,M,(300,300))     # 形成透视变换图
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()

6、Reference Materials

资料1:http://docs.opencv.org/3.3.0/da/d6e/tutorial_py_geometric_transformations.html

资料2(resize函数):

http://docs.opencv.org/3.3.0/d5/df1/group__imgproc__hal__functions.html#ga2fe39d2201b12e1b961ca56b2aff9ff2

资料3(warpAffine函数):

http://docs.opencv.org/3.3.0/d5/df1/group__imgproc__hal__functions.html#ga8a534cca6fb845c9ac77f10c35f67c0c

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: