how to fill color in image in particular area?
20,092
Solution 1
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
Solution 2
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:
The general algorithm is the same regardless of what technology you use.
Author by
Hardik Gajjar
Updated on July 20, 2020Comments
-
Hardik Gajjar almost 4 years
I want to fill the color in white area for Paint based application so please give me suggestion for how to do this work..
-
Shreyash Mahajan about 12 yearsNice one But If i just add this algorithm to My Android app if i call the function FloodFill(with respective attributes) then will it be works ? And what should i have to give as attribute Point in this function ?
-
Satyajitsinh Raijada almost 12 yearsthanks @Hardik, its really a nicely written algorithm. but for android phone it seem to be very slow. do you know any other way for flood filling?
-
dineshprasanna over 11 yearscould you please send the source code, because i cant understood this part
-
mpenkov over 11 yearsNo, I will not email you teh codez. The source code is in my answer, anyway. If you don't understand anything, then please read the documentation for the relevant functions -- it is readily available.
-
Pravesh almost 10 yearscan anyone explain how to provide the targetcolor and replacement color and in which format
-
isuru about 7 yearsIf I use SVG instead of normal image what will be the approach?
-
Zia Ur Rahman about 6 yearsthis Answer is not defined properly. You have to show the full implementation that how to use it in Android App, which works perfectly if apply.
-
Umair about 4 yearsHow can we use this floodfill algorithm in case of bitmap image?