Divide canvas into multiple stripes using RGB - java

I want to divide the canvas into 8 vertical black and white Stripes. I wrote a method changeValue to change the value of the color everytime the loop is executed but the canvas is complete white. I think my changeValue method doesn't do as supposed but I can't explain why.
So far I got this:
public class Stripe {
public boolean switch = false;
private void stripes(int[] pixels, int width, int height) {
// TODO set some values here
int counter = 1;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int r, g, b;
// TODO code for the flag.
r = 255;
g = 255;
b = 255;
for (int i = 0; i < 8; i++) {
if (x < (counter * (width - 1)) / 8) {
r = changeValue();
g = changeValue();
b = changeValue();
}
counter++;
switch ^= true;
}
pixels[pos] = 0xFF000000 | (r << 16) | (g << 8) | b;
}
}
}
public int changeValue() {
if (switch) {
return 255;
} else {
return 0;
}
}
}
I am supposed not to write 8 If statements for the 8 stripes but this for example is the code for the Italian flag which are 3 vertical Stripes but which actually works:
private void flagItalian(int[] pixels, int width, int height) {
// TODO set some values here
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pos = y * width + x;
int r, g, b;
// TODO code for the flag.
r = 255;
g = 0;
b = 0;
if (x < 2 * (width - 1) / 3) {
r = 255;
g = 255;
b = 255;
}
if (x < (width - 1) / 3) {
r = 0;
g = 255;
b = 0;
}
pixels[pos] = 0xFF000000 | (r << 16) | (g << 8) | b;
}
}
}

If you need alternating color stripes then you could go for the classic odd/even checks. Instead of you below code block
for (int i = 0; i < 8; i++) {
if (x < (counter * (width - 1)) / 8) {
r = changeValue();
g = changeValue();
b = changeValue();
}
counter++;
switch ^= true;
}
Try replacing with this one
if (x%2) {
r = 255;
g = 255;
b = 255
} else {
r = 0;
g = 0;
b = 0;
}

Related

How to implement 1-Bit Dithering using Java?

Recently, our teacher gave us the task to convert a colorful image to a 1-bit image using Java. After a little experimentation I had the following result:
BufferedImage image = ...
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int clr = image.getRGB(x, y);
int r = (clr & 0x00ff0000) >> 16;
int g = (clr & 0x0000ff00) >> 8;
int b = clr & 0x000000ff;
double mono = 0.2126*r + 0.7152*g + 0.0722*b;
int c = mono < 128 ? 1 : 0;
//Adding to image buffer
buffer.add(c);
}
}
Well, it works but a lot of details are unfortunately lost. Here is a comparison:
Original:
Output:
What I want: (HQ: https://i.stack.imgur.com/vlEAE.png)
I was considering adding dithering to my converter, but I haven't found a working way yet, let alone any pseudo code.
Can anyone help me?
Edit:
So I created a DitheringUtils-class:
import java.awt.Color;
import java.awt.image.BufferedImage;
public class DitheringUtils {
public static BufferedImage dithering(BufferedImage image) {
Color3i[] palette = new Color3i[] {
new Color3i(0, 0, 0),
new Color3i(255, 255, 255)
};
int width = image.getWidth();
int height = image.getHeight();
Color3i[][] buffer = new Color3i[height][width];
for(int y=0;y<height;y++) {
for(int x=0;x<width;x++) {
buffer[y][x] = new Color3i(image.getRGB(x, y));
}
}
for(int y=0; y<image.getHeight();y++) {
for(int x=0; x<image.getWidth();x++) {
Color3i old = buffer[y][x];
Color3i nem = findClosestPaletteColor(old, palette);
image.setRGB(x, y, nem.toColor().getRGB());
Color3i error = old.sub(nem);
if (x+1 < width) buffer[y ][x+1] = buffer[y ][x+1].add(error.mul(7./16));
if (x-1>=0 && y+1<height) buffer[y+1][x-1] = buffer[y+1][x-1].add(error.mul(3./16));
if (y+1 < height) buffer[y+1][x ] = buffer[y+1][x ].add(error.mul(5./16));
if (x+1<width && y+1<height) buffer[y+1][x+1] = buffer[y+1][x+1].add(error.mul(1./16));
}
}
return image;
}
private static Color3i findClosestPaletteColor(Color3i match, Color3i[] palette) {
Color3i closest = palette[0];
for(Color3i color : palette) {
if(color.diff(match) < closest.diff(match)) {
closest = color;
}
}
return closest;
}
}
class Color3i {
private int r, g, b;
public Color3i(int c) {
Color color = new Color(c);
this.r = color.getRed();
this.g = color.getGreen();
this.b = color.getBlue();
}
public Color3i(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public Color3i add(Color3i o) {
return new Color3i(r + o.r, g + o.g, b + o.b);
}
public Color3i sub(Color3i o) {
return new Color3i(r - o.r, g - o.g, b - o.b);
}
public Color3i mul(double d) {
return new Color3i((int) (d * r), (int) (d * g), (int) (d * b));
}
public int diff(Color3i o) {
return Math.abs(r - o.r) + Math.abs(g - o.g) + Math.abs(b - o.b);
}
public int toRGB() {
return toColor().getRGB();
}
public Color toColor() {
return new Color(clamp(r), clamp(g), clamp(b));
}
public int clamp(int c) {
return Math.max(0, Math.min(255, c));
}
}
And changed my function to this:
for (int y = 0; y < dithImage.getHeight(); ++y) {
for (int x = 0; x < dithImage.getWidth(); ++x) {
final int clr = dithImage.getRGB(x, y);
final int r = (clr & 0xFF0000) >> 16;
final int g = (clr & 0xFF00) >> 8;
final int b = clr & 0xFF;
if(382.5>(r+g+b)) {
buffer.add(0);
} else {
buffer.add(1);
}
}
}
But the output ends up looking... strange?
I really don't get why there are such waves.
I finally got it working! I improved the diff function and changed if(382.5>(r+g+b)) to if(765==(r+g+b)).
My DitheringUtils-class:
import java.awt.Color;
import java.awt.image.BufferedImage;
public class DitheringUtils {
public static BufferedImage dithering(BufferedImage image) {
Color3i[] palette = new Color3i[] {
new Color3i(0, 0, 0),
new Color3i(255, 255, 255)
};
int width = image.getWidth();
int height = image.getHeight();
Color3i[][] buffer = new Color3i[height][width];
for(int y=0;y<height;y++) {
for(int x=0;x<width;x++) {
buffer[y][x] = new Color3i(image.getRGB(x, y));
}
}
for(int y=0; y<image.getHeight();y++) {
for(int x=0; x<image.getWidth();x++) {
Color3i old = buffer[y][x];
Color3i nem = findClosestPaletteColor(old, palette);
image.setRGB(x, y, nem.toColor().getRGB());
Color3i error = old.sub(nem);
if (x+1 < width) buffer[y ][x+1] = buffer[y ][x+1].add(error.mul(7./16));
if (x-1>=0 && y+1<height) buffer[y+1][x-1] = buffer[y+1][x-1].add(error.mul(3./16));
if (y+1 < height) buffer[y+1][x ] = buffer[y+1][x ].add(error.mul(5./16));
if (x+1<width && y+1<height) buffer[y+1][x+1] = buffer[y+1][x+1].add(error.mul(1./16));
}
}
return image;
}
private static Color3i findClosestPaletteColor(Color3i match, Color3i[] palette) {
Color3i closest = palette[0];
for(Color3i color : palette) {
if(color.diff(match) < closest.diff(match)) {
closest = color;
}
}
return closest;
}
}
class Color3i {
private int r, g, b;
public Color3i(int c) {
Color color = new Color(c);
this.r = color.getRed();
this.g = color.getGreen();
this.b = color.getBlue();
}
public Color3i(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
public Color3i add(Color3i o) {
return new Color3i(r + o.r, g + o.g, b + o.b);
}
public Color3i sub(Color3i o) {
return new Color3i(r - o.r, g - o.g, b - o.b);
}
public Color3i mul(double d) {
return new Color3i((int) (d * r), (int) (d * g), (int) (d * b));
}
public int diff(Color3i o) {
int Rdiff = o.r - r;
int Gdiff = o.g - g;
int Bdiff = o.b - b;
int distanceSquared = Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff;
return distanceSquared;
}
public int toRGB() {
return toColor().getRGB();
}
public Color toColor() {
return new Color(clamp(r), clamp(g), clamp(b));
}
public int clamp(int c) {
return Math.max(0, Math.min(255, c));
}
}
The final writing function:
for (int y = 0; y < dithImage.getHeight(); ++y) {
for (int x = 0; x < dithImage.getWidth(); ++x) {
final int clr = dithImage.getRGB(x, y);
final int r = (clr & 0xFF0000) >> 16;
final int g = (clr & 0xFF00) >> 8;
final int b = clr & 0xFF;
if(765==(r+g+b)) {
buffer.add(0);
} else {
buffer.add(1);
}
}
}
Thanks everyone!
BufferedImage image = ...
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y));
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int mono = (red+green+blue)/255;
//Adding to image buffer
int col = (0 << 24) | (mono << 16) | (mono << 8) | mono;
image.setRGB(x,y,col);
}
}
Try this out
What you were doing wrong was, instead of trying to convert the picture to grayscale you were trying to convert to black and white.

Cylinder shape image using Jhlabs image processing library

I want to implement image processing in my project. I had used JLabs library. I want to do using this library following effects.
1) Deboss
2) Engrave
3) Satin etch
4) Custom shape like Cylinder.
This is sample image emboss code.
public class EmbossFilter
extends WholeImageFilter {
private static final float pixelScale = 255.9f;
private float azimuth = 2.3561945f;
private float elevation = 0.5235988f;
private boolean emboss = false;
private float width45 = 3.0f;
public void setAzimuth(float azimuth) {
this.azimuth = azimuth;
}
public float getAzimuth() {
return this.azimuth;
}
public void setElevation(float elevation) {
this.elevation = elevation;
}
public float getElevation() {
return this.elevation;
}
public void setBumpHeight(float bumpHeight) {
this.width45 = 3.0f * bumpHeight;
}
public float getBumpHeight() {
return this.width45 / 3.0f;
}
public void setEmboss(boolean emboss) {
this.emboss = emboss;
}
public boolean getEmboss() {
return this.emboss;
}
#Override
protected int[] filterPixels(int width, int height, int[] inPixels, Rect transformedSpace) {
int index = 0;
int[] outPixels = new int[width * height];
int bumpMapWidth = width;
int bumpMapHeight = height;
int[] bumpPixels = new int[bumpMapWidth * bumpMapHeight];
int i = 0;
while (i < inPixels.length) {
bumpPixels[i] = PixelUtils.brightness(inPixels[i]);
++i;
}
int Lx = (int) (Math.cos(this.azimuth) * Math.cos(this.elevation) * 255.89999389648438);
int Ly = (int) (Math.sin(this.azimuth) * Math.cos(this.elevation) * 255.89999389648438);
int Lz = (int) (Math.sin(this.elevation) * 255.89999389648438);
int Nz = (int) (1530.0f / this.width45);
int Nz2 = Nz * Nz;
int NzLz = Nz * Lz;
int background = Lz;
int bumpIndex = 0;
int y = 0;
while (y < height) {
int s1 = bumpIndex;
int s2 = s1 + bumpMapWidth;
int s3 = s2 + bumpMapWidth;
int x = 0;
while (x < width) {
int shade;
if (y != 0 && y < height - 2 && x != 0 && x < width - 2) {
int NdotL;
int Nx = bumpPixels[s1 - 1] + bumpPixels[s2 - 1] + bumpPixels[s3 - 1] - bumpPixels[s1 + 1] - bumpPixels[s2 + 1] - bumpPixels[s3 + 1];
int Ny = bumpPixels[s3 - 1] + bumpPixels[s3] + bumpPixels[s3 + 1] - bumpPixels[s1 - 1] - bumpPixels[s1] - bumpPixels[s1 + 1];
shade = Nx == 0 && Ny == 0 ? background : ((NdotL = Nx * Lx + Ny * Ly + NzLz) < 0 ? 0 : (int) ((double) NdotL / Math.sqrt(Nx * Nx + Ny * Ny + Nz2)));
} else {
shade = background;
}
if (this.emboss) {
int rgb = inPixels[index];
int a = rgb & -16777216;
int r = rgb >> 16 & 255;
int g = rgb >> 8 & 255;
int b = rgb & 255;
r = r * shade >> 8;
g = g * shade >> 8;
b = b * shade >> 8;
outPixels[index++] = a | r << 16 | g << 8 | b;
} else {
outPixels[index++] = -16777216 | shade << 16 | shade << 8 | shade;
}
++x;
++s1;
++s2;
++s3;
}
++y;
bumpIndex += bumpMapWidth;
}
return outPixels;
}
public String toString() {
return "Stylize/Emboss...";
}
}
How to achieve image effects using Jhlabs library or any other way please share with me.

jfeaturelib, java for glcm

I am doing my program using jfeaturelib for searching glcm features. I am using this haralick.java and already run my program perfectly using this demo HaralickDemo.java. All I want to know is how to use horizontal neigbhor 0 degree only or 90 degree only. This is the code:
private void calculate() {
calculateGreyValues();
final int imageWidth = image.getWidth();
final int imageHeight = image.getHeight();
final int d = HARALICK_DIST;
int i, j, pos;
// image is not empty per default
for (int y = 0; y < imageHeight; y++) {
for (int x = 0; x < imageWidth; x++) {
pos = imageWidth * y + x;
// horizontal neighbor: 0 degrees
i = x - d;
// j = y;
if (!(i < 0)) {
increment(grayValue[pos], grayValue[pos - d]);
}
// vertical neighbor: 90 degree
// i = x;
j = y - d;
if (!(j < 0)) {
increment(grayValue[pos], grayValue[pos - d * imageWidth]);
}
// 45 degree diagonal neigbor
i = x + d;
j = y - d;
if (i < imageWidth && !(j < 0)) {
increment(grayValue[pos], grayValue[pos + d - d * imageWidth]);
}
// 135 vertical neighbor
i = x - d;
j = y - d;
if (!(i < 0) && !(j < 0)) {
increment(grayValue[pos], grayValue[pos - d - d * imageWidth]);
}
}
}
And can you explain me detail of this haralick.java program?

How to import .java library in IntelliJ (folder of .java files)

I am a new programmer.
Here, I am trying to import a library (com.digitalmodular).
What I want to do is run the java program in here
package demos;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Arrays;
import com.digitalmodular.utilities.RandomFunctions;
import com.digitalmodular.utilities.gui.ImageFunctions;
import com.digitalmodular.utilities.swing.window.PixelImage;
import com.digitalmodular.utilities.swing.window.PixelWindow;
/**
* #author jeronimus
*/
// Date 2014-02-28
public class AllColorDiffusion extends PixelWindow implements Runnable {
private static final int CHANNEL_BITS = 7;
public static void main(String[] args) {
int bits = CHANNEL_BITS * 3;
int heightBits = bits / 2;
int widthBits = bits - heightBits;
new AllColorDiffusion(CHANNEL_BITS, 1 << widthBits, 1 << heightBits);
}
private final int width;
private final int height;
private final int channelBits;
private final int channelSize;
private PixelImage img;
private javax.swing.Timer timer;
private boolean[] colorCube;
private long[] foundColors;
private boolean[] queued;
private int[] queue;
private int queuePointer = 0;
private int remaining;
public AllColorDiffusion(int channelBits, int width, int height) {
super(1024, 1024 * height / width);
RandomFunctions.RND.setSeed(0);
this.width = width;
this.height = height;
this.channelBits = channelBits;
channelSize = 1 << channelBits;
}
#Override
public void initialized() {
img = new PixelImage(width, height);
colorCube = new boolean[channelSize * channelSize * channelSize];
foundColors = new long[channelSize * channelSize * channelSize];
queued = new boolean[width * height];
queue = new int[width * height];
for (int i = 0; i < queue.length; i++)
queue[i] = i;
new Thread(this).start();
}
#Override
public void resized() {}
#Override
public void run() {
timer = new javax.swing.Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
draw();
}
});
while (true) {
img.clear(0);
init();
render();
}
// System.exit(0);
}
private void init() {
RandomFunctions.RND.setSeed(0);
Arrays.fill(colorCube, false);
Arrays.fill(queued, false);
remaining = width * height;
// Initial seeds (need to be the darkest colors, because of the darkest
// neighbor color search algorithm.)
setPixel(width / 2 + height / 2 * width, 0);
remaining--;
}
private void render() {
timer.start();
for (; remaining > 0; remaining--) {
int point = findPoint();
int color = findColor(point);
setPixel(point, color);
}
timer.stop();
draw();
try {
ImageFunctions.savePNG(System.currentTimeMillis() + ".png", img.image);
}
catch (IOException e1) {
e1.printStackTrace();
}
}
void draw() {
g.drawImage(img.image, 0, 0, getWidth(), getHeight(), 0, 0, width, height, null);
repaintNow();
}
private int findPoint() {
while (true) {
// Time to reshuffle?
if (queuePointer == 0) {
for (int i = queue.length - 1; i > 0; i--) {
int j = RandomFunctions.RND.nextInt(i);
int temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
queuePointer = queue.length;
}
}
if (queued[queue[--queuePointer]])
return queue[queuePointer];
}
}
private int findColor(int point) {
int x = point & width - 1;
int y = point / width;
// Calculate the reference color as the average of all 8-connected
// colors.
int r = 0;
int g = 0;
int b = 0;
int n = 0;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
point = (x + i & width - 1) + width * (y + j & height - 1);
if (img.pixels[point] != 0) {
int pixel = img.pixels[point];
r += pixel >> 24 - channelBits & channelSize - 1;
g += pixel >> 16 - channelBits & channelSize - 1;
b += pixel >> 8 - channelBits & channelSize - 1;
n++;
}
}
}
r /= n;
g /= n;
b /= n;
// Find a color that is preferably darker but not too far from the
// original. This algorithm might fail to take some darker colors at the
// start, and when the image is almost done the size will become really
// huge because only bright reference pixels are being searched for.
// This happens with a probability of 50% with 6 channelBits, and more
// with higher channelBits values.
//
// Try incrementally larger distances from reference color.
for (int size = 2; size <= channelSize; size *= 2) {
n = 0;
// Find all colors in a neighborhood from the reference color (-1 if
// already taken).
for (int ri = r - size; ri <= r + size; ri++) {
if (ri < 0 || ri >= channelSize)
continue;
int plane = ri * channelSize * channelSize;
int dr = Math.abs(ri - r);
for (int gi = g - size; gi <= g + size; gi++) {
if (gi < 0 || gi >= channelSize)
continue;
int slice = plane + gi * channelSize;
int drg = Math.max(dr, Math.abs(gi - g));
int mrg = Math.min(ri, gi);
for (int bi = b - size; bi <= b + size; bi++) {
if (bi < 0 || bi >= channelSize)
continue;
if (Math.max(drg, Math.abs(bi - b)) > size)
continue;
if (!colorCube[slice + bi])
foundColors[n++] = Math.min(mrg, bi) << channelBits * 3 | slice + bi;
}
}
}
if (n > 0) {
// Sort by distance from origin.
Arrays.sort(foundColors, 0, n);
// Find a random color amongst all colors equally distant from
// the origin.
int lowest = (int)(foundColors[0] >> channelBits * 3);
for (int i = 1; i < n; i++) {
if (foundColors[i] >> channelBits * 3 > lowest) {
n = i;
break;
}
}
int nextInt = RandomFunctions.RND.nextInt(n);
return (int)(foundColors[nextInt] & (1 << channelBits * 3) - 1);
}
}
return -1;
}
private void setPixel(int point, int color) {
int b = color & channelSize - 1;
int g = color >> channelBits & channelSize - 1;
int r = color >> channelBits * 2 & channelSize - 1;
img.pixels[point] = 0xFF000000 | ((r << 8 | g) << 8 | b) << 8 - channelBits;
colorCube[color] = true;
int x = point & width - 1;
int y = point / width;
queued[point] = false;
for (int j = -1; j <= 1; j++) {
for (int i = -1; i <= 1; i++) {
point = (x + i & width - 1) + width * (y + j & height - 1);
if (img.pixels[point] == 0) {
queued[point] = true;
}
}
}
}
}
The thing is, I cant figure out how to import the library into IntelliJ. I have tried importing the library from Project Structure->Modules->Dependencies->Library->Java but failed. It appears that all the files in the given library are .java files, not .jar files
How should I import the library? Do I need to compile the whole library first? If yes, how?
This is my first question on this site, so my question may not be so clear :P
try
go to project settings -> Platform Settings -> SDKs -> Sourcepath (in the right panel) and add your downloaded zip -> Apply -> OK

Assign color to a pixel based on its neighbors

I've put all the black pixels of the image in an array and I want them to get the color of their left neighbor. I run the code without errors but the result is not really what I'm expecting.
Where those black stripes comes form? I was expecting it to be all red.
Here's my code and results.
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.*;
public class ImageTest {
public static BufferedImage Threshold(BufferedImage img) {
int height = img.getHeight();
int width = img.getWidth();
BufferedImage finalImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
int r = 0;
int g = 0;
int b = 0;
List<Integer> blackpixels = new ArrayList<Integer>();
for (int x = 0; x < width; x++) {
try {
for (int y = 0; y < height; y++) {
//Get RGB values of pixels
int rgb = img.getRGB(x, y);
r = ImageTest.getRed(rgb);
g = ImageTest.getGreen(rgb);
b = ImageTest.getBlue(rgb);
int leftLoc = (x-1) + y*width;
if ((r < 5) && (g < 5) && (b < 5)) {
blackpixels.add(rgb);
Integer[] simpleArray = new Integer[ blackpixels.size() ];
System.out.print(simpleArray.length);
int pix = 0;
while(pix < simpleArray.length) {
r = leftLoc;
pix = pix +1;
}
}
finalImage.setRGB(x,y,ImageTest.mixColor(r, g,b));
}
}
catch (Exception e) {
e.getMessage();
}
}
return finalImage;
}
private static int mixColor(int red, int g, int b) {
return red<<16|g<<8|b;
}
public static int getRed(int rgb) {
return (rgb & 0x00ff0000) >> 16;
}
public static int getGreen(int rgb) {
return (rgb & 0x0000ff00) >> 8;
}
public static int getBlue(int rgb) {
return (rgb & 0x000000ff) >> 0;
}
}
The following might work.
The main change is that it first collects the locations of ALL dark pixels, then goes over them to assign the colour from their left neighbours.
import java.awt.image.BufferedImage;
import java.util.*;
public class BlackRedImage
{
public static BufferedImage Threshold( BufferedImage img )
{
int height = img.getHeight();
int width = img.getWidth();
BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
List<Integer> blackpixels = new ArrayList<Integer>();
for ( int x = 0; x < width; x++ )
{
for ( int y = 0; y < height; y++ )
{
int rgb = img.getRGB(x, y); // Get the pixel in question
int r = BlackRedImage.getRed(rgb);
int g = BlackRedImage.getGreen(rgb);
int b = BlackRedImage.getBlue(rgb);
if ( (r < 5) && (g < 5) && (b < 5) )
{ // record location of any "black" pixels found
blackpixels.add(x + (y * width));
}
finalImage.setRGB(x, y, rgb);
}
}
// Now loop through all "black" pixels, setting them to the colour found to their left
for ( int blackPixelLocation: blackpixels )
{
if ( blackPixelLocation % width == 0 )
{ // these pixels are on the left most edge, therefore they do not have a left neighbour!
continue;
}
int y = blackPixelLocation / width;
int x = blackPixelLocation - (width * y);
int rgb = img.getRGB(x - 1, y); // Get the pixel to the left of the "black" pixel
System.out.println("x = " + x + ", y = " + y + ", rgb = " + rgb);
finalImage.setRGB(x, y, rgb);
}
return finalImage;
}
private static int mixColor( int red, int g, int b )
{
return red << 16 | g << 8 | b;
}
public static int getRed( int rgb )
{
return (rgb & 0x00ff0000) >> 16;
}
public static int getGreen( int rgb )
{
return (rgb & 0x0000ff00) >> 8;
}
public static int getBlue( int rgb )
{
return (rgb & 0x000000ff) >> 0;
}
}
EDIT: Here is a simpler version (doesn't collect the black pixels)
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.*;
public class ColourMove
{
public static BufferedImage Threshold( BufferedImage img )
{
int width = img.getWidth();
int height = img.getHeight();
BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for ( int x = 1; x < width; x++ ) // Start at 1 as the left most edge doesn't have a left neighbour
{
for ( int y = 0; y < height; y++ )
{
Color colour = new Color(img.getRGB(x, y));
int red = colour.getRed();
int green = colour.getGreen();
int blue = colour.getBlue();
if ( (red < 5) && (green < 5) && (blue < 5) )
{ // Encountered a "black" pixel, now replace it with it's left neighbour
finalImage.setRGB(x, y, img.getRGB(x - 1, y));
}
else
{ // Non-black pixel
finalImage.setRGB(x, y, colour.getRGB());
}
}
}
return finalImage;
}
}

Categories