Matlab Image Baseline (Offset Removal) Correction

685 views Asked by At

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. :(

1

There are 1 answers

9
MichaelTr7 On

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