Using "imhist" function in Matlab to plot multiple histograms on the same figure

13,278

Solution 1

If you want both on the same axes and you don't mind loosing the lower bar, try this (I don't have the Image Toolbox right now, so I haven't tested it):

a=imread('image1.jpg')
agray=rgb2gray(a)
b=imread('image2.jpg')
bgray=rgb2gray(b)
[counts,x] = imhist(agray)
stem(counts,x,'b')
hold on
[counts,x] = imhist(bgray)
stem(counts,x,'r')

Solution 2

Try this!

figure (x),
subplot(2,1,1); imhist(agray);
subplot(2,1,2); imhist(bgray);
Share:
13,278
user2650768
Author by

user2650768

Updated on June 26, 2022

Comments

  • user2650768
    user2650768 almost 2 years

    I'm new to Matlab and am trying to do a bit of image processing. I have two color images that I convert to grayscale. My goal is to be able to put the histograms for both grayscale images on the same figure so I can compare them. My code looks like this:

    a=imread('image1.jpg')
    agray=rgb2gray(a)
    b=imread('image2.jpg')
    bgray=rgb2gray(b)
    figure,imhist(agray)
    figure,imhist(bgray)
    

    The code works fine for looking at the two histograms independently, but I can find how to combine them into one figure for comparison. Please help!!

  • Buck Thorn
    Buck Thorn almost 11 years
    Looks nicer as stem(x,counts,'b','Marker','none'); (maybe just on my matlab version). Also, ; are your friends ;>)
  • Luis Mendo
    Luis Mendo almost 11 years
    Yes, much better without markers; and also more similar to what imhist without output arguments does
  • user2650768
    user2650768 almost 11 years
    Worked great!! Thanks so much for the help!!
  • Luis Mendo
    Luis Mendo almost 11 years
    You're welcome! If an answer has worked for you, it's standard practice in StackOverflow that you mark it as "accepted" (tick mark to the left of the answer). You can only mark as accepted one answer per question. I'm telling you this in case you don't know, since I see you are new here.