i wrote a java code to change all the red values of a black and white image to 255, so the output would be a red image.
But its not red, instead it outputs a brighter image.
What did I do wrong?
File bwgFile = new File("X:/Java/Documents/NetBeansProjects/colour/input/bwg.png");
BufferedImage bwgImage = ImageIO.read(bwgFile);
int width=bwgImage.getWidth();
int height=bwgImage.getHeight();
for(int w=0; w<width; w++){
for(int h=0; h<height; h++){
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
int c=bwg.getRed();
Color red = new Color(255,c,c);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
}
}
ImageIO.write(bwgImage, "png", new File("X:/Java/Documents/NetBeansProjects/colour/output/c.png"));
input
output
EDIT:
I have found out what the problem was, apparently when the input is a greyscale image it will try to make the output a greyscale image as well thus making it darker when blue and green colors get removed and brighter when red gets added. not using a grayscale image as input fixed it.
If I understand what you're trying to do, you're trying to create a greyscale image, except that it is "redscale", using only shades of red. Therefore, you need to compute the greyscale constant of each pixel.
From wikipedia (Greyscale), the luminance of a pixel Y = 0.2126R + 0.7152G + 0.0722B. So, try this
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
float c = (0.2126f * bwg.getRed() + 0.7152f * bwg.getGreen() + 0.0722f * bwg.getBlue());
int cc = (int)Math.round(c);
Color red = new Color(cc, 0, 0);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
Alternatively, you can simply retain the red component and set green and blue to 0. This will leave you with just the "redness" of each pixel.
int pixel = bwgImage.getRGB(w,h);
Color bwg = new Color(pixel);
int c=bwg.getRed();
Color red = new Color(c,0,0);
int cpixel = red.getRGB();
bwgImage.setRGB(w,h,cpixel);
NOTE: This solution above only works on images that are not using IndexColorModel. You can check the color model using BufferedImage's getColorModel(). For IndexColorModel, setRGB() does not work directly and instead picks a color in the index closest to the set color, as per HaraldK's comment. To achieve the desired result for images using IndexColorModel, you can create a new BufferedImage with TYPE_INT_ARGB:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Then, write the calculated pixel colors to this new image and save the new image instead.
Related
I have one image in android resource folder and second image is capturing from camera using application.
Now I want to get effect of first image(from android resource folder) and want to apply same effect on captured image by camera in android.
Approach Applied :
First I find RGB from first image for one Pixel and again find RGB from second image for One Pixel, calculating difference between both RGB values and difference applied for second RBG values for all pixels.
Output Image is in Gray Color.
private void gettingRGBDifference(){
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.lady);
int width=(bmp.getWidth()*88/100);
int height=(bmp.getHeight()*85/100);
Bitmap resizedBitmap=Bitmap.createBitmap(bmp,1220,560, width-1220, height - 560);
cropCircleImageView.setImageBitmap(resizedBitmap);
int colour = resizedBitmap.getPixel(160, 160);
int red = Color.red(colour);
int blue = Color.blue(colour);
int green = Color.green(colour);
int alpha = Color.alpha(colour);
byte[] rgbArray;
Bitmap circleImageBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.circle);
int colour1 = circleImageBitmap.getPixel(150, 150);
int red1 = Color.red(colour1);
int blue1 = Color.blue(colour1);
int green1 = Color.green(colour1);
int alpha1 = Color.alpha(colour1);
redDifference = red - red1;
blueDifference = blue - blue1;
greenDifference = green - green1;
}
Can anyone correct my approach?
How to set color in graphics in c#?
SetColor method not working.
How do I convert the following Java code to C#?
private Graphics g1;
g1.setColor(Color.getHSBColor(h, 0.8f, b));
Color col = Color.getHSBColor(h, 0.8f, b);
int red = col.getRed();
int green = col.getGreen();
int blue = col.getBlue();
The drawing model in C# is slightly different. Instead of setting resources, like color, on a graphic object, you create resources, such as Pen, or Brush that have properties on them, like Color. You then use those objects to draw on an image.
Graphics g = Graphics.FromImage(someBitMap); //create a graphics object for an existing BitMap
Color c = new Color(255,0,0); //a "red" color
Pen p = new Pen(c); //create a Pen using the Red color from earlier
p.Width = 5; //Pen is 5 pixels wide
g.DrawLine(p, 0,0,100,100); //draw a diagonal line
//Get rbg values.
int r = c.R;
int g = c.G;
int b = c.B;
//It is VERY important to call `.Dispose()` on GDI objects. They contain unmanaged system resources that can, and will, leak if you don't.
g.Dispose()
p.Dispose()
It looks like you might be trying to convert a color from the HSB color-space to RGB. Unfortunately, that isn't natively supported in .NET. The following blog post has some code that can do that for you: https://blogs.msdn.microsoft.com/cjacks/2006/04/12/converting-from-hsb-to-rgb-in-net/
I am trying to get the value of the White Colored pixel from a GrayScale image and replace it with another Color but when I run my code, the whole GrayScale image is transfered to another Color. Can anyone please tell me where is fault in the code or how can I get my desired results??
This is the code...
public class gray {
public static void main (String args[])throws IOException{
int width;
int height;
BufferedImage myImage = null;
File f = new File("E:\\eclipse\\workspace\\Graphs\\src\\ColorToGray\\1.png");
myImage = ImageIO.read(f);
width = myImage.getWidth();
height = myImage.getHeight();
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
int pixels[];
pixels = new int[width * height];
myImage.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++) {
if (pixels[i] == 0xFFFFFF) {
pixels[i] = 0x000000FF;
}
}
File f2 = new File("E:\\eclipse\\workspace\\Graphs\\src\\ColorToGray\\out 1.png");
image.setRGB(0, 0, width, height, pixels, 0, width);
ImageIO.write( image, "jpg", f2);
}
}
Image Before:
Image Before Output
Image After:
Image After Output
I looked into it, and found a bunch of problems.
First of all, when specifying the filename to save, you supply a ".png" extension, but when you call the ImageIO.write() function, you specify file type "jpg". That tends not to work very well. If you try to open up the resulting file, most programs will give you a "this is not a valid .PNG file" error. Windows explorer tries to be smart, and re-interprets the .PNG as a .JPG, but this spared you from the chance of discovering your mistake.
This takes care of the strange redness problem.
However, if you specify "png" in ImageIO.write(), you still don't get the right image. One would expect an image that looks mostly like the original, with just a few patches of blue there where bright white used to be, but instead what we get is an overall brighter version of the original image.
I do not have enough time to look into your original image to find out what is really wrong with it, but I suspect that it is actually a bright image with an alpha mask that makes it look less bright, AND there is something wrong with the way the image gets saved that strips away alpha information, thus the apparent added brightness.
So, I tried your code with another image that I know has no tricks in it, and still your code did not appear to do anything. It turns out that the ARGB format of the int values you get from myImage.getRGB(); returns 255 for "A", which means that you need to be checking for 0xFFFFFFFF, not 0x00FFFFFF.
And of course when you replace a value, you must replace it with 0xFF0000FF, specifying a full alpha value. Replacing a pixel with 0x000000FF has no visible effect, because regardless of the high blue value, alpha is zero, so the pixel would be rendered transparent.
I want to extract the R,G and B values of the pixels of an image. I do it in two ways.
File img_file = new File("../foo.png");
BufferedImage img = ImageIO.read(img_file);
1st method(which works fine):
img.getRaster().getPixel(i, j, rgb);
2nd method(which throws new IllegalArgumentException("More than one component per pixel"))
red = img.getColorModel().getRed(img.getRGB(i, j));
What is the reason for this behaviour?
Normally when I want to extract RGB from a BufferedImage I do something like this:
File img_file = new File("../foo.png");
BufferedImage img = ImageIO.read(img_file);
Color color = new Color(img.getRGB(i,j));
int red = color.getRed();
Based on the JavaDocs
An IllegalArgumentException is thrown if pixel values for this
ColorModel are not conveniently representable as a single int
It would suggest that the underlying color model is representable by a single int value
You may also want to take a look at this answer for some more details
Typically, you would simply take the int packed pixel from the image and use Color to generate a Color representation and then extract the values from there...
First, get the int packed value of the pixel at x/y...
int pixel = img.getRGB(i, j);
Use this to construct a Color object...
Color color = new Color(pixel, true); // True if you care about the alpha value...
Extract the R, G, B values...
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
Now you could simply do some bit maths, but this is simpler and is more readable - IMHO
How do you paint an alpha mask image/Bitmap as a specific color. I'm trying to multiply the alpha (white) on the image by a color and draw it.
A couple examples of masks:
Simple
Translucent
I'd like to have the black be transparent and the alpha/white be the color.
black = 0x00ffffff
white = 0xffffffff
The first two hex chars of a 32bit value of ARGB determine your alpha
final int FULL_ALPHA = 0xFF000000;
int pixel = FULL_ALPHA + some_value;
Paint p = new Paint();
p.setColor(pixel);
c.drawCircle(width, height, radius, p);
I think I may have found my answer, thanks to #petey.
Paint colorMultiplier = new Paint();
colorMultiplier.setColorFilter(new PorterDuffFilter(Color.parseColor("#123456"), PorterDuff.Mode.MULTIPLY));
canvas.drawBitmap(alphaMask, 0, 0, colorMultiplier);