Clear Filters
Clear Filters

imadjust returns error only supported for 2d-grayscal images

24 views (last 30 days)
Hi all, I am new to Matlab,
I have tried this over and over again, I am not sure what it means and it seems challenging.
Can someone pelase guide me through the imadjust command I am new to Matlab?
birds = imread('bird.jpg');
gbird = rgb2gray(birds);
bwbird = imbinarize(gbird);
%birds_imadjust = imadjust(birds);
birds_histeq = histeq(birds);
birds_adapthisteq = adapthisteq(birds);
This is my code. I am not sure if I downloaded the wrong image to use with it, but this is for a professional to decompose.
I appreciate you for taking the time to respond in advance! Thank you!

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jun 2019
.jpg images are almost always color images. Your code acknowledges that when it does the
gbird = rgb2gray(birds);
In your comment
%birds_imadjust = imadjust(birds);
you attempt to apply imadjust to the color image, not to the grayscale version of the image. imadjust is not designed to work on color images and gives you an error message when you try.
To be consistent with your other calls you would use imadjust(gbird)
Matpar on 17 Jun 2019
Ahhh! I see, I will try that to see if that was my error. Thanks a mill! Will stay in touch as a newbie I'm sure there will be more questions! Thanx once more
DGM on 26 Oct 2022
Edited: DGM on 26 Oct 2022
Imadjust() will work on color images, but for sake of inconsistency, it throws an error if you use the implicit inrange syntax when doing so.
When called with only one argument
B = imadjust(A);
Imadjust() internally calls stretchlim() to find a nominal set of input levels. It's equivalent to:
B = imadjust(A,stretchlim(A));
... but if A is not a 2D array, it just dumps an error instead.
You can always just call stretchlim() explicitly to avoid the error.
A = imread('peppers.png');
B = imadjust(A,stretchlim(A));
imshow([A B])
Though bear in mind that since the input levels are calculated independently per channel, you're likely to cause some shift in color balance. Is that the reason why imadjust() excludes this functionality? Consider that stretchlim() maintains said functionality and exists for the purpose of serving imadjust(). Make of that what you will.
You're always free to come up with input/output levels some other way. There are also other ways to do levels/contrast adjustment on color images.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 Jun 2019
imadjust() finds the tails of the histogram - the 1% points at each end of the histogram. Then it linearly maps those points to the min and max (typically 0 and 255). This has the effect of increasing the contrast of the image.
histeq() does a non-linear stretch according to the shape of the histogram. It is rarely, if ever, useful because it produces unnatural looking images that are not helpful at all. You can ignore it.
adapthisteq() does a linear histogram stretch within a moving window. This is useful for doing a background correction in cases where your objects of interest (foreground) are sitting on top of a background that changes over a large scale.
imbinarize() does a thresholding operation to binarize your image into foreground and background. It may or may not do a good job depending on your image. There are a variety of algorithms you can use to determine the threshold and the default Otsu method may or may not be a good one for your particular images.
Matpar on 16 Jun 2019
Edited: Matpar on 16 Jun 2019
Thanks for the answer and acknowledging me however, I've gathered the understanding of what they mean. What I am lacking is the understanding of my code and why it is not doing what it should as that of the tyre example! The task is the same referencing the example of the tyre in MATLABS enhancing the contrast of images. How is it that the tyre example is functional and my code produces the error? That conundrum is what I'm hoping to resolve! I apologise if the phrasing of my request was indifferent! Thanks in advance
Image Analyst
Image Analyst on 16 Jun 2019
Since they want the gray scale version of your image, use gbird rather than birds:
birds_histeq = histeq(gbird);
birds_adapthisteq = adapthisteq(gbird);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!