struct ImageExtractColor : INode {
virtual void apply() override {
auto image = get_input<PrimitiveObject>("image");
auto background = get_input2<std::string>("background");
auto &ud = image->userData();
int w = ud.get2<int>("w");
int h = ud.get2<int>("h");
vec3f up = get_input2<vec3f>("high_threshold");
vec3f low = get_input2<vec3f>("low_threshold");
float upr = up[0]/255, upg = up[1]/255, upb = up[2]/255;
float lr = low[0]/255,lg = low[1]/255,lb = low[2]/255;
zeno::log_info("up:{}, {}, {}",upr,upg,upb);
zeno::log_info("low:{}, {}, {}",lr,lg,lb);
if(background == "transparent"){
if(!image->has_attr("alpha")){
image->verts.add_attr<float>("alpha");
for(int i = 0; i < image->verts.size();i++){
image->verts.attr<float>("alpha")[i] = 1;
}
}
for (auto i = 0; i < image->verts.size(); i++) {
if(((upr < image->verts[i][0]) || (image->verts[i][0] < lr)) ||
((upg < image->verts[i][1]) || (image->verts[i][1] < lg)) ||
((upb < image->verts[i][2]) || (image->verts[i][2] < lb))){
image->verts.attr<float>("alpha")[i] = 0;
}
}
}
else if(background == "black"){
for (auto i = 0; i < image->verts.size(); i++) {
if(((upr < image->verts[i][0]) || (image->verts[i][0] < lr)) ||
((upg < image->verts[i][1]) || (image->verts[i][1] < lg)) ||
((upb < image->verts[i][2]) || (image->verts[i][2] < lb))){
image->verts[i] = {0,0,0};
}
}
}
else if(background == "white"){
for (auto i = 0; i < image->verts.size(); i++) {
if(((upr < image->verts[i][0]) || (image->verts[i][0] < lr)) ||
((upg < image->verts[i][1]) || (image->verts[i][1] < lg)) ||
((upb < image->verts[i][2]) || (image->verts[i][2] < lb))){
image->verts[i] = {1,1,1};
}
}
}
set_output("image", image);
}
};
ZENDEFNODE(ImageExtractColor, {
{
{"image"},
{"vec3f", "high_threshold", "255,255,255"},
{"vec3f", "low_threshold", "0,0,0"},
{"enum transparent black white", "background", "transparent"},
},
{
{"image"},
},
{},
{ "image" },
});
struct ImageDelColor: INode {
void apply() override {
auto image = get_input<PrimitiveObject>("image");
auto background = get_input2<std::string>("background");
auto &ud = image->userData();
int w = ud.get2<int>("w");
int h = ud.get2<int>("h");
vec3f up = get_input2<vec3f>("high_threshold");
vec3f low = get_input2<vec3f>("low_threshold");
float upr = up[0]/255, upg = up[1]/255, upb = up[2]/255;
float lr = low[0]/255,lg = low[1]/255,lb = low[2]/255;
if(background == "transparent"){
if(!image->has_attr("alpha")){
image->verts.add_attr<float>("alpha");
for(int i = 0; i < image->verts.size();i++){
image->verts.attr<float>("alpha")[i] = 1;
}
}
for (auto i = 0; i < image->verts.size(); i++) {
if(((lr <= image->verts[i][0]) && (image->verts[i][0] <= upr)) &&
((lg <= image->verts[i][1]) && (image->verts[i][1] <= upg)) &&
((lb <= image->verts[i][2]) && (image->verts[i][2] <= upb))){
image->verts.attr<float>("alpha")[i] = 0;
}
}
}
else if(background == "black"){
for (auto i = 0; i < image->verts.size(); i++) {
if(((lr <= image->verts[i][0]) && (image->verts[i][0] <= upr)) &&
((lg <= image->verts[i][1]) && (image->verts[i][1] <= upg)) &&
((lb <= image->verts[i][2]) && (image->verts[i][2] <= upb))){
image->verts[i] = {0,0,0};
}
}
}
else if(background == "white"){
for (auto i = 0; i < image->verts.size(); i++) {
if(((lr <= image->verts[i][0]) && (image->verts[i][0] <= upr)) &&
((lg <= image->verts[i][1]) && (image->verts[i][1] <= upg)) &&
((lb <= image->verts[i][2]) && (image->verts[i][2] <= upb))){
image->verts[i] = {1,1,1};
}
}
}
set_output("image", image);
}
};
ZENDEFNODE(ImageDelColor, {
{
{"image"},
{"vec3f", "high_threshold", "255,255,255"},
{"vec3f", "low_threshold", "0,0,0"},
{"enum transparent black white", "background", "transparent"},
},
{
{"image"},
},
{},
{"image"},
});