# Background removal of gradient background

I am trying to calculate the sum of intensities present in my agarose gel:

I want to compare the intensity of the total white band on the top with that of the one on the bottom. The idea is to binarize the image at a certain threshold and use that matrix (the 1's) to determine the gray scale value at that same index in the original picture. However in order to properly binarize I want to substract the background. As seen over here there is a kind of gradient present. Do you guys have any ideas how to properly identify the background and substract it from the original image.

EDIT:

I now try to cut the image in two parts beforehand (top part and bottom part) then I can neglect the background in a certain way. I use the following code to identify the bands with image

:

topintens = Import["Image_1"];
topintens = MedianFilter[topintens, 1];
imt = Binarize[TopHatTransform[topintens, 4.09], 0.013];
imtfil = DeleteSmallComponents[imt, 7]


However I then end up with not so smooth bands, I want to apply dilation/erosion in order to make neat bands however this does not work out for me yet:

So tips are welcome

• I don't think it's a question of definitions or identification. In my opinion, your picture simply does not have a uniform background. You can 1) take another picture; or 2) ignore the gradient artifact. It would also be helpful if you could express the procedure you described in Mathematica code and include it in your post. – MarcoB Jun 21 '16 at 20:13
• I edited my question, thanks for your response – Glenn Jun 21 '16 at 20:42
• Is there no other way for you to take a picture with better contrast during electrophoresis? – J. M. will be back soon Jun 21 '16 at 21:16
• Well the quality of the gels is always different, this was actually a pretty good image. Therefore we are searching for image analysis tools to interpret our data better – Glenn Jun 21 '16 at 22:01

One possibility would be to apply a highpass filter, since what you want to remove is the slowly undulating background.

img = Import["http://i.stack.imgur.com/rvRAc.png"];


then binarize:

Binarize[imgHP]


To follow your original idea of locating the background and subtracting it -- this can be approached with a lowpass filter, followed by taking the difference between the image and the lowpass-filtered version:

bg = LowpassFilter[img, 0.1];