I slightly disagree with using the bar/stem colors to denote the color represented by each channel. It's nice to color-code things, but there are two problems with doing it this way.
For stem plots, green offers poor visual contrast against white. It's harder to read and easier to miss details. Bar plots do have edges that can help with contrast, but for typical cases where nbins is large, the edges may need to be omitted for the face color to even be visible.
<-- A green stem with poor contrast
<-- histogram() with default edges
<-- no edges, but poor contrast againThe second problem shows up when you try to apply the same technique to other color models. If you're dealing with any opponent or polar model, there often isn't a single representative color for a given channel (e.g. hue). You can represent a channel using a color sweep, but not a single color.
The first problem can be improved significantly by using an edgeless bar plot and an overlaid stair plot.
rgbpict = imread('peppers.png');
greenpict = rgbpict(:,:,2);
hh = histogram(greenpict(:),'numbins',256,'facecolor',[0 1 0],'edgecolor','none','facealpha',1);
stairs(hh.BinEdges([1 1:end]),[0 hh.Values 0],'color',[0 0 0]);
Unlike trying to do similar with plot(), this still works even when nbins is small:
Similar can also be done using patch() and a line plot.
The way I usually prefer to do this is to just tailor the colorstripe instead of the stem/bar colors. At least that's extensible to other color models. If nbins is small, it's not worth trying to do that, since imhist() doesn't correctly generate the colorbar in the first place.
<-- leaves bars with good contrast
<-- can represent things other than RGB (e.g. hue)MIMT imhistFB() does allow the colorstripe to be specified directly. It also allows the use of plot styles other than stem, including filled stair plots like before.
That said, MIMT also has cshist(), which can configure these colored histogram plots for images in RGB or other color models, without a bunch of hassle, so even using imhistFB() directly is unnecessary.
yccpict = rgb2ycbcr(rgbpict);
All that said, this question prompted me to add a 'color' option to imhistFB() so that single-color stem/bar/patch/stair plots can be configured directly without needing to edit properties after the fact. That'll be out in the next update.
inpict = imread('cameraman.tif');
imhistFB(inpict,n,'style','stem','color',thiscolor);
imhistFB(inpict,n,'style','bar','color',thiscolor);
imhistFB(inpict,n,'style','patch','color',thiscolor);
imhistFB(inpict,n,'style','stairs','color',thiscolor);