histogram
3 ビュー (過去 30 日間)
古いコメントを表示
how to calculate threshold value using histogram ,that is average of two hightest velles in in histogram
0 件のコメント
回答 (3 件)
Walter Roberson
2011 年 4 月 10 日
sort() the values of interest, take the first two of the sorted results and average them.
3 件のコメント
DGM
2024 年 7 月 4 日
編集済み: DGM
2024 年 7 月 4 日
I know it's over a decade late, but of the two lines of code given, neither make any sense. Maybe the ridiculousness explains why the conversation got dropped like a hot potato, but I couldn't resist the allure of a painstakingly-misdescribed problem caused by doing something that would never make sense to attempt.
What's not being said is that both of these lines will throw errors if fed a mask. IPT histeq() does not support logical inputs, because it doesn't make any sense that it should. Since there are only two values, there's nothing to inform us of continuity anymore. If we need more white pixels, there are no "less black" black pixels which can be made white. All that information is gone.
inpict = imread('cameraman.tif');
BW = inpict>170; % this is a logical image
% this line will throw an error
%img = histeq(BW);
Error using histeq
Expected input number 1, I, to be one of these types:
uint8, uint16, double, int16, single
Instead its type was logical.
Error in histeq (line 106)
validateattributes(a,{'uint8','uint16','double','int16','single'}, ...
Error in AT_sandbox (line 25)
img = histeq(BW); % BW is a binary image
IPT imhist() has internal defaults for the bin counts -- 256 for numeric classes, and 2 for logical. Of course, the user can override this default as OP attempted -- but only for numeric inputs. For some reason, imhist() demands that N always is 2 for logical, but it does not actually implement anything other than an error message to ensure that. It forces the user to externally do its input handling for it. I don't like it, but that's the way it is.
% this line will throw an error because imhist() does support
% logical images, but requires that N is always 2 for logical inputs.
%imhist(BW,100)
Error using imhist (line 84)
must be set to two for a logical image.
Error in AT_sandbox (line 26)
imhist(BW,100) % 100 is number of bins
Okay, but are we really sure it's a logical image? Maybe its just a numeric binary image. That wouldn't have the same problems. Correct, but we're still up against the concept-level problems. You can run histeq() all you want on a binarized numeric image. You'll just get useless garbage out, because it still doesn't make sense. As the population of white pixels gets smaller, the gray value used for representing "black" gets lighter. The actual populations of the two classes never change.
BW = im2uint8(BW); % but cast it as something numeric
img = histeq(BW); % no error!
counts1 = imhist(BW,256); % no error!
u1 = unique(BW);
table(u1,counts1(double(u1)+1)) % this is the distribution before histeq()
counts2 = imhist(img,256);
u2 = unique(img);
table(u2,counts2(double(u2)+1)) % look what histeq() accomplished.
Okay, so maybe this is all just a big XY problem and we need to back up and ask why we're trying to look at the histogram. It turns out that we're doing that so that we can ... calculate the thresholding value ... for an image which has already been binarized.
So what is the actual task? Does it make any sense to calculate the threshold value of an ostensibly binary image? What does that even mean? Consider the interpreted possibilities:
1: The image is logical and we want it to be logical.
In this case, there's nothing to be done.
2: The image is a nominally-binarized numeric image and we want it to be logical.
In this case, just threshold at the central gray value implied by the numeric class. I suppose you could also threshold at the mean of the extrema as Walter suggests. It would probably suffice to use imbinarize() or im2bw(), though at that point I think we're resorting to unnecessary tools merely because they're the only ones that are (partially) class-agnostic.
3: The image is either logical or binarized numeric and we want to back-calculate the threshold value which was used to create it from an image which we no longer have (certainly none were mentioned).
In this case, the answer is no. If the original image is unavailable, then that information is gone.
If the image is still available, then:
% an image
inpict = imread('cameraman.tif');
% this is a clean logical image
BW0 = inpict >= 170;
% just to demonstrate the other cases,
% degrade the clean logical image to a
% nominally-binarized numeric image
imwrite(BW0,'iloveacrusty.jpg') % create a low-quality JPG
BW = imread('iloveacrusty.jpg'); % read it back (uint8)
% rebinarize the damaged image
ir = getrangefromclass(BW); % get implied range
BW = BW >= mean(ir); % threshold at central gray
% find the original threshold
thresh = min(inpict(BW))
... though I doubt that was ever the question.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Image Filtering and Enhancement についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!