1

I have this plot:

enter image description here

and want to flatten its baseline/reduce offset using Matlab.

Basically like a baseline correction for a spectrum but here I've got a mesh and can't get my head around it how to flatten its baseline when dealing with a matrix? Basically the dot should stay but the surrounding is actually zero. The noise can stay, though.

Here is the image:

enter image description here

I am wondering if something like this works:

        for x=1:1201
        for y=1:1201
             Ibasetest = polyfit(x,y,1);
        end
        end

Simply put do a baseline for each X along Y for Z data. But I can't get it to work. :(

nolimits
  • 43
  • 1
  • 3
  • 16
  • How would you proceed with a simple signal? What baseline removal technical would you use? What is your input and what is the expected output? – jlandercy Oct 06 '20 at 03:12
  • Just very basic. The dot in the middle should stay but I want to remove the offset around it. At the moment its like 10 to 60 offset. This should be pulled down to 0. Simple linear fit should do. Rest I can learn by playing around. – nolimits Oct 06 '20 at 03:36
  • Would you happen to be willing to post the input image you used? That might make it easier to play around and test some methods. – MichaelTr7 Oct 06 '20 at 03:59
  • Attached :) So basically the dark surrounding should be black. The noise can stay. I just want to remove the offset. Its not a constant offset across (that would be easy) but there's a tilt in the image baseline. – nolimits Oct 06 '20 at 04:22
  • Oh, just noticed your comment. I'll try to accommodate the tilt and edit my answer. – MichaelTr7 Oct 06 '20 at 04:35
  • @nolimits A few methods I can propose are filtering out the noise first, thresholding the image or doing a sliding/moving window average to estimate the amount of offset to remove for each point (pixel). I'm curious how involved of a method you're looking for. – MichaelTr7 Oct 06 '20 at 04:43
  • Thanks @MichaelTr7 I already applied medfilt3(). Thats fine. – nolimits Oct 06 '20 at 05:14

1 Answers1

1

Note: Another method to attempt may include moving/windowing averages to calculate the local amount to offset by.

Method 1: Discrete Cosine Transform (DC Offset Removal)

Converts the image into the frequency domain uses the Discrete Fourier Transform (DCT). Removes the DC coefficient in the top-left corner (set to zero) of the matrix and convert it back to the spatial domain using the Inverse Discrete Fourier Transform (IDCT).

DC Removal 1

Image = imread("Test_Image.jpg");

%Converting image to greyscale if RGB image%
[Image_Height,Image_Width,Depth] = size(Image);
if(Depth == 3)
Image = rgb2gray(Image);
end

%Removing image offset%    
Discrete_Cosine_Transformed_Image = dct2(Image);
Discrete_Cosine_Transformed_Image(1,1) = 0;
Inverse_Discrete_Cosine_Transformed_Image = idct2(Discrete_Cosine_Transformed_Image);

Calibrated_Image = medfilt2(Inverse_Discrete_Cosine_Transformed_Image,[20 20]);

% Plotting the original and thresholded image%
X_Axes = (1:1:Image_Height);
Y_Axes = (1:1:Image_Width);

subplot(1,2,1); surf(X_Axes,Y_Axes,Image,'EdgeColor','none');
title("Original Image Plot");
xlabel('X-Axis'); ylabel('Y-Label');
zlim([0 255]);

subplot(1,2,2); surf(X_Axes,Y_Axes,uint8(Calibrated_Image),'EdgeColor','none');
title("Calibrated Image Plot");
xlabel('X-Axis'); ylabel('Y-Label');
zlim([0 255]);

Key Discrete Cosine Transform (DCT) Filtering Code Lines

%Removing image offset%    
Discrete_Cosine_Transformed_Image = dct2(Image);
Discrete_Cosine_Transformed_Image(1,1) = 0;
Inverse_Discrete_Cosine_Transformed_Image = idct2(Discrete_Cosine_Transformed_Image)

Method 2: Standard Uniform Offset (no-tilt accommodation)

Uses a constant value and subtracts that across the whole image matrix.

Test Image 1: Using Lowest Intensity to Calculate Offset

Offset Removal Image 1

Test Image 2: Using Average/Mean to Calculate Offset

Offset Removal Image 2

Image = imread("Circular_Image.png");

%Converting image to greyscale if RGB image%
[Image_Height,Image_Width,Depth] = size(Image);
if(Depth == 3)
Image = rgb2gray(Image);
end

   %Removing image offset%
Lowest_Intensity_Value = min(Image,[],'all');
Average = mean(Image,'all');
Calibrated_Image = Image - Average;

% Plotting the original and thresholded image%
X_Axes = (1:1:Image_Height);
Y_Axes = (1:1:Image_Width);

subplot(1,2,1); surf(X_Axes,Y_Axes,Image,'EdgeColor','none');
title("Original Image Plot");
xlabel('X-Axis'); ylabel('Y-Label');
zlim([0 255]);

subplot(1,2,2); surf(X_Axes,Y_Axes,Calibrated_Image,'EdgeColor','none');
title("Calibrated Image Plot");
xlabel('X-Axis'); ylabel('Y-Label');
zlim([0 255]);

Using MATLAB version: R2019b

MichaelTr7
  • 4,737
  • 2
  • 6
  • 21
  • Oh well. I was after a simple function removing the offset/baseline, even as linear regression. Super simple with line graphs but I cant get it done with 2D. – nolimits Oct 06 '20 at 05:31
  • @nolimits Feel the same way on this one. I'll let you know if I find anything more concise and simple. – MichaelTr7 Oct 06 '20 at 05:33
  • I tried with a loop that slices each X, Y and does a baseline subtraction matrix but didn't really work. % for x=1:1201 % for y=1:1201 % Ibasetest = polyfit(x,y,1); % end % end % – nolimits Oct 06 '20 at 05:39
  • That's an interesting idea I'll have to look into. – MichaelTr7 Oct 06 '20 at 05:46
  • I thought it gives a linear baseline for each X line along the Y direction. Basically Y = 1200 graphs having a line. Then this should be done for each Y in X direction, right? Then subtracting this from the actual image. – nolimits Oct 06 '20 at 06:06
  • I forgot to mention this should be a simple method because I am processing 100 per second and more complex code is bad^^ – nolimits Oct 06 '20 at 06:07
  • That logic is correct. Hmm, not sure how well it would work. Might have a try myself. The biggest bottleneck I found is median filtering. The DCT portion is decently quick and 3 lines of code if that suffices. – MichaelTr7 Oct 06 '20 at 06:16
  • Another idea: Smooth out entire plane across 300 pixel and use resulting 'image' as baseline? – nolimits Oct 06 '20 at 06:35
  • That may work. But the speed may suffer depending on the smoothing method used. – MichaelTr7 Oct 06 '20 at 06:44