horshack

rawToRGB.m

Mar 30th, 2021
462
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. %%
  3. %% Returns RGB pixel values from a flat raw image buffer. This routine handles
  4. %% all CFA pattern types but is typically used for Fuji x-trans since bayer
  5. %% images can be handled with much simpler logic
  6. %%
  7. %% Parameters:
  8. %%
  9. %% cfaPatternDimensions - 1D array describing [#rows, #cols] in the CFA pattern.
  10. %% For fuji this would be [6 6]
  11. %%
  12. %% cfaPatternExif - 1D array containing CFA pattern from EXIF data. This is a flat
  13. %% array - this routine will use 'cfaPatternDimensions' to determine the
  14. %% pixel columns and rows of the CFA pattern. For example, the Fuji x-trans pattern
  15. %% is [0 2 1 2 0 1 1 1 0 1 1 2 1 1 2 1 1 0 2 0 1 0 2 1 1 1 2 1 1 0 1 1 0 1 1 2],
  16. %% which will become the following when passed with a cfaPatternDimensions of [6 6]:
  17. %%
  18. %%   COL: 0 1 2 3 4 5
  19. %% ROW 0: 0 2 1 2 0 1
  20. %% ROW 1: 1 1 0 1 1 2
  21. %% ROW 2: 1 1 2 1 1 0
  22. %% ROW 3: 2 0 1 0 2 1
  23. %% ROW 4: 1 1 2 1 1 0
  24. %% ROW 5: 1 1 0 1 1 2
  25. %%
  26. %% Which corresponds to the following colors (red=0, green=1, blue=2 values above):
  27. %%
  28. %%   COL: 0 1 2 3 4 5
  29. %% ROW 0: R B G B R G
  30. %% ROW 1: G G R G G B
  31. %% ROW 2: G G B G G R
  32. %% ROW 3: B R G R B G
  33. %% ROW 4: G G B G G R
  34. %% ROW 5: G G R G G B
  35. %%
  36. %% imgData - Image data containing pixel values, the dimensions of which must
  37. %% be even multiples of cfaPatternDimensions
  38. %%
  39. %% Returns three 1D arrays, corresponding to the red, green, and blue pixel
  40. %% values
  41. %%
  42. %% Sample logic for calling this function. The TIFF file should be generated
  43. %% from LibRaw's unprocessed_raw.exe with the -T option
  44. %%
  45. %% imgData = imread('rawdata.tiff'); %% read file created by unprocessed_raw.exe
  46. %% imgData = imgData(:, 1:6032); %% trim to visible size, reported by unprocessed_raw.exe as "Image size"
  47. %% [redPixels, greenPixels, bluePixels] = rawToRGB([6 6], %% CFA pattern dimensions of 6x6
  48. %%    [0 2 1 2 0 1 1 1 0 1 1 2 1 1 2 1 1 0 2 0 1 0 2 1 1 1 2 1 1 0 1 1 0 1 1 2], %% CFA pattern as flat array
  49. %%    imgData);
  50. %%  printf("std: %.2f %.2f %.2f\n", std(redPixels), std(greenPixels), std(bluePixels)); %% print stdev
  51. %%  printf("avg: %.2f %.2f %.2f\n", mean(redPixels), mean(greenPixels), mean(bluePixels)); %% print average
  52. %%
  53. function [redPixels, greenPixels, bluePixels] = rawToRGB(cfaPatternDimensions, cfaPatternExif, imgData)
  54.  
  55.   %%
  56.   %% Returns array with zero-based pixel-column indexes for specified CFA
  57.   %% color on the specified CFA pattern row.
  58.   %%
  59.   function colIndexesInRow = getPatternIndexes(row, cfaColor)
  60.     colIndexesInRow = find(cfaPatternExif(1 + row*cfaPatternRows : (row+1)*cfaPatternRows)  == cfaColor) - 1; %% -1 to make zero-based
  61.   end
  62.  
  63.   CFA_RED = 0;
  64.   CFA_GREEN = 1;
  65.   CFA_BLUE = 2;
  66.  
  67.   %%
  68.   %% build array of pixel indexes for each color on each of the rows
  69.   %% in the CFA pattern
  70.   %%
  71.   cfaPatternRows = cfaPatternDimensions(1);
  72.   cfaPatternCols = cfaPatternDimensions(2);
  73.   for row=0:cfaPatternRows-1
  74.     redPixelColIndexesForRow{row+1} = getPatternIndexes(row, CFA_RED);
  75.     greenPixelColIndexesForRow{row+1} = getPatternIndexes(row, CFA_GREEN);
  76.     bluePixelColIndexesForRow{row+1} = getPatternIndexes(row, CFA_BLUE);
  77.   end
  78.  
  79.   %%
  80.   %% get RGB pixel values. Each iteration of the following loop handles all the
  81.   %% rows corresponding to a CFA pattern rows. We're not looping for every
  82.   %% every row in the image data - each iteration culls all the rows
  83.   %% for a given CFA pattern row by utilizing vector ranges. We store the data
  84.   %% for each color in a cell array - we can't use vectors because the number
  85.   %% of pixel values will vary from row to row (vectors require equal dimensions)
  86.   %%
  87.   for row=0:cfaPatternRows-1
  88.     red_rows{row+1}   = imgData(row+1:cfaPatternRows:end, ismember(mod(0:end-1, cfaPatternRows), redPixelColIndexesForRow{row+1}));
  89.     green_rows{row+1} = imgData(row+1:cfaPatternRows:end, ismember(mod(0:end-1, cfaPatternRows), greenPixelColIndexesForRow{row+1}));
  90.     blue_rows{row+1}  = imgData(row+1:cfaPatternRows:end, ismember(mod(0:end-1, cfaPatternRows), bluePixelColIndexesForRow{row+1}));
  91.   end
  92.  
  93.   %% aggregate the RGB pixel value data from the cell array into 1D flat arrays
  94.   redPixels = [red_rows{:}](:);
  95.   greenPixels = [green_rows{:}](:);
  96.   bluePixels  = [blue_rows{:}](:);
  97. end
  98.  
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.

×