I want to convert this matrix into its original image that I have structured from.
I want to know the simplest way to do so.
int pixels[width][height]; //Filled with pixels of colored image(jpg)
i wonder how you going to manage color codes from int type.
Anyways, i think code will be like this. did not tested it
BufferedImage image;
for(int i=0; i<pixels.length; i++) {
for(int j=0; j<pixels[i].length; j++) {
int a = pixels[i][j];
Color newColor = new Color(R,G,B);
image.setRGB(j,i,newColor.getRGB());
}
}
Hope it helps
Related
I have an array representing the pixels of a grayscale image. I need an algorithm that can produce arrays of the same size that represent this image rotated at any angle, preferably without cropping any part of the image.
I've done some research and found various methods to flip images as well as some stuff on rotational matrices but I still don't have a good algorithm to do this.
Pretty simple for loop once you figure it out. If you are using a image processing library do tell because those solutions can be a lot faster.
//this function assumes a rectangular image,
// and rotates it 90 degrees right
public static Color[][] rotate(Color[][] image) {
Color[][] newImage = new Color[image[0].length][image.length];
for(int i = 0; i < image[0].length; i++) {
for(int j = 0; j < image.length; j++) {
newImage[i][j] = image[image.length-j-1][i];
}
}
return newImage;
}
I want to generate a random terrain with OpenSimplexNoise. To start I just want to get a result and draw it to a window.
My question is now: How can I get the correct output of OpenSimplexNoise (cause there are many methods and I just don't know which is the correct one) and how to draw this result.
It should look like this:
public double[][] generateMap(long seed, int width, int height) {
double[][] map = new double[width][height];
// start generating things here, just how?
OpenSimplexNoise simplex = new OpenSimplexNoise(seed);
return map;
}
public void drawMap(double[][] map, Graphics g) {
for(int x = 0; x < map.length; x++) {
for(int y = 0; y < map[0].length; y++) {
Color color = new Color(); // how to get the color here?
}
}
}
This is the current code I've got.
Here is the link to OpenSimplexNoise for anyone who needs it:
https://gist.github.com/KdotJPG/b1270127455a94ac5d19
There's actually only 3 public methods - one for each for 2D, 3D & 4D noise.
Since you're filling a 2D array for your map , use the 2D noise eval method,
something like this:
for(int x=0; x<width; x++){
for(int y=0<y<height; y++){
map[x][y] = simplex.eval(x, y);
}
}
Later on, you can generate a color from the map values as follows:
Color color = Color.color(map[x][y], ma[x][y], map[x][y]);
The author also provides example usage code in OpenSimplexNoiseTest; he's using the 3D eval method, but always holding the z coord at zero. My guess is the example code was written before he added the 2D & 4D implementations. At any rate, it still works, but it might be a little slower than directly using 2D.
First off I am new to this and also coding. I apologize in advance for anything that is misleading.
I am currently writing a Java program that uses an image as input. What I have currently is scanning each pixel by the width and height of the image saving the HSB in an array and then outputting the percentage of each color in the image. I now want to omit the background from that calculation. To start off lets just say the background is white. There are also pixels in the image that are not in the background that is white though.
thank you,
Oh, it's not as simple as you hope.
You cannot simply detect what's a background and what is part of image pixel by pixel.
You might try looking at this post to see how to remove one color layer of the image.
But detecting if the white pixel is a part of background or already the image?!
There are multiple possible ways:
assuming that background is just around and when (looking from any
side to the center) color changes, that is the end of the
"background". You can check every row and column from side to center
and keep record of where the "background" color ends.
or similar approach - if at least at one (of four) direction looking from the pixel to the side there is no color changes (it goes white all the way to the side), than it is part of background.
Or just take a look at another
post. From this you can try working your way up.
Anyway - you have to create a logic of detecting which (i.e.) white pixels are part of the picture and which are part of background.
I hope this at least gives you a bit more knowledge.
I am not sure what you mean. There is no code, so I can only give you an example. From what I have understood, you want to skip doing a calculation when the color is the same as the background. That is very simple. You can do something like this:
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
Color pixelColor = get pixel color at x and y;
Color backgroundColor = the background color;
if(pixelColor != backgroundColor) {
//Calculation will be done here
}
}
}
If this does not help you or you have any other questions, ask me.
Here is an example of full working code.
You can add a cursor to pick color and jslider for fuzz or threshold. Use backColor and threshold for your needs.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Convert {
private static final Color backColor = new Color(255,255,255);
private static final int THRESHOLD = 35;
private static final int TRANSPARENT = 0; // 0x00000000;
static File base = new File("f://mortar1.png");
static File base2 = new File("f://outtrans.png");
public static void main(String[] args) throws IOException {
System.out.println("Convert.main()");
for (File file : base.listFiles())
{
BufferedImage initImage = ImageIO.read(base);
int width = initImage.getWidth(null),
height = initImage.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.drawImage(initImage, 0, 0, null);
System.out.println("before: " + image.getRGB(0, 0));
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixel = image.getRGB(x, y);
Color color = new Color(pixel);
int dr = Math.abs(color.getRed() - backColor.getRed()),
dg = Math.abs(color.getGreen() - backColor.getGreen()),
db = Math.abs(color.getBlue() - backColor.getBlue());
if (dr < THRESHOLD && dg < THRESHOLD && db < THRESHOLD) {
image.setRGB(x, y, TRANSPARENT);
}
}
}
System.out.println(" after: " + image.getRGB(0, 0));
File file = new File("f://outtrans1.png");
ImageIO.write(image, "png", file);
}
}
}
Or simple set a png file with empty fill..
This idea seems good for me. But if you don't agree, please, tell me causes)
So after hours of searching I am ready to pull my hair out on this one.
I am doing some research in Computer Vision and am working with grayscale images. I need to end up with an "image" (a double scripted double array) of Sobel filtered double values. My Sobel converter is set up to take in a double scripted int array (int[][]) and go from there.
I am reading in a buffered image and I gather the grayscale int values via a method that I am 99% sure works perfectly (I can present it if need be).
Next I am attempting to convert this matrix of int values to a BufferedImage by the below method:
private BufferedImage getBIFromIntArr(int[][] matrix){
BufferedImage img = new BufferedImage(matrix.length * 4, matrix[0].length, BufferedImage.TYPE_INT_ARGB);
- Gather the pixels in the form of [alpha, r, g, b]
- multiply the size of the array by 4 for the model
int[] pixels = new int[(matrix.length * 4 * matrix[0].length)];
int index = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
int pixel = matrix[i][j];
pixels[index] = pixel;
index++;
for (int k = 0; k < 3; k++) {
pixels[index] = 0;
index++;
}
}
}
-get the raster
WritableRaster raster = img.getRaster();
-output the amount of pixels and a sample of the array
System.out.println(pixels.length);
for (int i = 0; i < pixels.length; i++) {
System.out.print(pixels[i] + " ");
}
- set the pixels of the raster
raster.setPixels(0, 0, matrix.length, matrix[0].length, pixels);
- paint the image via an external routing to check works (does not)
p.panel.setNewImage(img);
return img;
}
Here is my understanding. The ARGB Type consists of 4 values Alpha, Red, Green, and Blue. I am guessing that setting the alpha values in the new BufferedImage to the greyscale image int values (the matrix values passed in) then this will reproduce the image. Please correct me if I am wrong. So as you can see I create an array of pixels that stores the int values like this: [intValue, 0, 0, 0] repeatedly to try to stay with the 4 value model.
Then I create a writable raster and set the gathered pixels in it using the gathered pixels. The only thing is that I get nothing in the BufferedImage. No error with the code below and Im sure my indeces are correct.
What am I doing wrong? Im sure it is obvious but any help is appreciated because I cant see it. Perhaps my assumption about the model is wrong?
Thanks,
Chronic
I have an array called image[][] and i want to create a BufferedImage out of this so I can have the player store it in a file.
// Initialize Color[][] however you were already doing so.
Color[][] image;
// Initialize BufferedImage, assuming Color[][] is already properly populated.
BufferedImage bufferedImage = new BufferedImage(image.length, image[0].length,
BufferedImage.TYPE_INT_RGB);
// Set each pixel of the BufferedImage to the color from the Color[][].
for (int x = 0; x < image.length; x++) {
for (int y = 0; y < image[x].length; y++) {
bufferedImage.setRGB(x, y, image[x][y].getRGB());
}
}
This is a straightforward way of creating (and potentially storing) an image, if that's what you're trying to get at. However, this is not efficient by any means. Try it with a larger image and you'll see a noticeable speed difference.