Combined Grayscale and Color Images - CodeProject
Combined Grayscale and Color Images - CodeProject
Combined Grayscale and Color Images - CodeProject
articles quick answers discussions features community help Search for articles, questions, tips
https://www.codeproject.com/Articles/23193/Combined-Grayscale-and-Color-Images 1/7
1/10/21 7:18 Combined Grayscale and Color Images - CodeProject
Introduction
After growing tired of spending hours zoomed in at 800% magnification using the polygon lasso tool to create combined grayscale and color images in Macromedia Fireworks, I
decided that there had to be a better way. Thus I embarked upon creating this application to make my life and hopefully that of others easier. This is a first attempt, and if I receive
positive feedback that others would actually like to use this application, I will expand the features available when time permits.
Background
https://www.codeproject.com/Articles/23193/Combined-Grayscale-and-Color-Images 2/7
1/10/21 7:18 Combined Grayscale and Color Images - CodeProject
This application uses unsafe code and pointers for image manipulation. Check out the "Image Processing for Dummies" articles by Christian Graus on The Code Project for a good
introduction to this technique. Also, for selecting an area of the image, I used code from the article "The Secret of Marching Ants" by q123456789 on The Code Project.
I have found that one of the more effective methods of dealing with this is to use a ratio value between the pixel's colors. Once users select a pixel from the image that they would
like to keep, the program computes the difference in value between the red and green band, red and blue band, and green and blue band. This then is a ratio that can be used over
the entire image to find similar colored pixels, even though the light level may be very different. For example, this would match a pixel that had the values red = 50, green = 80, and
blue = 110 to a pixel that had the values red = 180, green = 210, and blue = 240 because the difference between red and green in each is 30, etc.
However, even using the ratio method described above still does not match very many pixels, a maximum of 256. To get a decent color selection, error values must be used on each
band difference. Therefore, the program provides three text boxes for the user to provide an error value for the red and green difference, red and blue difference, and green and blue
difference. By adding and subtracting this error value from the difference number, a much broader range of colors are included in the match. For example, if the difference between
the red and green pixels is 25, and the user enters an error value of 20, all pixels with a difference between the red and green bands of anywhere from 5 to 45 will be within this
range.
Another useful feature is that many times the color exists in several locations on the image and the user only wants to keep it in one section. Therefore, some boundary needs to be
supplied allowing the user to select a small area from the image to keep in color. For this example program, I have just added simple code for a rectangle, although it could be
expanded to any shape. I implemented just the drawing part of "The Secret of Marching Ants" program from The Code Project so that users can see the rectangle as they draw it. This
necessitated drawing the image directly onto the form instead of using a picturebox where scrollbars could easily be added. That said, here is the basic image processing function in
the program:
// Note: Do not use absolute value because each pixel in the image
int RGlow, RGhigh, RBlow, RBhigh, GBlow, GBhigh, xmin, xmax, ymin, ymax;
https://www.codeproject.com/Articles/23193/Combined-Grayscale-and-Color-Images 3/7
1/10/21 7:18 Combined Grayscale and Color Images - CodeProject
xmin = rect.X;
ymin = rect.Y;
unsafe
byte* p = (byte*)(void*)Scan0;
if (x > xmin && x < xmax && y > ymin && y < ymax &&
p[2] - p[1] > RGlow && p[2] - p[1] < RGhigh &&
p[2] - p[0] > RBlow && p[2] - p[0] < RBhigh &&
img_array_backup[y, x] = true;
p += 3;
p += nOffset;
user_image.UnlockBits(bmData);
}
Note that it is not altering the bitmap directly, rather pixels that match the selected color within the user's specified error limits are assigned to true on a boolean two dimensional
array. This array is used like a mask; all values of true correspond to a pixel that remains in color, otherwise they are turned into a grayscale color. This lets the function run
multiple times on the same image and thus allows the user to select multiple colors before generating the final image.
https://www.codeproject.com/Articles/23193/Combined-Grayscale-and-Color-Images 4/7
1/10/21 7:18 Combined Grayscale and Color Images - CodeProject
Now that we have an easy way to select the color and area of the image to affect, we now need an easy way for users to interpret what they have and have not already selected.
Again, there are several different ways this could be done. I simply passed the user's image and the mask created in the last step to a new form. The user can then select whether to
undo the color selection, add another color, or accept the image as final. When adding another color, it inverts the mask so that users see colors they have already selected as
grayscale. Once the user is satisfied with the image, the following code is run to create the final image.
unsafe
byte* p = (byte*)(void*)Scan0;
+ .114 * p[0]);
p += 3;
p += nOffset;
user_image.UnlockBits(bmdata);
DrawBackground(); // refresh the image
in the image you don't want, select the redefine color button. If you would like to add more colors, select the add color button and it will return you to the original form and show
your image with the colors already selected now in grayscale. Note that you will have to select the area of the image to affect again. If the image is exactly like you want it, select the
create image button and it will return you to the main form and show the final image.
History
January 25, 2008: Initial release
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
Matthew Baugher is an electrical engineer from the U.S.A. He is working part time writing image processing software for TerraVerde Technologies while pursing an MBA from
Oklahoma State University.
https://www.codeproject.com/Articles/23193/Combined-Grayscale-and-Color-Images 6/7
1/10/21 7:18 Combined Grayscale and Color Images - CodeProject
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink
Layout: fixed
|
fluid Article Copyright 2008 by mbaugher
Advertise
Everything else
Copyright © CodeProject, 1999-2021
Privacy
Cookies
Web01 2.8.20210930.1
Terms of Use
https://www.codeproject.com/Articles/23193/Combined-Grayscale-and-Color-Images 7/7