{"id":17277579,"url":"https://github.com/sherry-xll/hsv2gray","last_synced_at":"2026-04-28T11:36:16.880Z","repository":{"id":201451330,"uuid":"400421696","full_name":"Sherry-XLL/HSV2Gray","owner":"Sherry-XLL","description":"Have a basic understanding of RGB, HSV and Grayscale.","archived":false,"fork":false,"pushed_at":"2021-08-27T07:46:33.000Z","size":3054,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T14:19:07.060Z","etag":null,"topics":["grayscale","hsv","opencv","python","rgb"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sherry-XLL.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-08-27T07:12:45.000Z","updated_at":"2024-03-28T02:52:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"31d3eb67-e4dd-4a7b-84a9-e488469f51eb","html_url":"https://github.com/Sherry-XLL/HSV2Gray","commit_stats":null,"previous_names":["sherry-xll/hsv2gray"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Sherry-XLL/HSV2Gray","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sherry-XLL%2FHSV2Gray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sherry-XLL%2FHSV2Gray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sherry-XLL%2FHSV2Gray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sherry-XLL%2FHSV2Gray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sherry-XLL","download_url":"https://codeload.github.com/Sherry-XLL/HSV2Gray/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sherry-XLL%2FHSV2Gray/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32379617,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T11:25:28.583Z","status":"ssl_error","status_checked_at":"2026-04-28T11:25:05.435Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grayscale","hsv","opencv","python","rgb"],"created_at":"2024-10-15T09:09:23.572Z","updated_at":"2026-04-28T11:36:16.863Z","avatar_url":"https://github.com/Sherry-XLL.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 一、引言\n\n**HSV**(Hue, Saturation, Value)，也称六角锥体模型(Hexcone Model)，是一种较为直观的颜色模型，包含色调(H)，饱和度(S)和明度(V)三个参数。\n\n- 色调 Hue (H)\n色调 H 反映所处的光谱颜色的位置，在六角锥体模型中用角度度量，取值范围为 0°～360°。RGB 三个通道的取值范围通常是 0-255，因此 OpenCV、[ncnn](https://github.com/Tencent/ncnn) 等库倾向于用 UCHAR 表示颜色。由于 360 超出了 UCHAR 的表示范围，不同的软件在对 H 进行量化时的取值范围有所差异。[OpenCV](https://github.com/opencv/opencv/blob/master/modules/imgproc/src/color_hsv.simd.hpp \"OpenCV\") 实现了三种范围：0-360， 0-180 和 0-255，本质都是对 H 进行线性变换，将其限制在给定的范围中，cv2 默认 H 的取值范围为 0-180。\n- 饱和度 Saturation (S)\n饱和度 S 表示颜色接近光谱色的程度，为一比例值，通常取值范围为 0%～100%，OpenCV 将 S 的范围扩大至 0-255。\n- 亮度 Value (V)\n亮度 V 表示色彩的明亮程度，通常取值范围为 0%～100%，OpenCV 同样将 V 的范围扩大至 0-255。\n- 颜色对照表\n\n\u003cdiv  align=\"center\"\u003e  \n  \n|   |  黑 | 灰 | 白 | 红 | 橙 | 黄 | 绿 | 青 | 蓝 | 紫 |\n| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |\n| hmin | 0 | 0 | 0 | 0 / 156 | 11 | 26 | 35 | 78 | 100 | 125 |\n| hmax | 180 | 180 | 180 | 10 / 180 | 25 | 34 | 77 | 99 | 124 | 155 |\n| smin | 0 | 0 | 0 | 43  | 43  | 43 | 43 | 43 | 43 | 43 |\n| smax | 255 | 43 | 30 | 255 | 255 | 255 | 255 | 255 | 255 | 255 |\n| vmin | 0 | 46 | 221 | 46 | 46 | 46 | 46 | 46 | 46 | 46 |\n| vmax | 46 | 220 | 255 | 255 | 255 | 255 | 255 | 255 | 255 | 255 |\n  \n\u003c/div\u003e\n  \n本文使用的图片为 **Lenna 测试图**，即雷娜图。雷娜图（Lenna or Lena）是图像处理领域最受欢迎的测试图，图片中的美丽女性为《花花公子》的玩伴女郎雷娜（LenaSoderberg）。\n\n\u003cdiv  align=\"center\"\u003e    \n\u003cimg src=\"images/Lenna.png\" width = 250 height = 250 /\u003e\n\u003c/div\u003e\n\n文中对 RGB 三个颜色通道、转换后的 HSV 以及灰度图进行可视化，得出 HSV 和 Gray 进行颜色转换的方法。代码已上传至 [Github](https://github.com/Sherry-XLL/HSV2Gray)，如有建议欢迎留言评论。\n\n## 二、为什么需要 HSV ？\n\n尽管 RGB 是电视和数码相机中使用最广泛的图像表示法，但由于颜色之间的高度相关性，在目标识别和颜色提取任务中人们更多地使用 HSV 而不是 RGB。下图显示了 Lenna 图在 R、G、B 三个通道的颜色分割，可以观察到三个通道中的颜色协同变化，暴露了基于 RGB 模式识别的主要困难。\n\n```python\nimage = cv2.imread(\"lenna.png\")\n\n# cv2 默认为 BGR 格式，plt.imshow 默认为 RGB 格式\n(b, g, r) = cv2.split(image)\nbgr = cv2.merge([r, g, b])\n\n# show BGR\nplt.subplot(2, 2, 1)\nplt.imshow(bgr)\nplt.axis('off')\nplt.title('original image (BGR)')\n\nB = cv2.merge([zero, zero, b])\nplt.subplot(2, 2, 2)\nplt.imshow(B)\nplt.axis('off')\nplt.title('B')\n\nG = cv2.merge([zero, g, zero])\nplt.subplot(2, 2, 3)\nplt.imshow(G)\nplt.axis('off')\nplt.title('G')\n\nR = cv2.merge([r, zero, zero])\nplt.subplot(2, 2, 4)\nplt.imshow(R)\nplt.axis('off')\nplt.title('R')\nplt.show()\n```\n\n\n\u003cdiv  align=\"center\"\u003e    \n\u003cimg src=\"images/bgr.png\" width = 550 height = 550 /\u003e\n\u003c/div\u003e\n\n## 三、HSV 三个分量表示什么？\n\nHSV 包含色调(H)，饱和度(S)和明度(V)三个参数，下图基于 HSV 分割，将 HSV 三个通道的分量用灰度图进行表示。与 RGB 的颜色通道相比，色调和饱和度通道不受光照条件的影响，从而支持对象边界的有效识别。\n\n```python\nhsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n(h, s, v) = cv2.split(hsv)\n```\n\n![在这里插入图片描述](images/hsv.png#pic_center)\n\n\n其中，Value 分量表示色彩的明亮程度，通常取值范围为 0%～100%，在 ncnn 和 OpenCV 中都将 V 的范围扩大至了 0-255，此时 HSV 的 V 分量图即为 HSV 转换为灰度图的一种粗略做法。\n\n## 四、绘图误区\n\n值得注意的是，RGB 与 HSV 是两种不同的颜色表示方式，转换后每个颜色通道的意义和数值发生了改变，但仍表示原来的图片，即 RGB、HSV 只是颜色的不同表示方式，在 Photoshop 中可以有更直观的认识：\n\n\u003cdiv  align=\"center\"\u003e    \n\u003cimg src=\"images/前景色.jpg\" width = 450 height = 350 /\u003e\n\u003c/div\u003e\n\n网络上经常可以看到使用库函数将 RGB 转换为 HSV 表示，然后直接用 imshow 绘制图片，称其为对应的 HSV 图像，这是不对的：\n\n```python\nimage = cv2.imread(\"Lenna.png\")\nhsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\nplt.imshow(hsv)\nplt.title('HSV (plt.imshow)')\nplt.show()\n```\n\n![在这里插入图片描述](images/hsvshow.png#pic_center)\n\n\n在 Python 中，matplotlib.pyplot 的 imshow 默认所展示图像的三个通道为 R、G 和 B；cv2 的 imshow 默认所展示图像的三个通道为 B、G 和 R。也就是说，imshow 将 HSV 三个通道的分量当作 RGB 或 BGR 的三个通道得到了如上输出，这样得到的图片从理论上来说是没有意义的。\n\n## 五、HSV 和灰度图的转换\n\n尽管将 HSV 的 V 分量分离得到的图片可以作为灰度图，但图像质量较低。为了得到高质量的灰度图，常用的 HSV 转为灰度图的方法是以 RGB 作为中介。RGB 转换为灰度图的常用公式如下：\n\n\u003e Gray = R \\* 0.299 + G \\* 0.587 + B \\* 0.114\n\n![在这里插入图片描述](images/grayscale.png#pic_center)\n\n对比 RGB 值计算得到的灰度图和 HSV 的 V 分量图，不难看出 RGB 公式转换的灰度图质量更高：\n![在这里插入图片描述](images/graycmp.png#pic_center)\n\n因此，将 HSV 转换为灰度图的代码与 HSV 转换为 RGB 的代码类似，先将 HSV 按照公式转换为 RGB 的颜色表示，再根据RGB 转换成灰度图像的常用公式计算对应的灰度值即可，即 HSV -\u003e RGB -\u003e Gray，详见 [HSL and HSV](https://en.wikipedia.org/wiki/HSL_and_HSV)，此处不再赘述。\n\n将三通道的彩色图片转换为灰度图后失去了原图片的部分信息，因此无法实现一一对应的双向转换，在灰度图转 HSV 时只能进行近似处理，直接将灰度值赋于 V，然后 H 和 S 取值为 0.\n\n## 六、参考链接\n\n1. [Github 源码](https://github.com/Sherry-XLL/HSV2Gray)\n2. [ncnn](https://github.com/Tencent/ncnn)\n3. [Lenna](https://en.wikipedia.org/wiki/Lenna)\n4. [OpenCV](https://github.com/opencv/opencv/blob/master/modules/imgproc/src/color_hsv.simd.hpp \"OpenCV\")\n5. [HSL and HSV](https://en.wikipedia.org/wiki/HSL_and_HSV)\n6. [Add convert color hsv](https://github.com/Tencent/ncnn/pull/3119)\n7. [A high performance terabyte-order RGB to HSV parallel conversion implementation](https://www.tec.ac.cr/sites/default/files/media/doc/final_report_2015_2.pdf)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsherry-xll%2Fhsv2gray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsherry-xll%2Fhsv2gray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsherry-xll%2Fhsv2gray/lists"}