【开发日志】2023.05 ZENO----Image Processing----ImageInRange

ImageInRange

 

 

 

 

 

 

 

 

 

 

struct ImageInRange : INode {
    virtual void apply() override {
        auto image = get_input<PrimitiveObject>("image");
        auto mode = get_input2<std::string>("mode");
        auto &ud = image->userData();
        int w = ud.get2<int>("w");
        int h = ud.get2<int>("h");
        vec3i lb = get_input2<vec3i>("low_threshold");
        vec3i ub = get_input2<vec3i>("high_threshold");

        auto A11 = std::make_shared<PrimitiveObject>();
        A11->verts.resize(image->size());
        A11->userData().set2("isImage", 1);
        A11->userData().set2("w", w);
        A11->userData().set2("h", h);
        A11->verts.add_attr<float>("alpha");
        for(int i = 0;i < image->size();i++){
            A11->verts.attr<float>("alpha")[i] = 1;
        }
        std::vector<float> &Alpha = A11->verts.attr<float>("alpha");
        if(image->verts.has_attr("alpha")){
            A11->verts.attr<float>("alpha") = image->verts.attr<float>("alpha");
        }

        if(mode == "Transparent"){
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if(((lb[0] <= 255 * image->verts[i * w + j][0]) && (255 * image->verts[i * w + j][0] <= ub[0])) &&
                       ((lb[1] <= 255 * image->verts[i * w + j][1]) && (255 * image->verts[i * w + j][1] <= ub[1])) &&
                       ((lb[2] <= 255 * image->verts[i * w + j][2]) && (255 * image->verts[i * w + j][2] <= ub[2]))){
                        A11->verts[i * w + j] = image->verts[i * w + j];
                    }
                    else{

                        A11->verts.attr<float>("alpha")[i * w + j] = 0;
                    }
                }
            }
        }
        else if(mode == "Black"){
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if(((lb[0] <= 255 * image->verts[i * w + j][0]) && (255 * image->verts[i * w + j][0] <= ub[0])) &&
                       ((lb[1] <= 255 * image->verts[i * w + j][1]) && (255 * image->verts[i * w + j][1] <= ub[1])) &&
                       ((lb[2] <= 255 * image->verts[i * w + j][2]) && (255 * image->verts[i * w + j][2] <= ub[2]))){
                        A11->verts[i * w + j] = image->verts[i * w + j];
                    }
                    else{
                        A11->verts[i * w + j] = {0,0,0};
                    }
                }
            }
        }
        else if(mode == "White"){
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if(((lb[0] <= 255 * image->verts[i * w + j][0]) && (255 * image->verts[i * w + j][0] <= ub[0])) &&
                       ((lb[1] <= 255 * image->verts[i * w + j][1]) && (255 * image->verts[i * w + j][1] <= ub[1])) &&
                       ((lb[2] <= 255 * image->verts[i * w + j][2]) && (255 * image->verts[i * w + j][2] <= ub[2]))){
                        A11->verts[i * w + j] = image->verts[i * w + j];
                    }
                    else{
                        A11->verts[i * w + j] = {1,1,1};
                    }
                }
            }
        }
        set_output("image", A11);
    }
};
ZENDEFNODE(ImageInRange, {
    {
        {"image"},
        {"enum Transparent Black White", "mode", "Transparent"},
        {"vec3i", "high_threshold", "255,255,255"},
        {"vec3i", "low_threshold", "0,0,0"},
    },
    {
        {"image"},
    },
    {},
    { "comp" },
});

ImageInRange_black

 

 

 

 

 

 

struct ImageInRange : INode {
    virtual void apply() override {
        auto image = get_input<PrimitiveObject>("image");
        auto &ud = image->userData();
        int w = ud.get2<int>("w");
        int h = ud.get2<int>("h");
        vec3i lb = get_input2<vec3i>("low_threshold");
        vec3i ub = get_input2<vec3i>("high_threshold");

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if(((lb[0] <= 255 * image->verts[i * w + j][0]) && (255 * image->verts[i * w + j][0] <= ub[0])) &&
                ((lb[1] <= 255 * image->verts[i * w + j][1]) && (255 * image->verts[i * w + j][1] <= ub[1])) &&
                ((lb[2] <= 255 * image->verts[i * w + j][2]) && (255 * image->verts[i * w + j][2] <= ub[2]))){
                    image->verts[i * w + j] = {1,1,1};
                }
                else{
                    image->verts[i * w + j] = {0,0,0};
                }
            }
        }
        set_output("image", image);
    }
};

ZENDEFNODE(ImageInRange, {
    {
        {"image"},
        {"vec3i", "high_threshold", "255,255,255"},
        {"vec3i", "low_threshold", "0,0,0"},
    },
    {
        {"image"},
    },
    {},
    { "comp" },
});

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值