Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to perform some neighborhood analysis on an image, but ImageFilter seems to be interfering with the results outputted by the provided filtering function.

Initialization

Given the hue channel of an image

image = Import["http://i.imgur.com/IqnvrjC.png"]
hue = ColorSeparate[image, "HSB"][[1]]

We can see that there is some information "hidden" on the dark area

ImageAdjust[hue, {0, 0, 0.2}]

Processing

My intent is to replace the value of every pixel with the value of the variance of his direct (3x3), neighbors:

huevar = ImageFilter[Composition[Variance, Flatten], hue, 1]

The result is also very dark, as expected, but the dark areas are effectively black. As I haven't clipped the results, it is not what I expected.

The filter outputs small values, as I could observe in the following:

Reap[
  huevar = ImageFilter[Composition[Sow[#] &, Variance, Flatten], hue, 1],
   _,
  (reapvar = #2)&
];

reapvar // Take[#, 5] &
(* {0.000126874, 0.000250331, 0.0000807382, 8.11654*10^-6, 0.0000234952} *)

Flatten@ImageData@huevar // Take[#, 5] &
(* {0., 0., 0., 0., 0.} *)

If I reconstruct the image with data from reapvar I get exactly what I want: a dark image that can be re-scaled to show the detail in the dark area.

Image[Partition[reapvar, First@ImageDimensions@image]]
ImageAdjust[%, {0, 0, .1}]

Pre or mid-scaling

I've also tried pre-scaling the output directly in filtering function, as in

Reap[
  scaledhuevar = ImageFilter[Composition[Sow[1000*#] &, Variance, Flatten], hue, 1],
   _,
  (scaledreapvar = #2)&
];

The dark areas get brighter this way, but the precise values, even from the bright areas, are different from those obtained by sowing/reaping.

scaledreapvar // Take[#, 5] &
(* {0.126874, 0.250331, 0.0807382, 0.00811654, 0.0234952} *)

Flatten@ImageData@scaledhuevar // Take[#, 5] &
(* {0.12549, 0.25098, 0.0823529, 0.00784314, 0.0235294} *)

What is causing this difference in the results? When/where is the clipping occurring?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

It is caused by rounding errors, but the effect is gone if you work with Realimages:

image = Import["http://i.imgur.com/IqnvrjC.png"];
hue = ColorSeparate[image, "HSB"][[1]];
hueR = Image[ImageData@hue, "Real"];

Tr[Subtract@@ (Flatten/@ Reap@ImageData@
                ImageFilter[Sow@Variance@Flatten@#&, #, 1])]& /@ {hue, hueR}

(*
  {-0.748735, 0.}
*)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.