0

In my iOS project, I have a CGImage in RGB that I'd like to binarize (convert to black and white). I would like to use OpenCV to do this, but I'm new to OpenCV. I found a book on OpenCV, but it was not for iPhone.

How can I binarize such an image using OpenCV on iOS?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Dani
  • 73
  • 1
  • 11
  • By binary you mean pure black and white? And why would iPhone make any difference? – Mark Ransom Jun 07 '12 at 17:49
  • Yes I mean pure black and white image. for iPhone actually i am new to openCV and I am little bit confused in cvNamedWindow and cvDestroyWindow. – Dani Jun 07 '12 at 19:02
  • `cvNamedWindow()` creates a window, `cvShowImage()` displays an image inside that window, `cvWaitKey(0)` makes the window stay open until the user presses a key, and at the end of your program there should be a `cvDestroyWindow()` to release the resources previously allocated to the window. There are tons of examples around here, use the search box. – karlphillip Jun 07 '12 at 19:47
  • Actually i don't wanna use cvNamedWindow and cvDestroyWindow in my iOS project code. because i think there is no concept of window in iOS. Everything is displayed on views. – Dani Jun 08 '12 at 12:47

3 Answers3

7

If you don't want to set up OpenCV in your iOS project, my open source GPUImage framework has two threshold filters within it for binarization of images, a simple threshold and an adaptive one based on local luminance near a pixel.

You can apply a simple threshold to an image and then extract a resulting binarized UIImage using code like the following:

UIImage *inputImage = [UIImage imageNamed:@"inputimage.png"];    
GPUImageLuminanceThresholdFilter *thresholdFilter = [[GPUImageLuminanceThresholdFilter alloc] init];
thresholdFilter.threshold = 0.5;
UIImage *thresholdFilter = [thresholdFilter imageByFilteringImage:inputImage];

(release the above filter if not using ARC in your application)

If you wish to display this image to the screen instead, you can send the thresholded output to a GPUImageView. You can also process live video with these filters, if you wish, because they are run entirely on the GPU.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
2

Take a look at cv::threshold() adn pass thresholdType as cv::THRESH_BINARY:

double cv::threshold(const cv::Mat& src, 
                     cv::Mat& dst, 
                     double thresh, 
                     double maxVal, 
                     int thresholdType)

This example uses the C interface of OpenCV to convert an image to black & white.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
0

What you want to do is remove the low rate of changes and leave the high rate of changes, this is a high pass filter. I only have experience with audio signal processing so I don't really know what options are available to you but that is the direction I would be looking.

Nathan Day
  • 5,981
  • 2
  • 24
  • 40