opencv 最大类间方差(大津法OTSU)

参考:

otsu (大津算法):http://baike.baidu.com/link?url=mvcFw6K8H3ko-un4dibuDifmdU9u_skxLryut9OrHs_5V2GdIPVF5jssQe1msMhwTX78guD3P7ufhrNt4gSytq


#########################################################################


最近在做字符识别,看了很多资料,发现在对图像进行预处理过程中,对图像进行二值化是一个必不可少的方式。如何才能有效的将目标字符表现出来,OpenCV提供的阈值化方法有threshold和adaptiveThreshold,但这需要自己进行参数调整。在同学那里了解到一个很有效的方法,就是大津法(OTSU)。


OTSU算法就是一种对于图像进行二值化的高效算法。它是基于最大类间方法法原理。

百度百科上有较详细的讲解,有兴趣的可以查看一下。


我在使用过程中发现效果确实不错,能够有效的区分出前景和背景


##########################################################################


实验:使用以下照片,解析出字符


注:该图像由网上获取


C++:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <opencv2/opencv.hpp>  
  3. using namespace std;  
  4. using namespace cv;  
  5.   
  6. int main(int argc, char* argv[])  
  7. {  
  8.     Mat img = imread(argv[1], -1);  
  9.     if (img.empty())  
  10.     {  
  11.         cout <<"Error: Could not load image" <<endl;  
  12.         return 0;  
  13.     }  
  14.   
  15.     Mat gray;  
  16.     cvtColor(img, gray, CV_BGR2GRAY);  
  17.   
  18.     Mat dst;  
  19.     threshold(gray, dst, 0, 255, CV_THRESH_OTSU);  
  20.   
  21.     imshow("src", img);  
  22.     imshow("gray", gray);  
  23.     imshow("dst", dst);  
  24.     waitKey(0);  
  25.   
  26.     return 0;  
  27. }  



Python:

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/env python  
  2. #-*- coding: utf-8 -*-  
  3.   
  4. """ 
  5. 实现大津法 
  6. """  
  7.   
  8. __author__ = 'zj'  
  9.   
  10. import cv2  
  11. import os  
  12. import time  
  13.   
  14. if __name__ == '__main__':  
  15.     img = cv2.imread("te.png", -1)  
  16.     if img == None:  
  17.         print "Error: Could not load image"  
  18.         os._exit(0)  
  19.       
  20.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
  21.       
  22.     time0 = time.time()  
  23.     retval, dst = cv2.threshold(gray, 0255, cv2.THRESH_OTSU)  
  24.     time1 = time.time()  
  25.     total = (time1 - time0)  
  26.     print "otsu need time: %.3f s"%total  
  27.   
  28.     cv2.imshow("src", img)  
  29.     cv2.imshow("gray", gray)  
  30.     cv2.imshow("dst", dst)  
  31.     cv2.waitKey(0)  



有人写了一个Python模块Mahotas,同样可以实现OTSU算法

mahotas介绍:

http://www.open-open.com/news/view/6c916f

http://www.oschina.NET/p/mahotas/


C语言

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <cv.h>  
  2. #include <highgui.h>  
  3. #include <stdio.h>  
  4.   
  5. int main(int argc, char* argv[])  
  6. {  
  7.     IplImage *img = cvLoadImage("te.png", -1);  
  8.     if (img == NULL)   
  9.     {  
  10.         printf("Error: Could not load image\n");  
  11.         return 0;  
  12.     }  
  13.   
  14.     IplImage *gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);  
  15.     cvCvtColor(img, gray, CV_BGR2GRAY);  
  16.   
  17.     IplImage *dst = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);  
  18.     cvThreshold(gray, dst, 0, 255, CV_THRESH_OTSU);  
  19.   
  20.     cvNamedWindow("src");  
  21.     cvNamedWindow("gray");  
  22.     cvNamedWindow("dst");  
  23.   
  24.     cvShowImage("src", img);  
  25.     cvShowImage("gray", gray);  
  26.     cvShowImage("dst", dst);  
  27.       
  28.     cvWaitKey(0);  
  29.   
  30.     return 0;  
  31. }  




另参:http://blog.csdn.net/augusdi/article/details/9012043

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值