Dividing images into parts - java

i am doing XOR of pixel values using the following code
BufferedImage bi=ImageIO.read(new File(1.bmp));
BufferedImage img = new BufferedImage(bi.getWidth(),bi.getHeight(),BufferedImage.TYPE_INT_RGB);
int[] pixel;
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
pixel = bi.getRaster().getPixel(x, y, new int[3]);
img.setRGB(x, y, col);
}
}

You can use the getData(Ractangle rect) to return a region. Check it here

You need to turn these values into parameters - the two 0s and the height, width.
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
Changed it would look like this.
for (int y = y0; y < y1; y++) {
for (int x = x0; x < x1; x++) {
Now turn this into a function/method and make 4 calls to it passing these values.
y0=0; y1=bi.getHeight()/2; x0=0; x1=bi.getWidth()/2
y0=bi.getHeight()/2 + 1; y1=bi.getHeight(); x0=0; x1=bi.getWidth()/2
y0=0; y1=bi.getHeight()/2; x0=bi.getWidth()/2+1; x1=bi.getWidth()
y0=bi.getHeight()/2 + 1; y1=bi.getHeight(); x0=bi.getWidth()/2+1; x1=bi.getWidth()

Related

how to blur the image?

I need to implement image blur
to do this, I have to use a double array in the form of a matrix, and implement the following:
each coefficient of the convolution matrix must be multiplied by the color value of the corresponding neighbor of the current (changeable) pixel
I need to handle going beyond 0 to 255
I tried to create and fill in the matrix with the 1/9 numbers that are in the task, but then I don't understand what and by what should I multiply?
has anyone solved this?
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("image.jpg"));
WritableRaster raster = image.getRaster();
int width = raster.getWidth();
int height = raster.getHeight();
final int colorsCountInRgb = 3;
final int colorMaximum = 255;
int[] pixel = new int[colorsCountInRgb];
double[][] matrix = new double[3][3];
double matrixMultiplier = 1 / 9d;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
matrix[i][j] = matrixMultiplier;
}
}
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
raster.getPixel(x, y, pixel);
raster.setPixel(x, y, pixel);
}
}
ImageIO.write(image, "png", new File("out.png"));
}

How to get count of pixels red color on bitmap android

I want to get number of all red pixels on bitmap image, after I painted on it and merged with back image.
How can I do that? Please give me more details, I will be very grateful, thank you!
Example: Count of red pixels
Iterate through every single pixel in the bitmap
//define your red
static final int RED = Color.RED;
//counting
int count = 0;
for (int x = 0; x <= myBitmap.getWidth(); x++) {
for (int y = 0; x <= myBitmap.getHeight(); y++) {
if(myBitmap.getPixel(x, y))
count++;
}
}
//done. use the variable count
You have a Bitmap, you can get a pixel color from it using code below:
int countX = bitmap.getWidth();
int countY = bitmap.getHeight();
int redCount = 0;
for (int x = 0; x < countX; x++) {
for (int y = 0; y < countY; y--) {
int colorXY = bitmap.getPixel(x, y);
redCount += Color.red(colorXY);
}
}
I got something like this:
int countX = bm.getWidth();
int countY = bm.getHeight();
int redCount = 0;
for (int rangeX = 0; rangeX < countX; rangeX++) {
for (int rangeY = 0; rangeY < countY; rangeY++) {
int colorXY = bm.getPixel(rangeX, rangeY);
int r = Color.red(colorXY);
int g = Color.green(colorXY);
int b = Color.blue(colorXY);
if(Color.rgb(r,g,b) == Color.RED) {
redCount++;
/*bm.setPixel(rangeX,rangeY,Color.GREEN);*/
}
}
}

Repeated lines/stripes using for loops in Java

i want to paint black and white stripes on the image, switching every 20th column both horizontally and vertically on top of an image while staying inside it's borders. so far i can get a black square with 1 pixel wide vertical stripes. i've tried to at least get skinny white stripes on my horizontal lines by switching things around but it's still vertical.
public void zebraStripes() {
Image img = ImageViewer.getImage();
double numPixelsWide = img.getWidth();
int numPixelsHigh = img.getHeight();
Color c = Color.WHITE;
Color b = Color.BLACK;
double i = numPixelsWide;
if (i % 20 == 0) {
for (int x = 0; x < numPixelsHigh; x++) {
for (int y = 0; y < i; y++) {
img.setPixelColor(y, x, b);
}
for (int z = 19; z < i; z = z + 20) {
img.setPixelColor(z, x, c);
}
}
}
}
// paint black and white stripes (left to right) on the image, switching
// every 20th row
public void jailBird() {
Image img = ImageViewer.getImage();
double numPixelsWide = img.getWidth();
double numPixelsHigh = img.getHeight();
Color c = Color.WHITE;
Color b = Color.BLACK;
double i = numPixelsHigh;
if (i % 20 == 0) {
for (int x = 0; x < numPixelsHigh; x++) {
for (int y = 0; y < i; y++) {
img.setPixelColor(y, x, b);
}
for (int z = 19; z < i; z = z + 20) {
img.setPixelColor(z, x, c);
}
}
}
}
}
how do i get the white stripes to be 20 pixels wide and horizontal?
Not tested! Hope it gets you going.
// paint a 20 pixels wide horizontal line for every 40 pixels
for (int y = 0; y < numPixelsHigh; y += 40) {
// paint a stripe
for (int ys = y; ys < y + 20; ys++) {
for (int x = 0; x < numPixelsWide; x++) {
img.setPixelColor(x, ys, Color.BLACK);
}
}
}

Java for loop with multiple incrementers

Im writing a program which combines the RGB pixel values for 3 images, e.g. red pixel of image 1, green pixel of image 2 and blue pixel of image 3 and I want to then create a final image of it.
Im using the code below, but this seems to be incrementing x2 and x3 whilst x1 is the same, i.e. not giving the right pixel value for same co-ordinate for each image.
for (int x = 0; x < image.getWidth(); x++) {
for (int x2 = 0; x2 < image2.getWidth(); x2++) {
for (int x3 = 0; x3 < image3.getWidth(); x3++) {
for (int y = 0; y < image.getHeight(); y++) {
for (int y2 = 0; y2 < image2.getHeight(); y2++) {
for (int y3 = 0; y3 < image3.getHeight(); y3++) {
So I was wondering if anyone can tell me how to iterate through each of the 3 images on the same co-ordinate, so for example read 1, 1 of each image and record the red, green and blue value accordingly. Apologies if it doesnt make complete sense, its a bit hard to explain. I can iterate the values for one image fine but when I add in another, things start to go a bit wrong as obviously its quite a bit more complicated! I was thinking it might be easier to create an array and replace the according values in that just not sure how to do that effectively either.
Thanks
If I understand your question correctly, perhaps you could try something like:
public BufferedImage combine(final BufferedImage image1, final BufferedImage image2, final BufferedImage image3){
final BufferedImage image = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
for(int x = 0; x < image.getWidth(); x++)
for(int y = 0; y < image.getHeight(); y++)
image.setRGB(x, y, new Color(new Color(image1.getRGB(x, y)).getRed(), new Color(image2.getRGB(x, y)).getGreen(), new Color(image3.getRGB(x, y)).getBlue()).getRGB());
return image;
}
For a more readable solution:
public BufferedImage combine(final BufferedImage image1, final BufferedImage image2, final BufferedImage image3){
final BufferedImage image = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
for(int x = 0; x < image.getWidth(); x++){
for(int y = 0; y < image.getHeight(); y++){
final int red = new Color(image1.getRGB(x, y)).getRed();
final int green = new Color(image2.getRGB(x, y)).getGreen();
final int blue = new Color(image3.getRGB(x, y)).getBlue();
final int rgb = new Color(red, green, blue).getRGB();
image.setRGB(x, y, rgb);
}
}
return image;
}
My solution is based on the assumption that all 3 images are similar (same dimensions and type).
You could try a double for, just to iterate over the coordinates. I know it's not functional but the idea may help.
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
int R = image1.R(x, y);
int G = image2.G(x, y);
int B = image3.B(x, y);
// Some other stuff here
}
}

How can i find out where a BufferedImage has Alpha in Java?

I've got a BuferredImage and a boolean[][] array.
I want to set the array to true where the image is completely transparant.
Something like:
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
alphaArray[x][y] = bufferedImage.getAlpha(x, y) == 0;
}
}
But the getAlpha(x, y) method does not exist, and I did not find anything else I can use.
There is a getRGB(x, y) method, but I'm not sure if it contains the alpha value or how to extract it.
Can anyone help me?
Thank you!
public static boolean isAlpha(BufferedImage image, int x, int y)
{
return image.getRBG(x, y) & 0xFF000000 == 0xFF000000;
}
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
alphaArray[x][y] = isAlpha(bufferedImage, x, y);
}
}
Try this:
Raster raster = bufferedImage.getAlphaRaster();
if (raster != null) {
int[] alphaPixel = new int[raster.getNumBands()];
for (int x = 0; x < raster.getWidth(); x++) {
for (int y = 0; y < raster.getHeight(); y++) {
raster.getPixel(x, y, alphaPixel);
alphaArray[x][y] = alphaPixel[0] == 0x00;
}
}
}
public boolean isAlpha(BufferedImage image, int x, int y) {
Color pixel = new Color(image.getRGB(x, y), true);
return pixel.getAlpha() > 0; //or "== 255" if you prefer
}

Categories