histogram by not using the default imhist
7 次查看(过去 30 天)
显示 更早的评论
I1 = imread('RDL.jpg');
raw = im2double(I1(:,:,1));
m=max(max(raw));
m1=min(min(raw));
binsize=(m-m1)/10;
for i=1:1:size(raw,1)
for j=1:1:size(raw,2)
value=raw(i,j); %read input image level
if value >= min(min(raw)) && value <= max(max(raw))
for i=1:1:10
if value <= i*binsize+min(min(raw))+ value > (i-1)*binsize+min(min(raw))
% original histogram in pixels
imhist(i)=imhist(i)+1;
% normalized histogram pdf
%InputIm_normalized_histogram(i)=InputIm_histogram(i)/resolution;
end
end
end
end
end
imhist(i)
I have used the code to plot a histogram from the image below. I want the graph similar to the attached image. But I am not able to get it. Kindly let me know where have I made the mistake.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/548543/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/548543/image.jpeg)
3 个评论
Jan
2021-3-13
imhist(i) is a scalar, because you have defined it as a vector. Your code does not contain a command to dispaly a graph.
采纳的回答
Jan
2021-3-13
编辑:Jan
2021-3-13
Initially imhist is a command. Calling it as imhist(i) + 1 should cause an error. Do you get a corresponding message?
Use a different name and initialize your counter:
history = zeros(1, 10);
This line is meaningless:
if value >= min(min(raw)) && value <= max(max(raw))
because all values are >= the minimum and <= the maximum.
if value <= i*binsize+min(min(raw))+ value > (i-1)*binsize+min(min(raw))
% ^
Here you do not want a +, but an & to get a logical AND. You have evaluated min(min(raw)) already, so it is more efficient and easier to read, if you use m1 here.
A hint: "m" as maximum and "m1" as minimum are not easy to remember. maxV and minV might be better.
A leaner version of your code:
Img = imread('RDL.jpg');
R = im2double(Img(:,:,1)); % The red channel
maxR = max(max(R));
minR = min(min(R));
nBin = 10;
BinEdge = linspace(minR, maxR, nBin + 1);
BinEdge(end) = Inf; % any value > maxR to include it in last bin
hist = zeros(1, nBin);
for iBin = 1:nBin
hist(iBin) = sum(BinEdge(iBin) <= R(:) & R(:) < BinEdge(iBin + 1));
end
Instead of loops over the pixels, sum() is used to count the values of R matching into each bin.
更多回答(1 个)
Image Analyst
2021-3-14
Then why not simply use histogram() or histcounts()? Why do you want to write (buggy) code yourself?
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!