Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a histogram of a UIImage programmatically?

How to draw an image histogram of a UIImage on iPhone programmatically? e.g.:

alt text

Thanks!

like image 233
ohho Avatar asked Dec 07 '10 10:12

ohho


4 Answers

You can calculate histogram with help of Accelerate framework. There is function vImageHistogramCalculation_ARGB8888(). To call this function you need to create vImage_Buffer from the UIImage. Use vImageBuffer_InitWithCGImage() function for it.

For more information you can add this framework to your project and read docs in the header files:

Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_Utilities.h Accelerate.framework/Frameworks/vImage.framework/Headers/Histogram.h

like image 58
kelin Avatar answered Oct 28 '22 22:10

kelin


There's no api for that.

Edit: An API was introduced with iOS5. See kelin's answer.

You would want to get the raw data of the image, count the color values and draw the histogram yourself.

Access the pixels: How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

Use CoreGraphics to draw the histogram in a custom UIView subclass.

like image 2
tonklon Avatar answered Nov 08 '22 14:11

tonklon


Actually there is, I'll refer your to Brad Larson's answer : https://stackoverflow.com/a/10359305/300115

You can find the class GPUImageHistogramFilter in his library : https://github.com/BradLarson/GPUImage

The "Filter Showcase" demo in the lib already has a working version of a live histogram analysis. Live Histogram Analysis

like image 1
rnaud Avatar answered Nov 08 '22 14:11

rnaud


You can calculate histogram with help of Accelerate framework. There is function vImageHistogramCalculation_ARGB8888(). To call this function you need to create vImage_Buffer from the UIImage. Use vImageBuffer_InitWithCGImage() function for it.

For more information you can add this framework to your project and read docs in the header files:

Accelerate.framework/Frameworks/vImage.framework/Headers/vImage_Utilities.h Accelerate.framework/Frameworks/vImage.framework/Headers/Histogram.h

like image 4
kelin Avatar answered Nov 08 '22 15:11

kelin