How to give Gray color histogram gray shade instead of blue

조회 수: 26 (최근 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 CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by