移除除指定颜色外的所有颜色 - RemoveOtherColors
函数简介
移除图像中除指定颜色范围外的所有颜色,将非指定颜色范围的像素设置为透明。此函数可用于图像处理中的颜色提取、背景去除等场景。支持多个颜色范围的指定,每个颜色范围可以设置不同的匹配类型。
接口名称
RemoveOtherColors
DLL调用
long RemoveOtherColors(long ola, long ptr, string colorList)
参数定义:
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。ptr
(长整型数): OLAImage对象的地址colorList
(字符串): 颜色列表的JSON字符串,格式如下:[ { "StartColor": "3278FA", // 起始颜色(RRGGBB格式) "EndColor": "6496FF", // 结束颜色(RRGGBB格式) "Type": 0 // 匹配类型:0-普通模式取合集,1-反色模式取合集,2-普通模式取交集,3-反色模式取交集 } ]
示例:
// 定义要保留的颜色范围
string colorList = R"([
{
"StartColor": "3278FA",
"EndColor": "6496FF",
"Type": 0
},
{
"StartColor": "FF0000",
"EndColor": "FF3333",
"Type": 1
}
])";
// 加载原始图片
long originalImage = LoadImage(ola, "original.png");
if (originalImage != 0) {
// 移除其他颜色
long resultImage = RemoveOtherColors(ola, originalImage, colorList);
if (resultImage != 0) {
// 保存处理后的图片
SaveImageFromPtr(ola, resultImage, "result.png");
// 释放内存
FreeImagePtr(ola, resultImage);
}
// 释放原始图片内存
FreeImagePtr(ola, originalImage);
}
COM调用
long RemoveOtherColors(long ptr, string colorList)
参数定义:
ptr
(长整型数): OLAImage对象的地址colorList
(字符串): 颜色列表的JSON字符串,格式同DLL调用
示例:
# 定义要保留的颜色范围
colorList = '''[
{
"StartColor": "3278FA",
"EndColor": "6496FF",
"Type": 0
},
{
"StartColor": "FF0000",
"EndColor": "FF3333",
"Type": 1
}
]'''
# 加载原始图片
originalImage = ola.LoadImage("original.png")
if originalImage:
# 移除其他颜色
resultImage = ola.RemoveOtherColors(originalImage, colorList)
if resultImage:
# 保存处理后的图片
ola.SaveImageFromPtr(resultImage, "result.png")
# 释放内存
ola.FreeImagePtr(resultImage)
# 释放原始图片内存
ola.FreeImagePtr(originalImage)
返回值
长整型数:
- 成功:返回处理后的OLAImage对象地址
- 失败:返回0
注意事项
- 颜色值必须使用RRGGBB格式的十六进制字符串
- 支持多个颜色范围的指定,每个范围可以设置不同的匹配类型
- 颜色匹配类型说明:
- 0: 正常匹配,保留在颜色范围内的像素
- 1: 反色匹配,保留在颜色范围外的像素
- 2: 正常交集匹配,保留在颜色范围内的像素取交集
- 3: 反色交集匹配,保留在颜色范围外的像素取交集
- 处理后的图片中,非指定颜色范围的像素将被设置为透明
- 原始图片不会被修改,函数返回新的图片对象
- 使用完毕后必须调用 FreeImagePtr 释放内存
- 颜色范围必须有效,起始颜色不能大于结束颜色
- 建议在使用前检查图片指针和颜色列表的有效性
- 处理大图片时注意内存使用
- 颜色匹配是精确的,如果需要模糊匹配,请使用其他相关函数