I have such a problem. I want to make "average" image from the few diffirent. my idea was to load them to BufferImage sequentially, sum values in each bit, and than just divide by number of photos. The problem is that i can't divide byte[][] by number. What am i doing wrong, or what is the proper way to do this?
File[] others = getOtherImageFiles(file);
byte[][] pixels = new byte[300][];
List<byte[][]> pixelsList = new ArrayList<byte[][]>();
for (int i = 0; i < others.length; i++) {
try {
img = ImageIO.read(others[i]);
} catch (IOException e) {
}
for (int x = 0; x < img.getWidth(); x++) {
pixels[x] = new byte[img.getHeight()];
for (int y = 0; y < img.getHeight(); y++) {
pixels[x][y] = (byte) (img.getRGB(x, y));
}
}
pixelsList.add(pixels);
}
byte[][] pixelsSum = new byte[300][];
for (int i = 0; i < pixelsList.size(); i++) {
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
pixelsSum[x][y] += pixelsList.get(i)[x][y];
}
}
}
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
pixelsSum[x][y]/10;
}
}
}
i want to use them as a face comparer and the point is to create a scheme of faces.
You're close. You did the division by 10, but you never wrote the result back into the pixelsSum array, so the result is lost. You need to do:
pixelsSum[x][y] = pixelsSum[x][y] / 10;
or in shorthand:
pixelsSum[x][y] /= 10;
Another problem you will run into is that each byte in Java (or any other language) can only hold values between 0 and 255. So if you have a bytes x = 255, y = 1, x + y will turn out to be 0. When you add up 10 images, it will almost certainly go over the 255 limit. I suggest changing pixelsSum from byte[][] to int[][], and then turn it back to bytes after you do the division.
Instead of this set of loops:
for (int i = 0; i < pixelsList.size(); i++) {
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
pixelsSum[x][y] += pixelsList.get(i)[x][y];
}
}
}
you should put the pixelsList reference in the middle:
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
for (int i = 0; i < pixelsList.size(); i++) {
pixelsSum[x][y] += pixelsList.get(i)[x][y];
}
pixelsSum[x][y] /= pixelsList.size();
}
}
This should calculate the average immediately rather than looping through the pixelsSum array again. Note also the use of the /= operator which performs both division and assignment compared with the / operator which is limited to division in the context of numbers.
Related
I have this method that's able to rotate the array by 90 degrees. I want to be to flip vertically and horizontally (I'm binding them with different buttons). Here's the method.
private void Rotate90() {
String[][] temp = new String[totalX][totalY];
for (int y = 0; y < totalY; y++) {
for (int x = 0; x < totalX; x++) {
temp[x][y] = fields[x][y].getText();
}
}
for (int y = 0; y < totalY; y++) {
for (int x = 0; x < totalX; x++) {
fields[x][y].setText(temp[y][x]);
}
}
Draw();
}
#khriskooper code contains an obvious bug: it flips array twice i.e. effectively does nothing. To flip array you should iterate only half of the indices. Try something like this:
private void flipHorizontally() {
for (int y = 0; y < totalY; y++) {
for (int x = 0; x < totalX/2; x++) {
String tmp = fields[totalX-x-1][y].getText();
fields[totalX-x-1][y].setText(fields[x][y].getText());
fields[x][y].setText(tmp);
}
}
}
private void flipVertically() {
for (int x = 0; x < totalX; x++) {
for (int y = 0; y < totalY/2; y++) {
String tmp = fields[x][totalY - y - 1].getText();
fields[x][totalY - y - 1].setText(fields[x][y].getText());
fields[x][y].setText(tmp);
}
}
}
I'm a little stuck on the arithmetic for this program. I have a string theString that has a length of x*y*z. x, y, and z are the dimensions of a char[][][] array (lets call it cArr).
I want to insert the characters of theString into cArr based on a specific index. Here's an example of a 5x4x3 array.
String theString = someMethod(); //returns a string of length 60
char[][][] cArr = new char[5][4][3];
for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y][z] = theString.charAt(/*~~~*/);
}
}
}
I can figure out the arithmetic to insert characters into a char[5][4] array from a string of length 20:
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y] = theString.charAt((5*y)+x);
}
}
What arithmetic should I be using in the charAt() method for a 3-dimensional char array?
My best attempt has been:
char[][][] cArr = new char[5][4][3];
for (int z = 0; z < 3; z++) {
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 5; x++) {
cArr[x][y][z] = theString.charAt(((3^2)*z)+(4*y)+x);
}
}
}
But this makes all z-index layers of cArr the same.
The rule is simple for problems like this. You start from the innermost loop and proceed up with x + y*(length of x) + z*(length of y)*(length of x).
In this example, it would be,
cArr[x][y][z] = theString.charAt(x + y*5 + z*4*5);
For some reason the blocks keep being rendered in the same position. Can anybody help me?
Block[][] chunk = new Block[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS];
float[][] positions = new float[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS];
float frequency = 1.0f / (float) chunk.length;
for (int x = 0; x < chunk.length - 1; x++)
{
for (int y = 0; y < chunk[x].length - 1; y++)
{
positions[x][y] = SimplexNoise.Generate((float) x * frequency, (float) y * frequency);
g.drawRect(positions[x][0], positions[0][y], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT);
}
}
for (int x = 0; x < Chunk.CHUNK_WIDTH_BLOCKS; x++)
{
for (int y = 0; y < Chunk.CHUNK_HEIGHT_BLOCKS; y++)
{
if (positions[x][y] < 0f)
chunk[x][y] = new Block();
if (positions[x][y] >= -0f)
chunk[x][y] = new Block();
}
}
You have multiple problems with your code. For instance:
for (int x = 0; x < chunk.length - 1; x++)
That should be:
for (int x = 0; x < chunk.length; x++)
Also, consider the following:
g.drawRect(positions[x][0], positions[0][y], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT);
That will not use all values in "positions[x][y]".... I think that what you will want is for the array to be 3d... for instance:
float[][][] positions = new float[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS][2];
That way: posistions[x][y][0] is the value for x, and positions[x][y][1] is the value for y....
g.drawRect(positions[x][y][0], positions[x][y][1], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT);
I am not sure I understand your code exactly, but it does seem to have issues.
I am implementing a median filter in Java using a Bitmap image. I am using an algorithm implemented in another programming language and then converting it to Java for my implementation. The part of the algorithm that I do not understand is below:
for(int x = 0; x < w; x++)
for(int y = 0; y < h; y++)
{
int n = 0;
//set the color values in the arrays
for(int filterX = 0; filterX < filterWidth; filterX++)
for(int filterY = 0; filterY < filterHeight; filterY++)
{
int imageX = (x - filterWidth / 2 + filterX + w) % w;
int imageY = (y - filterHeight / 2 + filterY + h) % h;
red[n] = image[imageX][imageY].r;
green[n] = image[imageX][imageY].g;
blue[n] = image[imageX][imageY].b;
n++;
}
Here is the link to the Median Algorithm
I implemented the following code myself but I dont think it is correct. I would appreciate if someone can help me please.
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
int n = 0;
for(int filterX = 0; filterX < filterWidth; filterX++)
for(int filterY = 0; filterY < filterHeight; filterY++)
{
pixel = image.getPixel(x,y);
A = (pixel>>24) & 0xFF;
R = (pixel>>16) & 0xFF;
G = (pixel>>8) & 0xFF;
B = pixel & 0xFF;
RArray[n] = R;
GArray[n] = G;
BArray[n] = B;
n++;
}
I don't see where you are selecting the median of the input values. As Jim mentioned, be sure to handle the edge cases, for example...
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
int index = 0;
for(int filterX = -filterWidth/2; filterX < filterWidth/2; filterX++)
for(int filterY = -filterHeight/2;
filterY < filterHeight/2; filterY++)
{
int pixelX = x+filterX;
int pixelY = y+filterY;
//ensure we're in bounds.
if(pixelX>-1 && pixelY>-1 && pixelX<w && pixelY<h){
pixel = image.getPixel(x,y);
A = (pixel>>24) & 0xFF;
R = (pixel>>16) & 0xFF;
G = (pixel>>8) & 0xFF;
B = pixel & 0xFF;
RArray[index] = R;
GArray[index] = G;
BArray[index] = B;
++index;
}
}
//only sort the pieces of the array we've put data into,
//remember we could be on an edge of the image.
Arrays.sort(RArray,0,index);
Arrays.sort(BArray,0,index);
Arrays.sort(GArray,0,index);
int medianR = RArray[RArray.length/2];
int medianB = BArray[BArray.length/2];
int medianG = GArray[GArray.length/2];
//last step is to combine medians back into a single integer
You need to analyze N pixels and find the middle value to get the median.
You need to use the changing filterX and filterY values as offsets to get other pixels than just x and y.
getPixel(x+filterX,y+filterY)
I am trying to take an array that contains 400 integers, and split it into a 20x20 2-D array. I thought I had the right algorithm, but the sum of the 1-D array does not match the sum of the 2-D array, so I’m obviously doing something wrong. Here is my code:
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
for (int z = 0; z < 400; z++)
{
array[x][y] = inArray[z];
}
}
}
}
What am I doing wrong?
For each pair of x and y, your code assigns every value from inArray to array[x][y]. This is clearly incorrect.
Here is one way to fix the code:
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0, z = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[z++];
}
}
}
You're assigning the last element in the input array to every element in the output array. The inner loop over the input array must be changed into a single assignment that picks the correct input element.
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[x * 20 + y]; // or devise your own calculation
}
}
}
The current approach will assign each element in the array with the last element in the original.
You should not iterate z but use it as a counter, in the same loops.
int z = 0;
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[z++];
}
}
You're basically assigning array[x][y] equal to inArray[0], then inArray[1], then inArray[2], all the way to [400].
I would do something like
private static void processArray(int[] inArray)
{
int[][] array = new int[20][20];
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < 20; y++)
{
array[x][y] = inArray[x * 20 + y];
}
}
}