When clicking a button in a new layout, my app crashes - java

I am trying to create an app that requires a login. Just to test to see how it works, I made a login button to go to a separate layout where you press a button to randomize numbers. The login button works fine and it brings me to another layout, but it is when I click the button on the separate layout that my app crashes.
here is my code:
private TextView R1;
private TextView R2;
private TextView R3;
private TextView R4;
private TextView R5;
private TextView R6;
private Animation R1_roll_rotate;
private Animation R2_roll_rotate;
private Animation R3_roll_rotate;
private Animation R4_roll_rotate;
private Animation R5_roll_rotate;
private Animation R6_roll_rotate;
private Button Randomize_Button;
private Animation Button_Rotate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Randomize_Button= (Button) findViewById(R.id.Randomize);
R1= (TextView) findViewById(R.id.RandomOne);
R2= (TextView) findViewById(R.id.RandomTwo);
R3= (TextView) findViewById(R.id.RandomThree);
R4= (TextView) findViewById(R.id.RandomFour);
R5= (TextView) findViewById(R.id.RandomFive);
R6= (TextView) findViewById(R.id.RandomSix);
Button_Rotate= AnimationUtils.loadAnimation(Main_Activity.this, R.anim.Button_Rotate);
R1_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomOne_roll_rotate);
R2_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomTwo_roll_rotate);
R3_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomThree_roll_rotate);
R4_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomFour_roll_rotate);
R5_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomFive_roll_rotate);
R6_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomSix_roll_rotate);
public void onClick(View view) {
int randomnum1 = r.nextInt(70);
int randomnum2 = r.nextInt(70);
int randomnum3 = r.nextInt(70);
int randomnum4 = r.nextInt(70);
int randomnum5 = r.nextInt(70);
int randomnum6 = r.nextInt(27);
int num1 = randomnum1;
int num2 = randomnum2;
int num3 = randomnum3;
int num4 = randomnum4;
int num5 = randomnum5;
int num6 = randomnum6;
String randomnum7 = String.valueOf(randomnum1);
String randomnum8 = String.valueOf(randomnum2);
String randomnum9 = String.valueOf(randomnum3);
String randomnum10 = String.valueOf(randomnum4);
String randomnum11 = String.valueOf(randomnum5);
String randomnum12 = String.valueOf(randomnum6);
try {
Randomize_Button.startAnimation(Button_Rotate);
R1.startAnimation(R1_roll_rotate);
R2.startAnimation(R2_roll_rotate);
R3.startAnimation(R3_roll_rotate);
R4.startAnimation(R4_roll_rotate);
R5.startAnimation(R5_roll_rotate);
R6.startAnimation(R6_roll_rotate);*/
for (int i = num1; i > 0 && i == num1; i++) {
String ii = Integer.toString(i);
R1.setText(ii);
}
for (int j = num2; j > 0 && j == num2; j++) {
String jj = Integer.toString(j);
R2.setText(jj);
}
for (int k = num3; k > 0 && k == num3; k++) {
String kk = Integer.toString(k);
R3.setText(kk);
}
for (int d = num4; d > 0 && d == num4; d++) {
String dd = Integer.toString(d);
R4.setText(dd);
}
for (int s = num5; s > 0 && s == num5; s++) {
String ss = Integer.toString(s);
R5.setText(ss);
}
for (int g = num6; g > 0 && g == num6; g++) {
String gg = Integer.toString(g);
R6.setText(gg);
}
for (int g = num1; g == num2; g++) {
g = r.nextInt(70);
String gg = Integer.toString(g);
R1.setText(gg);
}
for (int d = num1; d == num3; d++) {
d = r.nextInt(70);
String dd = Integer.toString(d);
R1.setText(dd);
}
for (int a = num1; a == num3; a++) {
a = r.nextInt(70);
String aa = Integer.toString(a);
R1.setText(aa);
}
for (int w = num1; w == num4; w++) {
w = r.nextInt(70);
String ww = Integer.toString(w);
R1.setText(ww);
}
for (int q = num1; q == num5; q++) {
q = r.nextInt(70);
String qq = Integer.toString(q);
R1.setText(qq);
}
for (int y = num2; y == num3; y++) {
y = r.nextInt(70);
String yy = Integer.toString(y);
R2.setText(yy);
}
for (int o = num2; o == num4; o++) {
o = r.nextInt(70);
String oo = Integer.toString(o);
R2.setText(oo);
}
for (int p = num2; p == num5; p++) {
p = r.nextInt(70);
String pp = Integer.toString(p);
R2.setText(pp);
}
for (int x = num3; x == num4; x++) {
x = r.nextInt(70);
String xx = Integer.toString(x);
R3.setText(xx);
}
for (int c = num3; c == num5; c++) {
c = r.nextInt(70);
String cc = Integer.toString(c);
R3.setText(cc);
}
for (int b = num4; b == num5; b++) {
b = r.nextInt(70);
String bb = Integer.toString(b);
R4.setText(bb);
}
} catch (Exception e){
Toast.makeText(Main_Activity.this,e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
Does anyone know why? If so, your help would be deeply appreciated! Thanks.

The possible reason what I can figure out reading your question is most probably you would have forgot the add the button in your another layout.
Since, the Button View would be null in this case so once you click, it would throw NullPointerException like this
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setEnabled(boolean)' on a null object reference
First of all check in your another layout whether the button view is added or not.
Then in Activity class check whether button view that you have initialized using the correct id which should match against the xml one.
These could be the reason for the crash you encountered.
Hope this helps.

Add a try-catch block in button click like..
try{
//Your Code for randomize number.
}
catch(Exception e)
{
Toast.makeText(context,e.getMessage(), Toast.LENGTH_SHORT).show();
}
This will show exception in toast and your app won't crash.

Related

EventDispatchThread Execption

I create alist method to insert principal values and remaining balances according to the payments.But when I implement this method in actionListner method error occurs(EventDispatchThread Execption).I try to solve it.Please give any help for me to solve this.
public class Loan {
float lamt,irate,inst,lbalance;
int duration,nop;
String amtlist[][] = new String[duration+1][5];
Loan(float lamt, float irate, int duration, int nop) {
this.lamt = lamt;
this.irate = irate;
this.duration = duration;
this.nop = nop;
}
public void alist() {
amtlist[0][0] = "0" ;
amtlist[0][1] = "0";
amtlist[0][2] = "0";
amtlist[0][3] = "0";
amtlist[0][4] = Float.toString(inst*annuity(duration,irate));
float v = 1 / (1 + irate);
for (int i = 1; i < duration + 1; i++) {
amtlist[i][0] = Float.toString(i);
amtlist[i][1] = Float.toString(irate * annuity(duration + 1 - i, irate));
amtlist[i][2] = Float.toString(instalment());
amtlist[i][3] = Double.toString(instalment() * Math.pow(v, i));
amtlist[i][4] = Float.toString(instalment() * annuity(duration - i, irate));
}
}
public float annuity(int duration, float irate) {
float v = 1 / (1 + irate);
float an = (float) ((1 - Math.pow(v, duration)) / irate);
return an;
}
public float instalment(){
return inst = lamt / annuity(duration, irate);
}
public float lbalance() {
return lbalance = inst * annuity(duration - nop, irate);
}
}
public void actionPerformed(ActionEvent e) {
String lamtstr = tf1.getText();
String iratestr = tf2.getText();
String durationstr = tf3.getText();
String nopstr = tf4.getText();
float lamt = Float.parseFloat(lamtstr);
float irate = Float.parseFloat(iratestr);
int duration = Integer.parseInt(durationstr);
int nop = Integer.parseInt(nopstr);
Loan l = new Loan(lamt, irate, duration, nop);
float inst = 0, balance = 0;
if (e.getSource() == b1) {
inst =l.instalment();
balance =l.lbalance();
}
if (e.getSource() == b2) {
l.alist();
fscr.dispose();
new AmTable(l.amtlist);
}
tf5.setText(String.valueOf(inst));
tf6.setText(String.valueOf(balance));
}
I tried solve it by changing alist method to value returning method but my attempt got vain.
if (e.getSource() == b2) {
l.alist();
fscr.dispose();
new AmTable(l.amtlist);
}
I expected to call AmTable function with amlist array.

Java NameLengthNotValidException always gets thrown in domainclass

IMPORTANT: Probably a rookie question, but I'm clueless on how to solve it.
I have a UI class which I use to input a name and gender (geslacht) using a scanner (see code below)
private static void geefNaamEnGeslacht(int aantal) {
String naam, geslacht;
for (int i = 0; i < aantal; i++) {
System.out.print("Wat is de naam van speler " + (i + 1) + " (min 6 tekens, max 12 tekens)? ");
naam = sc.next();
naam += sc.nextLine();
System.out.print("Wat is het geslacht van speler " + (i + 1) + "(man=m vrouw=v)? ");
geslacht = sc.next();
System.out.println(naam.length());
try {
dc.maakSpelerAan(naam, geslacht.charAt(0));
} catch (NameLengthNotValidException e) {
System.out.println(e.getMessage());
i--;
}
}
}
In my domain class I check the length of that name in the setter, but it always throws the exception, no matter what I tried. When debugging, the length of any name I picked checks out to be between 6 and 12 characters strangely enough. (see code below)
public final void setNaam(String naam) {
int lengte = naam.length();
if (naam.length() < 6 || naam.length() > 12) {
throw new NameLengthNotValidException("Je naam moet tussen 6 en 12 tekens liggen!");
}
this.naam = naam;
}
Any ideas or tips are apreciated.
EDIT1 code of dc.maakSpelerAan
public void maakSpelerAan(String naam, char geslacht){
s.maakSpeler(naam, geslacht);
}
EDIT2 code of s.maakspeler
public void maakSpeler(String naam, char geslacht){
if (spelCompleet()) {
throw new ListFilledException("De lijst met spelers is vol !");
}
if (!controleerUniekeNaam(naam)) {
throw new NotUniqueNameException();
}
Kaart[] schatkaarten = new Kaart[STARTAANTAL];
Kaart[] kerkerkaarten = new Kaart[STARTAANTAL];
for (int i = 0; i > STARTAANTAL; i++) {
schatkaarten[i] = stapels.get(SCHATKAART).geefBovensteKaart();
kerkerkaarten[i] = stapels.get(KERKERKAART).geefBovensteKaart();
}
try{
Speler s = new Speler(naam, geslacht);
s.getKaarten(kerkerkaarten, schatkaarten);
spelers.add(s);
}catch (IllegalArgumentException | NameLengthNotValidException e){
throw e;
}
if (spelCompleet()) {
//UC2
spelers.get(EERSTESPELERINDEX).setBeurt(true);
}
}
EDIT3 REPOST WITH CORRECT IMAGE Image of variable "naam" right before it goes in the IF loop while debugging. MINLENGTHNAAM and MAXLENGTHNAAM are set to 6 and 12 respectively in an interface class.
EDIT4 Code in my interface class for MINLENGTH AND MAXLENGTH
public interface SpelInfo {
final int STARTAANTAL = 2;
final int MAXAANTALPUNTEN = 10;
final int INDEXNAAMDETAILS = 0;
final int INDEXGESLACHTDETAILS = 1;
final int INDEXPUNTENDETAILS = 2;
final int MINAANTALSPELERS = 3;
final int MAXAANTALSPELERS= 6;
final int EERSTESPELERINDEX = 0;
final int MAXLENGTHNAAM = 6;
final int MINLENGTHNAAM = 12;
final int AANTALSTAPELS = 2;
final char MAN = 'm';
final char VROUW = 'v';
final int SCHATKAART = 0;
final int KERKERKAART = 1;
}
In "Edit4" you need to change the green line into:
if(naam.length() > MAXLENGTHNAAM || naam.length() < MINLENGTHNAAM) {
Just a little logical issue :)

Simplify some code in Java and Android Studio

I am trying to create something like that: After clicking a button, the program should show four random numbers ( between 1 and 9), but with some particularities. Together and after a set of conditions (operations), the final result is 22. I had already written the code, but I think it is too big for the usage, and there are some little things I wanted to change. For example, instead of clicking the button several times until the right set of numbers appear, I would like it to automatically show those four "magic" numbers in the first click. I have no idea how to do it:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void button_action (View v){ int TF = 22;
Random a = new Random();
int i1 = a.nextInt(10 - 1) + 1;
TextView tv = (TextView) findViewById(R.id.textView1);
Random b = new Random();
int i2 = b.nextInt(10-1)+1;
TextView tv2 = (TextView) findViewById(R.id.textView2);
Random c = new Random();
int i3 = c.nextInt(10-1)+1;
TextView tv3 = (TextView) findViewById(R.id.textView3);
Random d = new Random();
int i4 = d.nextInt(10-1)+1;
TextView tv4 = (TextView) findViewById(R.id.textView4);
int soma = i1 + i2 + i3 + i4;
int mult0 = i1 * i2 * i3 * i4;
int mult1 = (i1*i2*i3)+i4;
int mult2 = (i1*i2)+i3+i4;
int mult3 = i1 * (i2 + i3 + i4);
int mult4 = i2 * (i1+i3+i4);
int mult5 = i3 * (i1+i2+i4);
int mult6 = i4 * (i1+i2+i3);
int mult7 = (i1*i3)+i2+i4;
int mult8 = (i1*i4)+i2+i3;
int mult9 = (i2*i3)+i1+i4;
int mult10 = (i2*i4)+i1+i3;
int mult11 = (i4*i3)+i1+i2;
int mult12 = (i1*i3*i4)+i2;
int mult13 = (i1*i2*i4)+i3;
int mult14 = (i4*i2*i3)+i1;
if (soma == TF){
tv.setText(String.valueOf(i1));
tv2.setText(String.valueOf(i2));
tv3.setText(String.valueOf(i3));
tv4.setText(String.valueOf(i4));}
if (mult0 == TF){
tv.setText(String.valueOf(i1));
tv2.setText(String.valueOf(i2));
tv3.setText(String.valueOf(i3));
tv4.setText(String.valueOf(i4));}
if (mult1 == TF){
tv.setText(String.valueOf(i1));
tv2.setText(String.valueOf(i2));
tv3.setText(String.valueOf(i3));
tv4.setText(String.valueOf(i4));}
if (mult2 == TF){
tv.setText(String.valueOf(i1));
tv2.setText(String.valueOf(i2));
tv3.setText(String.valueOf(i3));
tv4.setText(String.valueOf(i4));}
if (mult3 == TF){
tv.setText(String.valueOf(i1));
tv2.setText(String.valueOf(i2));
tv3.setText(String.valueOf(i3));
tv4.setText(String.valueOf(i4));}
if (mult4 == TF){
tv.setText(String.valueOf(i1));
tv2.setText(String.valueOf(i2));
tv3.setText(String.valueOf(i3));
tv4.setText(String.valueOf(i4));} ... }
}
Any suggestions?

Calling the appropriate function

I'm new to android development and having a hard time trying to call the appropriate function on my setOnClickListener, I have 3 function each have a set of editText that the user can input some double values this values are then return by the function and when the user clicks calculate displays the right information, however, I'm trying to check if there's a value either in the editText or the function so I can display the right information, but I can't figure it out.
1 function at a time works
public class MainWindowActivity extends AppCompatActivity {
private Button calculateButton;
private EditText length1Text;
private EditText length2Text;
private EditText length3Text;
private EditText width1Text;
private EditText width2Text;
private EditText width3Text;
private EditText thick1Text;
private EditText thick2Text;
private EditText thick3Text;
private EditText squareYards1Text;
private EditText squareYards2Text;
private EditText squareYards3Text;
private EditText result1TotalText;
private EditText result2TotalText;
private EditText result3TotalText;
private EditText grandTotalText;
private Double length1 = 0.0;
private Double length2 = 0.0;
private Double length3 = 0.0;
private Double width1 = 0.0;
private Double width2 = 0.0;
private Double width3 = 0.0;
private Double thick1 = 0.0;
private Double thick2 = 0.0;
private Double thick3 = 0.0;
private Double syard1 = 0.0;
private Double syard2 = 0.0;
private Double syard3 = 0.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_window);
calculateButton = (Button) findViewById(R.id.calculateButtonPressed);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(CalculateArea1() == null){
CalculateArea2();
CalculateArea3();
Total = CalculateArea2() + CalculateArea3();
}
grandTotalText.setText(String.format("%.1f",Total));
}
});
public Double CalculateArea1(){
length1Text = (EditText) findViewById(R.id.length1Text);
width1Text = (EditText) findViewById(R.id.width1Text);
thick1Text = (EditText) findViewById(R.id.thickness1Text);
squareYards1Text = (EditText) findViewById(R.id.squareYards1Text);
result1TotalText = (EditText) findViewById(R.id.result1TotalText);
grandTotalText = (EditText) findViewById(R.id.grandTotalText);
if (squareYards1Text.getText().toString().trim().length() == 0) {
length1 = Double.valueOf(length1Text.getText().toString());
width1 = Double.valueOf(width1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}else{
syard1 = Double.valueOf(squareYards1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}
//area 1
double yard = length1 * width1 / 9;
double t = yard / thick1;
double y1 = 12.20 / thick1;
double sy1 = syard1 / y1;
if (thick1 == 1) {
t = yard / 12.20;
}else if(thick1 == 1.25){
t = yard / 9.76;
}else if (thick1 == 1.5){
t = yard / 8.13;
}else if (thick1 == 1.75){
t = yard / 6.97;
}else if (thick1 == 2){
t = yard / 6.1;
}
Double result;
if (length1Text.getText().toString().trim().length() == 0) {
result1TotalText.setText(String.format("%.1f", sy1));
grandTotalText.setText(String.format("%.1f", sy1));
result = sy1;
} else {
result1TotalText.setText(String.format("%.1f", t));
squareYards1Text.setText(String.format("%.1f", yard));
grandTotalText.setText(String.format("%.1f", t));
result = t;
}
return result;
}
public Double CalculateArea2(){
length2Text = (EditText) findViewById(R.id.length2Text);
width2Text = (EditText) findViewById(R.id.width2Text);
thick2Text = (EditText) findViewById(R.id.thickness2Text);
squareYards2Text = (EditText) findViewById(R.id.squareYards2Text);
result2TotalText = (EditText) findViewById(R.id.result2TotalText);
grandTotalText = (EditText) findViewById(R.id.grandTotalText);
if (squareYards2Text.getText().toString().trim().length() == 0) {
length2 = Double.valueOf(length2Text.getText().toString());
width2 = Double.valueOf(width2Text.getText().toString());
thick2 = Double.valueOf(thick2Text.getText().toString());
}else{
syard2 = Double.valueOf(squareYards2Text.getText().toString());
thick2 = Double.valueOf(thick2Text.getText().toString());
}
//area 2
double yard2 = length2 * width2 / 9;
double t2 = yard2 / thick2;
double y2 = 12.20 / thick2;
double sy2 = syard2 / y2;
if (thick2 == 1) {
t2 = yard2 / 12.20;
}else if(thick2 == 1.25){
t2 = yard2 / 9.76;
}else if (thick2 == 1.5){
t2 = yard2 / 8.13;
}else if (thick2 == 1.75){
t2 = yard2 / 6.97;
}else if (thick2 == 2){
t2 = yard2 / 6.1;
}
Double result;
if (length2Text.getText().toString().trim().length() == 0) {
result2TotalText.setText(String.format("%.1f", sy2));
grandTotalText.setText(String.format("%.1f", sy2));
result = sy2;
} else {
result2TotalText.setText(String.format("%.1f", t2));
squareYards2Text.setText(String.format("%.1f", yard2));
grandTotalText.setText(String.format("%.1f", t2));
result = t2;
}
return result;
}
public Double CalculateArea3(){
length3Text = (EditText) findViewById(R.id.length3Text);
width3Text = (EditText) findViewById(R.id.width3Text);
thick3Text = (EditText) findViewById(R.id.thickness3Text);
squareYards3Text = (EditText) findViewById(R.id.squareYards3Text);
result3TotalText = (EditText) findViewById(R.id.result3TotalText);
grandTotalText = (EditText) findViewById(R.id.grandTotalText);
if (squareYards3Text.getText().toString().trim().length() == 0) {
length3 = Double.valueOf(length3Text.getText().toString());
width3 = Double.valueOf(width3Text.getText().toString());
thick3 = Double.valueOf(thick3Text.getText().toString());
}else{
syard3 = Double.valueOf(squareYards3Text.getText().toString());
thick3 = Double.valueOf(thick3Text.getText().toString());
}
//area 3
double yard3 = length3 * width3 / 9;
double t3 = yard3 / thick3;
double y3 = 12.20 / thick3;
double sy3 = syard3 / y3;
if (thick3 == 1) {
t3 = yard3 / 12.20;
}else if(thick3 == 1.25){
t3 = yard3 / 9.76;
}else if (thick3 == 1.5){
t3 = yard3 / 8.13;
}else if (thick3 == 1.75){
t3 = yard3 / 6.97;
}else if (thick3 == 2){
t3 = yard3 / 6.1;
}
Double result;
if (length3Text.getText().toString().trim().length() == 0) {
result3TotalText.setText(String.format("%.1f", sy3));
grandTotalText.setText(String.format("%.1f", sy3));
result = sy3;
} else {
result3TotalText.setText(String.format("%.1f", t3));
squareYards3Text.setText(String.format("%.1f", yard3));
grandTotalText.setText(String.format("%.1f", t3));
result = t3;
}
return result;
}
}
I just did the check on the first to keep it short.
Any help will be greatly appreciated.
Give us your code so we can help, I need to see your CalculateArea{1,2,3}.
Okay mate, I think I find the solution and it is really easy. When you write OnClick() function like that you need to assign OnClick function on XML file as well so the button knows that it has a "function" to do something.
Here is an example ;
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="OnClick" />
Hope it will be helpful.
if (squareYards1Text.getText().toString().trim().length() == 0) {
length1 = Double.valueOf(length1Text.getText().toString());
width1 = Double.valueOf(width1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}else{
syard1 = Double.valueOf(squareYards1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
// Toast.makeText(MainWindowActivity.this, "inside the 'ELSE' statement", Toast.LENGTH_LONG).show();
}
the code above was the problem, I had to do one more check and change the values if the condition was true like this.
if (squareYards1Text.getText().toString().trim().length() == 0 && length1Text.getText().toString().trim().length() == 0) {
length1 = 0.0;
syard1 = 0.0;
width1 = 0.0;
}else if(squareYards1Text.getText().toString().trim().length() == 0){
length1 = Double.valueOf(length1Text.getText().toString());
width1 = Double.valueOf(width1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}else{
syard1 = Double.valueOf(squareYards1Text.getText().toString());
thick1 = Double.valueOf(thick1Text.getText().toString());
}
Now it doesn't matter if only one area is being use or 2 or 3 or all of them, it will never find an "invalid double". I had to do this for all areas.
Thanks.

How to combine 2 images to form hybrid image?

I am implementing hybrid image with ImageJ and stuck at merging low filter image and high filter image to form a hybrid image.
This is what I already done.I have 2 images from Gaussian Blur and Laplician of Gaussian filer. I need to merge these 2 images by layer after that. Any idea how to achieve it?
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.plugin.*;
import ij.io.*;
import java.io.*;
public class HybridImage_Plugin implements PlugInFilter{
int cfsize=3;
String img_lowPass;
String img_highPass;
private double[][] filter;
private double sigma;
float w=2 ,delta=0 , thr=0;
int mode=0;
//dialogbox
private boolean GUI()
{
GenericDialog gd = new GenericDialog("Enter Values", IJ.getInstance());
gd.addNumericField("Sigma (3,5,9,17,35)", cfsize, 0);
gd.addStringField("Low-Pass", "/home/atrx/ImageJ/plugins/hybridimage/l1.tif");
gd.addStringField("High-Pass", "/home/atrx/ImageJ/plugins/hybridimage/l2.tif");
return getUserParams(gd);
}
//get parameters
private boolean getUserParams(GenericDialog gd)
{
gd.showDialog();
if (gd.wasCanceled())
{
return false;
}
cfsize = (int) gd.getNextNumber();
img_lowPass = gd.getNextString();
img_highPass= gd.getNextString();
return true;
}
public int setup(String arg, ImagePlus imp) {
return PlugInFilter.NO_IMAGE_REQUIRED;
}
public void run(ImageProcessor ip) {
int[][] result;
if(GUI() == false)
{
return;
}
else
{
Opener opener1 = new Opener();
Opener opener2 = new Opener();
ImagePlus imp1= opener1.openImage(img_lowPass);
ImagePlus imp2= opener2.openImage(img_highPass);
//imp1.show("Low Pass Image");
//imp2.show("HighPass Image");
ImageProcessor ip1 = imp1.getProcessor();
ImageProcessor ip2 = imp2.getProcessor();
//lowpass filter(Gaussian Blur)
ip1.blurGaussian(cfsize);
showProcessor(ip1,"Low Pass Filtered Image");
//highpass filter(LoG)
int csize = ip2.getHeight();
int rsize = ip2.getWidth();
Rectangle rect = ip2.getRoi();
int d0,a0,acr,dow,it;
int i,x,y;
double h12, h21, ft, h1h2, h2h1, fmu, dh, dv;
double r, dt, dmx, dmn;
float logaus[] = new float[(rect.width>rect.height)? rect.width : rect.height];
float gaus[] = new float[(rect.width>rect.height)? rect.width : rect.height];
float dgaus[] = new float[(rect.width>rect.height)? rect.width : rect.height];
long zcn =0;
byte pixels[] = (byte[])ip2.getPixels();
int img_in[] = new int[rect.width*rect.height];
if (cfsize<0) cfsize=3;
if (cfsize>35) cfsize=35;
if(w<0) w=0;
int fsize = (int)(cfsize*w);
if (fsize%2 == 0)
{
fsize += 1;
}
double dimg[] = new double[rect.height*rect.width];
double dr[] = new double[rect.height*rect.width];
i=0;
for(y=rect.y;y<(rect.y+rect.height);y++)
{
for(x=rect.x;x<(rect.x+rect.width);x++)
{
img_in[i] = (pixels[(y*rsize)+x]&0xff);
i++;
}
}
int size = rect.width + fsize -1;
int image[] = new int[(rect.width+fsize-1)*(rect.height+fsize-1)];
int extension= (fsize/2);
for( i=0; i<rect.height;i++)
{
System.arraycopy(img_in,(i*rect.width),image,( ((i+extension)*(rect.width+fsize-1))+ extension ),rect.width);
}
h1h2= h2h1 = h12 =0.0;
for(i=1; i<( (fsize+1) /2);i++)
{
w = (float)cfsize/(float)2.0/(float)1.414;
ft = i/w;
gaus[i] = (float)Math.exp(-ft*ft/2);
h1h2 += 2.0 *(gaus[i]);
logaus[i] =(float)(1-ft*ft)*(float)Math.exp(-ft*ft/2);
h2h1 += 2.0*(logaus[i]);
dgaus[i] =(float)ft*(float)Math.exp(-ft*ft/2);
}
fmu = (h2h1 + 1)* (h1h2+1);
int prel[] = new int[rect.width+1];
dmx = -99999.9;
dmn = 99999.9;
int limit = ((rect.width+fsize-1)*(rect.height+fsize-1));
for(d0=0;d0<rect.height;d0++)
{
for(a0=0;a0<rect.width;a0++)
{
acr = a0 + fsize/2;
dow = d0 + fsize/2;
dh = dv = 0.0;
h1h2 = h2h1 = 0.0;
for (int j=1; j<(fsize+1)/2; j++)
{
int a0d0, a0d1, a1d0, a1d1;
h12=h21=0.0;
for(i=1;i<(fsize+1)/2;i++)
{
a0d0 = acr-i+((dow-j)*size);
a0d1 = acr-i+((dow+j)*size);
a1d0 = acr+i+((dow-j)*size);
a1d1 = acr+i+((dow+j)*size);
h12 += logaus[i]*(image[a0d0] + image[a0d1]+
image[a1d0] + image[a1d1]);
h21 += gaus[i]* (image[a0d0] + image[a0d1] +
image[a1d0] + image[a1d1]);
}
a0d0 = acr-j+dow*size;
a0d1 = acr+(dow-j)*size;
a1d0 = acr+j+dow*size;
a1d1 = acr+(dow+j)*size;
h1h2 += gaus[j] * (h12+ image[a0d0]+image[a0d1]+
image[a1d0]+image[a1d1]);
h2h1 += logaus[j]*(h21+ image[a0d0]+ image[a0d1] +
image[a1d0] + image[a1d1] );
if(thr != 0.0)
{
dh += dgaus[j] * ( image[a1d0] - image[a0d0] );
dv += dgaus[j] * ( image[a1d1] - image[a0d1] );
}
}
dt = dimg[d0*rect.width+a0] = h1h2 + h2h1 + (2*image[dow*size+acr]) ;
if (dt > dmx) dmx = dt;
if (dt < dmn) dmn = dt;
if( thr!= 0.0)
{
dr[(d0*rect.width)+a0] = Math.abs(dh) + Math.abs(dv);
}
}
}
dmx = (dmx-dmn) / 2;
dmn += dmx;
int row=0, column=0;
for(d0=0;d0<rect.height;d0++)
{
for(a0=0;a0<rect.width;a0++)
{
int id = (d0*rect.width) +a0;
int index = rsize*(rect.y+d0) + (a0+rect.x);
int k = 15;
it = (int)(dt = (dimg[id] - (dmn-delta*dmx))*255 / (dmx*(1+Math.abs(delta))));
switch(mode){
case 0:
pixels[index] = (byte)((dt-dmn+dmx)/dmx*127);
break;
case 1:
pixels[index] = (byte)Math.abs(it);
break;
case 2:
pixels[index] = (byte)( ((dt!=0)?((dt>0) ? 1: -1) : 0) * 192);
break;
case 3:
default:
r = dr[id];
it = ( (dt!=0) ? ((dt>0) ? 1: -1) : 0);
if( it==0 && r>=thr)
{
k = 255;
zcn++;
}
else
{
if( (it*prel[a0]<0 || it*prel[a0+1]<0) && r>=thr)
{
k = 255;
zcn++;
}
}
prel[a0+1] = it;
if(k==255 || mode!=3)
pixels[index] = (byte)k;
break;
}
}
}
showProcessor(ip2,"High Pass Filtered Image");
}
}
static void showProcessor(ImageProcessor ip, String title){
ImagePlus win = new ImagePlus(title,ip);
win.show();
}
}
Have you tried performing a weighted sum?
OUT = w*LPF + (1 - w)*HPF
This kind of sum is used everywhere. In particular, image blending, alpha matting and even in some optimization schemes.
However because there are patches of varying spatial frequencies all around your image, you may have to make the weight adaptive. You also have to choose which one you want to emphasize more. Do you want the low pass or high pass information to stand out more? Depending on which you want, you might want to use information in either one of those images and run it through some distance or similarity measure to get the right weight.

Categories

Resources