Info

This question is closed. Reopen it to edit or answer.

i am not getting the output of color image histogram whn i execute this program ?

1 view (last 30 days)
clc;
clear all;
close all;
I1=imread('gray.jpg','jpg');
figure(1);imshow(I1,[]);title('gray image');
%set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
%I=genpath;
%P=imread('I');
figure(2);imhist(I1);title('HISTOGRAM OF GRAY IMAGE');
%zpoint(I,ADJ );
I2=imread('idea.jpg','jpg');
figure(3);imshow(I2,[]);title('color image');
%figure
%set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
figure(4);imshow(I2);title('COLOUR IMAGE');
I2conv=rgb2gray(I2);
imhist(I2conv);title('HISTOGRAM OF COLOR IMAGE')
grayImage = imread('gray.jpg');
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image');
[pixelCounts bins] = imhist(grayImage, 256);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
subplot(2, 2, 3);
bar(bins, pixelCounts);
title('Original Histogram');
smoothedHist = medfilt1(pixelCounts, 3)
subplot(2, 2, 4);
bar(bins, smoothedHist);
title('Smoothed Histogram');
msgbox('Done!');

Answers (1)

Walter Roberson
Walter Roberson on 19 Jan 2016
Your code has
figure(4);imshow(I2);title('COLOUR IMAGE');
I2conv=rgb2gray(I2);
imhist(I2conv);title('HISTOGRAM OF COLOR IMAGE')
grayImage = imread('gray.jpg');
subplot(2, 2, 1);
Mentally step through this code. You create figure #4. You send imshow(I2) to graphics memory, but you do not make it visible to the user yet. You send the image histogram to graphics memory, but you do not make it visible to the user yet. The image histogram was in the same figure and axes as the imshow(I2) so the imshow(I2) graphics object will be thrown out before it has even been displayed, because by default when you plot something with a high-level call such as imhist(), whatever was already there is to be thrown away.
You still have not asked to make anything visible, but you proceed to subplot(2,2,1), still on figure 4. When you ask to subplot(), MATLAB looks for any existing plots that might be overlapped by the newly created axes. It is going to find the image histogram, which is on an axes that covers much of the figure 4. The plots that would be overlapped are deleted automatically, so the image histogram is discarded before it has been displayed.
imshow(grayImage, []);
title('Original Grayscale Image');
[pixelCounts bins] = imhist(grayImage, 256);
Now inside the 2,2,1 subplot, you imshow() into graphics memory, but you do not ask for it to be displayed yet. Then you imhist() in the same subplot, sending the image histogram to graphics memory, but you do not make it visible to the user yet. The image histogram was in the same figure and axes as the imshow(grayImage) so the imshow() graphics object will be thrown out before it has even been displayed, because by default when you plot something with a high-level call such as imhist(), whatever was already there before is to be thrown away.
After that you create subplot 2,2,3 and 2,2,4 (but not 2,2,2) without any further accidental overwriting of graphics objects.
Finally, you call msgbox(). That happens to need to create a new figure, and creating a new figure is one of the actions defined as triggering the actual rendering to the display of all graphics that are pending, so as part of creating the msgbox() drawing would be done.
If you did not happen to have coded msgbox() there, then your routine would end, with graphics potentially not drawn yet. The graphics would continue to remain undrawn until the code created a new figure or raised a figure to the front, or until pause() was called, or until drawnow() was called, or until waitfor() or uiwait() was called, or the keyboard to be read from (most typically because MATLAB has finished running everything and is ready to prompt the user for input.)
If you want to trigger immediate drawing of graphics, invoke drawnow() . But that will only lead to a flicker if something immediately replaces the graphics. Best is to not draw two different graphs to the same location before requesting user input, but it is also an option to create some graphics, pause() for a while so the user can see them, and then continue and draw something else.
  1 Comment
Image Analyst
Image Analyst on 19 Jan 2016
And of course the "histogram of color image" is not that. It's really the histogram of the gray scale version of the color image.

Tags

Community Treasure Hunt

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

Start Hunting!