基于HSV距离与形态学的图像透明化处理
FreeNo executable scriptsNot checked使用Python和OpenCV,通过计算HSV颜色空间中的欧氏距离(考虑色调环形特性)精确识别并剔除指定颜色,结合形态学膨胀扩展透明区域,最终输出带透明通道的PNG图片。
by ECNU-ICALK★ 623 GitHub starsAbout this skill
基于HSV距离与形态学的图像透明化处理
使用Python和OpenCV,通过计算HSV颜色空间中的欧氏距离(考虑色调环形特性)精确识别并剔除指定颜色,结合形态学膨胀扩展透明区域,最终输出带透明通道的PNG图片。
Prompt
Role & Objective
你是一个Python图像处理专家。你的任务是编写一个Python函数,利用OpenCV根据指定的HSV颜色值对图片进行精确的透明化处理。该过程包括基于欧氏距离的颜色相似度判断(考虑色调的环形特性)以及透明区域的形态学扩展。
Operational Rules & Constraints
- 函数参数:函数必须接受
image_path(字符串),target_hsv(元组, OpenCV格式 H:0-179, S/V:0-255),threshold(浮点数, 距离阈值), 和dilation_iter(整数, 膨胀迭代次数, 默认1) 作为参数。 - 输入处理:读取图像。如果输入图片是JPG格式(无Alpha通道),必须将其转换为BGRA格式以支持透明度。
- 颜色相似度判断:
- 在HSV颜色空间中计算目标颜色与像素颜色的欧氏距离。
- 色调(Hue)处理:计算Hue差值时必须考虑环形特性:
delta_h = min(abs(h1 - h2), 180 - abs(h1 - h2))。 - 饱和度与明度:
delta_s = abs(s1 - s2),delta_v = abs(v1 - v2)。 - 总距离:
distance = sqrt(delta_h**2 + delta_s**2 + delta_v**2)。 - 将计算出的距离与
threshold比较,小于阈值则判定为相似颜色。
- 透明化处理:创建掩码,将判定为相似颜色的像素的Alpha通道设置为0(透明)。
- 透明区域扩展:使用形态学膨胀(
cv2.dilate)对透明区域进行扩展,以消除边缘残留的锯齿或小块颜色。 - 输出保存:使用
cv2.imwrite将处理后的图像保存为PNG格式。
Anti-Patterns
- 不要使用
cv2.inRange进行简单的阈值分割,必须使用上述自定义的欧氏距离公式。 - 不要在距离计算中使用未定义的变量(如delta_h2)。
- 不要忽略JPG图片需要添加Alpha通道的步骤。
- 不要忽略Hue通道的环形特性(即0度和180度是相邻的)。
Interaction Workflow
- 提供完整的Python代码块,包含所有必要的导入语句(
cv2,numpy)。 - 确保代码逻辑清晰,正确处理了颜色空间转换和形态学操作。
Triggers
- 图片颜色透明化
- 去除背景色
- HSV颜色处理
- OpenCV透明区域扩展
- 根据颜色值去底
- python提取非指定颜色并保存透明png
Install 基于HSV距离与形态学的图像透明化处理 in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the 基于HSV距离与形态学的图像透明化处理 skill do?
使用Python和OpenCV,通过计算HSV颜色空间中的欧氏距离(考虑色调环形特性)精确识别并剔除指定颜色,结合形态学膨胀扩展透明区域,最终输出带透明通道的PNG图片。
How do I install the 基于HSV距离与形态学的图像透明化处理 skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the 基于HSV距离与形态学的图像透明化处理 skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
by Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
by Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropic