python-opencv第三期:cvtColor函数详解
Posted Kamen Black君
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-opencv第三期:cvtColor函数详解相关的知识,希望对你有一定的参考价值。
概要:众嗦粥之所周知,在如今计算机视觉(Computer Version short for CV)是人工智能与机器人技术发展的一个重大研究方向,而opencv作为一个专门为计算机视觉编程提供技术与函数支持的第三方库,自然是一个需要重点研究的内容。而本期所要介绍的函数就是opencv库下的函数——cvtColor()。这个函数的主要作用在于将图像从一个颜色空间转换为另一个颜色空间(就是切换滤镜的意思),通常情况下我们是需要将图片从RGB空间转化到其他的颜色空间,以便我们对图片进行相应的分析与处理。(就是得套层马甲,让人家认识认识)
本文依旧是请出“饥荒”中我最爱使用的一个角色“机器人”(机器人:我有名字的,我叫WX-78),我们在他捡齿轮,不注意时,将他作为我们本期的素材,如下图所示(WX-78:我靠,年轻人不讲武德,来骗,来偷袭我100滴血的小同志)。话不多说,我是Kamen Black君,马上开始今天的学习,我已经等不及力。
正文内容:
print("祝大家每天快乐,love and peace!")
①使用准备:
与前两期一样,依旧是调用我们的工具人老伙伴----opencv,作为我们使用cvtcolor的大前提。
import cv2
②语法说明:
dst=cv2.cvtColor(src,code[, dst[, dstCn]])
其中,dst(destination)就表示我们经过函数处理后得到的目的图像,此时是矩阵形式;
src(source)表示我们要进行转换的源图像对象,也是矩阵形式;
code是opencv中色彩空间定义的宏常量,展示了我们要图像转换所要达到的结果,常用的有COLOR_BGR2GRAY、COLOR_GRAY2BGR、COLOR_BGR2HSV、COLOR_BGR2RGB等,但是实际上可以调用的参数多达247种。(4.5.2版本统计)(就像那些福布斯榜上的富豪,自己所用的财富不过占其身家的很小一部分,主要还是作为一种实力的展示);
dstCn为目标图像的通道数,如果设置为0,会自动从源图像计算目标图像的通道数;
下面展示一下code的参数,以供大家参考与学习(要懂得ctrl+F的重要性):
cv::ColorConversionCodes | cv::COLOR_BGR2BGRA = 0, cv::COLOR_RGB2RGBA = COLOR_BGR2BGRA, cv::COLOR_BGRA2BGR = 1, cv::COLOR_RGBA2RGB = COLOR_BGRA2BGR, cv::COLOR_BGR2RGBA = 2, cv::COLOR_RGB2BGRA = COLOR_BGR2RGBA, cv::COLOR_RGBA2BGR = 3, cv::COLOR_BGRA2RGB = COLOR_RGBA2BGR, cv::COLOR_BGR2RGB = 4, cv::COLOR_RGB2BGR = COLOR_BGR2RGB, cv::COLOR_BGRA2RGBA = 5, cv::COLOR_RGBA2BGRA = COLOR_BGRA2RGBA, cv::COLOR_BGR2GRAY = 6, cv::COLOR_RGB2GRAY = 7, cv::COLOR_GRAY2BGR = 8, cv::COLOR_GRAY2RGB = COLOR_GRAY2BGR, cv::COLOR_GRAY2BGRA = 9, cv::COLOR_GRAY2RGBA = COLOR_GRAY2BGRA, cv::COLOR_BGRA2GRAY = 10, cv::COLOR_RGBA2GRAY = 11, cv::COLOR_BGR2BGR565 = 12, cv::COLOR_RGB2BGR565 = 13, cv::COLOR_BGR5652BGR = 14, cv::COLOR_BGR5652RGB = 15, cv::COLOR_BGRA2BGR565 = 16, cv::COLOR_RGBA2BGR565 = 17, cv::COLOR_BGR5652BGRA = 18, cv::COLOR_BGR5652RGBA = 19, cv::COLOR_GRAY2BGR565 = 20, cv::COLOR_BGR5652GRAY = 21, cv::COLOR_BGR2BGR555 = 22, cv::COLOR_RGB2BGR555 = 23, cv::COLOR_BGR5552BGR = 24, cv::COLOR_BGR5552RGB = 25, cv::COLOR_BGRA2BGR555 = 26, cv::COLOR_RGBA2BGR555 = 27, cv::COLOR_BGR5552BGRA = 28, cv::COLOR_BGR5552RGBA = 29, cv::COLOR_GRAY2BGR555 = 30, cv::COLOR_BGR5552GRAY = 31, cv::COLOR_BGR2XYZ = 32, cv::COLOR_RGB2XYZ = 33, cv::COLOR_XYZ2BGR = 34, cv::COLOR_XYZ2RGB = 35, cv::COLOR_BGR2YCrCb = 36, cv::COLOR_RGB2YCrCb = 37, cv::COLOR_YCrCb2BGR = 38, cv::COLOR_YCrCb2RGB = 39, cv::COLOR_BGR2HSV = 40, cv::COLOR_RGB2HSV = 41, cv::COLOR_BGR2Lab = 44, cv::COLOR_RGB2Lab = 45, cv::COLOR_BGR2Luv = 50, cv::COLOR_RGB2Luv = 51, cv::COLOR_BGR2HLS = 52, cv::COLOR_RGB2HLS = 53, cv::COLOR_HSV2BGR = 54, cv::COLOR_HSV2RGB = 55, cv::COLOR_Lab2BGR = 56, cv::COLOR_Lab2RGB = 57, cv::COLOR_Luv2BGR = 58, cv::COLOR_Luv2RGB = 59, cv::COLOR_HLS2BGR = 60, cv::COLOR_HLS2RGB = 61, cv::COLOR_BGR2HSV_FULL = 66, cv::COLOR_RGB2HSV_FULL = 67, cv::COLOR_BGR2HLS_FULL = 68, cv::COLOR_RGB2HLS_FULL = 69, cv::COLOR_HSV2BGR_FULL = 70, cv::COLOR_HSV2RGB_FULL = 71, cv::COLOR_HLS2BGR_FULL = 72, cv::COLOR_HLS2RGB_FULL = 73, cv::COLOR_LBGR2Lab = 74, cv::COLOR_LRGB2Lab = 75, cv::COLOR_LBGR2Luv = 76, cv::COLOR_LRGB2Luv = 77, cv::COLOR_Lab2LBGR = 78, cv::COLOR_Lab2LRGB = 79, cv::COLOR_Luv2LBGR = 80, cv::COLOR_Luv2LRGB = 81, cv::COLOR_BGR2YUV = 82, cv::COLOR_RGB2YUV = 83, cv::COLOR_YUV2BGR = 84, cv::COLOR_YUV2RGB = 85, cv::COLOR_YUV2RGB_NV12 = 90, cv::COLOR_YUV2BGR_NV12 = 91, cv::COLOR_YUV2RGB_NV21 = 92, cv::COLOR_YUV2BGR_NV21 = 93, cv::COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21, cv::COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21, cv::COLOR_YUV2RGBA_NV12 = 94, cv::COLOR_YUV2BGRA_NV12 = 95, cv::COLOR_YUV2RGBA_NV21 = 96, cv::COLOR_YUV2BGRA_NV21 = 97, cv::COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21, cv::COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21, cv::COLOR_YUV2RGB_YV12 = 98, cv::COLOR_YUV2BGR_YV12 = 99, cv::COLOR_YUV2RGB_IYUV = 100, cv::COLOR_YUV2BGR_IYUV = 101, cv::COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV, cv::COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV, cv::COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12, cv::COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12, cv::COLOR_YUV2RGBA_YV12 = 102, cv::COLOR_YUV2BGRA_YV12 = 103, cv::COLOR_YUV2RGBA_IYUV = 104, cv::COLOR_YUV2BGRA_IYUV = 105, cv::COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV, cv::COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV, cv::COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12, cv::COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12, cv::COLOR_YUV2GRAY_420 = 106, cv::COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420, cv::COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420, cv::COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420, cv::COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420, cv::COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420, cv::COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420, cv::COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420, cv::COLOR_YUV2RGB_UYVY = 107, cv::COLOR_YUV2BGR_UYVY = 108, cv::COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY, cv::COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY, cv::COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY, cv::COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY, cv::COLOR_YUV2RGBA_UYVY = 111, cv::COLOR_YUV2BGRA_UYVY = 112, cv::COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY, cv::COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY, cv::COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY, cv::COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY, cv::COLOR_YUV2RGB_YUY2 = 115, cv::COLOR_YUV2BGR_YUY2 = 116, cv::COLOR_YUV2RGB_YVYU = 117, cv::COLOR_YUV2BGR_YVYU = 118, cv::COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2, cv::COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2, cv::COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2, cv::COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2, cv::COLOR_YUV2RGBA_YUY2 = 119, cv::COLOR_YUV2BGRA_YUY2 = 120, cv::COLOR_YUV2RGBA_YVYU = 121, cv::COLOR_YUV2BGRA_YVYU = 122, cv::COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2, cv::COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2, cv::COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2, cv::COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2, cv::COLOR_YUV2GRAY_UYVY = 123, cv::COLOR_YUV2GRAY_YUY2 = 124, cv::COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY, cv::COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY, cv::COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2, cv::COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2, cv::COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2, cv::COLOR_RGBA2mRGBA = 125, cv::COLOR_mRGBA2RGBA = 126, cv::COLOR_RGB2YUV_I420 = 127, cv::COLOR_BGR2YUV_I420 = 128, cv::COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420, cv::COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420, cv::COLOR_RGBA2YUV_I420 = 129, cv::COLOR_BGRA2YUV_I420 = 130, cv::COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420, cv::COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420, cv::COLOR_RGB2YUV_YV12 = 131, cv::COLOR_BGR2YUV_YV12 = 132, cv::COLOR_RGBA2YUV_YV12 = 133, cv::COLOR_BGRA2YUV_YV12 = 134, cv::COLOR_BayerBG2BGR = 46, cv::COLOR_BayerGB2BGR = 47, cv::COLOR_BayerRG2BGR = 48, cv::COLOR_BayerGR2BGR = 49, cv::COLOR_BayerBG2RGB = COLOR_BayerRG2BGR, cv::COLOR_BayerGB2RGB = COLOR_BayerGR2BGR, cv::COLOR_BayerRG2RGB = COLOR_BayerBG2BGR, cv::COLOR_BayerGR2RGB = COLOR_BayerGB2BGR, cv::COLOR_BayerBG2GRAY = 86, cv::COLOR_BayerGB2GRAY = 87, cv::COLOR_BayerRG2GRAY = 88, cv::COLOR_BayerGR2GRAY = 89, cv::COLOR_BayerBG2BGR_VNG = 62, cv::COLOR_BayerGB2BGR_VNG = 63, cv::COLOR_BayerRG2BGR_VNG = 64, cv::COLOR_BayerGR2BGR_VNG = 65, cv::COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG, cv::COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG, cv::COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG, cv::COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG, cv::COLOR_BayerBG2BGR_EA = 135, cv::COLOR_BayerGB2BGR_EA = 136, cv::COLOR_BayerRG2BGR_EA = 137, cv::COLOR_BayerGR2BGR_EA = 138, cv::COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA, cv::COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA, cv::COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA, cv::COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA, cv::COLOR_BayerBG2BGRA = 139, cv::COLOR_BayerGB2BGRA = 140, cv::COLOR_BayerRG2BGRA = 141, cv::COLOR_BayerGR2BGRA = 142, cv::COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA, cv::COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA, cv::COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA, cv::COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA, cv::COLOR_COLORCVT_MAX = 143 |
具体内容可参见OpenCV的文档:Color Space Conversions
③实例操作:
1、网抑云灰色的机器人:
>>> import cv2
>>> src=cv2.imread("F://src.jpg")
>>> dst1=cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
>>> cv2.imwrite("F://1.jpg",dst1)
True
2、san值崩坏的机器人:
>>> dst2=cv2.cvtColor(src,cv2.COLOR_BGR2HSV)
>>> cv2.imwrite("F://2.jpg",dst2)
True
3、阴间冷色调的机器人(BGR空间→RGB空间导致通道的顺序发生了变化):
>>> dst3=cv2.cvtColor(src,cv2.COLOR_BGR2RGB)
>>> cv2.imwrite("F://3.jpg",dst3)
True
结语:本期文章我们学习了cvtColor函数,了解了如何转换图像的色彩空间,以及其中的原理与使用方法,希望各位同学能够活学活用,自己动手实验一下,那可真是十分甚至九分地有趣(赞赏)。
好了以上就是所有的内容,希望大家多多关注,点赞,收藏,这对我有很大的帮助。
祝国康家安,大家下次再见喽!!!溜溜球~~
与 python-openCv 匹配的简单模板
【中文标题】与 python-openCv 匹配的简单模板【英文标题】:Simple template matching with python-openCv 【发布时间】:2015-11-25 15:03:39 【问题描述】:我正在尝试在图像中检测一些简单的“红色图案”。这是我遵循的算法: 1)过滤掉所有其他颜色而不是“红色”并创建黑白图像。我使用带有适当遮罩的“cvtColor”,然后应用“GaussianBlur”来减少噪音。到目前为止一切都很好。
2) 我使用如下函数“matchTemplate”来检测图像中的“箭头”模板。
问题:当'箭头'模板在照片中时,它被正确检测到。 但是当它不在照片中时,算法会检测到其他一些错误的形状。 有人可以修改代码,以便当箭头模板不在图片中时,什么都不会被检测到。 这是我的代码:
template = cv2.imread(address,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image['blured'], template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(self.image['blured'],top_left, bottom_right, 255, 2)
cv2.rectangle(self.image['normal'], top_left, bottom_right, 255,2)
结果如下:
错误检测:
我的模板图像,我完全从主照片中裁剪出来的:
谁能发现我的错误?我是图像处理的新手。提前致谢。
【问题讨论】:
【参考方案1】:您必须查看您的max_val
并为其设置阈值。
假设图像包含箭头时max_val
为x1
,图像不包含箭头时为x2
,则应该是x1 > x2
。作为第一个暂定值,您可以选择threshold=(x1+x2)/2
,然后如果max_val > threshold
,则在图像中找到图案,否则找不到图案。
原因是matchTemplate
滑过
image
,比较大小为w
的重叠块 \timesh
againsttempl
使用指定的方法并存储 比较结果在result
。
因此,无论图像中是否存在箭头,您的图像 res
将始终具有最大值。
【讨论】:
【参考方案2】:非常感谢亚历山德罗·雅各普森。 我修改了代码并以这种方式解决了问题。这几乎是你提出的:
template = cv2.imread(address,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image['blured'], template,cv2.TM_CCOEFF_NORMED)
threshold = 0.9
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(self.image['blured'], pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2.rectangle(self.image['normal'], pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
以这种方式检测模式非常准确。 新问题:它对方向和旋转很敏感。如果它被旋转,它不会检测到图案。例如大约 45 度。 有没有我可以设置的参数,并让算法找到模式,尽管它是旋转或缩放的?
【讨论】:
对于 新问题,您应该提出一个新问题...无论如何,我认为这将与 ***.com/questions/10666436/… 重复,所以请在提出新问题之前问题,阅读答案。以上是关于python-opencv第三期:cvtColor函数详解的主要内容,如果未能解决你的问题,请参考以下文章