Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%
- %% Returns RGB pixel values from a flat raw image buffer. This routine handles
- %% all CFA pattern types but is typically used for Fuji x-trans since bayer
- %% images can be handled with much simpler logic
- %%
- %% Parameters:
- %%
- %% cfaPatternDimensions - 1D array describing [#rows, #cols] in the CFA pattern.
- %% For fuji this would be [6 6]
- %%
- %% cfaPatternExif - 1D array containing CFA pattern from EXIF data. This is a flat
- %% array - this routine will use 'cfaPatternDimensions' to determine the
- %% pixel columns and rows of the CFA pattern. For example, the Fuji x-trans pattern
- %% 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],
- %% which will become the following when passed with a cfaPatternDimensions of [6 6]:
- %%
- %% COL: 0 1 2 3 4 5
- %% ROW 0: 0 2 1 2 0 1
- %% ROW 1: 1 1 0 1 1 2
- %% ROW 2: 1 1 2 1 1 0
- %% ROW 3: 2 0 1 0 2 1
- %% ROW 4: 1 1 2 1 1 0
- %% ROW 5: 1 1 0 1 1 2
- %%
- %% Which corresponds to the following colors (red=0, green=1, blue=2 values above):
- %%
- %% COL: 0 1 2 3 4 5
- %% ROW 0: R B G B R G
- %% ROW 1: G G R G G B
- %% ROW 2: G G B G G R
- %% ROW 3: B R G R B G
- %% ROW 4: G G B G G R
- %% ROW 5: G G R G G B
- %%
- %% imgData - Image data containing pixel values, the dimensions of which must
- %% be even multiples of cfaPatternDimensions
- %%
- %% Returns three 1D arrays, corresponding to the red, green, and blue pixel
- %% values
- %%
- %% Sample logic for calling this function. The TIFF file should be generated
- %% from LibRaw's unprocessed_raw.exe with the -T option
- %%
- %% imgData = imread('rawdata.tiff'); %% read file created by unprocessed_raw.exe
- %% imgData = imgData(:, 1:6032); %% trim to visible size, reported by unprocessed_raw.exe as "Image size"
- %% [redPixels, greenPixels, bluePixels] = rawToRGB([6 6], %% CFA pattern dimensions of 6x6
- %% [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
- %% imgData);
- %% printf("std: %.2f %.2f %.2f\n", std(redPixels), std(greenPixels), std(bluePixels)); %% print stdev
- %% printf("avg: %.2f %.2f %.2f\n", mean(redPixels), mean(greenPixels), mean(bluePixels)); %% print average
- %%
- function [redPixels, greenPixels, bluePixels] = rawToRGB(cfaPatternDimensions, cfaPatternExif, imgData)
- %%
- %% Returns array with zero-based pixel-column indexes for specified CFA
- %% color on the specified CFA pattern row.
- %%
- function colIndexesInRow = getPatternIndexes(row, cfaColor)
- colIndexesInRow = find(cfaPatternExif(1 + row*cfaPatternRows : (row+1)*cfaPatternRows) == cfaColor) - 1; %% -1 to make zero-based
- end
- CFA_RED = 0;
- CFA_GREEN = 1;
- CFA_BLUE = 2;
- %%
- %% build array of pixel indexes for each color on each of the rows
- %% in the CFA pattern
- %%
- cfaPatternRows = cfaPatternDimensions(1);
- cfaPatternCols = cfaPatternDimensions(2);
- for row=0:cfaPatternRows-1
- redPixelColIndexesForRow{row+1} = getPatternIndexes(row, CFA_RED);
- greenPixelColIndexesForRow{row+1} = getPatternIndexes(row, CFA_GREEN);
- bluePixelColIndexesForRow{row+1} = getPatternIndexes(row, CFA_BLUE);
- end
- %%
- %% get RGB pixel values. Each iteration of the following loop handles all the
- %% rows corresponding to a CFA pattern rows. We're not looping for every
- %% every row in the image data - each iteration culls all the rows
- %% for a given CFA pattern row by utilizing vector ranges. We store the data
- %% for each color in a cell array - we can't use vectors because the number
- %% of pixel values will vary from row to row (vectors require equal dimensions)
- %%
- for row=0:cfaPatternRows-1
- red_rows{row+1} = imgData(row+1:cfaPatternRows:end, ismember(mod(0:end-1, cfaPatternRows), redPixelColIndexesForRow{row+1}));
- green_rows{row+1} = imgData(row+1:cfaPatternRows:end, ismember(mod(0:end-1, cfaPatternRows), greenPixelColIndexesForRow{row+1}));
- blue_rows{row+1} = imgData(row+1:cfaPatternRows:end, ismember(mod(0:end-1, cfaPatternRows), bluePixelColIndexesForRow{row+1}));
- end
- %% aggregate the RGB pixel value data from the cell array into 1D flat arrays
- redPixels = [red_rows{:}](:);
- greenPixels = [green_rows{:}](:);
- bluePixels = [blue_rows{:}](:);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement