Merging Channels + Difference Operator

Hey guys I have a problem with my code…I’m writing with Java and using Macros. So far so good but I don’t get the image I want to at the end.

What I’m doing:
I select 2 images, do an image calculation like AND and merge the red and blue channel of the image with the image itself but let the green channel empty --> I get a purple image and it’s what I expect. Now I do an image calculation with OR with the selected 2 images and convert it into RGB.

When I do a final image calculation with the operator difference of the merged channeled image and the or calculated image, there should be something green in the new image, but it’s just all white… Any Ideas how to solve this?

Code:

The 2 images were simply selected by a dialog box which works correct…

ImageCalculator ic = new ImageCalculator();
//Perform selected Operator
ImagePlus imageFour;
ImagePlus imageThree = ic.run("Or create", imageOne, imageTwo);

new ImageConverter(imageThree).convertToRGB();
imageThree.setTitle("Union RGB");
imageThree.show();

if(operator == "Durchschnitt") {
	imageFour = ic.run("And create", imageOne, imageTwo);
} else if(operator == "Differenz") {
	imageFour = ic.run("Subtract create", imageOne, imageTwo);
} else if(operator == "Symmetrische Differenz") {
	imageFour = ic.run("Difference create", imageOne, imageTwo);
} else return;

//Merge Channels
ImagePlus impRB = imageFour.duplicate();
impRB.setTitle("Red-Blue");
impRB.show();
IJ.run("Merge Channels...", "c1=Red-Blue c3=Red-Blue");

//Merge Union + Result
ImagePlus imageFive = ic.run("Difference create", imageThree, imageFour); //<-- This is the part where I want the green image output, if I'm using impRB I get a Null Pointer Exception
imageFive.setTitle("Union + Result");
imageFive.show();

Edit:
Image 1:

Image 2:

And the result at the end should look like this:

but all I get is this image without the green part, so that’s the problem.

Hello Hans, it sounds like the pixels in your processed images are saturated ie. the upper bit depth limit is exceeded leading to all white pixels (as better described in this thread by @stelfrich).

This limitation is overcome by convertion to 32-bit, see:

imageFour = ic.run("And create 32-bit", imageOne, imageTwo)

You’ll find more information in the thread I linked, including some example python code.

Thanks for the quick response, maybe I should mention that the selected images at the beginning are binary. I think the provided link isn’t what I want to.

I misunderstood then. Maybe you can share examples of imageOne and imageTwo so people can try to reproduce your issue?

Updated my post with the images :wink: Hope someone is out there to help me :smiley:

Possibly you are not merging the two RGB images that you want.

IJ.run("Merge Channels...") creates a new window called RGB. So I think you might want:

ImagePlus imageFive = ic.run("Difference create", "Union RGB", "RGB");

For best results, if you have a multi-step process like this, it’s easier for people to help if you provide a complete working piece of code. You can do this by loading sample images in place of your own images.

Here’s a macro to illustrate (because I’m not proficient in java) – if you look at the “difference of…” images, I think it reproduces the problem and the solution.

run("Blobs (25K)");
setOption("BlackBackground", true);
run("Make Binary");
rename("image1");

run("Duplicate...", "title=image2");
run("Rotate 90 Degrees Left"); // image2 is rotated version of image1

imageCalculator("OR create", "image1","image2"); // image3
selectWindow("Result of image1");
rename("image3");

run("Duplicate...", "title=Union RGB");
run("RGB Color");
rename("Union RGB"); // RGB version of image3


imageCalculator("AND create", "image1","image2"); // image4
selectWindow("Result of image1");
rename("image4");

run("Duplicate...", "title=red-blue");
run("Merge Channels...", "c1=red-blue c3=red-blue"); // creates image titled "RGB"

imageCalculator("Difference create", "image3","image4");  // combining 8-bit binary images
rename("difference of binary images");
 
imageCalculator("Difference create", "Union RGB","RGB");  // combining rgb images 
rename("difference of RGB images");

Hope it helps.

1 Like