horshack

applyRawFlatFrame.m

Apr 8th, 2021
373
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %%
  2. %% Applies a raw flat frame to a raw image using zero blur, which allows the
  3. %% removal of sensor dust spots (and also vignetting correction) at the expense
  4. %% of introducing some noise from the flat frame
  5. %%
  6. %% Parameters:
  7. %%
  8. %% imgFilename        - Filename of image to correct, generated via LibRaw's unprocessed_raw.exe
  9. %% flatFieldFilename  - Filename of flat-field image, generated via LibRaw's unprocessed_raw.exe
  10. %% outputFilename     - Filename to hold output bayer PGM, which caller needs to convert toc
  11. %%                      raw via pgm2dng.exe
  12. %% imgDimensions      - Dimensions of image to use. Example: [4024 6018] for 6018x4024
  13. %%
  14. %% Output:
  15. %%
  16. %% PGM bayer-interlaced file, corrected for sensor dust via flat-frame
  17. %%
  18. function applyRawFlatFrame(imgFilename, flatFieldFilename, outputFilename, imgDimensions)
  19.  
  20.   cfaPatternStr = "RGGB";
  21.  
  22.   function imgData = loadImgData(filename)
  23.     imgData = double(imread(filename));
  24.     imgData = resize(imgData, imgDimensions);
  25.   end
  26.  
  27.   function avg = calcChannelCenterAverage(channelData)
  28.     avg = mean(channelData(floor(end/2-128):floor(end/2+128), floor(end/2-128):floor(end/2+128))(:));
  29.   end
  30.  
  31.   %% read target and flat-field raw data generated by LibRaw's unprocessed_raw.exe
  32.   imgData = loadImgData(imgFilename, imgDimensions);
  33.   imgDataFlatField = loadImgData(flatFieldFilename, imgDimensions);
  34.  
  35.   %% split the raw data into its separate RGGB channels
  36.   [r, g1, g2, b] = rawBayerToChannels(cfaPatternStr, imgData);
  37.   [rFlat, g1Flat, g2Flat, bFlat] = rawBayerToChannels(cfaPatternStr, imgDataFlatField);
  38.  
  39.   %% generate flat-field divisor tables for each color channel
  40.   rDivisors  = rFlat  ./ calcChannelCenterAverage(r);
  41.   g1Divisors = g1Flat ./ calcChannelCenterAverage(g1);
  42.   g2Divisors = g2Flat ./ calcChannelCenterAverage(g2);
  43.   bDivisors  = bFlat  ./ calcChannelCenterAverage(b);
  44.  
  45.   %% apply flat-field divisors to image data
  46.   r  = r  ./ rDivisors;
  47.   g1 = g1 ./ g1Divisors;
  48.   g2 = g2 ./ g2Divisors;
  49.   b  = b  ./ bDivisors;
  50.  
  51.   %% put the modified channels back into a bayer pattern
  52.   imgDataOut = channelsToRawBayer(cfaPatternStr, r, g1, g2, b);
  53.  
  54.   %% write a PGM with the new data
  55.   imwrite(uint16(imgDataOut), outputFilename);
  56. end
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×