Skip to main content

Detecting and correcting gradational brightness variations in a grayscale image


I am trying to quantify the proportions of various components in a greyscale image (backscattered electron image of a polished rock sample). Here is the original image:


bse = Import@"http://i.stack.imgur.com/eQece.png";


enter image description here


The image is comprised of components (minerals) that should have the same intensity response. For example, the abundant 'mid-grey' component (plagioclase) should have a fixed grey-level across the entire image. It doesn't.


By masking the other components of the image and adjusting the contrast, the gradations in brightness become more obvious:


bsek = ImageSubtract[bse, ColorNegate@Binarize[bse, {0.16, 0.24}]];
ImageAdjust[bsek, {3, 1.1}]

enter image description here


There seems to be two effects: a first order decrease in brightness towards the bottom corners and a second-order periodic vertical striping (comprised of short wavelength gradients).


Why bother? - Correcting for variations in brightness is an important step prior to segmentation analysis of the image



By approximating the variation in grey-values for the plagioclase, the difference in birghtness between the top and bottom of the image can be modelled (albeit crudely).


ksim[im_] := Module[{kdf, kdl},
kdf = SmoothKernelDistribution[First@im];
kdl = SmoothKernelDistribution[Last@im];
Plot[{PDF[kdf, x], PDF[kdl, x]}, {x, 0, 0.3}, Filling -> Axis,
Exclusions -> None]]
ksim[ImageData[bsek]]

enter image description here


The following creates a gradational filter that causes the grey-level peaks to overlap (by adjusting the value of shift).



bsedim = ImageDimensions[bse]
shift = 0.2;
grad = 1 +
Transpose@
Array[Table[x, {x, -shift, 0., shift/bsedim[[1]]}] &, bsedim[[1]]];
gradi = Image[grad];

bsekm = ImageMultiply[bsek, gradi];
ksim[ImageData[bsekm]]


enter image description here


This filter can then be applied to the original image:


bsem = ImageMultiply[bse, gradi]

enter image description here


This clumsy method does an OK job of levelling the first-order brightness defect. However, this effect looks to have a spherical gradient (dimmer in the bottom corners than the middle) which is not accounted for.


As yet, I have not found a way to correct the second order periodic effect.


Can anyone suggest a more straightforward way to detect and correct these kinds of brightness variation defects? Extra bonus gratitude to anyone who can help to correct the second-order periodic aberration.




Update



Here is a contrast stretched image showing the vertical aberrations:


 ImageAdjust[bse, {4.5, 3.85}]

enter image description here



Answer



Does this help? I've treated the error as additive, though you could easily change it to a multiplicative correction.


Starting with your bsek image, find the mean offset for rows and for columns:


id = ImageData[bsek];
horiz = (# - Mean[#]) &[Total[id]/Total[Unitize[id]]];
vert = (# - Mean[#]) &[Total[id, {2}]/Total[Unitize[id], {2}]];


ListLinePlot[horiz]
ListLinePlot[vert]

enter image description here enter image description here


The horizontal plot shows an overall darkening at the left and right sides of the image, plus the periodic component. The vertical plot shows an overall darkening towards the bottom of the image, with a periodic component also.


Combining the horizontal and vertical offsets into an overall offset:


a = Outer[Plus, vert, horiz];
Image[a] // ImageAdjust


enter image description here


Now subtract the offset from the original data:


bse2 = Image[ImageData[bse] - a]

enter image description here


Using s.s.o.'s ClusteringComponents visualisation appears to show an improvement:


ClusteringComponents[bse, 10] // Colorize
ClusteringComponents[bse2, 10] // Colorize

enter image description here enter image description here



Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...