How to give Gray color histogram gray shade instead of blue

24 ビュー (過去 30 日間)
sadiqa ilyas
sadiqa ilyas 2019 年 12 月 7 日
編集済み: DGM 2024 年 12 月 26 日 10:10
Hi, I want the gray shade histogram for grayscale image. here is my code. Can any one help me
% for red color
figure;
imhist(Image_Data(:,:,2));
myHist = findobj(gca, 'Type', 'Stem');
myHist.Color = [0 1 0]
saveas(gcf,'Hist_Org_B.jpg');
%for green color
figure;
imhist(Image_Data(:,:,3));
myHist = findobj(gca, 'Type', 'Stem');
myHist.Color = [0 0 1]
saveas(gcf,'Hist_Org_G.jpg');

採用された回答

DGM
DGM 2024 年 12 月 25 日 7:27
編集済み: DGM 2024 年 12 月 26 日 10:10
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 again
The 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);
hold on
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.
% just one line
cshist(rgbpict,'rgb'); % needs MIMT
% also works with other models
yccpict = rgb2ycbcr(rgbpict); % not RGB anymore
cshist(yccpict,'ycbcr'); % colorbars, limits, and labels preconfigured
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');
n = 32;
thiscolor = [0.7 0.2 1];
subplot(4,1,1)
imhistFB(inpict,n,'style','stem','color',thiscolor);
subplot(4,1,2)
imhistFB(inpict,n,'style','bar','color',thiscolor);
subplot(4,1,3)
imhistFB(inpict,n,'style','patch','color',thiscolor);
subplot(4,1,4)
imhistFB(inpict,n,'style','stairs','color',thiscolor);

その他の回答 (2 件)

Gautam
Gautam 2024 年 8 月 29 日
Hello Sadika,
I assume that you want to change the colour of the histogram to grey just like the way you have changed to green and blue.
To do this you can use the RGB triplet value of [0.3711 0.3711 0.3711] which is for a shade of grey
figure;
imhist(Image_Data(:,:,3));
myHist = findobj(gca, 'Type', 'Stem');
myHist.Color = [0.3711 0.3711 0.3711];
This gives the plot

Image Analyst
Image Analyst 2024 年 8 月 29 日
Try using the 'FaceColor' input of histogram.
% Create sample data.
data = rand(1, 1000);
% Define custom color.
grayColor = [0.4, 0.4, 0.4];
% Plot histogram with 10 gray bins.
histogram(data, 'NumBins', 10, 'FaceColor', grayColor)
  1 件のコメント
DGM
DGM 2024 年 12 月 25 日 6:13
When using histogram(), bear in mind that the default facealpha is 0.6. If left against the default axes background, the rendered color will not be what was specified.
rgbpict = imread('peppers.png');
redpict = rgbpict(:,:,1);
histogram(redpict(:),'numbins', 256,'facecolor',[1 0 0],'edgecolor','none')
figure
histogram(redpict(:),'numbins', 256,'facecolor',[1 0 0],'edgecolor','none','facealpha',1)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeHistograms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by