In the Image Processing Toolbox 3.2 (R13), why does IMHIST error out when called with RGB images?
15 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Edited: Image Analyst
on 28 Nov 2021
The following code will reproduce this problem:
rgb = imread('flowers.tif');
imhist(rgb)
??? Error using ==> checkinput (check_attributes)
Function imhist expected its first input argument, I or X,
to be two-dimensional.
Error in ==> D:\Applications\MATLAB6p5\toolbox\images\images\checkinput.m
On line 37 ==> check_attributes(A, attributes, function_name, variable_name, ...
Error in ==> D:\Applications\MATLAB6p5\toolbox\images\images\imhist.m (parse_inputs)
On line 173 ==> checkinput(a, 'double uint8 logical uint16', '2d', mfilename, 'I or X', 1);
Error in ==> D:\Applications\MATLAB6p5\toolbox\images\images\imhist.m
On line 49 ==> [a, n, isScaled, top, map] = parse_inputs(varargin{:});
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
This is the expected behavior of IMHIST. According to the documentation, IMHIST was never meant to accept RGB images. However, in versions of the Image Processing Toolbox prior to 3.2 (R13), IMHIST would follow through execution without erroring out. The results would most likely not be the desired results. IMHIST R13 now errors out so that you will not receive erroneous results.
In order to fix the problem, you will need to read the documentation on IMHIST and understand the image types that it supports. You will basically need to convert your RGB image into a grayscale or indexed image.
For a simple example, you can use the following code as a guide:
% Read in RGB image
rgb = imread('flowers.tif');
% Convert RGB to grayscale
gray = rgb2gray(rgb);
% Now call IMHIST
figure;
imhist(gray)
1 Comment
Image Analyst
on 28 Nov 2021
Edited: Image Analyst
on 28 Nov 2021
OK, but you could easily determine what kind of image was sent into imhist() and, if it's a color image, return three histogram vectors (one for each color channel) instead of erroring out. That would be the most user friendly thing to do in my opinion. Basically internally do
grayCounts = imhist(rgb(:, :, 1)); % works for gray scale or color images.
if ndims(rgb) == 3
greencounts = imhist(rgb(:, :, 2);
blueCounts = imhist(rgb(:, :, 3);
end
More Answers (0)
See Also
Categories
Find more on Explore and Edit Images with Image Viewer App in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!