Question regarding strange outputs from histogram plot

Hi folks,
I have an image which is mostly black, with specks of purple. When I plot the RGB histogram for the image, I get an sharp line in the blue region but zero elsewhere, which I don't think is correct.
Can you please advise me on where I've gone wrong?
Thanks
R3=imhist(img3(:,:,1));
G3=imhist(img3(:,:,2));
B3=imhist(img3(:,:,3));
[pk31, locs31] = max(R3);
[pk32, locs32] = max(G3);
[pk33, locs33] = max(B3);
str31 = strcat('\leftarrow', num2str(locs31));
str32 = strcat('\leftarrow', num2str(locs32));
str33 = strcat('\leftarrow', num2str(locs33));
figure
hold on,
plot(R3,'r'),
text(locs31, pk31, str31),
plot(G3,'g')
text(locs32, pk32, str32),
plot(B3,'b'),
text(locs33, pk33, str33),
legend(' Red channel','Green channel','Blue channel','Location','best');
hold off,title('Mask 2 - RGB Histograms');
xlim([0 20])
ylim([0 5000])

 採用された回答

Benjamin Großmann
Benjamin Großmann 2021 年 4 月 30 日
編集済み: Benjamin Großmann 2021 年 4 月 30 日

0 投票

If you do not provide any code, it is hard to tell what went wrong here. If I use your image, everything looks fine:
imageURL = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/602390/image.jpeg';
img = imread(imageURL);
tiledlayout(3,1)
bins = [0:255];
nexttile()
histogram(img(:,:,1), bins)
title('R')
nexttile()
histogram(img(:,:,2), bins)
title('G')
nexttile()
histogram(img(:,:,3), bins)
title('B')

4 件のコメント

Teshan Rezel
Teshan Rezel 2021 年 4 月 30 日
Hi @Benjamin Großmann, apologies! I've ammended my post to include the code now.
Benjamin Großmann
Benjamin Großmann 2021 年 4 月 30 日
@Teshan Rezel The are two things to regard here: First, specify two ouputs from imhist: [counts, binLocations] to also get the bin locations. Second, I am pretty sure that what you think is a "sharp line in the blue region", is the line from the blue channel overlapping red an green channel since it is plotted last. Use a tiledlayout to see the channels separated.
Furthermore, use histogram() function to plot a histogram not the plot function.
Teshan Rezel
Teshan Rezel 2021 年 4 月 30 日
Hi @Benjamin Großmann thanks for this! The reason I used imhist instead of histogram is because I wasn't aware of how to plot histogram() outputs on one graph, which I need to do for comparative purposes. Is there a way around this?
Benjamin Großmann
Benjamin Großmann 2021 年 4 月 30 日
the command "hold on" that you used, works on the axes and can also be used for histograms. It sets the axes property "NextPlot" to "add" instead of "replace". I prefer to set the property instead of "hold on"
imageURL = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/602390/image.jpeg';
img = imread(imageURL);
bins = [0:255];
fig = figure();
ax = axes('Parent', fig, 'NextPlot', 'Add');
histogram(img(:,:,1), bins, 'DisplayName', 'Red Channel')
histogram(img(:,:,2), bins, 'DisplayName', 'Green Channel')
histogram(img(:,:,3), bins, 'DisplayName', 'Blue Channel')
legend(ax, 'show')

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by