Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fill color in image in particular area?

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

enter image description here

like image 735
Hardik Gajjar Avatar asked Jan 10 '12 09:01

Hardik Gajjar


2 Answers

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

like image 183
Hardik Gajjar Avatar answered Sep 21 '22 19:09

Hardik Gajjar


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.

like image 43
mpenkov Avatar answered Sep 19 '22 19:09

mpenkov