Why am I getting an error when I use grayscale images with

Illustration
Simran_khurana - 2022-01-17T13:54:43+00:00
Question: Why am I getting an error when I use grayscale images with

Why am I getting an error when I use grayscale images with "makehdr"in Image Processing Toolbox 8.1 (R2012b)? I am receiving the following error when trying to use the "makehdr" utility.   low dynamic images must be RGB   I suspect the reason is that my images are black-and-white. Is there any workaround for this issue?  

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

"makehdr" works only on RGB images. The workaround for this issue is to convert the grayscale image to an RGB image.
 
Grayscale images are 2D (m x n) matrices, while RGB images are 3D (m x n x 3) matrices. Therefore one must expand the 2D matrix to fill the 3D matrix. This can be accomplished a couple ways
 
1. Use REPMAT to replicate the 2D matrix 3 times.
 
% Step 1 - Load grayscale image
imGray = imread('grayscaleImage.tif');

% Step 2 - Replicate image into RGB layers using REPMAT
nRV = 1; % Number Of Replications Vertically
nRH = 1; % Number Of Replications Horizontally
nRL = 3; % Number Of Replication Layers
imRGB  = repmat(imGray,[nRV, nRH, nRL]); % Replicate grayscale image 3 times

% Step 3 - Write resized image to file
imageName = 'rgbImage.tif';
imwrite(imRGB,imageName)

% Step 4 - Generate HDR from TIF File
imageNames = {imageName}; % MAKEHDR uses a cell array of strings as input.
RE = 1; % Currently a scalar, but would be a matrix for multiple images.
imHDR = makehdr(imageNames,'RelativeExposure',RE); % Create HDR image.

% Step 5 - Display image
imLDR = tonemap(imHDR); % Create LDR image from HDR image for display purposes.
figure; imshow(imLDR) % Display LDR image.

2. Use CAT to concatenate the 2D image 3 times

% Step 2 - Concatenate image into RGB layers using CAT
nRL = 3; % Number Of Replication Layers
imRGB = cat(nRL,grayImage,grayImage,grayImage);


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!