Extracting data from a histogram
古いコメントを表示
Hello all, on the x-axis I have the gray levels, and on the y-axis I have the number of pixels, I think? I'm still trying to understand histograms, but I think what its telling me here is that there is a lot of dark pixels in my image, comparatively to histogram 2, which seems to have a lot less darker pixels. I want to make some sort of if statement, where: if there are around 200+ pixels of gray levels 50-60, then the image probably falls into category 1, else, it belongs in category 2. My roundabout solution to this problem is simply to create a threshold that only excepts pixels between 50-60 and then counting the number of objects (which would be 0 ideally in category 2's case), but this can't be the best way of accomplishing that. Any ideas would be appreciated!
12 件のコメント
Hi Kimo, It would be helpful to have axis labels which can be added like this
xlabel('Gray level (units)');
ylabel('frequency')
I'm not sure what function was used to create these plots but there is a technical difference between a histogram and a bar chart. Histograms are typically used with continuous data (ie, time) grouped into bins where each bin gets its own bar and there's usually no space between the bars. Bar charts are typically used with categorical data where each bar represents a categorie (ie, gender) and there's usually space between the bars (as in your images). So I'm not sure if your data are categorical (ie, distinct shades of gray) or continuous (a smooth transition of gray grouped into bins). I'll assume the prior and call it a bar chart.
Histograms usually plot frequencies or densities along the y axis. So if a bar that extends between x=0.5 and x=1 terminates at y=100 that means there are 100 data points in your population that exist between 0.5 and 1. Bar charts can represent a wide variety of variables along the y axis. You mentioned that your bar charts represent number of pixels along the y axis but you sound uncertain so you'd have to look into the data that was used to create the plots. Your x axis is 'gray levels' -- does that mean smaller values are closer to white while larger values are closer to black?
To compare the two distributions, note that neither distribution is normal so I'd start with medians and quantiles. Eye-balling it, the range of gray values differ but slightly -- you have a larger range which means you have darker and lighter grays than in hist2. It looks like your median is around 70 while the hist2 median is around 67 but those estimates are close so you'd have to calculate it from the data. The most salient difference is the number of pixels. Your data has almost an order of magnitude greater number of pixels than the hist2 data. For example, the hist2 data has ~70 pixels with a gray value of 60 or less. Your data has ~1000 pixels within that same range of gray values.
Now, about your idea to categorize the distributions... I initially thought you wanted to categorize based on darkness but your suggestion is actually to categorize based on number of pixels. You want to sample the 50-60 range and count the number of pixels. If two cameras take identical photos but camera B has a lower resolution than camera A, camera A will win even though there's identical darkness in the photo. So, if you could state more clearly what your goal is, the constraints, etc, it would be helpful.
KALYAN ACHARJYA
2018 年 6 月 30 日
%Are you looking for this..As per your question, it seems easy, still need more clarification on your problem
a>gray image
loop
if a(i,j)=>50 & a(i,j)<=60
px_1=px_1+1
end
%loop end
if px_1=>200
image cetegory 1
esle
image cetegory 2
end
Kimo Kalip
2018 年 7 月 3 日
If I understand your plots correctly (which I might not), each bin along the x axis is a range of grays (except maybe for the first and last which are white and black). The y axis is some frequency, let's say number of pixels. For simplicity, let's say you only have 9 bins which are summarized in the image below -- these are your x values. You are proposing to use only the 5th bin (50-60 bin labeled in the image) to judge if picture B is darker than picture A. That's where you lose me. That bin only represents one shade of gray and if the y value for picB is greater than picA that just means picB has more pixels with that shade of gray. Pic B could have 9000 pixels there and picture A could have 1 pixel there but picture A also has 9-million black pixels making it much darker than picA.
If I'm correct that your x data are shades of gray and your y data are frequencies, I'd suggest using means (if ~normally distributed) or medians (otherwise) of the entire distributions. You could also calculate confidence intervals or perform t-tests to determine if the difference is statistically significant.
Feel free to ask follow up questions or correct my assumptions.

To illustrate my point further, if you took two pictures of the same object in a highly controlled environment with a very sensitive camera where the only thing that changed was luminance and you made sure there was no image processing of the two photos, then the one taken in the darker environment would presumably have the same (or very similar) distribution as the lighter one but its distribution would be shifted rightward (toward black).
Notice the arrow in the plot below points to such an overlap. The blue distribution has the higher value in that bin (50-60) but the red distribution has more darker pixels since it's shifted rightward (toward black).
This is why you shouldn't categorize darkness based on one bin (if I understood your data correctly).

Kimo Kalip
2018 年 7 月 3 日
編集済み: Kimo Kalip
2018 年 7 月 3 日
Kimo Kalip
2018 年 7 月 3 日
Image Analyst
2018 年 7 月 3 日
If you want more advice, you'll probably have to upload the original gray scale image.
Kimo Kalip
2018 年 7 月 3 日
Kimo Kalip
2018 年 7 月 3 日
Kimo Kalip
2018 年 7 月 3 日
編集済み: Image Analyst
2018 年 7 月 4 日
Image Analyst
2018 年 7 月 4 日
Sounds very ad hoc. Sure you can find the boundary and then take slopes perpendicular to the edge in a few places and see if the slopes are as expected. Or you can take the x-y path of the boundary curve and see if that is "straight enough" or "too bulged" whatever that means. Fit a section to a quadratic or whatever and look at the residuals of the actual to the fit. Pretty simple with polyfit() and polyval():
coefficients = polyfit(xActual, yActual, 2);
yFit = polyval(coefficients, xActual);
meanResidual = mean(abs(yFit-yActual))
or something like that. Basically you can do whatever you want - it just depends on how you define a "normal" looking curve.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Histograms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

