# How to flatten this OCT image with respect to the marked line

I am working on a medical image processing project where I have to use retinal OCT images. I am using the dataset available here. This dataset has a lot of images and I want to flatten all of them. I have a flattening code which works correctly only for some images but fails as a generalised solution to all the provided images:

For instance, if the input image is:

The output after flattening is:

The code that I had used for performing the flattening process is:

function img_shift = img_flatten(img)

% Find bottom line indices
%find the location of the curve which is white/gray in color different
%from the black
threshold = 100;
retina = NaN(1, size(img,2));
for j = 1:size(img,2)
retina(j) = find(img(:,j) >= threshold, 1, 'last');
end

% Fit polynomial
x = 1:length(retina);
p = polyfit(x, retina, 2);
retina_fit = round(polyval(p, x));

% Plot the fit of the retina
%figure(1);
%plot([retina', retina_fit']);

% Shift the data in the image
img_shift = zeros(size(img), 'uint8');

for j = 1:length(retina_fit)
img_shift(:,j) = circshift(img(:,j), -retina_fit(j));
end%for j

% Plot the image
%figure(2);
% subplot(2,1,1);
%imshow(img,[]);
%subplot(2,1,2);
imshow(img_shift);
end


I tried changing the threshold but it didn't make any difference to the code.

I have the following questions regarding my code:

1) Is there any other better method to perform flattening of OCT images?

2) Can I tweak my code to have a generalized flattening code for all the images belonging to the dataset?