Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Scott's photo enhancer Matlab code to Python #1

Open
pierre-tenedero-aims opened this issue Feb 24, 2023 · 0 comments
Open

Port Scott's photo enhancer Matlab code to Python #1

pierre-tenedero-aims opened this issue Feb 24, 2023 · 0 comments
Assignees

Comments

@pierre-tenedero-aims
Copy link
Collaborator

Write python code to do something similar to the Matlab code below.
Work with Scott Bainbridge to discuss the goals and results.
The Matlab code is too slow. Ideally we want to process at least 4 photos per second.
Test using photos from
\pearl\techdev\field_data\Trip8004_CCIPOdysseyWhitsundaysDec2022\ReefScanTransomData

% input root directory with the images 

inputPath='E:\whitsunday_cots_presentation\COTS';

% actual files to read (just jpegs)
inputfiles=dir('E:\whitsunday_cots_presentation\COTS\*.jpg');

%output path
outputFiles='E:\whitsunday_cots_presentation\COTS';


% read all files 
for k=1:length(inputfiles)
    fn=fullfile(inputfiles(k).folder, inputfiles(k).name);

    [folder, baseFileNameNoExt, extension] = fileparts(fn);

    % create the output directory if it doesn't exist

    newFilePath = (strcat(outputFiles,extractAfter(folder,inputPath)));

    if ~exist(newFilePath, 'dir')
       mkdir(newFilePath)
    end

    % get the bits of the filke name to create the output file name, this
    % bit needs cleaning up
    adaptHistName = strcat(newFilePath,'\',baseFileNameNoExt, '_enh.jpg');
    disp(adaptHistName)
    oName = adaptHistName;
    outputFileName = fullfile(adaptHistName);
    baseFileName = inputfiles(k).name;
    inputFileName = fullfile(inputfiles(k).folder, baseFileName);
    iName = strcat(inputfiles(k).folder,'\',baseFileName);

    % check if the output file exists
    if isfile(outputFileName)

        fprintf(1, 'Skipping: File already exists - %s\n', outputFileName);

    else

        % if not then read the exif data from the input file to get the
        % altitude
        fprintf(1, 'Now reading %s\n', fn);

        % get the exif altitude data from the file if prtesent

        try
            exifInfo = imfinfo(fn);
            altitude = (exifInfo.GPSInfo.GPSAltitude);
            if altitude > 20
                altitude = altitude / 10.0;
            end
            disp(altitude);
        catch

            % if not set to 6m as an average
            altitude = 6.0;
        end

        % colour balance and altitude equation done empirically - this is
        % what we will need to re-do in Python

        bRedClipLimit = -0.0000011296 * altitude^4 + 0.0000575441 * altitude^3 - 0.0009774864 * altitude^2 + 0.0056842405 * altitude - 0.0017444152;
        bGreenClipLimit = 0.0000038958 * altitude^3 - 0.0001131430 * altitude^2 + 0.0004288439 * altitude + 0.0064228875;
        bBlueClipLimit = 0.0000050696 * altitude^3 - 0.0001263203 * altitude^2 + 0.0005117638 * altitude + 0.0049041834;


        beta = 0.0001931321* altitude^3 - 0.0071039255* altitude^2 + 0.0850709324* altitude;

        imageFile = imread(fn);



        % extract the colour channels
        redChannel = imageFile(:,:,1); % Red channel
        greenChannel = imageFile(:,:,2); % Green channel
        blueChannel = imageFile(:,:,3); % Blue channel

        % remove noise from each channel
        redChannel_denoise = wiener2(redChannel,[6 6]);
        greenChannel_denoise = wiener2(greenChannel,[5 5]);
        blueChannel_denoise = wiener2(blueChannel,[4 4]);

        % do an adaptive histogram stretch using the clip limits from above
        adaptImageRed = adapthisteq(redChannel_denoise,'NumTiles',[8, 6], ClipLimit=bRedClipLimit, Range="full", NBins=2048, Distribution='uniform');
        adaptImageGreen = adapthisteq(greenChannel_denoise,'NumTiles',[8, 6], ClipLimit=bGreenClipLimit, Range="full", NBins=2048, Distribution='uniform');
        adaptImageBlue = adapthisteq(blueChannel_denoise,'NumTiles',[8, 6], ClipLimit=bBlueClipLimit, Range="full", NBins=2048, Distribution='uniform');

        % merge the channnel back into an image
        adaptImage = cat(3, adaptImageRed, adaptImageGreen, adaptImageBlue);

        % sharpen the image
        sharpImage = imsharpen(adaptImage,'Radius',2,'Amount',1.2);

        % dehaze the image - use the altoitude to de-noise more for deeper
        % images
        dehaze_amount = altitude / 120;

        AInv = imcomplement(sharpImage);
        BInv = imreducehaze(AInv, dehaze_amount, 'Method','approxdcp', 'ContrastEnhancement','none');
        deHazeImage = imcomplement(BInv);
        
        % optinally brighten the final image as the de-hazing can make it
        % darker

        finalImage = imlocalbrighten(deHazeImage,beta);

        % write the EXIF dat from the opriginal to the new file
        % in this case I am just using the non-brightenede image as this
        % was not working that well...

        status = putexif(deHazeImage, oName, iName);

        % so final image is the deHazeImage image



@pierre-tenedero-aims pierre-tenedero-aims self-assigned this Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant