Gabor Image Processing in Java without MathLab - java

I'm looking for some help in applying 2D Gabor Wavelets Formula to an image in java.
This is the formula that I'm using.
Gabor Formula
I need my output to look like this
I've read the image in and its currently stored in a 2D array. My code is as follows:` public void RunGabor() throws IOException
{
double[][]pixels=getImage();
int H= pixels.length;
int W =pixels[0].length;
size=H*W;
gaussian=size/2;
System.out.println(gaussian);
GaborGrid = new int[H][W];
GaborNorm = new int[H][W];
double X=0,Y=0, gx=-gaussian,gy=-gaussian, count=0, total=0;
int ax=0, dy=0;
for(int x=0; x<pixels.length; x++)
{
for(int k=0;k< pixels[0].length; k++)
{
X=gx*Math.cos(theta)+gy*Math.sin(theta);
Y=-gx*Math.sin(theta)+gy*Math.cos(theta);
pixels[dy][ax]=((Math.exp(-(Math.pow(X, 2)+(Math.pow(Y, 2)*
Math.pow(upsi,2)))/(2*Math.pow(sigma, 2))))*
(Math.cos((kappa*X+varphi))));
System.out.println("Pixels" +pixels[dy][ax]);
total+=pixels[dy][ax];
count++;
System.out.println("Count" +count);
gx+=1;
ax++;
}
System.out.println("second loop");
ax=0;
dy++;
gy+=1;
gx=-gaussian;
}
mean=total/count;
System.out.println("Mean" +mean);
NormaliseImage(pixels);
}`
From there it calls a method normaliseImage
public void NormaliseImage(double[][] pixels)
{
double minII = pixels[0][0];
double maxII = pixels[0][0];
for(int y=0; y<pixels.length; y++){
for(int x= 0; x<pixels[0].length; x++){
if(pixels[y][x] <= minII){
minII =pixels[y][x];
}
if(pixels[y][x]>= maxII){
maxII=pixels[y][x];
}
}
My create image class looks like this
public CreateImage(int[][] data, String IMGname)
{
name = IMGname;
width = data[0].length;
height = data.length;
System.out.println("NEW IMAGE 1");
pixels = new int[height*width];
int count=0;
for(int i=0; i<data.length; i++)
{
for(int j=0; j<data[0].length; j++)
{
pixels[count] = (int)Math.abs(data[i][j]);
pixels[count] = convert2pixel(pixels[count]);
count++;
}
}
Create(width, height, pixels, name);
}
public int convert2pixel(int pixel)
{
return ((0xff<<24)|(pixel<<16)|(pixel<<8)|pixel);
}
public int convert2grey(double pixel)
{
int red=((int)pixel>>16) & 0xff;
int green = ((int)pixel>>8) & 0xff;
int blue = (int)pixel & 0xff;
return (int)(0.3*red+0.6*green+0.1*blue);
}
public void Create(int Width, int Height, int pixels[], String n)//throws Exception
{
//System.out.println("Inside Create Image");
MemoryImageSource MemImg = new MemoryImageSource(Width,Height,pixels,0,Width);
Image img2= Toolkit.getDefaultToolkit().createImage(MemImg);
BufferedImage bfi = new BufferedImage(Height,Width, BufferedImage.TYPE_INT_BGR);
Graphics2D g2D = bfi.createGraphics();
g2D.drawImage(img2, 0, 0, Width, Height, null);
try
{
ImageIO.write(bfi, "png", new File(n+".png"));
}
catch(Exception e){}
}
}
My output is just a grey screen, any help would be appreciated.
Update: Gabor Driver Class `
public class Gabor_Driver{
public static void main(String[]args)throws IOException
{
double lamda=75;
double theta=45;
double varphi=90;
double upsi=10;
double bandW=10;
//int size=500;
Gabor gabor = new Gabor(lamda, theta, varphi, upsi, bandW );
}
}
`
Gabor class :
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Gabor
{
CreateImage ci;
private double lamda=0;
private double theta=0;
private double varphi=0;
private double upsi=0;
private double bandW=0;
private double B=0;
private double sigma=0;
private double kappa=0;
private int[][] GaborGrid;
private int[][] GaborNorm;
int size=0;
double toRadians=180/Math.PI, min=500, max=-500, mean=0;;
int gaussian=0;
double rotation;
double GLFmean=0;
//Standard Gabor no quantization
public Gabor(double l, double t, double v, double u, double b) throws IOException
{
lamda=l;
theta=t/toRadians;
varphi=v/toRadians;
upsi=u;
bandW=b;
kappa=(2*Math.PI)/lamda;
Calculate_Sigma();
RunGabor();
}
public void RunGabor() throws IOException
{
double[][]pixels=getImage();
int H= pixels.length;
int W =pixels[0].length;
size=H*W;
gaussian=size/2;
System.out.println(gaussian);
GaborGrid = new int[H][W];
GaborNorm = new int[H][W];
double X=0,Y=0, gx=-gaussian,gy=-gaussian, count=0, total=0;
int ax=0, dy=0;
for(int x=0; x<pixels.length; x++)
{
for(int k=0;k< pixels[0].length; k++)
{
X=gx*Math.cos(theta)+gy*Math.sin(theta);
Y=-gx*Math.sin(theta)+gy*Math.cos(theta);
pixels[dy][ax]=((Math.exp(-(Math.pow(X, 2)+(Math.pow(Y, 2)*
Math.pow(upsi,2)))/(2*Math.pow(sigma, 2))))*
(Math.cos((kappa*X+varphi))));
System.out.println("Pixels" +pixels[dy][ax]);
total+=pixels[dy][ax];
count++;
System.out.println("Count" +count);
gx+=1;
ax++;
}
System.out.println("second loop");
ax=0;
dy++;
gy+=1;
gx=-gaussian;
}
mean=total/count;
System.out.println("Mean" +mean);
NormaliseImage(pixels);
}
public double[][] getImage() throws IOException{
int[][]pixels =null;
double[][] doubles = null;
JFileChooser fc = new JFileChooser();
int returnValue = fc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fc.getSelectedFile();
BufferedImage image = ImageIO.read(selectedFile);
System.out.println(selectedFile.getName());
int W =image.getWidth();
int H= image.getHeight();
int width = image.getWidth();
int height = image.getHeight();
pixels = new int[height][width];
for (int row = 0; row < height; row++) {
image.getRGB(0, row, width, 1, pixels[row], 0, width);
}
doubles = new double[pixels.length][pixels[0].length];
for(int i=0; i<pixels.length; i++) {
for(int j=0; j<pixels[0].length; j++)
doubles[i][j] = pixels[+i][+j];
}
}
return doubles;
}
public void NormaliseImage(double[][] pixels)
{
double minII = pixels[0][0];
double maxII = pixels[0][0];
for(int y=0; y<pixels.length; y++){
for(int x= 0; x<pixels[0].length; x++){
if(pixels[y][x] <= minII){
minII =pixels[y][x];
}
if(pixels[y][x]>= maxII){
maxII=pixels[y][x];
}
}
}
int count=0;
double total=0;
for(int y=0; y<pixels.length; y++){
for(int x= 0; x<pixels[0].length; x++){
total += pixels[y][x];
count++;
}
}
double average =(double)total/count;
for(int y=0; y<pixels.length; y++){
for(int x= 0; x<pixels[0].length; x++){
double normalise= ((((pixels[y][x]-min))/((max-min)))*255);
if(normalise<=average){
normalise =0;
}
GaborNorm[y][x] = (int) normalise;
}
}
ci = new CreateImage(GaborNorm, "Gabor");
}
private void Calculate_Sigma()
{
B=(1/Math.PI)*(0.588705011)*((Math.pow(2, bandW)+1)/(Math.pow(2, bandW)-1));
sigma=B*lamda;
}
}
Create image class:
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
public class CreateImage
{
int[] pixels;
int width=0, height=0;
String name;
public CreateImage(int[][] data, String IMGname)
{
name = IMGname;
width = data[0].length;
height = data.length;
System.out.println("NEW IMAGE 1");
pixels = new int[height*width];
int count=0;
for(int i=0; i<data.length; i++)
{
for(int j=0; j<data[0].length; j++)
{
pixels[count] = (int)Math.abs(data[i][j]);
pixels[count] = convert2pixel(pixels[count]);
count++;
}
}
Create(width, height, pixels, name);
}
public int convert2pixel(int pixel)
{
return ((0xff<<24)|(pixel<<16)|(pixel<<8)|pixel);
}
public int convert2grey(double pixel)
{
int red=((int)pixel>>16) & 0xff;
int green = ((int)pixel>>8) & 0xff;
int blue = (int)pixel & 0xff;
return (int)(0.3*red+0.6*green+0.1*blue);
}
public void Create(int Width, int Height, int pixels[], String n)//throws Exception
{
//System.out.println("Inside Create Image");
MemoryImageSource MemImg = new MemoryImageSource(Width,Height,pixels,0,Width);
Image img2= Toolkit.getDefaultToolkit().createImage(MemImg);
BufferedImage bfi = new BufferedImage(Height,Width, BufferedImage.TYPE_INT_BGR);
Graphics2D g2D = bfi.createGraphics();
g2D.drawImage(img2, 0, 0, Width, Height, null);
try
{
ImageIO.write(bfi, "png", new File(n+".png"));
}
catch(Exception e){}
}
}

Your calculations are mostly correct. You are calculating the function itself in RunGabor - I think gx, gy should be replaced with x, k. This
for(int x=0; x<pixels.length; x++)
{
for(int k=0;k< pixels[0].length; k++)
{
X=gx*Math.cos(theta)+gy*Math.sin(theta);
Y=-gx*Math.sin(theta)+gy*Math.cos(theta);
pixels[dy][ax]=((Math.exp(-(Math.pow(X, 2)+(Math.pow(Y, 2)*
Math.pow(upsi,2)))/(2*Math.pow(sigma, 2))))*
(Math.cos((kappa*X+varphi))));
should be replaced with
public Kernel getKernel() {
double sigma = calculateSigma(waveLength, bandwidth);
float[] data = new float[width*height];
for(int k = 0, x = -width/2; x <= width/2; x++) {
for(int y = -height/2; y <= height/2; y++) {
for(double orientation : orientations) {
double x1 = x*Math.cos(orientation) + y*Math.sin(orientation);
double y1 = -x*Math.sin(orientation) + y*Math.cos(orientation);
data[k] += (float)(gaborFunction(x1, y1, sigma, aspectRatio, waveLength, phaseOffset));
}
k++;
}
}
But you have to apply the function on the pixels that you are reading, the image. If you look at this other implementation https://github.com/clumsy/gabor-filter/blob/master/src/main/java/GaborFilter.java
you will find many parallels. See if you can fix it.
At least as a first step you should try to print the values of the gabor function either 3x3 or 5x5. If they look reasonable you can go ahead to apply it to the image.

Related

How to specify the Z direction(depth) for a cube

I would like to know how can I specify the z-direction for my cube. It needs to be translated to -5 along z-direction.
The whole problem statement is like this: Implement the transformation pipeline, by defining a cube in 3d space, setting up a camera in the origin of the coordinate system and a projection matrix that has a 90° FOV. The cube size should be 2 unites (2x2x2), laid down 5 unites along the negative z axis
package transformations;
import java.util.*;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
public class Transformations {
//declare list of polygons
static ArrayList<Integer>p_left=new ArrayList<Integer>();
static ArrayList<Integer>p_right=new ArrayList<Integer>();
static ArrayList<Integer>p_down=new ArrayList<Integer>();
static ArrayList<Integer>p_top=new ArrayList<Integer>();
static ArrayList<Integer>p_near=new ArrayList<Integer>();
static ArrayList<Integer>p_far=new ArrayList<Integer>();
static int count=0;
public static BufferedImage gradientSetRaster(BufferedImage img) {
int width = img.getWidth();
int height = img.getHeight();
WritableRaster raster = img.getRaster();
int[] pixel = new int[3]; //RGB
for (int y = 0; y < height; y++) {
int val = (int) (y * 255f / height);
for (int shift = 1; shift < 3; shift++) {
pixel[shift] = val;
}
for (int x = 0; x < width; x++) {
raster.setPixel(x, y, pixel);
}
}
return img;
}
public static BufferedImage drawLine(BufferedImage img, int x0, int y0, int xn, int yn)
{
WritableRaster raster=img.getRaster();
int[] pixel = new int[3]; //RGB
for(int k=0; k<3; k++)
{
pixel[k]=255;
}
int dx=Math.abs(xn-x0);
int dy=Math.abs(yn-y0);
int m=dy/dx;
int twoDy=2*dy;
int twoDyMintwoDx=twoDy-2*dx;
if(dx>dy) //slope is positive and is less than or equal to 1
{
int p=twoDy-dx;
for(int k=0; k<dx-1; k++)
{
if(p<0)
{
x0=x0+1;
raster.setPixel(x0, y0, pixel);
p=p+twoDy;
}
else
{
x0=x0+1;
y0=y0+1;
raster.setPixel(x0, y0, pixel);
p=p+twoDy-2*dx;
}
}
}
if(dy>dx)
{
int p=2*dx-dy;
int temp1=x0;
x0=y0;
y0=temp1;
int temp2=xn;
xn=yn;
yn=temp2;
for(int k=0; k<dx-1; k++)
{
x0=x0+1;
y0=y0+1;
raster.setPixel(x0, y0, pixel);
}
}
if(dy==dx)
{
x0=x0+1;
y0=y0+1;
raster.setPixel(x0, y0, pixel);
}
return img;
}
public static BufferedImage drawUpPolygon(Graphics g, BufferedImage img, ArrayList<Integer>p_top)
{
g.drawLine(p_top.get(0), p_top.get(1), p_top.get(2), p_top.get(3));
g.drawLine(p_top.get(0), p_top.get(1), p_top.get(4), p_top.get(5));
g.drawLine(p_top.get(4), p_top.get(5), p_top.get(6), p_top.get(7));
g.drawLine(p_top.get(6), p_top.get(7), p_top.get(2), p_top.get(3));
return img;
}
public static BufferedImage drawDownPolygon(Graphics g, BufferedImage img, ArrayList<Integer>p_top)
{
g.drawLine(p_down.get(0), p_down.get(1), p_down.get(2), p_down.get(3));
g.drawLine(p_down.get(0), p_down.get(1), p_down.get(4), p_down.get(5));
g.drawLine(p_down.get(4), p_down.get(5), p_down.get(6), p_down.get(7));
g.drawLine(p_down.get(6), p_down.get(7), p_down.get(2), p_down.get(3));
return img;
}
public static BufferedImage drawBetween(Graphics g, BufferedImage img, ArrayList<Integer>p_top, ArrayList<Integer>p_down)
{
g.drawLine(p_top.get(0), p_top.get(1), p_down.get(0), p_down.get(1));
g.drawLine(p_top.get(2), p_top.get(3), p_down.get(2), p_down.get(3));
g.drawLine(p_top.get(4), p_top.get(5), p_down.get(4), p_down.get(5));
g.drawLine(p_top.get(6), p_top.get(7), p_down.get(6), p_down.get(7));
return img;
}
public static void main(String... args) {
Frame w = new Frame("Raster"); //window
final int imageWidth = 500;
final int imageHeight = 500;
w.setSize(imageWidth,imageHeight);
w.setLocation(100,100);
w.setVisible(true);
Graphics g = w.getGraphics();
BufferedImage img = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
gradientSetRaster(img);
g.drawImage(img, 0, 0, (img1, infoflags, x, y, width, height) -> false); //draw the image. You can think of this as the display method.
w.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//order will be(vertex->index)
//lowerLeft->0,1
//upperLeft->2,3
//lowerRight->4,5
//upperRight->6,7
count++;
int t_lowLeftX=e.getX();
int t_lowLeftY=e.getY();
//top polygon
int t_lowRightX;
int t_lowRightY;
int t_uppLeftX;
int t_uppLeftY;
int t_uppRightX;
int t_uppRightY;
//bottom polygon
int d_lowLeftX;
int d_lowLeftY;
int d_lowRightX;
int d_lowRightY;
int d_uppLeftX;
int d_uppLeftY;
int d_uppRightX;
int d_uppRightY;
if(count==1) {
//lowerLeft indexes
t_lowLeftX=e.getX();
t_lowLeftY=e.getY();
t_lowRightX=t_lowLeftX+100;
t_lowRightY=t_lowLeftY;
t_uppLeftX=t_lowLeftX+50;
t_uppLeftY=t_lowLeftY+50;
t_uppRightX=t_uppLeftX+100;
t_uppRightY=t_uppLeftY;
d_lowLeftX=t_lowLeftX;
d_lowLeftY=t_lowLeftY-70;
d_lowRightX=t_lowRightX;
d_lowRightY=t_lowRightY-70;
d_uppLeftX=t_uppLeftX;
d_uppLeftY=t_uppLeftY-70;
d_uppRightX=t_uppRightX;
d_uppRightY=t_uppRightY-70;
//connect(lowLeft->uppLeft, lowLeft->lowRight, uppLeft->uppRight,
//uppRight->lowRight ...)
p_top.add(t_lowLeftX);
p_top.add(t_lowLeftY);
p_top.add(t_lowRightX);
p_top.add(t_lowRightY);
p_top.add(t_uppLeftX);
p_top.add(t_uppLeftY);
p_top.add(t_uppRightX);
p_top.add(t_uppRightY);
p_down.add(d_lowLeftX);
p_down.add(d_lowLeftY);
p_down.add(d_lowRightX);
p_down.add(d_lowRightY);
p_down.add(d_uppLeftX);
p_down.add(d_uppLeftY);
p_down.add(d_uppRightX);
p_down.add(d_uppRightY);
// g.drawLine(lowLeftX, lowLeftY, uppLeftX, uppLeftY);
// g.drawLine(lowLeftX, lowLeftY, lowRightX, lowRightY);
// g.drawLine(uppLeftX, uppLeftY, uppRightX, uppRightY);
// g.drawLine(uppRightX, uppRightY, lowRightX, lowRightY);
drawUpPolygon(g, img, p_top);
drawDownPolygon(g, img, p_down);
drawBetween(g, img, p_top, p_down);
int[] pixel = {255,255,255};
img.getRaster().setPixel(t_lowLeftX,t_lowLeftY,pixel);
img.getRaster().setPixel(t_uppLeftX, t_uppLeftY,pixel);
img.getRaster().setPixel(t_lowLeftX, t_lowLeftY, pixel);
img.getRaster().setPixel(t_lowRightX, t_lowRightY, pixel);
img.getRaster().setPixel(t_uppRightX, t_uppRightY, pixel);
img.getRaster().setPixel(d_lowLeftX,d_lowLeftY,pixel);
img.getRaster().setPixel(d_uppLeftX, d_uppLeftY,pixel);
img.getRaster().setPixel(d_lowLeftX, d_lowLeftY, pixel);
img.getRaster().setPixel(d_lowRightX, d_lowRightY, pixel);
img.getRaster().setPixel(d_uppRightX, d_uppRightY, pixel);
};
//top polygon's lower left vertex
}
});
w.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
w.dispose();
g.dispose();
System.exit(0);
}
});
}
}

Java - counting the occurrences of a character in a random generator method using a separate method

I'm trying to figure out how to count the number of stars that were printed last time the print() method was used.
I'm confused on how to take the value of starsInLastPrint variable into the starsInLastPrint() method. My understanding is that this isn't possible. I assume there are plenty of things wrong with my current code that isn't helping. Below is my current state as I am stuck.
import java.util.Random;
public class NightSky {
private double density;
private int width;
private int height;
private int starsInLastPrint;
public NightSky(double density) {
width = 20;
height = 10;
this.density = density;
}
public NightSky(int width, int height) {
density = 0.1;
this.width = width;
this.height = height;
}
public NightSky(double density, int width, int height) {
this.density = density;
this.width = width;
this.height = height;
}
public void printLine() {
Random starPlacement = new Random();
String[] stars = new String[(this.width)];
for (int i = 0; i < this.width; i++) {
double random = starPlacement.nextDouble();
if (random <= this.density) {
stars[i] = "*";
this.starsInLastPrint++;
} else {
stars[i] = " ";
}
}
int j = 0;
while (j < stars.length) {
System.out.print(stars[j]);
j++;
}
System.out.println("");
}
public void print() {
NightSky nightSky = new NightSky(this.density, this.width, this.height);
this.starsInLastPrint = 0;
int i = 0;
while (i < this.height) {
nightSky.printLine();
i++;
}
}
public int starsInLastPrint() {
return this.starsInLastPrint;
}
}
You are on the right track. Though, you don't need to instantiate another NightSky object inside the print method. You can just do the following,
public void print() {
this.starsInLastPrint = 0;
int i=0;
while (i < this.height) {
printLine();
i++;
}
}
So, everytime you call print, it will update the stars count for that print method call. Here is the whole code,
import java.util.Random;
public class NightSky {
private double density;
private int width;
private int height;
private int starsInLastPrint;
public static void main(String[] args){
NightSky sky = new NightSky(5,5);
sky.print();
System.out.println(sky.starsInLastPrint());
sky.print();
System.out.println(sky.starsInLastPrint());
}
public NightSky(double density) {
width = 20;
height = 10;
this.density = density;
}
public NightSky(int width, int height) {
density = 0.1;
this.width = width;
this.height = height;
}
public NightSky(double density, int width, int height) {
this.density = density;
this.width = width;
this.height = height;
}
public void printLine() {
Random starPlacement = new Random();
String[] stars = new String[(this.width)];
for (int i = 0; i < this.width; i++) {
double random = starPlacement.nextDouble();
if (random <= this.density) {
stars[i] = "*";
this.starsInLastPrint++;
} else {
stars[i] = " ";
}
}
int j = 0;
while (j < stars.length) {
System.out.print(stars[j]);
j++;
}
System.out.println("");
}
public void print() {
this.starsInLastPrint = 0;
int i=0;
while (i < this.height) {
printLine();
i++;
}
}
public int starsInLastPrint() {
return this.starsInLastPrint;
}
}
Sample run of the above code:
* *
*
*
4
0

Transformation of photo to photo in the form of matrix

I wrote the following code, but the photographs must be in the form of a matrix, not side by side.
My goal is not to leave any empty space at the end of the canvas.
import java.awt.Color;
public class aaa {
public static Color karstr ( Color x,Color y,double lambda ){
int r= (int)((1-lambda)*x.getRed()+lambda*y.getRed());
int g= (int)((1-lambda)*x.getGreen()+lambda*y.getGreen());
int b= (int)((1-lambda)*x.getBlue()+lambda*y.getBlue());
return new Color (r,g,b);
}
public static void main(String[] args) {
int genilik =50;
int ykseklik=100;
Picture p=new Picture("c:/data/a.jpg");
Picture q=new Picture("c:/data/b.jpg");
Picture r= new Picture(p.width()+400,p.height()+10);
for (int i = 0; i < p.width(); i++)
for (int j = 0; j < p.height(); j++) {
Color x=p.get(i, j);
Color y=q.get(i,j);
r.set(i*genilik/p.width(),j*ykseklik/p.height(), x);
Color c=karstr(x,y,(double)1/5);
r.set(i*genilik/p.width()+50,j*ykseklik/p.height(), c);
Color a=karstr(x,y,(double)1/4);
r.set(i*genilik/p.width()+100,j*ykseklik/p.height(),a);
Color b=karstr(x,y,(double)1/3);
r.set(i*genilik/p.width()+150,j*ykseklik/p.height(),b);
Color f=karstr(x,y,(double)1/2);
r.set(i*genilik/p.width()+200,j*ykseklik/p.height(),f);
Color g=karstr(x,y,(double)1/1.2);
r.set(i*genilik/p.width()+250,j*ykseklik/p.height(),g);
r.set(i*genilik/p.width()+300,j*ykseklik/p.height(), y);
}
r.show();
}
}
import java.awt.Color;
public class PhotoTransformation {
private Picture source;
private Picture dest;
public PhotoTransformation(Picture source, Picture dest) {
source = source;
dest = dest;
}
public static void main(String[] args) {
Picture p=new Picture("c:/data/a.jpg");
Picture q=new Picture("c:/data/b.jpg");
PhotoTransformation photoTransformation = new PhotoTransformation(p, q);
Picture[] photoSeries = photoTransformation.produceTransformationPhotos(8);
// Show the photos
}
public Picture[] produceTransformationPhotos(int transitionPhotoNumber) {
int photoNumber = transitionPhotoNumber + 2;
Picture[] photos = new Picture[photoNumber];
int width = source.width();
int height = source.height();
for (int p = 0; p < photoNumber; p++) {
Picture newPhoto = new Picture(width, height);
photos[p] = newPhoto;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Color sourceColor = source.get(i, j);
Color destColor = dest.get(i, j);
double weight = (double) p / (double) (photoNumber - 1);
Color transformationColor = produceWeightedMeanColor(sourceColor, destColor, weight);
newPhoto.set(i, j, transformationColor);
}
}
}
return photos;
}
private static Color produceWeightedMeanColor(Color x, Color y, double weight) {
int r = (int)((1-weight) * x.getRed() + weight * y.getRed());
int g = (int)((1-weight) * x.getGreen() + weight * y.getGreen());
int b = (int)((1-weight) * x.getBlue() + weight * y.getBlue());
return new Color(r,g,b);
}
}

JPanel size not changing

Hi guys i'm trying to create a draughts game in Java and am using JPanels to represent the squares, if I were to change the size of the panels how would I do so ? if I use a layout manager the squares are not big enough. At the moment i'm not using a layout manager to try and change the size, but the size doesnt seem to change - just stays at 1,1 pixel.
private void createSquares(){
for(int i = 0; i < 65; i++){
squares[i] = new JPanel();
squares[i].setLayout(null);
squares[i].setSize(20,20);
board.add(squares[i]);
}
}
You could always "borrow" an image or two online, and put that into your program. For example this code:
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class GetChessSquareImages {
public static final String PATH_TO_SQUARES = "http://www.colourbox.com/preview/" +
"4578561-622234-seamless-oak-square-chess-like-parquet-texture.jpg";
private static final int IMG_SIDE_COUNT = 4;
private static final double SCALE = 0.8;
private Map<SquareColor, List<Icon>> squareColorMap = new HashMap<SquareColor, List<Icon>>();
private Random random = new Random();
public void downloadImages() throws IOException {
URL lrgImgUrl = new URL(PATH_TO_SQUARES);
BufferedImage largeImg = ImageIO.read(lrgImgUrl);
int w = largeImg.getWidth() / IMG_SIDE_COUNT;
int h = largeImg.getHeight() / IMG_SIDE_COUNT;
for (int i = 0; i < IMG_SIDE_COUNT; i++) {
int x = (i * largeImg.getWidth()) / IMG_SIDE_COUNT;
for (int j = 0; j < IMG_SIDE_COUNT; j++) {
if (j != 1 && j != 2) {
int y = (j * largeImg.getHeight()) / IMG_SIDE_COUNT;
extractSubImg(largeImg, i, j, x, y, w, h);
}
}
}
}
private void extractSubImg(BufferedImage largeImg,
int i, int j, int x, int y, int w, int h) {
Image subImg = largeImg.getSubimage(x, y, w, h);
int width = (int) (w * SCALE);
int height = (int) (h * SCALE);
subImg = subImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);
List<Icon> iconList = null;
if (i % 2 == j % 2) {
iconList = squareColorMap.get(SquareColor.LIGHT);
if (iconList == null) {
iconList = new ArrayList<Icon>();
squareColorMap.put(SquareColor.LIGHT, iconList);
}
} else {
iconList = squareColorMap.get(SquareColor.DARK);
if (iconList == null) {
iconList = new ArrayList<Icon>();
squareColorMap.put(SquareColor.DARK, iconList);
}
}
iconList.add(new ImageIcon(subImg));
}
public Icon getRandomIcon(SquareColor sqrColor) {
List<Icon> iconList = squareColorMap.get(sqrColor);
if (iconList == null) {
return null;
} else {
return iconList.get(random.nextInt(iconList.size()));
}
}
public static void main(String[] args) {
GetChessSquareImages getImages = new GetChessSquareImages();
try {
getImages.downloadImages();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
int side = 8;;
JPanel panel = new JPanel(new GridLayout(side , side));
for (int i = 0; i < side; i++) {
for (int j = 0; j < side; j++) {
SquareColor sqrColor = (i % 2 == j % 2) ? SquareColor.LIGHT : SquareColor.DARK;
Icon icon = getImages.getRandomIcon(sqrColor);
panel.add(new JLabel(icon));
}
}
JOptionPane.showMessageDialog(null, panel);
}
}
enum SquareColor {
DARK, LIGHT
}
returns this JPanel:
Then your square size will be based on the sizes of your ImageIcons. For example, I have scaled my squares back with a scale factor of 0.8 (the SCALE constant above) to make the grid a more reasonable size.

How to store rgb values of each pixel in array

I have this code, in which I extracted the value of RGB for each pixel, but I'm wondering how to store each RGB value in an array so that I can use it further in my project. I want to take these stored RGB values as an input for backpropagation algorithm.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
public class PrintImageARGBPixels
{
public static void main(String args[])throws IOException
{
BufferedImage image = ImageIO.read(new File("C:\\Users\\ark\\Desktop\\project_doc\\logo_1004.jpg"));
int r=0,g=0,b=0;
int w = image.getWidth();
int h = image.getHeight();
System.out.println("Image Dimension: Height-" + h + ", Width-"+ w);
int total_pixels=(h * w);
ArrayList <Color> arr = new ArrayList<Color>();
for(int x=0;x<w;x++)
{
for(int y=0;y<h;y++)
{
int rgb = image.getRGB(x, y);
Color c = new Color(rgb);
r=c.getRed();
g=c.getGreen();
b=c.getBlue();
}
}
Color co = new Color(r,g,b);
arr.add(co);
for(int i=0;i<total_pixels;i++)
System.out.println("Element 1"+i+1+", color: Red " + arr.get(i).getRed() + " Green +arr.get(i).getGreen()+ " Blue " + arr.get(i).getBlue());
}
}
// Store the color objects in an array
int total_pixels = (h * w);
Color[] colors = new Color[total_pixels];
int i = 0;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
colors[i] = new Color(image.getRGB(x, y));
i++;
}
}
// Later you can retrieve them
for (int i = 0; i < total_pixels; i++)
{
Color c = colors[i];
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
System.out.println("Red" + r + "Green" + g + "Blue" + b);
}
IGNORE THE BELOW, it is my old answer
Use a multidimensional array:
[
[255, 255, 255],
[108, 106, 107],
[100, 100, 55],
...
]
You can then refer to each pixel, [0][x] to get the colour values.
Why don't you just create a RGB Object like this
public class RGB {
private int R, G, B;
public RGB(int R, int G, int B){
this.R = R;
this.G = G;
this.B = B;
}
public int getR() {
return R;
}
public void setR(int r) {
R = r;
}
public int getG() {
return G;
}
public void setG(int g) {
G = g;
}
public int getB() {
return B;
}
public void setB(int b) {
B = b;
}
}
So you can store RGB Objects in your array to use them later.
Greetings!
It can be achieved easier with HashMap, where Key is an int[] (x and y) and value is the another int[] (r, g, b).

Categories

Resources