function xc = bayer2rgb(xb, M, N, method) %BAYER2RGB Summary of this function goes here % Detailed explanation goes here % Initializes the struct that's going to store the RGB image xc = struct; xc.red = zeros(M, N); xc.green = zeros(M, N); xc.blue = zeros(M, N); % Initializes helper variables bayerPatternDimY = size(xb, 1); bayerPatternDimX = size(xb, 2); % Calculates the distance between two grid points for both axes gridPointsStepLengthY = (bayerPatternDimY - 1) / (M - 1); gridPointsStepLengthX = (bayerPatternDimX - 1) / (N - 1); % Calculates the coordinates of the grid points for both axes gridPointsCoordinatesY = 1:gridPointsStepLengthY:bayerPatternDimY; gridPointsCoordinatesX = 1:gridPointsStepLengthX:bayerPatternDimX; if (strcmp(method, 'nearest')) for currentRow = 1:M % Determines the index of the even row of the original image % that is closest to this ordinate (currentRow) of the new % image idx = mod(gridPointsCoordinatesY(currentRow), 2) >= 1; nearestEvenRow = floor(gridPointsCoordinatesY(currentRow)); nearestEvenRow(idx) = nearestEvenRow(idx)+1; if (nearestEvenRow > bayerPatternDimY) nearestEvenRow = nearestEvenRow - 2; end % Determines the index of the odd row of the original image % that is closest to this ordinate (currentRow) of the new % image idx = mod(gridPointsCoordinatesY(currentRow), 2) < 1; nearestOddRow = floor(gridPointsCoordinatesY(currentRow)); nearestOddRow(idx) = nearestOddRow(idx)+1; if (nearestOddRow > bayerPatternDimY) nearestOddRow = nearestOddRow - 2; end % Determines the index of the row (even or odd) of the original % image that is closest to this ordinate (currentRow) of the % new image totalNearestRow = round(gridPointsCoordinatesY(currentRow)); for currentCol = 1:N % Determines the index of the even column of the original % image that is closest to this abscissa (currentCol) of % the new image idx = mod(gridPointsCoordinatesX(currentCol), 2) >= 1; nearestEvenCol = floor(gridPointsCoordinatesX(currentCol)); nearestEvenCol(idx) = nearestEvenCol(idx)+1; if (nearestEvenCol > bayerPatternDimX) nearestEvenCol = nearestEvenCol - 2; end % Determines the index of the even column of the original % image that is closest to this abscissa (currentCol) of % the new image idx = mod(gridPointsCoordinatesX(currentCol), 2) < 1; nearestOddCol = floor(gridPointsCoordinatesX(currentCol)); nearestOddCol(idx) = nearestOddCol(idx)+1; if (nearestOddCol > bayerPatternDimX) nearestOddCol = nearestOddCol - 2; end % Determines the index of the column (even or odd) of the % original image that is closest to this abscissa % (currentCol) of the new image totalNearestCol = round(gridPointsCoordinatesX(currentCol)); if (mod(totalNearestRow, 2) == 0 && mod(totalNearestCol, 2) ~= 0) totalNearestCol = totalNearestCol + 1; elseif (mod(totalNearestRow, 2) ~= 0 && mod(totalNearestCol, 2) == 0) totalNearestCol = totalNearestCol - 1; end % Closest neighbors per colour can be determined by % observing the Bayer pattern xc.red(currentRow, currentCol) = xb(nearestOddRow, nearestEvenCol); xc.blue(currentRow, currentCol) = xb(nearestEvenRow, nearestOddCol); xc.green(currentRow, currentCol) = xb(totalNearestRow, totalNearestCol); end end elseif (strcmp(method, 'linear')) end % Combines colours to a single image array and shows the result rgbImage = cat(3, xc.blue, xc.green, xc.red); figure(); imshow(rgbImage); end