I am making an image editing like program, and when I want to edit large images it really starts to slow down. What is a good way to edit large image quickly? This example adjusts the image's brightness, it works, but when I get large images such as 3456x2304 its really slow.
I have a slider, which calls this function every time it moves.
// Slider in a dialog box
private void sldBrightnessStateChanged(javax.swing.event.ChangeEvent evt) {
// Get the position of the slider
int val = sldBrightness.getValue();
// Set the text in the textbox
txtBrightness.setText("" + val);
// New Brightness class (see below)
Brightness adjustment = new Brightness();
adjustment.amount(val);
adjustment.applyFilter();
// get the result built by applyFilter();
Canvas.preview = Preview.getImage();
// Update main program
this.getParent().repaint();
}
Then the filter:
package pocketshop.graphics.adjustments;
import java.awt.image.BufferedImage;
import pocketshop.Canvas;
import pocketshop.graphics.Colors;
import pocketshop.graphics.Preview;
public class Brightness{
protected int amount = 0;
public void amount(int amount){
this.amount = amount;
}
public void applyFilter(){
int width = Canvas.image.getWidth();
int height = Canvas.image.getHeight();
int[] pixels = new int[width * height];
Canvas.image.getRGB(0, 0, width, height, pixels, 0, width);
for(int i = 0; i < pixels.length; i++){
int pixel = pixels[i];
//int pixel = Canvas.image.getRGB(x, y);
int red = Colors.red(pixel);
int green = Colors.green(pixel);
int blue = Colors.blue(pixel);
red += amount;
if(red > 255){
red = 255;
}else if(red < 0){
red = 0;
}
green += amount;
if(green > 255){
green = 255;
}else if(green < 0){
green = 0;
}
blue += amount;
if(blue > 255){
blue = 255;
}else if(blue < 0){
blue = 0;
}
pixels[i] = Colors.rgba(red, green, blue);
}
//BrightnessContrastDialog.preview.setRGB(0, 0, width, height, pixels, 0, width);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
img.setRGB(0, 0, width, height, pixels, 0, width);
Preview.setImage(img);
}
}
I have a slider, which calls this function every time it moves.
Don't adjust the image until the slider stops moving. I don't know Swing, but I'm betting there is a test for evt which says whether it is moving or has stopped.
The way you have it, applyFilter may be called 100 times or more as the slider is moved.
As I understand the picture is presented for user in order to give immediate feedback of changes that are made, what you can do us display downsampled version of picture and perform the brightness change to it while slider is moving which will be fast. Once user is satisfied with the value she selected using slider you can apply the change to original image. You can add apply button or something
I would suggest that you investigate OpenCL and its Java binding, JOCL. OpenCL is a library for interacting directly with the GPU on various different graphics cards. JOCL is a Java binding library for the OpenCL API.
Fair warning, this may be much more than you want to tackle, as you will be working at a much lower level than Swing.
I am not sure but it looks like you are using a BufferedImage of type BufferedImage.TYPE_INT_ARGB. This means that the BufferedImage is using a WritableRaster that is using a DataBuffer of type DataBufferInt. In its simplest form a DataBufferInt is nothing more than a wrapper around an int[]. If you can get a hold of the BufferedImage that the Canvas is using then get its WritableRaster and from there get the DataBufferInt:
WritableRaster raster = Canvas.image.getRaster();
DataBufferInt dataBuffer = (DataBufferInt)raster.getDataBuffer();
int[] pixels = dataBuffer.getData();
Now that you have the int[] that represents the pixels you can just loop over it and change the components:
for (int i = 0, len = pixels.len; i < len; ++i) {
int pixel = pixels[i];
int red = ((pixel & 0x00FF0000) >> 16) + amount;
if (red < 0) red = 0 else if (red > 255) red = 255;
int green = ((pixel & 0x0000FF00) >> 8) + amount;
if (green < 0) green = 0 else if (green > 255) green = 255;
int blue = (pixel & 0x000000FF) + amount;
if (blue < 0) blue = 0 else if (blue > 255) blue = 255;
pixels[i] = (pixels[i] & 0xFF000000) + (red << 16) + (green << 8) + blue;
}
This means that the pixels in the BufferedImage that the Preview has are being changed in-place without having to create another int[] of new pixels and another BufferedImage.
I am not sure if this will work but many times it helps in Java to cut out the middle-man and don't create as many objects.
If this does not work then look into Java Advanced Imaging.
Related
I'm attempting to convert an image to black and white only (not grey scale).
I've used this:
BufferedImage blackAndWhiteImage = new BufferedImage(
dWidth.intValue(),
dHeight.intValue(),
BufferedImage.TYPE_BYTE_BINARY);
Graphics2D graphics = blackAndWhiteImage.createGraphics();
graphics.drawImage(colourImage, 0, 0, null);
return blackAndWhiteImage;
Everything fine, until I decided to try out brighter colors, like the Google logo for example:
and it came out with this:
Then I tried first to pass trough grey scale first using:
BufferedImage blackAndWhiteImage2 = new BufferedImage(
dWidth.intValue(),
dHeight.intValue(),
BufferedImage.TYPE_USHORT_GRAY);
And it seemed to have saved the Blue color, but not the brightest (in this case yellow), and as you may see it decreased in quality:
Any suggestions are much appreciated; I believe what I am seeking for is to convert every colour to Black except White (which would be the background color), this is already done when applying TYPE_BYTE_BINARY removing the alpha channel.
EDIT:
Maybe I have not explained very clear:
the final image has to have White background **1
every other color has to be converted to Black
**1 - there are some cases where the image is actually White on Black..which is annoying (whiteOnBlackExample) as it complicates a lot this process, and I will leave this later on, as priority now is to convert "normal" images.
What I did was, first strip out the alpha channel if it exists -> therefore convert the alpha channel to White; then convert every other color to Black
If you use JavaFX you can use the ColorAdjust effect with brightness of -1 (minimum), which makes all the (non-white) colors black:
public class Main extends Application {
Image image = new Image("https://i.stack.imgur.com/UPmqE.png");
#Override
public void start(Stage primaryStage) {
ImageView colorView = new ImageView(image);
ImageView bhView = new ImageView(image);
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(-1);
bhView.setEffect(colorAdjust);
primaryStage.setScene(new Scene(new VBox(colorView, bhView)));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
These Effects are optimized so they are probably faster than what you would achieve by applying them manually.
Edit
Since your requirements are that
any pixel which is not opaque should be transformed to white, and
any pixel which is not white should be transformed to black,
the predesigned effects won't suit you as far as I can tell - they are too specific. You can do pixel by pixel manipulation:
WritableImage writableImage = new WritableImage(image.getPixelReader(), (int) image.getWidth(), (int) image.getHeight());
PixelWriter pixelWriter = writableImage.getPixelWriter();
PixelReader pixelReader = writableImage.getPixelReader();
for (int i = 0; i < writableImage.getHeight(); i++) {
for (int j = 0; j < writableImage.getWidth(); j++) {
Color c = pixelReader.getColor(j, i);
if (c.getOpacity() < 1) {
pixelWriter.setColor(j, i, Color.WHITE);
}
if (c.getRed() > 0 || c.getGreen() > 0 || c.getBlue() > 0) {
pixelWriter.setColor(j, i, Color.BLACK);
}
}
}
ImageView imageView = new ImageView(writableImage);
Note that the order in which you apply the rules matter. A transparent non-white pixel will turn white if you apply 1 and then 2, but if you apply 2 and then 1 it will end up black. This is because the predefined WHITE and BLACK colors are opaque. You can manually set the red, green and blue values while not changing the alpha value instead. It all depends on your exact requirements.
Remember that due to lossy compression of some file formats you might not find true white in them at all, but a value which is close to true white and your eye won't be able to tell the difference.
Here is the example from my comment. At first open the input image and create a new one for output.
BufferedImage myColorImage = ImageIO.read(fileInput);
BufferedImage myBWImage = new BufferedImage(myColorImage.getWidth(), myColorImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
Then iterate through all the pixels and compare rgb values with a threshold:
for (int x = 0; x < myColorImage.getWidth(); x++)
for (int y = 0; y < myColorImage.getHeight(); y++)
if (rgbToGray(myColorImage.getRGB(x, y), MODE.AVERAGE) > threshold)
myBWImage.setRGB(x, y, 0);
else
myBWImage.setRGB(x, y, 0xffffff);
Here is rgbToGray method implementation to compare with threshold:
private static int rgbToGray(int rgb, MODE mode) {
// split rgb integer into R, G and B components
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
int gray;
// Select mode
switch (mode) {
case LIGHTNESS:
gray = Math.round((Math.max(r, Math.max(g, b)) + Math.min(r, Math.min(g, b))) / 2);
break;
case LUMINOSITY:
gray = Math.round(0.21f * r + 0.72f * g + 0.07f * b);
break;
case AVERAGE:
default:
gray = Math.round((r + g + b) / 3);
break;
}
return gray;
}
An utility enum:
private enum MODE {
LIGHTNESS, AVERAGE, LUMINOSITY
}
I got the following result:
Note: for your google image even threshold = 1 is suitable, for other images you should pick another values from range [0..255]. For photos, most likely, more appropriate values are about 100-150. MODE also will affect the final result.
I'm building an application that uses OCR to read text from an image (using Tess4J for Google's Tesseract), but I want to ignore the tan-colored text and only read the grey.
In the image below, for instance, I only want to read "Ricki" and ignore "AOA".
http://i.imgur.com/daCuTbB.png
To accomplish this, I figured removing the tan color from the image before performing OCR was my best option.
/* Remove RGB Value for Group Tag */
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++) {
//If pixel is between dark-tan value and light-tan value
if (pixels[i] > 0xFF57513b && pixels[i] < 0xFF6b6145) {
// Set pixel to black
System.out.println("pixel found");
pixels[i] = 0xFF000000;
}
}
image.setRGB(0, 0, width, height, pixels, 0, width);
But this code removes almost all of the grey text as well. You aren't able to simply compare hex color values for a range of values the way I have. Is there another way to approach detecting a range of colors? Or a better different approach to this problem?
haraldK pointed me in the right direction by mentioning converting RGB. Bit shifting to get individual r, g, and b int values from the hex value allowed me to compare the color within a range and black out a range of colors from the image.
int baser = 108; //base red
int baseg = 96; //base green
int baseb = 68; //base blue
int range = 10; //threshold + and - from base values
/* Set all pixels within +- range of base RGB to black */
for (int i = 0; i < pixels.length; i++) {
int a = (pixels[i]>>24) &0xFF; //alpha
int r = (pixels[i]>>16) &0xFF; //red
int g = (pixels[i]>>8) &0xFF; //green
int b = (pixels[i]>>0) &0xFF; //blue
if ( (r > baser-range && r < baser+range) &&
(g > baseg-range && g < baseg+range) &&
(b > baseb-range && b < baseb+range) ) {
pixels[i] = 0xFF000000; //Set to black
}
}
I want to generate a 64x64 pixel art for any image provided by the user. I'm currently lost on how to do it. OpenCV does not seem to provide any such functionality. Any pointers in the right direction would be gladly appreciated.
Edit
Pixel art is a pixelated image of a given image. The screen shot below shows what I've achieved so far.
Basically what I've done is as follows.
Scale the image with aspect ratios maintained so that it would fit within a 64x64 grid
Image sourceImage;
Rectangle imageBound = sourceImage.getBounds();
double sx = (double) 64 / (double) imageBound.width;
double sy = (double) 64 / (double) imageBound.height;
double s = Math.min(sx, sy);
int dx = (int) (s * imageBound.width);
int dy = (int) (s * imageBound.height);
Image scaledImage = new Image(d, sourceImage.getImageData().scaledTo(dx, dy));
Read each pixel from the scaled image and output it on the screen.
Following shows how I extract the color from each pixel. I use SWT framework.
Display d;
ImageData data = scaledImage.getImageData();
for(int i=0; i<data.width; i++) {
for(int j=0; j<data.height; j++) {
int pixel = data.getPixel(i,j);
int red = (pixel & 0x00ff0000) >> 16;
int green = (pixel & 0x0000ff00) >> 8;
int blue = pixel & 0x000000ff;
pixelGrid[i][j].setBackground(new Color(d, red, green, blue));
}
}
But as you can see, there is a major difference in the colors after resizing the image. What I want to know is whether I'm on the correct path to achieve this and if so, how can I retain the actual colors while scaling.
You probably have an image in RGBA format, and are extracting the wrong values from the image (as if it were ARGB). It looks very blue currently, and I fail to see any reds. That should have put you on the right track:
int red = (pixel>>24) & 0xff;
int green = (pixel>>16) & 0xff;
int blue = (pixel>>8) & 0xff;
int alpha = pixel & 0xff; // always 0xff for images without transparency
When a spaceship is destroyed I create list containg pixels of spaceship's image. Pixels are objects of my Pixel class. After creating list it's added to main list where various actions are performed on them. This is how my code looks like:
//Code which creates an array
List<Pixel> pixels = new LinkedList<>();
BufferedImage buff = (BufferedImage)image;
for (int px = 0; px < buff.getWidth(); px++) {
for (int py = 0; py < buff.getHeight(); py++) {
int rgb = buff.getRGB(px, py);
int red = (rgb & 0x00ff0000) >> 16;
int green = (rgb & 0x0000ff00) >> 8;
int blue = rgb & 0x000000ff;
int alpha = (rgb >> 24) & 0xff;
if (alpha == 255) {
pixels.add(new Pixel(px, py, red, green, blue));
}
}
}
//Pixel class constructor
Pixel(float x, float y, int red, int green, int blue) {
super(x, y);
BufferedImage buff = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
WritableRaster raster = buff.getRaster();
//LOOKS EVERYTHING IS OKAY SINCE THIS LINE SO THE ERROR MUST BE SOMEWHERE IN THOSE 2 LINES
raster.setPixel(0, 0, new int[]{red, blue, green, 255});
image = buff;
}
Short explanation: image is private field of type Image. It's used in repaint() method which paints pixel using drawImage() method. And about my problem: Eveything works almost okay. Pixels are creating on right position but all are violet-color. They have different tones(brighter and darker) but are all violet instead of having the same colors as image's colors! Why is this happening? Why violet? Could someone help me unserstand this strange behaviour?
It's probably a mixup of green and blue values in your setPixel method. Colors are usually given in RGB order, which is how you unpacked them from your BufferedImage.
Instead of
raster.setPixel(0, 0, new int[]{red, blue, green, 255});
try
raster.setPixel(0, 0, new int[]{red, green, blue, 255});
If that doesn't work you may have to tinker with different variable orders in your array until it looks right.
I making App in netbeans platform using java Swing and JAI. In this i want to do image processing. I capture .tiff black and white image using X-Ray gun. after that i want to plot histogram of that Black and White image. so, for plot to histogram , first we have to get gray or black and white image pixel value. then we can plot histogram using this pixel value.so, how can i get this pixel value of black and white image?
This should work if you use java.awt.image.BufferedImage.
Since you want to create a histogram, I suppose you will loop through all the pixels. There is the method for returning a single pixel value.
int getRGB(int x, int y)
However, since looping will take place I suppose you'd want to use this one:
int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
When you get the array, use:
int alpha = (pixels[i] >> 24) & 0x000000FF;
int red = (pixels[i] >> 16) & 0x000000FF;
int green = (pixels[i] >>8 ) & 0x000000FF;
int blue = pixels[i] & 0x000000FF;
To extract the channel data. Not sure if the variables can be declared as byte (we are using only one byte of the integer in the array, although byte is signed and different arithmetic takes place - two's complement form), but you can declare them as short.
Then preform some maths on these values, for example:
int average = (red + green + blue) / 3;
This will return the average for the pixel, giving you a point you can use in a simple luminosity histogram.
EDIT:
Regarding histogram creation, I have used this class. It takes the image you want the histogram of as an argument to its setImage(BufferedImage image) method. Use updateHistogram() for array populating. The drawing data is in paintComponent(Graphics g). I must admit, it is sloppy, especially when calculating the offsets, but it can be easily simplified.
Here is the whole class:
class HistogramCtrl extends JComponent
{
BufferedImage m_image;
int[] m_histogramArray = new int[256]; //What drives our histogram
int m_maximumPixels;
public HistogramCtrl(){
m_maximumPixels = 0;
for(short i = 0; i<256; i++){
m_histogramArray[i] = 0;
}
}
void setImage(BufferedImage image){
m_image = image;
updateHistogram();
repaint();
}
void updateHistogram(){
if(m_image == null) return;
int[] pixels = m_image.getRGB(0, 0, m_image.getWidth(), m_image.getHeight(), null, 0, m_image.getWidth());
short currentValue = 0;
int red,green,blue;
for(int i = 0; i<pixels.length; i++){
red = (pixels[i] >> 16) & 0x000000FF;
green = (pixels[i] >>8 ) & 0x000000FF;
blue = pixels[i] & 0x000000FF;
currentValue = (short)((red + green + blue) / 3); //Current value gives the average //Disregard the alpha
assert(currentValue >= 0 && currentValue <= 255); //Something is awfully wrong if this goes off...
m_histogramArray[currentValue] += 1; //Increment the specific value of the array
}
m_maximumPixels = 0; //We need to have their number in order to scale the histogram properly
for(int i = 0; i < m_histogramArray.length;i++){ //Loop through the elements
if(m_histogramArray[i] > m_maximumPixels){ //And find the bigges value
m_maximumPixels = m_histogramArray[i];
}
}
}
protected void paintComponent(Graphics g){
assert(m_maximumPixels != 0);
Rectangle rect = g.getClipBounds();
Color oldColor = g.getColor();
g.setColor(new Color(210,210,210));
g.fillRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
g.setColor(oldColor);
String zero = "0";
String thff = "255";
final short ctrlWidth = (short)rect.getWidth();
final short ctrlHeight = (short)rect.getHeight();
final short activeWidth = 256;
final short activeHeight = 200;
final short widthSpacing = (short)((ctrlWidth - activeWidth)/2);
final short heightSpacing = (short)((ctrlHeight - activeHeight)/2);
Point startingPoint = new Point();
final int substraction = -1;
startingPoint.x = widthSpacing-substraction;
startingPoint.y = heightSpacing+activeHeight-substraction;
g.drawString(zero,widthSpacing-substraction - 2,heightSpacing+activeHeight-substraction + 15);
g.drawString(thff,widthSpacing+activeWidth-substraction-12,heightSpacing+activeHeight-substraction + 15);
g.drawLine(startingPoint.x, startingPoint.y, widthSpacing+activeWidth-substraction, heightSpacing+activeHeight-substraction);
g.drawLine(startingPoint.x,startingPoint.y,startingPoint.x,heightSpacing-substraction);
double factorHeight = (double)activeHeight / m_maximumPixels; //The height divided by the number of pixels is the factor of multiplication for the other dots
Point usingPoint = new Point(startingPoint.x,startingPoint.y);
usingPoint.x+=2; //I want to move this two points in order to be able to draw the pixels with value 0 a bit away from the limit
Point tempPoint = new Point();
for(short i = 0; i<256; i++){
tempPoint.x = usingPoint.x;
tempPoint.y = (int)((heightSpacing+activeHeight-substraction) - (m_histogramArray[i] * factorHeight));
if((i!=0 && (i % 20 == 0)) || i == 255){
oldColor = g.getColor();
g.setColor(oldColor.brighter());
//Draw horizontal ruler sections
tempPoint.x = widthSpacing + i;
tempPoint.y = heightSpacing+activeHeight-substraction+4;
g.drawLine(tempPoint.x,tempPoint.y,widthSpacing + i,heightSpacing+activeHeight-substraction-4);
if(i <= 200){
//Draw vertical ruler sections
tempPoint.x = widthSpacing - substraction - 3;
tempPoint.y = heightSpacing+activeHeight-substraction-i;
g.drawLine(tempPoint.x,tempPoint.y,widthSpacing - substraction + 4, heightSpacing+activeHeight-substraction-i);
}
tempPoint.x = usingPoint.x;
tempPoint.y = usingPoint.y;
g.setColor(oldColor);
}
g.drawLine(usingPoint.x, usingPoint.y, tempPoint.x, tempPoint.y);
usingPoint.x++; //Set this to the next point
}
}
}