agentsclimarketplace

基于hsv距离与形态学的图像透明化处理

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/基于hsv距离与形态学的图像透明化处理

使用Python和OpenCV,通过计算HSV颜色空间中的欧氏距离(考虑色调环形特性)精确识别并剔除指定颜色,结合形态学膨胀扩展透明区域,最终输出带透明通道的PNG图片。From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill 基于hsv距离与形态学的图像透明化处理

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

3.1 KB, 970 tokens by cl100k_base, as published. Nobody here has run it

基于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)。
  • 确保代码逻辑清晰,正确处理了颜色空间转换和形态学操作。

Triggers

  • 图片颜色透明化
  • 去除背景色
  • HSV颜色处理
  • OpenCV透明区域扩展
  • 根据颜色值去底
  • python提取非指定颜色并保存透明png

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.