颜色转换 cvtColor —— opencv学习记录(三)

颜色转换 cvtColor —— opencv学习记录(三) mini菜 2023-07-10 11:00:13 1094

在我们读取图像时通常会用到imread()函数,里面flags可以决定通道数,来得到我们想要的图像,比如:
-1 按解码得到的方式读入图像;
0 单通道 灰度图;
1 三通道 彩色;
当我们想要其他类型的图像时,这种方法就有一些局限性了,所以我们有必要了解cvtColor 这是一种颜色空间转换函数。
先看源码:

  1. void cvtColor(InputArray src, OutputArray dst, int code, int dstCn = 0);
  2. src 输入图像
  3. dst 输出图像
  4. code 代码颜色空间转换代码
  5. dstCn 目标图像中的信道数; 如果该参数为0,则通道的数量自动从SRCcode派生。

可以支持RGB HSV 等颜色空间转换(建议不要使用人脸尝试,有些效果懂的都懂)

代码:

  1. int main()
  2. {
  3. Mat img1, img2, img3, img4;
  4. img1 = imread("猫1.jpg");
  5. imshow("原图", img1);
  6. cvtColor(img1, img2, COLOR_RGB2GRAY);
  7. imshow("灰度图", img2);
  8. cvtColor(img1, img3, COLOR_RGB2HSV);
  9. imshow("HSV", img3);
  10. cvtColor(img1, img4, COLOR_RGB2BGR);
  11. imshow("BGR", img4);
  12. waitKey(0);
  13. }

效果图:

附:代码颜色空间转换代码

  1. enum ColorConversionCodes {
  2. COLOR_BGR2BGRA = 0, //!< add alpha channel to RGB or BGR image
  3. COLOR_RGB2RGBA = COLOR_BGR2BGRA,
  4. COLOR_BGRA2BGR = 1, //!< remove alpha channel from RGB or BGR image
  5. COLOR_RGBA2RGB = COLOR_BGRA2BGR,
  6. COLOR_BGR2RGBA = 2, //!< convert between RGB and BGR color spaces (with or without alpha channel)
  7. COLOR_RGB2BGRA = COLOR_BGR2RGBA,
  8. COLOR_RGBA2BGR = 3,
  9. COLOR_BGRA2RGB = COLOR_RGBA2BGR,
  10. COLOR_BGR2RGB = 4,
  11. COLOR_RGB2BGR = COLOR_BGR2RGB,
  12. COLOR_BGRA2RGBA = 5,
  13. COLOR_RGBA2BGRA = COLOR_BGRA2RGBA,
  14. COLOR_BGR2GRAY = 6, //!< convert between RGB/BGR and grayscale, @ref color_convert_rgb_gray "color conversions"
  15. COLOR_RGB2GRAY = 7,
  16. COLOR_GRAY2BGR = 8,
  17. COLOR_GRAY2RGB = COLOR_GRAY2BGR,
  18. COLOR_GRAY2BGRA = 9,
  19. COLOR_GRAY2RGBA = COLOR_GRAY2BGRA,
  20. COLOR_BGRA2GRAY = 10,
  21. COLOR_RGBA2GRAY = 11,
  22. COLOR_BGR2BGR565 = 12, //!< convert between RGB/BGR and BGR565 (16-bit images)
  23. COLOR_RGB2BGR565 = 13,
  24. COLOR_BGR5652BGR = 14,
  25. COLOR_BGR5652RGB = 15,
  26. COLOR_BGRA2BGR565 = 16,
  27. COLOR_RGBA2BGR565 = 17,
  28. COLOR_BGR5652BGRA = 18,
  29. COLOR_BGR5652RGBA = 19,
  30. COLOR_GRAY2BGR565 = 20, //!< convert between grayscale to BGR565 (16-bit images)
  31. COLOR_BGR5652GRAY = 21,
  32. COLOR_BGR2BGR555 = 22, //!< convert between RGB/BGR and BGR555 (16-bit images)
  33. COLOR_RGB2BGR555 = 23,
  34. COLOR_BGR5552BGR = 24,
  35. COLOR_BGR5552RGB = 25,
  36. COLOR_BGRA2BGR555 = 26,
  37. COLOR_RGBA2BGR555 = 27,
  38. COLOR_BGR5552BGRA = 28,
  39. COLOR_BGR5552RGBA = 29,
  40. COLOR_GRAY2BGR555 = 30, //!< convert between grayscale and BGR555 (16-bit images)
  41. COLOR_BGR5552GRAY = 31,
  42. COLOR_BGR2XYZ = 32, //!< convert RGB/BGR to CIE XYZ, @ref color_convert_rgb_xyz "color conversions"
  43. COLOR_RGB2XYZ = 33,
  44. COLOR_XYZ2BGR = 34,
  45. COLOR_XYZ2RGB = 35,
  46. COLOR_BGR2YCrCb = 36, //!< convert RGB/BGR to luma-chroma (aka YCC), @ref color_convert_rgb_ycrcb "color conversions"
  47. COLOR_RGB2YCrCb = 37,
  48. COLOR_YCrCb2BGR = 38,
  49. COLOR_YCrCb2RGB = 39,
  50. COLOR_BGR2HSV = 40, //!< convert RGB/BGR to HSV (hue saturation value), @ref color_convert_rgb_hsv "color conversions"
  51. COLOR_RGB2HSV = 41,
  52. COLOR_BGR2Lab = 44, //!< convert RGB/BGR to CIE Lab, @ref color_convert_rgb_lab "color conversions"
  53. COLOR_RGB2Lab = 45,
  54. COLOR_BGR2Luv = 50, //!< convert RGB/BGR to CIE Luv, @ref color_convert_rgb_luv "color conversions"
  55. COLOR_RGB2Luv = 51,
  56. COLOR_BGR2HLS = 52, //!< convert RGB/BGR to HLS (hue lightness saturation), @ref color_convert_rgb_hls "color conversions"
  57. COLOR_RGB2HLS = 53,
  58. COLOR_HSV2BGR = 54, //!< backward conversions to RGB/BGR
  59. COLOR_HSV2RGB = 55,
  60. COLOR_Lab2BGR = 56,
  61. COLOR_Lab2RGB = 57,
  62. COLOR_Luv2BGR = 58,
  63. COLOR_Luv2RGB = 59,
  64. COLOR_HLS2BGR = 60,
  65. COLOR_HLS2RGB = 61,
  66. COLOR_BGR2HSV_FULL = 66,
  67. COLOR_RGB2HSV_FULL = 67,
  68. COLOR_BGR2HLS_FULL = 68,
  69. COLOR_RGB2HLS_FULL = 69,
  70. COLOR_HSV2BGR_FULL = 70,
  71. COLOR_HSV2RGB_FULL = 71,
  72. COLOR_HLS2BGR_FULL = 72,
  73. COLOR_HLS2RGB_FULL = 73,
  74. COLOR_LBGR2Lab = 74,
  75. COLOR_LRGB2Lab = 75,
  76. COLOR_LBGR2Luv = 76,
  77. COLOR_LRGB2Luv = 77,
  78. COLOR_Lab2LBGR = 78,
  79. COLOR_Lab2LRGB = 79,
  80. COLOR_Luv2LBGR = 80,
  81. COLOR_Luv2LRGB = 81,
  82. COLOR_BGR2YUV = 82, //!< convert between RGB/BGR and YUV
  83. COLOR_RGB2YUV = 83,
  84. COLOR_YUV2BGR = 84,
  85. COLOR_YUV2RGB = 85,
  86. //! YUV 4:2:0 family to RGB
  87. COLOR_YUV2RGB_NV12 = 90,
  88. COLOR_YUV2BGR_NV12 = 91,
  89. COLOR_YUV2RGB_NV21 = 92,
  90. COLOR_YUV2BGR_NV21 = 93,
  91. COLOR_YUV420sp2RGB = COLOR_YUV2RGB_NV21,
  92. COLOR_YUV420sp2BGR = COLOR_YUV2BGR_NV21,
  93. COLOR_YUV2RGBA_NV12 = 94,
  94. COLOR_YUV2BGRA_NV12 = 95,
  95. COLOR_YUV2RGBA_NV21 = 96,
  96. COLOR_YUV2BGRA_NV21 = 97,
  97. COLOR_YUV420sp2RGBA = COLOR_YUV2RGBA_NV21,
  98. COLOR_YUV420sp2BGRA = COLOR_YUV2BGRA_NV21,
  99. COLOR_YUV2RGB_YV12 = 98,
  100. COLOR_YUV2BGR_YV12 = 99,
  101. COLOR_YUV2RGB_IYUV = 100,
  102. COLOR_YUV2BGR_IYUV = 101,
  103. COLOR_YUV2RGB_I420 = COLOR_YUV2RGB_IYUV,
  104. COLOR_YUV2BGR_I420 = COLOR_YUV2BGR_IYUV,
  105. COLOR_YUV420p2RGB = COLOR_YUV2RGB_YV12,
  106. COLOR_YUV420p2BGR = COLOR_YUV2BGR_YV12,
  107. COLOR_YUV2RGBA_YV12 = 102,
  108. COLOR_YUV2BGRA_YV12 = 103,
  109. COLOR_YUV2RGBA_IYUV = 104,
  110. COLOR_YUV2BGRA_IYUV = 105,
  111. COLOR_YUV2RGBA_I420 = COLOR_YUV2RGBA_IYUV,
  112. COLOR_YUV2BGRA_I420 = COLOR_YUV2BGRA_IYUV,
  113. COLOR_YUV420p2RGBA = COLOR_YUV2RGBA_YV12,
  114. COLOR_YUV420p2BGRA = COLOR_YUV2BGRA_YV12,
  115. COLOR_YUV2GRAY_420 = 106,
  116. COLOR_YUV2GRAY_NV21 = COLOR_YUV2GRAY_420,
  117. COLOR_YUV2GRAY_NV12 = COLOR_YUV2GRAY_420,
  118. COLOR_YUV2GRAY_YV12 = COLOR_YUV2GRAY_420,
  119. COLOR_YUV2GRAY_IYUV = COLOR_YUV2GRAY_420,
  120. COLOR_YUV2GRAY_I420 = COLOR_YUV2GRAY_420,
  121. COLOR_YUV420sp2GRAY = COLOR_YUV2GRAY_420,
  122. COLOR_YUV420p2GRAY = COLOR_YUV2GRAY_420,
  123. //! YUV 4:2:2 family to RGB
  124. COLOR_YUV2RGB_UYVY = 107,
  125. COLOR_YUV2BGR_UYVY = 108,
  126. //COLOR_YUV2RGB_VYUY = 109,
  127. //COLOR_YUV2BGR_VYUY = 110,
  128. COLOR_YUV2RGB_Y422 = COLOR_YUV2RGB_UYVY,
  129. COLOR_YUV2BGR_Y422 = COLOR_YUV2BGR_UYVY,
  130. COLOR_YUV2RGB_UYNV = COLOR_YUV2RGB_UYVY,
  131. COLOR_YUV2BGR_UYNV = COLOR_YUV2BGR_UYVY,
  132. COLOR_YUV2RGBA_UYVY = 111,
  133. COLOR_YUV2BGRA_UYVY = 112,
  134. //COLOR_YUV2RGBA_VYUY = 113,
  135. //COLOR_YUV2BGRA_VYUY = 114,
  136. COLOR_YUV2RGBA_Y422 = COLOR_YUV2RGBA_UYVY,
  137. COLOR_YUV2BGRA_Y422 = COLOR_YUV2BGRA_UYVY,
  138. COLOR_YUV2RGBA_UYNV = COLOR_YUV2RGBA_UYVY,
  139. COLOR_YUV2BGRA_UYNV = COLOR_YUV2BGRA_UYVY,
  140. COLOR_YUV2RGB_YUY2 = 115,
  141. COLOR_YUV2BGR_YUY2 = 116,
  142. COLOR_YUV2RGB_YVYU = 117,
  143. COLOR_YUV2BGR_YVYU = 118,
  144. COLOR_YUV2RGB_YUYV = COLOR_YUV2RGB_YUY2,
  145. COLOR_YUV2BGR_YUYV = COLOR_YUV2BGR_YUY2,
  146. COLOR_YUV2RGB_YUNV = COLOR_YUV2RGB_YUY2,
  147. COLOR_YUV2BGR_YUNV = COLOR_YUV2BGR_YUY2,
  148. COLOR_YUV2RGBA_YUY2 = 119,
  149. COLOR_YUV2BGRA_YUY2 = 120,
  150. COLOR_YUV2RGBA_YVYU = 121,
  151. COLOR_YUV2BGRA_YVYU = 122,
  152. COLOR_YUV2RGBA_YUYV = COLOR_YUV2RGBA_YUY2,
  153. COLOR_YUV2BGRA_YUYV = COLOR_YUV2BGRA_YUY2,
  154. COLOR_YUV2RGBA_YUNV = COLOR_YUV2RGBA_YUY2,
  155. COLOR_YUV2BGRA_YUNV = COLOR_YUV2BGRA_YUY2,
  156. COLOR_YUV2GRAY_UYVY = 123,
  157. COLOR_YUV2GRAY_YUY2 = 124,
  158. //CV_YUV2GRAY_VYUY = CV_YUV2GRAY_UYVY,
  159. COLOR_YUV2GRAY_Y422 = COLOR_YUV2GRAY_UYVY,
  160. COLOR_YUV2GRAY_UYNV = COLOR_YUV2GRAY_UYVY,
  161. COLOR_YUV2GRAY_YVYU = COLOR_YUV2GRAY_YUY2,
  162. COLOR_YUV2GRAY_YUYV = COLOR_YUV2GRAY_YUY2,
  163. COLOR_YUV2GRAY_YUNV = COLOR_YUV2GRAY_YUY2,
  164. //! alpha premultiplication
  165. COLOR_RGBA2mRGBA = 125,
  166. COLOR_mRGBA2RGBA = 126,
  167. //! RGB to YUV 4:2:0 family
  168. COLOR_RGB2YUV_I420 = 127,
  169. COLOR_BGR2YUV_I420 = 128,
  170. COLOR_RGB2YUV_IYUV = COLOR_RGB2YUV_I420,
  171. COLOR_BGR2YUV_IYUV = COLOR_BGR2YUV_I420,
  172. COLOR_RGBA2YUV_I420 = 129,
  173. COLOR_BGRA2YUV_I420 = 130,
  174. COLOR_RGBA2YUV_IYUV = COLOR_RGBA2YUV_I420,
  175. COLOR_BGRA2YUV_IYUV = COLOR_BGRA2YUV_I420,
  176. COLOR_RGB2YUV_YV12 = 131,
  177. COLOR_BGR2YUV_YV12 = 132,
  178. COLOR_RGBA2YUV_YV12 = 133,
  179. COLOR_BGRA2YUV_YV12 = 134,
  180. //! Demosaicing
  181. COLOR_BayerBG2BGR = 46,
  182. COLOR_BayerGB2BGR = 47,
  183. COLOR_BayerRG2BGR = 48,
  184. COLOR_BayerGR2BGR = 49,
  185. COLOR_BayerBG2RGB = COLOR_BayerRG2BGR,
  186. COLOR_BayerGB2RGB = COLOR_BayerGR2BGR,
  187. COLOR_BayerRG2RGB = COLOR_BayerBG2BGR,
  188. COLOR_BayerGR2RGB = COLOR_BayerGB2BGR,
  189. COLOR_BayerBG2GRAY = 86,
  190. COLOR_BayerGB2GRAY = 87,
  191. COLOR_BayerRG2GRAY = 88,
  192. COLOR_BayerGR2GRAY = 89,
  193. //! Demosaicing using Variable Number of Gradients
  194. COLOR_BayerBG2BGR_VNG = 62,
  195. COLOR_BayerGB2BGR_VNG = 63,
  196. COLOR_BayerRG2BGR_VNG = 64,
  197. COLOR_BayerGR2BGR_VNG = 65,
  198. COLOR_BayerBG2RGB_VNG = COLOR_BayerRG2BGR_VNG,
  199. COLOR_BayerGB2RGB_VNG = COLOR_BayerGR2BGR_VNG,
  200. COLOR_BayerRG2RGB_VNG = COLOR_BayerBG2BGR_VNG,
  201. COLOR_BayerGR2RGB_VNG = COLOR_BayerGB2BGR_VNG,
  202. //! Edge-Aware Demosaicing
  203. COLOR_BayerBG2BGR_EA = 135,
  204. COLOR_BayerGB2BGR_EA = 136,
  205. COLOR_BayerRG2BGR_EA = 137,
  206. COLOR_BayerGR2BGR_EA = 138,
  207. COLOR_BayerBG2RGB_EA = COLOR_BayerRG2BGR_EA,
  208. COLOR_BayerGB2RGB_EA = COLOR_BayerGR2BGR_EA,
  209. COLOR_BayerRG2RGB_EA = COLOR_BayerBG2BGR_EA,
  210. COLOR_BayerGR2RGB_EA = COLOR_BayerGB2BGR_EA,
  211. //! Demosaicing with alpha channel
  212. COLOR_BayerBG2BGRA = 139,
  213. COLOR_BayerGB2BGRA = 140,
  214. COLOR_BayerRG2BGRA = 141,
  215. COLOR_BayerGR2BGRA = 142,
  216. COLOR_BayerBG2RGBA = COLOR_BayerRG2BGRA,
  217. COLOR_BayerGB2RGBA = COLOR_BayerGR2BGRA,
  218. COLOR_BayerRG2RGBA = COLOR_BayerBG2BGRA,
  219. COLOR_BayerGR2RGBA = COLOR_BayerGB2BGRA,
  220. COLOR_COLORCVT_MAX = 143
  221. };
声明:本文内容由易百纳平台入驻作者撰写,文章观点仅代表作者本人,不代表易百纳立场。如有内容侵权或者其他问题,请联系本站进行删除。
mini菜
红包 点赞 收藏 评论 打赏
评论
0个
内容存在敏感词
手气红包
    易百纳技术社区暂无数据
相关专栏
置顶时间设置
结束时间
删除原因
  • 广告/SPAM
  • 恶意灌水
  • 违规内容
  • 文不对题
  • 重复发帖
打赏作者
易百纳技术社区
mini菜
您的支持将鼓励我继续创作!
打赏金额:
¥1易百纳技术社区
¥5易百纳技术社区
¥10易百纳技术社区
¥50易百纳技术社区
¥100易百纳技术社区
支付方式:
微信支付
支付宝支付
易百纳技术社区微信支付
易百纳技术社区
打赏成功!

感谢您的打赏,如若您也想被打赏,可前往 发表专栏 哦~

举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

审核成功

发布时间设置
发布时间:
是否关联周任务-专栏模块

审核失败

失败原因
备注
拼手气红包 红包规则
祝福语
恭喜发财,大吉大利!
红包金额
红包最小金额不能低于5元
红包数量
红包数量范围10~50个
余额支付
当前余额:
可前往问答、专栏板块获取收益 去获取
取 消 确 定

小包子的红包

恭喜发财,大吉大利

已领取20/40,共1.6元 红包规则

    易百纳技术社区