MATLAB contains demosaic() function to convert Bayer pattern encoded image to true color, does it contain opposite function as well?

34 ビュー (過去 30 日間)
I am writing a VHDL entity that processes Bayer pattern image to generate a true color image. It would make things easier if MATLAB has a method to generate Bayer pattern encoded image from a given RGB image. I can then use it inside my testbench. How can this be achieved?

回答 (2 件)

Image Analyst
Image Analyst 2017 年 8 月 15 日
No - it would be in the "See also" section if there were. However, you can easily make one simply by interleaving your data in the required pattern.
% Read in sample image.
rgbImage = imread('peppers.png');
% Display the image.
subplot(2,1,1);
imshow(rgbImage);
title('Original RGB image', 'FontSize', 20);
% Create mosaiced image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
mosaicedImage = zeros(rows, columns, 'uint8');
for col = 1 : columns
for row = 1 : rows
if mod(col, 2) == 0 && mod(row, 2) == 0
% Pick red value.
mosaicedImage(row, col) = rgbImage(row, col, 1);
elseif mod(col, 2) == 0 && mod(row, 2) == 1
% Pick green value.
mosaicedImage(row, col) = rgbImage(row, col, 2);
elseif mod(col, 2) == 1 && mod(row, 2) == 0
% Pick green value.
mosaicedImage(row, col) = rgbImage(row, col, 2);
elseif mod(col, 2) == 1 && mod(row, 2) == 1
% Pick blue value.
mosaicedImage(row, col) = rgbImage(row, col, 3);
end
end
end
subplot(2,1,2);
imshow(mosaicedImage);
title('Mosaiced image', 'FontSize', 20);
  1 件のコメント
Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 8 日
編集済み: Setsuna Yuuki. 2020 年 11 月 8 日
Muchas gracias por la ayuda, me sirvió mucho esta respuesta :D

サインインしてコメントする。


Joseph Majdi
Joseph Majdi 2024 年 2 月 27 日
It's probably easiest to use index notation to "subsample" the RGB in every 2nd by 2nd pixel into that sub-color, ie:
originalRGB = imread('peppers.png');
pepperMosaic = zeros(size(originalRGB,[1 2]),'uint8');
pepperMosaic(1:2:end,1:2:end) = originalRGB(1:2:end,1:2:end,2);%Green Pixels (1 of 2)
pepperMosaic(2:2:end,2:2:end) = originalRGB(2:2:end,2:2:end,2);%Green Pixels (2 of 2)
pepperMosaic(2:2:end,1:2:end) = originalRGB(2:2:end,1:2:end,1);%Red Pixels
pepperMosaic(1:2:end,2:2:end) = originalRGB(1:2:end,2:2:end,3);%Blue Pixels
figure; imagesc(originalRGB); title('Original Peppers'); axis equal tight; colormap gray
figure; imagesc(pepperMosaic); title('Peppers Bayer GBRG'); axis equal tight; colormap gray
figure; imagesc(demosaic(pepperMosaic,'gbrg')); title('Reconstructed Peppers from Mosaic'); axis equal tight;
This assumes you want the GBRG mosaic pattern, and you can adjust the indices for GRBG, BGGR, or RGGB (the other bayer formats matlab recognizes in the demosaic function).

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by