-1

I am using openCV in objective C. I want to convert image into Black and White, I already done it, but my output Black and White image is not clear, there is black shade on image.

Any one can help?

- (IBAction)blackAndWhite:(id)sender {

imageView.image=orignalImage;

cv::Mat  dst;

cv::Mat src=[self cvMatFromUIImage:imageView.image];


if( !src.data )
{ cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
}

/// Convert to grayscale
cvtColor( src, src, CV_BGR2GRAY );

/// Apply Histogram Equalization
equalizeHist( src, dst );


imageView.image =[self UIImageFromCVMat:dst];

}

Thanks

orignal image :

enter image description here

Black and White image :

enter image description here

skywinder
  • 21,291
  • 15
  • 93
  • 123
QueueOverFlow
  • 1,336
  • 5
  • 27
  • 48

4 Answers4

2

Firstly the image output you have is not a black and white (binary) image. It is a grayscale image.

This grayscale image is a single channel image with 256 colours (0-255), whereas binary images have only 0(black) or 255(white) in them.

You can use thresholding (cvThreshold) to convert it to binary from grayscale. There are many binarization algorithms available which can help you in achieving what you require as well. Local binarization methods are more adaptable and can help remove parts of the shaded regions as well.

Hope this helps.

sri1683
  • 21
  • 1
  • thanks, can you provide me any links for converting binary from grayscale – QueueOverFlow Nov 05 '12 at 11:24
  • simplest method is using cvThreshold function explained in this following [link](http://dasl.mem.drexel.edu/~noahKuntz/openCVTut7.html) a few local binarization methods are discussed in this paper [link](http://www.math-info.univ-paris5.fr/~vincent/articles/DRR_nick_binarization_09.pdf) – sri1683 Nov 05 '12 at 11:55
1

Look into gamma correction. I assume that the gamma value needs to be adjusted in this case to suit the contrast of the lines in the image. Since it's a blurry image, you also find some trouble, I assume. You may also want to increase some of the contrast, while you're at it.

Links:

Understanding Gamma Correction

Wikipedia - Gamma Correction

OpenCV Gamma Correction (C++)

Changing contrast and brightness of an image - OpenCV

Mr_Spock
  • 3,815
  • 6
  • 25
  • 33
1

Well, I don't know why there are already 3 answers and none of them is correct.

Why do you apply image equalization after converting image to grayscale? The output without it is the following:

enter image description here

Here you can read about histogram equalization.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
  • thanks for your answer, Actually I am working on business card reader, So i wants increase contrast of letters in an image, So I identify it, you have any idea how to improve quality of image in black and white shade? – QueueOverFlow Nov 05 '12 at 13:17
  • @Gryphon Maybe this helps: http://stackoverflow.com/questions/11266150/how-to-get-threshold-value-from-histogram – ArtemStorozhuk Nov 05 '12 at 16:42
0

As already pointed out, you are not really binarizing your image but you are converting it to gray scale. You can binarize using cv::threshold but due to strong artefacts in illumination in your sample image, you'd better off with determining a local threshold value, using cv::adaptiveThreshold.

Here is a useful link that explains binarization in case of non uniform lighting conditions.

A sample program, in C++:

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cstdlib>

int
main(int argc, char **argv)
{

  cv::Mat image = cv::imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
  assert(!image.empty());

  cv::Mat binary;
  cv::adaptiveThreshold(image, binary, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 5, 7);

  cv::imshow("Orig", image);
  cv::imshow("Binary", binary);
  cv::waitKey(0);
  cv::imwrite("binary.png", binary);

  return EXIT_SUCCESS;

}

Result: enter image description here

remi
  • 3,914
  • 1
  • 19
  • 37
  • Thanks, this is very helpful to me, Please look on my question link http://stackoverflow.com/questions/13248680/ocr-using-opencv . how we get the second and third image. your any suggestion may help me. Thanks again – QueueOverFlow Nov 07 '12 at 06:21
  • This is a difficult task, so there is not a single answer to your question. But if you find something useful, let us know! – remi Nov 07 '12 at 08:49
  • Actually, I wants read characters from image (OCR). I already implement code. but because my image not very clear ,accuracy is not good, IS there any way to improve quality of above black and white image. like increase contrast of characters or can we do more dark characters in your black and white image? – QueueOverFlow Nov 07 '12 at 08:53