I am doing OCR and sometime I have a light text on a dark background. When I encounter this situation I need the program to know that it has to invert the colours. The code I have wrote doesn't work how I want. It is detecting dark colours as light and light colours as dark. Any ideas what I've done wrong?
File input = new File("/Users/unknown1/Desktop/t5.png");
BufferedImage imagegrey = ImageIO.read(input);
toGray(imagegrey);
int width = imagegrey.getWidth();
int height = imagegrey.getHeight();
int light = 0;
int dark = 0;
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color color = robot.getPixelColor(j, i);
int grey = ((color.getRed() + color.getBlue() + color.getGreen())/3);
//System.out.println(grey);
if (grey >= 237) {
light++;
}
else {
dark++;
}
}
}
System.out.println(light);
System.out.println(dark);
This as itself should work - pick up the color at location j i on the screen. Now if that location is covered by the image you intend is another issue we [here] dont' know what is in that spot. And if toGray() does some wierd transform that negates the image. So check these
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color color = robot.getPixelColor(j, i);
int grey = ((color.getRed() + color.getBlue() + color.getGreen())/3);
//System.out.println(grey);
if (grey >= 237) {
light++;
}
else {
dark++;
}
}
Related
I want to create 3 images from one bitmap by setting each pixel to the red channel but I keep getting an error with ("java.lang.IllegalStateException") the code that I have now. Original Image
Desired Result
This is the result I'm looking for in these pictures. And then to do the same with the green and blue channels.
I've tried a few things but this is what I have currently:
private Bitmap createRGB(Bitmap r) {
//Red image
for (int x = 0; x < r.getWidth(); x++)
{
for (int y = 0; y < r.getHeight(); y++)
{
r.setPixel(x, y, r.getPixel(x, y) & 0xFFFF0000);
}
}
return r;
}
The original code works seem to be an issue with the scale of the image when going through the loop. The image size of 1200x1100 did not work however, 600x500 produced the correct result. (This is only for my machine, may work for bigger sizes on other android projects)
private Bitmap createRGB(Bitmap r) {
for(int i = 0; i < r.getWidth(); i++){
for(int j = 0; j < r.getHeight(); j++){
r.setPixel(i, j, r.getPixel(i, j) & 0xFFFF0000);
}
}
return r;
}
This works and changing the colour value will also produce Green and Blue results.
This one is for a school assignment.
I am new to Processing software and I want to create a rainbow-filled window exactly like in the picture (at the center) below.
The program on the left is the one I have right now.
The program in the center is what I want it to look like.
On the right is the code I am using. I'll copy-paste it here.
void setup() {
size(255, 255);
}
void draw() {
noStroke();
colorMode(RGB, 255,255,255);
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
stroke(j,i,128);
point(i, j);
}
}
}
Any help, suggestions, adjustments to the code would be greatly appreciated. Thanks in advance.
You would benefit from some pseudocode. Never underestimate the power of pseudocode.
In this image, everything you need to do is written plain as day:
Since we're working in RGB, and that the image tells you what to do with red, green and blue, you're already golden, but to make things more transparent we'll alter the code a little bit. Let's forget about the loops for now. Here's what the picture tells you to do:
R -> vertical slider, the closer to the bottom the more red you have
G -> horizontal slider, left is less and right is more
B -> vertical slider, the opposite to the red slider
Now, knowing that your values are on a [0-255] scale, dans that your image also is a 256 pixels wide square, you just have to use the index of your loops to get your RGB values:
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
int r = j; // up == more red
int g = i; // right == more green
int b = 255 - j; // down == less blue
stroke(color(r, g, b));
point(i, j);
}
}
Also, just for kicks, as this is a static image and not an animation, you can put this code in the setup() method and it'll have the same result:
void setup() {
size(255, 255);
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
int r = j; // up == more red
int g = i; // right == more green
int b = 255 - j; // down == less blue
stroke(color(r, g, b));
point(i, j);
}
}
}
void draw() {} // you still need this method even if it's empty
Which gives you this result:
Have fun!
I am trying to convert the contents of a JPanel into a BufferedImage. After looking around I have got this code.
BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
this.paint(g);
I iterate through the image looking for pixels that are colored black using the following.
for(int i = 0; i < image.getWidth(); i++){
for(int j = 0; j < image.getHeight(); j++){
Color tempColor = new Color(image.getRGB(i, j));
if(tempColor == Color.BLACK){
System.out.println(tempColor); //Debugging
}
}
}
The JPanel contains many pixels that were painted using Color.BLACK (so yes they are black), although when running this code, it never prints the debugging line.
I believe the error in my code has to do with the way I am copying the contents of the JPanel into the BufferedImage, I can't seem to figure out what I am doing wrong. Any help is greatly appreciated, thanks.
You are performing a reference equality test when testing tempColor == Color.BLACK. But new Color(…) always creates a new object which can never be the same object as the predefined Color.BLACK instance, thus the == check will always be false.
Use equals or simply omit dealing with Color objects at all and just check whether image.getRGB(i, j) == 0 or if you don't want to use zero for black you can also use image.getRGB(i, j) == Color.BLACK.getRGB()
Thanks to #Holger for this answer.
for(int i = 0; i < image.getWidth(); i++){
for(int j = 0; j < image.getHeight(); j++){
Color tempColor = new Color(image.getRGB(i, j));
if(tempColor.equals(Color.BLACK)){ // Error was here
System.out.println(tempColor); //Debugging
}
}
}
Originally I had the code
if(tempColor == Color.BLACK)
instead of
if(tempColor.equals(Color.BLACK))
What I had to begin with will always evaluate to false, which was the error.
I'm trying to understand why a
bufferedImg.setRGB(x, y, color.getRGB());
sets no data (white pixels) at all, if I print one immediately before it by
System.out.println(color.getRGB());
as in following java code:
...
int height = img.getHeight();
int width = img.getWidth();
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
Color c = new Color(img.getRGB(j, i));
int red = (int)(c.getRed() * 0.299);
int green = (int)(c.getGreen() * 0.587);
int blue = (int)(c.getBlue() *0.114);
Color newColor = new Color(red + green + blue,
red + green + blue, red + green + blue);
System.out.println(newColor.getRGB()); // resets data
img.setRGB(j, i, newColor.getRGB());
}
}
Additional infos:
Its an implementation for converting RGB to grayscale
it works perfectly fine by removing the print/log line
(multiple) calls of println() before or/and after shows correct data
buffered image source is an openCV mat
i didnt find any specific reasons on the internet
Hoping for a person to give me some insight.
I am trying to implement displacement algorithm, same as is used in Gimp or photohop. Describtion of this algorithm can be found here: http://docs.gimp.org/en/plug-in-displace.html
Everything works fine, very similar to photoshop, but i have 1 problem :)
I cant manage to properly blend 2 images (oryginal and this image after displace), and in resultant image i have a lot of black pixels. for example:
This is the code:
Mat img = Highgui.imread(path);
img.convertTo(img, CvType.CV_32SC3);
Mat map = Highgui.imread(path);
map.convertTo(map, CvType.CV_32SC3);
Mat displacedImg;
int coefficient = 15;
int displacement, intensity;
int[] dataMap = new int[3];
int[] dataImg = new int[3];
int[] dataDisplacedImg = new int[3];
Mat temp = new Mat(img.size(), img.type());
for (int i = 0; i < img.height(); i++) {
for (int j = 0; j < img.width(); j++) {
map.get(i, j, dataMap);
intensity = dataMap[0];
displacement = (int) (((intensity - 127.5) / 127.5) *(coefficient));
img.get(i, j, dataImg);
if (j + displacement <= img.width() && j + displacement >= 0) {
temp.put(i, j + displacement, dataImg);
}
}
}
displacedImg = temp;
Do you guys have any idea what i can in this case do ?
i want to use pixels from oryginal image instead of these black pixels.
I was thinking about transparent images, but i didnt find any solution.
Can you help ?