Repeating an image vertically and horizontally in java - java

I need to try and duplicate and image, created by array(a), both vertically and horizontally to make a square full of the repeating image. So starting with 1 square image and I want to duplicate it 2 times horizontally and 2 times vertically it will create another image with 4 of the initial image, 2x2.
public static int[][] replicate(int[][] a) {
int[][] replicated = new int[a.length * 2][a[0].length * 2];
for (int r = 0; r < 2; r++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
replicated[i][j + r * a[i].length] = a[i][j];
}
}
}
for (int r = 0; r < 2; r++) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
replicated[i + r * a.length][j] = a[i][j];
}
}
}
return replicated;
}
This gets them across the horizontal for 1 line, and vertical for 1 line but does not fully fill in. So if I ran this I would be missing the bottom right of the 4 images. I'm having trouble putting them together.

public static int[][] replicate(int[][] image) {
int[][] replicated = new int[a.length * 2][];
for (int y=0; y<image.length; y++) {
int[] column = image[y];
replicated[y] = new int[column.length * 2];
replicated[y + image.length] = new int[column.length * 2];
System.arraycopy(column, 0, replicated[y], 0, column.length);
System.arraycopy(column, 0, replicated[y], column.length, column.length);
System.arraycopy(column, 0, replicated[y + image.length], 0, column.length);
System.arraycopy(column, 0, replicated[y + image.length], column.length, column.length);
}
return replicated;
}
There might be some problems with this code since I wrote it not in front of a compiler, but I think it gets the point across.
Also, if this is for painting the image, you should consider using a TexturePaint instead.

Related

Java implement cross correlation 2d image

enter image description here
Can i implement the cross-correlation in the same way as the convolution?
I want to implement the formula in as in the picture, where Li the kernel in 4 different direction filters; Ci is the magnitude map for direction i. So what I did is to find the cross-correlation in the four directions separately and add them up. I learned that the cross correlation can be the same as convolution in image line sharping; s as the result should be stroke line of an image but what I actually get are discrete points. I am not sure if I implemented the formula correctly. Please help
private static void sharpTheLine(){
int[][] cC_0= crossCorrelation(KERNEL_0,CMap_0);
int[][] cC_45=crossCorrelation(KERNEL_45,CMap_45);
int[][] cC_90=crossCorrelation(KERNEL_90,CMap_90);
int[][] cC_135=crossCorrelation(KERNEL_135,CMap_135);
//generate S
for(int i=0; i<imageWidth; i++){
for(int j =0; j<imageHight; j++) {
SMap[i][j] = cC_0[i][j]+cC_45[i][j]+cC_90[i][j]+cC_135[i][j];
}
}
}
private static int[][] crossCorrelation(int [][] kernel,int[][] CMapVal){
int horizontalWalk = imageWidth - K_R;
int verticalWalk = imageHight - K_C;
int res[][]=new int[imageWidth][imageHight];
for (int i = 0; i < horizontalWalk; i++) {
for (int j = 0; j < verticalWalk; j++) {
int sample[][] = new int[K_R][K_C];
for (int k = i; k < K_R + i; k++) {
for (int m = j; m < K_C + j; m++) {
sample[k - i][m - j] = CMapVal[k][m];
OnePixelConvolution(sample, i, j, kernel, res);
}
}
}
}
return res;
}
private static void OnePixelConvolution(int[][] sample, int x, int y, int [][]kernel, int [][] res) {
int resrgb = 0;
for (int i = 0; i < K_R; i++) {
for (int j = 0; j < K_C; j++) {
resrgb = resrgb + sample[i][j] * kernel[i][j];
}
}
res[x][y] = resrgb;
}

Create a 2d array from an ArrayList<Integer>

I have an ArrayList and I want to create a method that will turn it into a 2d array, int[][].
This new 2d array will represent a matrix and it has to be square, so for example if I use [8, 2, 3, 0] the ressult will be {8,2}
{3,0}
public static int[][] convertIntegers(ArrayList<Integer> integers){
int m = (int) Math.sqrt(integers.size());
int[][] ret = new int[m][m];
int cont = 0;
for(int i=0; i<m+1 ; i++)
{
for(int j=0; j<m; j++)
{
cont = cont + 1;
ret[i][j] = integers.get(cont);
;
}
}
return ret;}
Your implementation is almost ok, except for some off-by-one errors:
You need to increment cont after the integers.get call, not before. If you increment before, then the first element of the list will be skipped. An easy way to fix that is to move the incrementing inside the inner loop, counting it together with j.
The outer loop should go until i < m instead of i < m + 1
With the errors fixed:
for (int i = 0, cont = 0; i < m; i++) {
for (int j = 0; j < m; j++, cont++) {
ret[i][j] = integers.get(cont);
}
}
Btw, another way is without using cont at all,
calculating the correct position using i, j and m:
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
ret[i][j] = integers.get(i * m + j);
}
}

Horizontal Transformation of Array (2D)

It's a simple matter of flipping an image horizontally and/or vertically. The premise is that given a 2D integer array that was created from importing a picture, I must create a method with a int[][] param and horizontally flip it before returning void.
The syntax is below:
public static void horizontalFlip(int[][] imgArray)
{
int temp;
for (int i = 0; i < imgArray.length; i++)
{
for (int j = 0; j < imgArray[i].length / 2; j++)
{
temp = imgArray[i][j];
imgArray[i][j] = imgArray[imgArray.length - 1 - i][j];
imgArray[imgArray.length - 1 - i][j] = temp;
}
}
}
I use imgArray as the array param and use temp as a placeholder while the loop swaps pixels, or rather, that was the intention. Currently the window does nothing after prompting the flip. Can somebody help me find the problem with the logic or syntax?
Thanks in advance, please specify any details I should provide
P.S. I can confirm the unreferenced supplied code is functional and tested.
It is happening because you are using i instead of j. But i will not stop after halfway, but it is continued and re-swap the array.
Here is a correct code :
for (int i = 0; i < imgArray.length; i++) {
for (int j = 0; j < imgArray[i].length / 2; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[i][imgArray.length - 1 - j];
imgArray[i][imgArray.length - 1 -j] = temp;
}
}
Or if you want to swap columns, not rows :
for (int i = 0; i < imgArray.length / 2; i++) {
for (int j = 0; j < imgArray[i].length; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[imgArray.length - 1 - i][j];
imgArray[imgArray.length - 1 -i][j] = temp;
}
}
This will correctly flip the image horizontally:
public static void horizontalFlip(int[][] imgArray)
{
int temp;
for (int i = 0; i < imgArray.length; i++) {
for (int j = 0; j < imgArray[i].length/2; j++) {
temp = imgArray[i][j];
imgArray[i][j] = imgArray[i][imgArray[i].length - 1 - j];
imgArray[i][imgArray[i].length - 1 - j] = temp;
}
}
}
Please see my solution below,
for(int i=0; i<matrix.length / 2; i++)
{
int[] row = matrix[i];
int[] temp = row;
matrix[i] = matrix[matrix.length - 1];
matrix[matrix.length - 1] = row;
}

Matrix -> blocks division

I want to load some matrix into my program and then I want to divide it into smaller blocks.
What I want exactly can be seen on an image below:
http://postimg.org/image/aki19hjx9/ba463111/
In red squares are 3 examples of my "blocks" in which I would like to divide whole matrix. In this case each block should be (smaller) 3x3 matrix. I know how to load it into 2d array, but what should I do then?
int[][] bigMatrix = new int[9][9];
// initialize bigMatrix
int[][][] smallMatrices = new int[3][3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
smallMatrices[i][j][k] = bigMatrix[3*i+j][3*i+k];
}
}
}
// The submatrices are now in smallMatrices[i], 0 <= i < 3

Java AWT graphics for loop grid

I need some help with creating a grid in java AWT. You get a certain number of columns, for now let's say 13.
for(int i = 0; i < 13; i++)
{
Graphics.fillRect((i * 15), 10, 10, 10);
}
Graphics takes in x, y, width, height.
This results in;
XXXXXXXXXXXXX
This will put all cubes next to each other. This is not what I want. I need the y position to go down by 15 pixels every time 4 cubes have been drawn.
The result would be
XXXX
XXXX
XXXX
X
Thanks for your time!
Basically, you have to increment the x, and every time you reach the limit, you bring the x back to zero, and increase the y:
// These should be constants, defined at the class level
public static final int NUM_RECTANGLES = 13;
public static final int NUM_COLUMNS = 4;
// And your loop
int col = 0;
int row = 0;
for(int i = 0; i < NUM_RECTANGLES; i++)
{
Graphics.fillRect((col * 15), 10 + (row * 15), 10, 10);
col++;
if( col == NUM_COLUMNS ) {
col = 0;
row++;
}
}
As you see, after each rectangle, you increase the column. If you get to the limit for the column (remember you started from 0), you move to the next line. This means the column is again 0, and the row is increased.
Of course, if your 13 and 4 are actually parameters, ignore my remark about constants.
Just use a double for() loop
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++) {
Graphics.fillRect(i * 15, j * 15, 10, 10);
}
}
This will create a 4x4 grid of rectangles. If you want to limit it to a 4x4-like grid that only has 1 rectangle in the last row, which it seems like you want, use j < ((i == 3) ? 1 : 4) as your termination condition in the second for loop, or use a simple if condition like so:
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++) {
Graphics.fillRect(i * 15, j * 15, 10, 10);
if(i == 3) break;
}
}
edit: Generic Solution
int nRow, nCol, nLastCol;
//Initialize values
for(int i = 0; i < cRow; i++){
for(int j = 0; j < ((i == nRow - 1) ? nLastCol : nCol); j++) {
Graphics.fillRect(i * 15, j * 15, 10, 10);
}
}
You need to set how many columns and keep track of which column you're drawing so that you can increment the row. On a side note, I also wouldn't put a magic number in the loop, instead make that a variable (just good programming practice). Something like this:
int numRectangles = 13;
int numColumns = 4;//set this to however many columns you need
int currentRow = 1;//start at one, not zero
int tempRow = 1;
for(int i = 0; i < numRectangles; i++)
{
Graphics.fillRect((i * 15), (tempRow * 10), 10, 10);
currentRow++;
if(currentRow > numColumns) {
currentRow = 1;
tempRow++;
}
}

Categories