OpenCV:将图像转换为黑白时,阈值无效?

时间:2014-06-23 19:12:16

标签: ios objective-c opencv

我正在尝试将图像转换为黑白图像,以便任何非绝对黑色的图像都是白色的,并将其用作蒙版。

+ (UIImage *) getBlackAndWhiteImage:(UIImage *)image;
    {

      IplImage* im_rgb = [self CreateIplImageFromUIImage:image];
      IplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
     cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);

     IplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
    cvThreshold(im_gray, im_bw, 127, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); 
//    I have change 127 to 10 :255 that has no effect
    return [self UIImageFromIplImage:im_bw];
  }

我怎样才能获得正确的黑色和白色图像?

1 个答案:

答案 0 :(得分:1)

不要将CV_THRESH_OTSU传递给cvThreshold()。如果您传递此标记,则会忽略提供的阈值,而是使用Otsu's method代替,这会自动选择一个阈值。

您的调用应如下所示:

cvThreshold(im_gray, im_bw, 127, 255, CV_THRESH_BINARY);