Advertisement
Guest User

Untitled

a guest
Feb 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.81 KB | None | 0 0
  1. %%
  2. close all
  3. % Read image
  4. image = imread('toronto_original.png');
  5. [rows, columns, channels] = size(image);
  6.  
  7. figure('Name', 'Original');
  8. imshow(image)
  9.  
  10. for col = 1:columns
  11.     for row = 1:rows
  12.         if mod(row, 2) && mod(col, 2) %Both odd
  13.             % Blue
  14.             image(row,col,[1,2]) = 0;
  15.         elseif ~mod(row, 2) && ~mod(col, 2) %Both even
  16.             % Red
  17.             image(row,col,[2,3]) = 0;
  18.         else %One odd one even
  19.             % Green
  20.             image(row,col,[1,3]) = 0;          
  21.         end
  22.     end
  23. end
  24.  
  25. figure('Name', 'CFA')
  26. imshow(image)
  27.  
  28. %%
  29.  
  30. BIimage = zeros(rows, columns, channels, 'uint8');
  31.  
  32. for col = 1:columns
  33.     for row = 1:rows
  34.         % Accounts for image edges
  35.         rowMinus = max(row-1, 1);
  36.         rowPlus = min(row+1, rows);
  37.         colMinus = max(col-1, 1);
  38.         colPlus = min(col+1, columns);
  39.        
  40.         submatrix = image(rowMinus:rowPlus,colMinus:colPlus,:);
  41.        
  42.         if mod(row, 2) && mod(col, 2) %Both odd
  43.             % Blue
  44.             red = mean( nonzeros( submatrix(:,:,1) ));
  45.             green = mean( nonzeros( submatrix(:,:,2) ));
  46.             blue = image(row,col,3);
  47.            
  48.         elseif ~mod(row, 2) && ~mod(col, 2) %Both even
  49.             % Red
  50.             blue = mean( nonzeros( submatrix(:,:,3) ));
  51.             green = mean( nonzeros( submatrix(:,:,2) ));
  52.             red = image(row,col,1);
  53.            
  54.         else %One odd one even
  55.             % Green
  56.             red = mean( nonzeros( submatrix(:,:,1) ));
  57.             blue = mean( nonzeros( submatrix(:,:,3) ));
  58.             green = image(row,col,2);          
  59.         end
  60.         BIimage(row,col,1) = red;
  61.         BIimage(row,col,2) = green;
  62.         BIimage(row,col,3) = blue;
  63.     end
  64. end
  65.  
  66. figure('Name', 'Bilinear Interpolation')
  67. imshow(BIimage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement