I've got a two-dimensional array of doubles which is filtered values of an image. I want to convert this array back to a BufferedImage. How is it possible to cast an double[][] to a BufferedImage?
BufferedImage b = new BufferedImage(arr.length, arr[0].length, 3);
Graphics c = b.getGraphics();
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
for(int i=0; i< arr.length; i++){
for (int j=0; j<arr[0].length; j++){
c.drawString(String.valueOf(arr[i][j]), i, j);
writer.print(arr[i][j]+" \t");
}
writer.println();
}
ImageIO.write(b, "jpg", new File("CustomImage.jpg"));
System.out.println("end");
When I am plot the file-name.txt in matlab with imshow I can see my filtered image. However the CustomImage.jpg contains just one color. Any idea why?
THe result with c.drawString(String.valueOf(arr[i][j]), i, j):
c.drawString(String.valueOf(arr[i][j]), 0+(i*10), 0+(j*10)):
Matlab plor the double of arr first the double of arrays and second the initial gray scaled image:
Your Code
BufferedImage b = new BufferedImage(arr.length, arr[0].length, 3);
Graphics c = b.getGraphics();
for(int i = 0; i<arr.length; i++) {
for(int j = 0; j<arr[0].length; j++) {
c.drawString(String.valueOf(arr[i][j]), 0+(i*10), 0+(i*10));
}
}
ImageIO.write(b, "Doublearray", new File("Doublearray.jpg"));
System.out.println("end");
After Refactoring
int xLenght = arr.length;
int yLength = arr[0].length;
BufferedImage b = new BufferedImage(xLenght, yLength, 3);
for(int x = 0; x < xLenght; x++) {
for(int y = 0; y < yLength; y++) {
int rgb = (int)arr[x][y]<<16 | (int)arr[x][y] << 8 | (int)arr[x][y]
b.setRGB(x, y, rgb);
}
}
ImageIO.write(b, "Doublearray", new File("Doublearray.jpg"));
System.out.println("end");
You need to set the BufferedImagelike this:
double[][] values;///your 2d double array
for(int y=0;y<values.length;y++){
for(int x=0;x<values[y].length;x++){
int Pixel=(int)values[x][y]<<16 | (int)values[x][y] << 8 | (int)values[x][y];
img.setRGB(x, y,Pixel);
}
}
Related
I want to swap two 2d array of floats.
float[][] bluePath ={
{-.025f,-.303f},
{-.025f,.05f},
{-0.38f,-.215f}
}
float[][] greenPath ={
{-.4f,-.23f},
{-.55f,.03f},
{-0.58f,-.55f}
}
After swapping I want following result:-
float[][] bluePath ={
{-.4f,-.23f},
{-.55f,.03f},
{-0.58f,-.55f}
}
float[][] greenPath ={
{-.025f,-.303f},
{-.025f,.05f},
{-0.38f,-.215f}
}
please help me regarding this.
Use a temporary vareiable and copy byte per byte.
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 2, j++ )
{
float fHelp = redPath[i][j];
redPath[i][j] = greenPath[i][j];
greenPath[i][j] = fHelp;
}
}
You can use this code:
float temp;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
temp = bluePath[i][j];
bluePath[i][j] = greenPath[i][j];
greenPath[i][j] = temp;
}
}
I just need to insert rows and columns of zeros in a matrix
tried to get exmples from web searches
ArrayList<double [][]> al = new ArrayList<>();
for (int i1= 0; i1<=i; i1++)
al.add(k);
System.out.println(al.size());
double[][] firstMatrix = al.get(i);
double [][] matrix1 = al.get(i);
double [][] matrix2 = al.get(i);
Matrix Matrix1 = new Matrix (matrix1);
Matrix1.show();
System.out.println();
Matrix1.insertColumn(0,0.0, 0.0, 0.0, 0.0);
Matrix1.show();
System.out.println();
Your question boils down to adding a new column/row to a Java array. This is not possible per se, because your array has an exact memory area allocated to it and you would need a larger memory area for the resulting array, so, the solution is to create a new array and solve the problem that way:
Adding a row
double[][] addRow(double[][] input, int where) {
double[][] output = new double[input.length + 1][input[0].length];
for (int rIndex = 0; rIndex < output.length; rIndex++) {
for (int cIndex = 0; cIndex < output[0].length; cIndex++) {
if (rIndex < where) output[rIndex][cIndex] = input[rIndex][cIndex];
else if (rIndex == where) output[rIndex][cIndex] = 0;
else output[rIndex][cIndex] = input[rIndex - 1][cIndex];
}
}
return output;
}
Adding a column
double[][] addColumn(double [][]input, int where) {
double[][] output = new double[input.length][input[0].length + 1];
for (int rIndex = 0; rIndex < output.length; rIndex++) {
for (int cIndex = 0; cIndex < output[0].length; cIndex++) {
if (cIndex < where) output[rIndex][cIndex] = input[rIndex][cIndex];
else if (cIndex == where) output[rIndex][cIndex] = 0;
else output[rIndex][cIndex] = input[rIndex][cIndex - 1];
}
}
return output;
}
and make sure you assign the result of these methods to whatever variable/member you have used as the data source.
So I've got a school project and we have to work with a couple classes our prof gave us and make our own to make an image organizer.
The first part consists of making a set of static methods to edit the images themselves as 2D arrays of Color arrays(ColorImage type).
The first first problem is making a tool to downscale an image by a factor of f(f sided square of pixels in the original becomes 1 pixel in the output), and mine works, but I think it shouldn't and I can't figure why it works, so any help is appreciated. Specifically I'm taking about the loop that averages the colours of each position in the buffer array(avgArr[][]) (line 16). I'm thinking: the value of reds blues and greens would just be overwritten for each iteration and avgColor would just get the vlaue of the last pixel it got the rgb values off of avgArr.
static ColorImage downscaleImg(ColorImage img, int f) {
ColorImage dsi = new ColorImage(img.getWidth()/f, img.getHeight()/f);
Color[][] avgArr = new Color[f][f];
int reds = 0;
int greens = 0;
int blues = 0;
for(int i = 0; i < dsi.getWidth(); i++) {
for(int j = 0; j < dsi.getHeight(); j++) {
for(int x = i*f, xc = 0; x < i*f + (f-1); x++, xc++){
for(int y = j*f, yc = 0; y < j*f + (f-1); y++, yc++) {
avgArr[xc][yc] = img.getColor(x, y);
}
}
for(int k = 0; k < f - 1; k++){
for(int w = 0; w < f - 1; w++) {
reds += avgArr[k][w].getR();
greens += avgArr[k][w].getG();
blues += avgArr[k][w].getB();
}
}
int count = f*f;
Color avgColor = new Color(reds/count, greens/count, blues/count);
dsi.setColor(i, j, avgColor);
reds = 0;
greens = 0;
blues = 0;
}
}
return dsi;
}
Thanks,
EDIT: Turns out, it was in fact just taking the colour of, the last position of avgArr that it looked at. Any suggestions to correct are welcome.
I think you can solve your problem by summing the reds/greens/blues and then dividing them by the total pixels at the end to find the average:
int reds = 0;
int greens = 0;
int blues = 0;
...
for(int k = 0; k < f - 1; k++){
for(int w = 0; w < f - 1; w++) {
reds += avgArr[k][w].getR(); // <-- note the +=
greens += avgArr[k][w].getG();
blues += avgArr[k][w].getB();
}
}
int count = (f-1)*(f-1);
Color avgColor = new Color(reds/count, greens/count, blues/count);
I want to Fill a 2D-Array with Javafx Labels in Which I can change the Text when I click it.
This is my actual Code but it's returning a NullPointer Exception.
Blockquote
`public static Label[][] initWelt() {
Label[][] welt = new Label[DIM1][DIM2];
for (int x = 1; x < welt.length - 1; x++) {
for (int y = 1; y < welt.length - 1; y++) {
if (Math.random() > 0.4) {
welt[x][y].setText("X");
}
else{
welt[x][y].setText(" ");
}
}
}
return welt;
}`
it's returning a NullPointer Exception.
The only thing the below code does is initialise a two-dimensional array, it doesn't populate the two-dimensional array, hence the NullPointerException occurs.
Label[][] welt = new Label[DIM1][DIM2];
Basically you can't call this:
welt[x][y].setText("X");
without populating the two-dimensional array with object references.
to overcome the problem first populate the two dimensional array, something like below:
Label[][] welt = new Label[DIM1][DIM2];
for(int i = 0; i < DIM1; i++){
for(int j = 0; j < DIM2; j++){
welt[i][j] = new Label();
}
}
then you can proceed with your current task at hand.
so now your code becomes like this:
public static Label[][] initWelt() {
Label[][] welt = new Label[DIM1][DIM2];
for(int i = 0; i < DIM1; i++){ //populate the array
for(int j = 0; j < DIM2; j++){
welt[i][j] = new Label();
}
}
for (int x = 0; x < DIM1; x++) {
for (int y = 0; y < DIM2; y++) {
if (Math.random() > 0.4) {
welt[x][y].setText("X");
}
else{
welt[x][y].setText(" ");
}
}
}
return welt;
}
Note - personally I think it would be better to refactor the current method and insert the code that populates the two-dimensional array in a different method.
I have written a series of matrix operations where I take a 2 dimensional float array, treat it as a matrix, and perform matrix operations on it to acquire an inverse. My problem has been that although the array I am using with the class methods isn't part of the class, every time I run the method with the array as the parameter, the array itself also becomes modified.
First I will describe how I got the inverse of my matrix and then I will show the output.
The steps to taking the inverse of a matrix are as follows:
Get the cofactor matrix (i.e. create a matrix of matrix minors of the original matrix and then negate every other entry. If C = Cofactor Matrix, M = Matrix of Minors, i is the current row, and j is the current column, then C[ i ][ j ] = M[ i ][ j ]*( -1 )^( i + j )
Convert the cofactor matrix to the adjugate (also known as adjoint) matrix by transposing (replacing row, column entry by its analogous column, row entry and vice versa) the cofactor matrix. If C = Cofactor Matrix, A = Adjugate Matrix, i is the current row, and j is the current column, then A[ i ][ j ] = C[ j ][ i ]
Finally, take one over determinant of the original matrix and multiply the adjugate matrix by that value. If I = Inverse Matrix, A = Adjugate Matrix and D = Determinant, then I = (1/D)*A
In order to test if you have truly acquired the Matrix Inverse of a Matrix, one can multiply the original matrix by its inverse to get the identity matrix.
If I = Inverse, O = Original Matrix, and id = Identity Matrix then O*I = id
Now I will present the code where I implement these operations. For the sake of conciseness, I will not describe how to get the Matrix of Minors or the Determinant, but the problem I have been encountering will become apparent anyways.
public class MatrixOperations {
//Note: this method works fine. There are no problems.
public float determinant(float [][] a)
{
float [][] temp_mat;
float res = 0;
//assuming a square matrix
/*If it's a 2X2, then use the formula for a determinant of
2X2 matrices.*/
if(a.length == 2)
{
return a[0][0]*a[1][1]-a[0][1]*a[1][0];
}
/*Otherwise do the determinant formula recursively until your
determinant is made up of 2X2 matrix determinants and scalar products*/
else
{
temp_mat = new float[a.length-1][a.length-1];
int placej = 0;
int placei = 0;
for(int k = 0; k<a.length;k++)
{
for(int j = 0; j<a.length; j++)
{
for(int i = 1; i < a.length; i++)
{
placei = i-1;
if(j != k)
{
if(j < k)
{
temp_mat[placei][j] = a[i][j];
}
else if(j > k)
{
if (i == 1){
placej = j-1;
}
temp_mat[placei][placej] = a[i][j];
}
}
}
}
res+=a[0][k]*determinant(temp_mat)*(int)Math.pow(-1, k);
}
return res;
}
}
//Note: this method also works fine
//Scalar product method
public float[][] mul(float[][] m, float r)
{
float[][] res = new float[m.length][m.length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
res[i][j]= m[i][j]*r;
}
}
return res;
}
//Note: This method also works fine
public float[][] mul(float[][] m,float[][] n)
{
float[][] res = new float[m.length][m.length];
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
for(int k = 0; k < m.length; k++)
{
res[i][j] += m[i][k]*m[k][i];
}
}
}
return res;
}
//The method for creating a matrix of minors
//Here I start having problems
public float[][] minor(float [][] m)
{
float [][] minor_mat = new float [m.length][m.length];
//If the matrix is greater than a 2X2, use this to generate a matrix of minors
if(m.length > 2)
{
float [][] current_minor = new float [m.length-1][m.length-1];
int placei = 0;
int placej = 0;
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
for(int k = 0; k < m.length; k++)
{
for(int l = 0; l < m.length; l++)
{
if(i != k && j != l)
{
if(k<i)
placei = k;
else if(k>i)
placei = k-1;
if(l<j)
placej = l;
else if(l>j)
placej = l-1;
current_minor[placei][placej] = m[k][l];
}
}
}
minor_mat[i][j] = this.determinant(current_minor);
}
}
}
//otherwise use the definition for 2X2 matrix of minors
else
{
//even though minor_mat is using m.clone() rather than m, when I return the result, m has still been modified for some reason.
minor_mat = m.clone()
float temp;
temp = minor_mat[0][0];
minor_mat[0][0] = minor_mat[1][1];
minor_mat[1][1] = temp;
temp = minor_mat[0][1];
minor_mat[0][1] = minor_mat[1][0];
minor_mat[1][0] = temp;
}
return minor_mat;
}
//the same problem occurs here as it did in the minor method
//m appears to get modified even though I only use m.clone()
public float[][] cofactor(float [][] m)
{
float[][] res = m.clone();
res = this.minor(res)
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
res[i][j] = res[i][j]*(int)Math.pow(-1, i + j);
}
}
return res;
}
//The following transpose, adjugate, and inverse methods have the same problem
public float[][] transpose(float[][] m)
{
float[][] res = new float[m.length][m.length];
float temp = 0;
for(int i = 0; i < m.length; i++)
{
for(int j = 0; j < m.length; j++)
{
temp = m[i][j];
res[i][j] = m[j][i];
res[j][i] = temp;
}
}
return res;
}
public float[][] adjugate(float[][] m)
{
float[][] res = this.transpose(this.cofactor(m));
return res;
}
public float[][] inverse(float[][] m)
{
float[][] res = this.mul(this.adjugate(m), (1/this.determinant(m)));
return res;
}
//print out the matrix in square form
public void matrixprint(float [][] m)
{
for(int i = 0; i < m.length; i++)
{
System.out.println("");
for(int j = 0; j < m[i].length; j++){
System.out.print(m[i][j] + " ");
}
}
System.out.println("\n");
}
}
Now the main class and the main method that creates an instance of the MatrixOperations class and uses its methods on a 2X2 matrix.
public class Main {
public static void main(String[] args) {
MatrixOperations mo = new MatrixOperations();
//Create a 2X2 matrix called "matrix" and set its elements
//Then perform each step on "matrix" and finally test if you have acquired the correct inverse
float [][] matrix = new float[2][2];
matrix[0][0] = 2;
matrix [0][1] = 5;
matrix [1][0] = 4;
matrix [1][1] = 3;
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Minor = ");
mo.matrixprint(mo.minor(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Cofactor = ");
mo.matrixprint(mo.cofactor(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Adjugate = ");
mo.matrixprint(mo.adjugate(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Determinant = ");
System.out.println(mo.determinant(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Inverse = ");
mo.matrixprint(mo.inverse(matrix));
System.out.println("Matrix = ");
mo.matrixprint(matrix);
System.out.println("Identity = ");
mo.matrixprint(mo.mul(mo.inverse(matrix), matrix));
}
}
Now you will see that when I show the output, every time I use a method on "matrix", and reprint "matrix", "matrix" itself has been modified even though my methods only use a copy of "matrix" and not "matrix" itself.
Output:
Matrix =
2.0 5.0
4.0 3.0
Minor =
3.0 4.0
5.0 2.0
Matrix =
3.0 4.0
5.0 2.0
Cofactor =
3.0 -4.0
-5.0 2.0
Matrix =
3.0 -4.0
-5.0 2.0
Adjugate =
3.0 5.0
4.0 2.0
Matrix =
3.0 4.0
5.0 2.0
Determinant =
-14.0
Matrix =
3.0 4.0
5.0 2.0
Inverse =
-0.21428573 0.35714287
0.2857143 -0.14285715
Matrix =
3.0 -4.0
-5.0 2.0
Identity =
0.1479592 0.1479592
0.12244898 0.12244898
Any help/explanation as to why this happens would be appreciated.
This line does a shallow clone;
float[][] res = m.clone();
This copies the res which is array fo references to arrays. but not any of the arrays res points to. Most likely what you wanted is
float[][] res = new float[m.length][];
for (int i = 0; i < m.length; i++)
res[i] = m[i].clone();
It's because you are passing reference of matrix object in the methods of MatrixOperations class. It's not a copy of matrix object.
From Java doc:
Reference data type parameters, such as objects, are also passed into
methods by value. This means that when the method returns, the
passed-in reference still references the same object as before.
A two-dimensional array is just an array of arrays.
clone() on an array just does a shallow clone.
So you have a new cloned outer array, but it references the same entries (the inner arrays).
After cloning the outer array, iterate over the outer array and clone all inner arrays to get a deep clone.