how to fill color in image in particular area?

后端 未结 4 1286
后悔当初
后悔当初 2020-11-29 23:20

I want to fill the color in white area for Paint based application so please give me suggestion for how to do this work..

相关标签:
4条回答
  • 2020-11-29 23:55

    I found the Solution with Flood fill algoritham

    private void FloodFill(Bitmap bmp, Point pt, int targetColor, int replacementColor){
    Queue<Point> q = new LinkedList<Point>();
    q.add(pt);
    while (q.size() > 0) {
        Point n = q.poll();
        if (bmp.getPixel(n.x, n.y) != targetColor)
            continue;
    
        Point w = n, e = new Point(n.x + 1, n.y);
        while ((w.x > 0) && (bmp.getPixel(w.x, w.y) == targetColor)) {
            bmp.setPixel(w.x, w.y, replacementColor);
            if ((w.y > 0) && (bmp.getPixel(w.x, w.y - 1) == targetColor))
                q.add(new Point(w.x, w.y - 1));
            if ((w.y < bmp.getHeight() - 1)
                    && (bmp.getPixel(w.x, w.y + 1) == targetColor))
                q.add(new Point(w.x, w.y + 1));
            w.x--;
        }
        while ((e.x < bmp.getWidth() - 1)
                && (bmp.getPixel(e.x, e.y) == targetColor)) {
            bmp.setPixel(e.x, e.y, replacementColor);
    
            if ((e.y > 0) && (bmp.getPixel(e.x, e.y - 1) == targetColor))
                q.add(new Point(e.x, e.y - 1));
            if ((e.y < bmp.getHeight() - 1)
                    && (bmp.getPixel(e.x, e.y + 1) == targetColor))
                q.add(new Point(e.x, e.y + 1));
            e.x++;
        }
    }}
    

    flood fill in android:

    See this FloodFill

    0 讨论(0)
  • 2020-11-29 23:59

    after lot of searching, i came up with the following. It is fast and efficient

    private void myFloodFillMethod(Point node,int targetColor, int fillColor) {
        if (targetColor != fillColor) {
            Queue<Point> queue = new LinkedList<>();
            do {
                int x = node.x;
                int y = node.y;
    
                while (x > 0 && getPixelColorFromArr(x - 1, y) == targetColor) {
                    x--;                }
                boolean spanUp = false;
                boolean spanDown = false;
    
                while (x < width && checkEquality(getPixelColorFromArr(x, y),targetColor)) {
                    setPixelColorFromArr(x, y, fillColor);
                    if (!spanUp && y > 0
                            && checkEquality(getPixelColorFromArr(x, y - 1),targetColor)) {
                        queue.add(new Point(x, y - 1));
                        spanUp = true;
                    } else if (spanUp && y > 0
                            && !checkEquality(getPixelColorFromArr(x, y - 1),targetColor)) {
                        spanUp = false;
                    }
                    if (!spanDown && y < height - 1
                            && checkEquality(getPixelColorFromArr(x, y + 1), targetColor)) {
                        queue.add(new Point(x, y + 1));
                        spanDown = true;
                    } else if (spanDown && y < height - 1
                            && !checkEquality(getPixelColorFromArr(x, y + 1),targetColor)) {
                        spanDown = false;
                    }
                    x++;
                }
    
            } while ((node = queue.poll()) != null);
        }
    
    }
    
    private boolean checkEquality(int pixelColorFromArr, int targetColor) {
        return pixelColorFromArr == targetColor;
    }
    
    void fillColorInImage(Bitmap image, Point node, int targetColor, int replacementColor){
    
        if(targetColor ==  replacementColor) return;
    
        width = image.getWidth();
        height = image.getHeight();
    
        pixelArr = new int[width*height];
    
        image.getPixels(pixelArr,0,width,0,0,width,height);
        myFloodFillMethod(node,targetColor,replacementColor);
    
        image.setPixels(pixelArr,0,width,0,0,width,height);
    
    }
    

    how to call:

    fillColorInImage(bmp, point, targetColor, replacementColor);
    

    where

    bmp is the bitmap of image(extract form the image view)

    point is Point(x,y) where user has tapped:get it from event.getX() and event.getY()

    targetColor is the color of the point: get it from bitmap.getPixel()

    replacementColor is the color you want to replace it to

    0 讨论(0)
  • 2020-11-30 00:05

    1)Have the Split up image for each color portion separately with same size as actual image and other portion are transparent. 2)Have the complete image with each portion painting in different color in ur drawable folder - this is just reference image.

    3) Add the all split up images in frame layout and set all split up invisible initially and set visible to actual image only

    4) Hot code the color for each split up from ur reference image (step2) for Eg handSplitImageColor = green;

    4) Set the listener for frame layout find out the x and y position and pass the x and y position to ur reference image (step 2) and find out the color in that particular location and match the color with step 4 and fill the particular in that image and set the visibility true for that split up image. So only that portion will get filled by color since other portion are transparent.

    These are the steps i used for one of my same type of problems.

    But I don't think this is the better solution but it works well for me.

    0 讨论(0)
  • 2020-11-30 00:15

    Here's a quick application using Python and OpenCV (should be available on Android if you try hard enough):

    """Flood fills with random color on click.  Press `q' to exit."""
    import cv
    import sys
    import random
    
    TOL = 10
    TOL_BGR = (TOL, TOL, TOL, 0)
    
    def click(event,x,y,flags,im):
        if event == cv.CV_EVENT_LBUTTONDOWN:
            b,g,r = [ random.random() * 255 for i in range(3) ]
            cv.FloodFill(im, (x,y), (b,g,r,0), TOL_BGR, TOL_BGR)
    
    im = cv.LoadImage(sys.argv[1], cv.CV_LOAD_IMAGE_COLOR)
    cv.NamedWindow(__file__, 1)
    cv.SetMouseCallback(__file__, click, im)
    while True:
        cv.ShowImage(__file__, im)
        key = cv.WaitKey(33)
        if chr(key & 0xff) == 'q':
            break
    cv.SaveImage('floodfill.png', im)
    

    Every time the user clicks an image, the application flood-fills using the click location as a seed. The color is picked randomly. You can change the tolerances by modifying the value of TOL (or TOL_BGR). Here's the result after a couple of clicks:

    enter image description here

    The general algorithm is the same regardless of what technology you use.

    0 讨论(0)
提交回复
热议问题