2

I'm trying to retrieve the luminance component of a set of 'tif' images in matlab. The code is bellow:

function [defaultImages] = readImgLum(nFiles) 
% readImgLum reads a specified number of images in 'tif' format
% and retrieves the luminance component
narginchk(1, 1);
defaultImages = cell(nFiles, 1);                % store all images in a vector

for i = 1 : nFiles
    imagePreffix = int2str(i);
    imageFullName = strcat(imagePreffix, '.tif');
    image = imread(imageFullName);
    imageYCbCr = rgb2ycbcr(image);    
    defaultImages{i} = squeeze(imageYCbCr(:,:,1));
end

Am I correctly extracting the luminance component?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Sebi
  • 4,262
  • 13
  • 60
  • 116

1 Answers1

3

As the comments have stated, there is no need for squeeze. This code also looks fine to me. However, if you want to skip computing all components of YCbCr just to extract the luminance, use the SMPTE / PAL standard for calculating luminance instead. This is actually done in rgb2gray in MATLAB if you want to look up the source.

In any case, assuming your images are unsigned 8-bit integer:

image = double(image); 
defaultImages{i} = uint8(0.299*image(:,:,1) + 0.587*image(:,:,2) + 0.114*image(:,:,3));

BTW, image is a built-in command in MATLAB. It takes in any matrix, and visualizes it as an image in a new figure. I highly suggest you use another variable to store your temporary image as you may be calling further code later on that requires image as a function.

rayryeng
  • 102,964
  • 22
  • 184
  • 193