4
votes

I have two images. The first one is of background noise + content, and the second one is just of the background noise. I would like to subtract the second image from the first to remove noise from the content. The image is in greyscale.

I'm confused between the various ways to handle this, as well as the handling of greyscale values in mathematica.

1) Firstly, we may use ImageSubtract[imageOne, imageTwo].

2) By using ImageDifference[imageOne, imageTwo], we avoid negative pixel values, but the image is artificial at places where we would've had to have negative pixels when using ImageSubtract.

3) We obtain the values of each pixel using ImageData, subtract each corresponding value and then display the result using Image.

Each of these methods yields different results.

1
Please upload your images somewhere ...Dr. belisarius

1 Answers

6
votes

For images with real data types, pixel values can be negative, and these three operations are equivalent:

real1 = Image[RandomReal[1, {10, 10}]];
real2 = Image[RandomReal[1, {10, 10}]];

ImageData[ImageDifference[real1, real2]] ==
Abs@ImageData[ImageSubtract[real1, real2]] ==
Abs[ImageData[real1] - ImageData[real2]]

Out[4]= True

But it is not the case with images of integer datatypes. That is because only positive values can be stored in such images, and negative results from the subtraction are clipped to zero in the output image:

int1 = Image[RandomInteger[255, {10, 10}], "Byte"];
int2 = Image[RandomInteger[255, {10, 10}], "Byte"];

This is still True:

ImageData[ImageDifference[int1, int2]]
== Abs[ImageData[int1] - ImageData[int2]]

But these two are different because of clipping:

ImageData[ImageDifference[int1, int2]]
== Abs@ImageData[ImageSubtract[int1, int2]]

There would be less puzzling results when converting both input images to "Real" or "Real32" data type.