转换图像文件,黑白配的OpenCV [英] Convert image document to black and white with OpenCV

查看:258
本文介绍了转换图像文件,黑白配的OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的OpenCV和图像处理,我不知道如何解决我的问题。
我的iPhone发文件的照片,我想那个文档转换为黑色和白色。我试图用阈值,但文字不太好(有点模糊,无法读取)。我想文字看起来跟原来一样的形象,只有黑色的,和背景将是白色的。我能做什么?
附:当我提出的文件,其中的文本是相当大的一部分照片,那么结果是正确的。

I'm new to OpenCV and image processing and I'M not sure how to solve my problem. I have a photo of document made in iPhone and I want to convert that document to black and white. I tried to use threshold but the text was not so good (a little blurry and unreadable). I'd like to text looks same as on the original image, only black, and background will be white. What can I do? P.S. When I made a photo of part of the document, where text is quite big, then result is ok.

我将感谢任何帮助。

下面是我用这个例子形象,结果如下:

Here are the example image I use and the result:

推荐答案

我学尝试,也许有点超过你的可读性:

My attemp, maybe a little more readable than yours:

IplImage * pRGBImg  = 0;
pRGBImg = cvLoadImage(input_file.c_str(), CV_LOAD_IMAGE_UNCHANGED);
if(!pRGBImg)
{   
    std::cout << "ERROR: Failed to load input image" << std::endl;
    return -1; 
}   

// Allocate the grayscale image
IplImage * pGrayImg = 0;
pGrayImg = cvCreateImage(cvSize(pRGBImg->width, pRGBImg->height), pRGBImg->depth, 1); 

// Convert it to grayscale
cvCvtColor(pRGBImg, pGrayImg, CV_RGB2GRAY);

// Dilate
cvDilate(pGrayImg, pGrayImg, 0, 0.2);

cvThreshold(pGrayImg, pGrayImg, 30, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
cvSmooth(pGrayImg, pGrayImg, CV_BLUR, 2, 2);

cvSaveImage("out.png", pGrayImg);

这篇关于转换图像文件,黑白配的OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆