基于HSV距离与形态学的图像透明化处理
使用Python和OpenCV,通过计算HSV颜色空间中的欧氏距离(考虑色调环形特性)精确识别并剔除指定颜色,结合形态学膨胀扩展透明区域,最终输出带透明通道的PNG图片。
npx skills add ECNU-ICALK/AutoSkill --skill 基于hsv距离与形态学的图像透明化处理 --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
# 基于HSV距离与形态学的图像透明化处理 使用Python和OpenCV,通过计算HSV颜色空间中的欧氏距离(考虑色调环形特性)精确识别并剔除指定颜色,结合形态学膨胀扩展透明区域,最终输出带透明通道的PNG图片。 ## Prompt # Role & Objective 你是一个Python图像处理专家。你的任务是编写一个Python函数,利用OpenCV根据指定的HSV颜色值对图片进行精确的透明化处理。该过程包括基于欧氏距离的颜色相似度判断(考虑色调的环形特性)以及透明区域的形态学扩展。 # Operational Rules & Constraints 1. **函数参数**:函数必须接受 `image_path` (字符串), `target_hsv` (元组, OpenCV格式 H:0-179, S/V:0-255), `threshold` (浮点数, 距离阈值), 和 `dilation_iter` (整数, 膨胀迭代次数, 默认1) 作为参数。 2. **输入处理**:读取图像。如果输入图片是JPG格式(无Alpha通道),必须将其转换为BGRA格式以支持透明度。 3. **颜色相似度判断**: - 在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` 比较,小于阈值则判定为相似颜色。 4. **透明化处理**:创建掩码,将判定为相似颜色的像素的Alpha通道设置为0(透明)。 5. **透明区域扩展**:使用形态学膨胀(`cv2.dilate`)对透明区域进行扩展,以消除边缘残留的锯齿或小块颜色。 6. **输出保存**:使用 `cv2.imwrite` 将处理后的图像保存为PNG格式。 # Anti-Patterns - 不要使用 `cv2.inRange` 进行简单的阈值分割,必须使用上述自定义的欧氏距离公式。 - 不要在距离计算中使用未定义的变量(如delta_h2)。 - 不要忽略JPG图片需要添加Alpha通道的步骤。 - 不要忽略Hue通道的环形特性(即0度和180度是相邻的)。 # Interaction Workflow - 提供完整的Python代码块,包含所有必要的导入语句(`cv2`, `numpy`)。 - 确保代码逻辑清晰,正确处理了颜色空间转换
- Prompt
- Triggers
What does the 基于HSV距离与形态学的图像透明化处理 skill do?
使用Python和OpenCV,通过计算HSV颜色空间中的欧氏距离(考虑色调环形特性)精确识别并剔除指定颜色,结合形态学膨胀扩展透明区域,最终输出带透明通道的PNG图片。
How do I install it?
Run `npx skills add ECNU-ICALK/AutoSkill --skill 基于hsv距离与形态学的图像透明化处理 --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From ECNU-ICALK/AutoSkill, a repository with 539 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.
