zoukankan      html  css  js  c++  java
  • Java基于opencv实现图像数字识别(三)—灰度化和二值化

    Java基于opencv实现图像数字识别(三)—灰度化和二值化

    一、灰度化

    灰度化:在RGB模型中,如果R=G=B时,则彩色表示灰度颜色,其中R=G=B的值叫灰度值;因此,灰度图像每个像素点只需一个字节存放灰度值(又称强度值、亮度值),灰度范围为0-255。一般常用的是加权平均法来求像素点的灰度值,opencv开发库所采用的一种求灰度值算法如下;
    :)Gray = 0.072169 * B + 0.715160 * G + 0.212671 * R

    有两种方式可以实现灰度化,如下

    方式1
    @Test
    public void toGray() {
    	// 这个必须要写,不写报java.lang.UnsatisfiedLinkError
    	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    
    	File imgFile = new File("C:/Users/admin/Desktop/open/test.png");
    	String dest = "C:/Users/admin/Desktop/open";
    		
    	//方式一
    	Mat src = Imgcodecs.imread(imgFile.toString(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    	//保存灰度化的图片
    	Imgcodecs.imwrite(dest + "/toGray" + imgFile.getName(), src);
    }
    
    方式2
    @Test
    public void toGray() {
    	// 这个必须要写,不写报java.lang.UnsatisfiedLinkError
    	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    
    	File imgFile = new File("C:/Users/admin/Desktop/open/test.png");
    	String dest = "C:/Users/admin/Desktop/open";
    		
    	//方式二
    	Mat src = Imgcodecs.imread(imgFile.toString());
    	Mat gray = new Mat();
    	Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
    	src = gray;
    	//保存灰度化的图片
    	Imgcodecs.imwrite(dest + "/toGray2" + imgFile.getName(), src);
    }
    

    二值化:图像的二值化,就是将图像上的像素点的灰度值设置位0或255这两个极点,也就是将整个图像呈现出明显的只有黑和白的视觉效果

    常见的二值化方法为固定阀值和自适应阀值,固定阀值就是制定一个固定的数值作为分界点,大于这个阀值的像素就设为255,小于该阀值就设为0,这种方法简单粗暴,但是效果不一定好.另外就是自适应阀值,每次根据图片的灰度情况找合适的阀值。自适应阀值的方法有很多,这里采用了一种类似K均值的方法,就是先选择一个值作为阀值,统计大于这个阀值的所有像素的灰度平均值和小于这个阀值的所有像素的灰度平均值,再求这两个值的平均值作为新的阀值。重复上面的计算,直到每次更新阀值后,大于该阀值和小于该阀值的像素数目不变为止。

    代码如下

    @Test
    public void binaryzation(Mat mat) {
    	int BLACK = 0;
    	int WHITE = 255;
    	int ucThre = 0, ucThre_new = 127;
    	int nBack_count, nData_count;
    	int nBack_sum, nData_sum;
    	int nValue;
    	int i, j;
    
    	int width = mat.width(), height = mat.height();
    	//寻找最佳的阙值
    	while (ucThre != ucThre_new) {
    		nBack_sum = nData_sum = 0;
    		nBack_count = nData_count = 0;
    
    		for (j = 0; j < height; ++j) {
    			for (i = 0; i < width; i++) {
    				nValue = (int) mat.get(j, i)[0];
    
    				if (nValue > ucThre_new) {
    					nBack_sum += nValue;
    					nBack_count++;
    				} else {
    					nData_sum += nValue;
    					nData_count++;
    				}
    			}
    		}
    
    		nBack_sum = nBack_sum / nBack_count;
    		nData_sum = nData_sum / nData_count;
    		ucThre = ucThre_new;
    		ucThre_new = (nBack_sum + nData_sum) / 2;
    	}
    	
    	//二值化处理
    	int nBlack = 0;
    	int nWhite = 0;
    	for (j = 0; j < height; ++j) {
    		for (i = 0; i < width; ++i) {
    			nValue = (int) mat.get(j, i)[0];
    			if (nValue > ucThre_new) {
    				mat.put(j, i, WHITE);
    				nWhite++;
    			} else {
    				mat.put(j, i, BLACK);
    				nBlack++;
    			}
    		}
    	}
    
    	// 确保白底黑字
    	if (nBlack > nWhite) {
    		for (j = 0; j < height; ++j) {
    			for (i = 0; i < width; ++i) {
    				nValue = (int) (mat.get(j, i)[0]);
    				if (nValue == 0) {
    					mat.put(j, i, WHITE);
    				} else {
    					mat.put(j, i, BLACK);
    				}
    			}
    		}
    	}
    }
    

    测试二值化

    @Test
    public void binaryzation() {
    	// 这个必须要写,不写报java.lang.UnsatisfiedLinkError
    	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    
    	File imgFile = new File("C:/Users/admin/Desktop/open/test.png");
    	String dest = "C:/Users/admin/Desktop/open";
    	//先经过一步灰度化
    	Mat src = Imgcodecs.imread(imgFile.toString());
    	Mat gray = new Mat();
    	Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
    	src = gray;
    	//二值化
    	binaryzation(src);
    	Imgcodecs.imwrite(dest + "/binaryzation" + imgFile.getName(), src);
    }
    

    Opencv自己也提供了二值化的接口,好像没有上面的效果好,这里也把代码放出来

    @Test
    
    public  void  testOpencvBinary() {
    
     // 这个必须要写,不写报java.lang.UnsatisfiedLinkError
    
     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    
     File imgFile = new  File("C:/Users/admin/Desktop/open/test.png");
    
     String dest = "C:/Users/admin/Desktop/open";
    
     Mat src = Imgcodecs.imread(imgFile.toString(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    
     Imgcodecs.imwrite(dest + "/AdaptiveThreshold1" + imgFile.getName(), src);
    
     Mat dst = new Mat();
    
     Imgproc.adaptiveThreshold(src, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 13, 5);
    
     Imgcodecs.imwrite(dest + "/AdaptiveThreshold2" + imgFile.getName(), dst);
    
     Imgproc.adaptiveThreshold(src, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 13, 5);
    
     Imgcodecs.imwrite(dest + "/AdaptiveThreshold3" + imgFile.getName(), dst);
    
     Imgproc.adaptiveThreshold(src, dst, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 13, 5);
    
     Imgcodecs.imwrite(dest + "/AdaptiveThreshold4" + imgFile.getName(), dst);
    
     Imgproc.adaptiveThreshold(src, dst, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 13, 5);
    
     Imgcodecs.imwrite(dest + "/AdaptiveThreshold5" + imgFile.getName(), dst);
    
    }
    

    本文章参考了很多博客,感谢;主要是跟着一个博客来实现的https://blog.csdn.net/ysc6688/article/category/2913009(也是基于opencv来做的,只不过他是用c++实现的)感谢

  • 相关阅读:
    ExtJs2.0学习系列(2)Ext.Panel
    ExtJs2.0学习系列(1)Ext.MessageBox
    ExtJs2.0学习系列(3)Ext.Window
    微软挖IBM公司Lotus合伙人 炫耀协同软件优势
    Vector
    H264和MPEG4起始码(startcode)
    Android有趣的全透明效果Activity及Dialog的全透明(附android系统自带图标大全)
    C++中的vector使用范例
    关于Vector
    用vector取代Cstyle的数组
  • 原文地址:https://www.cnblogs.com/qjmnong/p/9169343.html
Copyright © 2011-2022 走看看