You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Drawing the gradient histogram of a grey image's one
6 views (last 30 days)
Show older comments
Hi folks,
I'm trying to plot the differential/gradient of a gray image's histogram, to highlight the points of interest more readily.
Searching for a function that does this hasn't yielded anything, but I'm unsure how to go about this in Matlab.
Is there a tutorial or something similar you can point me towards please?
Thanks in advance
Accepted Answer
Ameer Hamza
on 9 May 2020
See imgradient(): https://www.mathworks.com/help/images/ref/imgradient.html. Can you further clarify your question about the histogram.
20 Comments
Teshan Rezel
on 10 May 2020
Edited: Teshan Rezel
on 10 May 2020
Hi Ameer, thanks for your response. To clarify, if I have a histogram for a bw image, I'd like to get the differential of the straight line, to give the rate of change per bin. does that make sense?
So similar to how if you differentiate the graph for displacement vs time, you would get the velocity-time graph, I'm looking to do the same thing to a binary histogram because that should highlight the points of interest in an image.
Ameer Hamza
on 10 May 2020
Ok. I misunderstood the question. If you just want to take the difference between two consecutive values, then see diff(): https://www.mathworks.com/help/releases/R2020a/matlab/ref/diff.html. If you are looking for something similar to time-velocity then use gradient(): https://www.mathworks.com/help/releases/R2020a/matlab/ref/gradient.html.
Teshan Rezel
on 11 May 2020
Hi Ameer, thanks for this. Do you know how to represent the histogram in such a form that it can be differentiated?
Ameer Hamza
on 11 May 2020
How are you creating the histogram? Do you want to extract the numeric data? Also see histcount().
Teshan Rezel
on 11 May 2020
I'm using histogram() to create the histogram. I've tried using histcount() and then gradient(N, edges) but to no avail...I'm trying to differentiate the frequency by the pixel value (from 0-255) but not entirely sure how to do that in Matlab!
Image Analyst
on 11 May 2020
Exactly what are the "points of interest" in that image? I mean of course you can call imhist(), but then what? What are you really trying to do? Like get the areas or something? The gradient of the histogram won't give you anything interesting I believe. Maybe you meant the "histogram of oriented gradients" (or HOG), which actually IS something. It tells you about the anisotropy/homogeneity of a region.
Teshan Rezel
on 11 May 2020
Hi Ameer, this is my code:
I = rgb2gray(I);
imshow(I);
histogram(I)
xlim([100, 256])
ylim([0, 30000])
[N, edges] = histcounts(I);
y = gradient(N, edges);
histogram(y)
Teshan Rezel
on 11 May 2020
Hi Image Analyst, what I'm trying to do is to highlight the peaks in the histogram a bit better. The peaks correspond to different types of objects in the image, and by differentiating the histogram to make the peaks more clear, it is hoped that can shed some light on the original image.
Can this be done with HOG?
Image Analyst
on 11 May 2020
Edited: Image Analyst
on 11 May 2020
No, HOG is for texture. Tell me what gray level(s) you think corresponds to a feature in your image. I want to know the gray level, or gray level range, and what it means about the pixels if they have that gray level, or are in that gray level range?
Ameer Hamza
on 11 May 2020
Teshan, the histogram only has significant peaks at 0 and 255. Which other peaks are you interested in?
Teshan Rezel
on 11 May 2020
Hi Ameer, once we remove the 0 peak for the black packground, there appear to be about 3-4 peaks of interest,right after 0, around 170, and around 250...it's highlighting those that I'm looking to do, but also taking the differential to make them more evident
Teshan Rezel
on 11 May 2020
Hi image Analyst, just based on the histogram, I'd say the areas of interest are around 0-20, ~170-180 and >250.
Ameer Hamza
on 11 May 2020
What about findpeaks()
I = imread('image.jpeg');
I = rgb2gray(I);
[N, edges] = histcounts(I);
edges = (edges(1:end-1)+edges(2:end))/2; % mid-points of histogram edges
N([1 end]) = 0;
findpeaks(N, edges);
Teshan Rezel
on 12 May 2020
Hi Ameer, thanks for this, its really helpful! Is there any way to then differentiate this graph? I believe the peaks of the differential are to be used to predict a different output from the image, so its important that its done!
Ameer Hamza
on 12 May 2020
You can calculate the gradient vector like this
I = imread('image.jpeg');
I = rgb2gray(I);
[N, edges] = histcounts(I, 50);
edges = (edges(1:end-1)+edges(2:end))/2; % mid-points of histogram edges
N([1 end]) = 0;
grad = gradient(N, edges);
plot(edges, grad);
The point at which gradient change direction is the same as the locations given by findpeaks.
More Answers (1)
Image Analyst
on 13 May 2020
If you want you can use kmeans() to partition the histogram into sections. Personally I don't think it does a very good job but it will find the peak regions.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)