在android中从位图图像中识别物体的形状?

发布于 2024-11-18 00:51:15 字数 110 浏览 4 评论 0原文

最近,我参与了一个油漆项目。首先,我需要 选择一种颜色。当我触摸图像时,所选颜色将会 替换为位图图像的相同颜色区域。我怎样才能认出 图像中相同颜色区域的形状。

此致,

哈比卜

Recently, I worked with a paint project. First of all ,I need to
select a color. When I touch the image then selected color will be
replaced with same color region of bitmap image. How I can recognize
the shape of same color region in the image.

Sincerely,

Habib

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

A君 2024-11-25 00:51:15

这段代码可以帮助完整

imageViewEdit.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        setColor = newbitmap.getPixel((int)event.getX(),(int)event.getY());
        replaceX=(int)event.getX();
        replaceY=(int)event.getY();

        setColor_R = (setColor & 0x00ff0000) >> 16;
        setColor_G = (setColor & 0x0000ff00) >> 8;
        setColor_B = setColor & 0x000000ff;

        boolean[][] visited = new boolean[newbitmap.getWidth()][newbitmap.getHeight()];


        Queue<Pixel> pixel=new LinkedList<Pixel>();
        pixel.add(new Pixel(replaceX, replaceY));

        while(!pixel.isEmpty()){

            Pixel loc = pixel.remove();
            visited[loc._x][loc._y] = true;

            putIfValid(loc._x+1,loc._y-1,pixel,visited);
            putIfValid(loc._x+1,loc._y,pixel,visited);
            putIfValid(loc._x+1,loc._y+1,pixel,visited);
            putIfValid(loc._x-1,loc._y-1,pixel,visited);
            putIfValid(loc._x-1,loc._y,pixel,visited);
            putIfValid(loc._x-1,loc._y+1,pixel,visited);
            putIfValid(loc._x,loc._y+1,pixel,visited);
            putIfValid(loc._x,loc._y-1,pixel,visited);
        }
        imageViewEdit.setImageBitmap(newbitmap);

        return false;
    }


    public void putIfValid(int x, int y, Queue<Pixel> pixel, boolean[][] visited) {

            int colors=newbitmap.getPixel(x, y);

            int  current_R = (colors & 0x00ff0000) >> 16;
            int  current_G = (colors & 0x0000ff00) >> 8;
            int  current_B = colors & 0x000000ff;

            if(((setColor_R-RGBThreshold<=current_R) && (setColor_R+RGBThreshold>=current_R))
              &&((setColor_G-RGBThreshold<=current_G)&&(setColor_G+RGBThreshold>=current_G))
              &&((setColor_B-RGBThreshold<=current_B)&& (setColor_B+RGBThreshold>=current_B))
              && valid(x,y,visited)){

                pixel.add(new Pixel(x,y));
                newbitmap.setPixel(x, y, getColor);
                visited[x][y]=true;
            }
    }

    private boolean valid(int x, int y, boolean[][] visited) {

        return x >= 2 && x < (newbitmap.getWidth()-2)&&
               y >= 2 && y < (newbitmap.getHeight()-2) &&
               !visited[x][y];
    }
});


public static class Pixel {
    final public int _x;
    final public int _y;
    public Pixel(int x,int y){
        _x=x;
        _y=y;
    }
}

This code can be help full

imageViewEdit.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        setColor = newbitmap.getPixel((int)event.getX(),(int)event.getY());
        replaceX=(int)event.getX();
        replaceY=(int)event.getY();

        setColor_R = (setColor & 0x00ff0000) >> 16;
        setColor_G = (setColor & 0x0000ff00) >> 8;
        setColor_B = setColor & 0x000000ff;

        boolean[][] visited = new boolean[newbitmap.getWidth()][newbitmap.getHeight()];


        Queue<Pixel> pixel=new LinkedList<Pixel>();
        pixel.add(new Pixel(replaceX, replaceY));

        while(!pixel.isEmpty()){

            Pixel loc = pixel.remove();
            visited[loc._x][loc._y] = true;

            putIfValid(loc._x+1,loc._y-1,pixel,visited);
            putIfValid(loc._x+1,loc._y,pixel,visited);
            putIfValid(loc._x+1,loc._y+1,pixel,visited);
            putIfValid(loc._x-1,loc._y-1,pixel,visited);
            putIfValid(loc._x-1,loc._y,pixel,visited);
            putIfValid(loc._x-1,loc._y+1,pixel,visited);
            putIfValid(loc._x,loc._y+1,pixel,visited);
            putIfValid(loc._x,loc._y-1,pixel,visited);
        }
        imageViewEdit.setImageBitmap(newbitmap);

        return false;
    }


    public void putIfValid(int x, int y, Queue<Pixel> pixel, boolean[][] visited) {

            int colors=newbitmap.getPixel(x, y);

            int  current_R = (colors & 0x00ff0000) >> 16;
            int  current_G = (colors & 0x0000ff00) >> 8;
            int  current_B = colors & 0x000000ff;

            if(((setColor_R-RGBThreshold<=current_R) && (setColor_R+RGBThreshold>=current_R))
              &&((setColor_G-RGBThreshold<=current_G)&&(setColor_G+RGBThreshold>=current_G))
              &&((setColor_B-RGBThreshold<=current_B)&& (setColor_B+RGBThreshold>=current_B))
              && valid(x,y,visited)){

                pixel.add(new Pixel(x,y));
                newbitmap.setPixel(x, y, getColor);
                visited[x][y]=true;
            }
    }

    private boolean valid(int x, int y, boolean[][] visited) {

        return x >= 2 && x < (newbitmap.getWidth()-2)&&
               y >= 2 && y < (newbitmap.getHeight()-2) &&
               !visited[x][y];
    }
});


public static class Pixel {
    final public int _x;
    final public int _y;
    public Pixel(int x,int y){
        _x=x;
        _y=y;
    }
}
柠栀 2024-11-25 00:51:15

您可能正在寻找存储桶工具实现。 这是一篇描述通用解决方案的文章。

You are probably looking for a bucket tool implementation. This is an article describing a general solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文