java识别照片是彩色还是黑白照

RGB与HSB  

rgb三个分量的值相等时即为灰度图,值的大小体现了图的灰度。
彩图的颜色就是通过rgb值的差异进行体现的,rgb不等值的情况,只说明这不是完全的灰度图,
或者叫做彩色饱和度不高的彩图,只是肉眼分辨不出来。
为此,要将rgb模式转换为hsb模式再去判断,其中:h色相,s饱和度,b对比度。
转换为hsb后,判断饱和度,如果s<10%即可认为是灰度图,至于这个阈值是10%还是15%,需要根据实际情况来做判断;

 

 

RGB与HSB是什么 

1.RGB是一种加色模型,就是将不同比例的Red/Green/Blue混合在一起得到新颜色.通常RGB颜色模型表示为:
2.HSB(HSV) 通过色相/饱和度/亮度三要素来表达颜色.
H(Hue):表示颜色的类型(例如红色,绿色或者黄色).取值范围为0—360.其中每一个值代表一种颜色.
S(Saturation):颜色的饱和度.从0到1.有时候也称为纯度.(0表示灰度图,1表示纯的颜色)
B(Brightness or Value):颜色的明亮程度.从0到1.(0表示黑色,1表示特定饱和度的颜色)

 

RGB到HSV(HSB)的转换 公式


 

 

HSV(HSB)到RGB的转换公式 写道

 

 

代码:

Java代码 

 收藏代码

  1. /** 
  2.      * rgb2hsb 单独 RGB转HSB 
  3.      * @param rgbR 
  4.      * @param rgbG 
  5.      * @param rgbB 
  6.      * @return float[] 
  7.      */  
  8.       
  9.     public static float[] rgb2hsb(int rgbR, int rgbG, int rgbB) {    
  10.         assert 0 <= rgbR && rgbR <= 255;    
  11.         assert 0 <= rgbG && rgbG <= 255;    
  12.         assert 0 <= rgbB && rgbB <= 255;    
  13.         int[] rgb = new int[] { rgbR, rgbG, rgbB };    
  14.         float[] hsb = new float[5];  
  15.         Arrays.sort(rgb);    
  16.         int max = rgb[2];    
  17.         int min = rgb[0];    
  18.         
  19.         float hsbB = max / 255.0f;    
  20.         float hsbS = max == 0 ? 0 : (max - min) / (float) max;    
  21.         
  22.         float hsbH = 0;    
  23.         if (max == rgbR && rgbG >= rgbB) {    
  24.             hsbH = (rgbG - rgbB) * 60f / (max - min) + 0;    
  25.         } else if (max == rgbR && rgbG < rgbB) {    
  26.             hsbH = (rgbG - rgbB) * 60f / (max - min) + 360;    
  27.         } else if (max == rgbG) {    
  28.             hsbH = (rgbB - rgbR) * 60f / (max - min) + 120;    
  29.         } else if (max == rgbB) {    
  30.             hsbH = (rgbR - rgbG) * 60f / (max - min) + 240;    
  31.         }    
  32.         hsb[0] = hsbH;  
  33.         hsb[1] = hsbS;  
  34.         hsb[2] = hsbB;  
  35.         return hsb;    
  36.     }    
  37.         
  38.     /** 
  39.      * hsb2rgb  单独 HSB转RGB 
  40.      * @param h 
  41.      * @param s 
  42.      * @param v 
  43.      * @return int[] 
  44.      */  
  45.     public static int[] hsb2rgb(float h, float s, float v) {    
  46.         assert Float.compare(h, 0.0f) >= 0 && Float.compare(h, 360.0f) <= 0;    
  47.         assert Float.compare(s, 0.0f) >= 0 && Float.compare(s, 1.0f) <= 0;    
  48.         assert Float.compare(v, 0.0f) >= 0 && Float.compare(v, 1.0f) <= 0;    
  49.         
  50.         float r = 0, g = 0, b = 0;    
  51.         int i = (int) ((h / 60) % 6);    
  52.         float f = (h / 60) - i;    
  53.         float p = v * (1 - s);    
  54.         float q = v * (1 - f * s);    
  55.         float t = v * (1 - (1 - f) * s);    
  56.         switch (i) {    
  57.         case 0:    
  58.             r = v;    
  59.             g = t;    
  60.             b = p;    
  61.             break;    
  62.         case 1:    
  63.             r = q;    
  64.             g = v;    
  65.             b = p;    
  66.             break;    
  67.         case 2:    
  68.             r = p;    
  69.             g = v;    
  70.             b = t;    
  71.             break;    
  72.         case 3:    
  73.             r = p;    
  74.             g = q;    
  75.             b = v;    
  76.             break;    
  77.         case 4:    
  78.             r = t;    
  79.             g = p;    
  80.             b = v;    
  81.             break;    
  82.         case 5:    
  83.             r = v;    
  84.             g = p;    
  85.             b = q;    
  86.             break;    
  87.         default:    
  88.             break;    
  89.         }    
  90.         return new int[] { (int) (r * 255.0), (int) (g * 255.0),    
  91.                 (int) (b * 255.0) };    
  92.     }    
  93.       

 

 

  

读取一张图片的RGB值  


Java代码 

 收藏代码

  1. /** 
  2.      * 读取一张图片的RGB值   
  3.      * @param image 
  4.      * @return 
  5.      * @throws Exception 
  6.      */  
  7.     public static ArrayList<int[]> getImagePixel(File imagefile) throws Exception {    
  8.          
  9. //        File file = new File(image);    
  10.         BufferedImage bi = null;    
  11.         ArrayList<int[]> rgblist = new ArrayList<int[]>();  
  12.         try {    
  13.             bi = ImageIO.read(imagefile);    
  14.         } catch (Exception e) {    
  15.             e.printStackTrace();    
  16.         }    
  17.         int width = bi.getWidth();  //返回 BufferedImage 的宽度。  
  18.         int height = bi.getHeight();  //返回 BufferedImage 的高度。  
  19.         int minx = bi.getMinX();  //返回此 BufferedImage 的最小 x 坐标。  
  20.         int miny = bi.getMinY();  //返回此 BufferedImage 的最小 y 坐标。  
  21.         System.out.println("width=" + width + ",height=" + height + ".");    
  22.         System.out.println("minx=" + minx + ",miniy=" + miny + ".");    
  23.  //循环遍历添加至list中         
  24.         for (int i = minx; i < width; i++) {    
  25.             for (int j = miny; j < height; j++) {    
  26.                 int[] rgb = new int[5];  //rgb数组  
  27.                 int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字    
  28.                 rgb[0] = (pixel & 0xff0000) >> 16;  //AB位  
  29.                 rgb[1] = (pixel & 0xff00) >> 8;   //CD位  
  30.                 rgb[2] = (pixel & 0xff);            //EF位  
  31.                 rgb[3] = i; // x 坐标。  
  32.                 rgb[4] = j; //y 坐标。  
  33.                 rgblist.add(rgb);  
  34. //                System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + ","    
  35. //                        + rgb[1] + "," + rgb[2] + ")");    
  36.             }    
  37.         }    
  38.         return rgblist;  
  39.     }    

 

 

 

package com.main;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

public class ImageColorArea {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//		System.out.println("Welcome to OpenCV " + Core.VERSION);

		// 读入图片D:\\test\\imgs\\scsc.png
		Mat image = Imgcodecs.imread("D:\\test\\imgs\\zqzm.png");
		if (image.empty())
			return;
		
		Mat newimage = colorArea(image);
	}
	public static Mat colorArea(Mat image) {
		int width = image.cols();
		int height = image.rows();
		int dims = image.channels();
		byte[] data = new byte[width * height * dims];
		image.get(0, 0, data);

		int index = 0;
		int r = 0, g = 0, b = 0;
		long allpoint = width*height;
		long whitepoint = 0;
		long blackpoint = 0;
		long colorpoint = 0;
		long redpoint = 0;
		for (int row = 0; row < height; row++) {
			for (int col = 0; col < width * dims; col += dims) {
				index = row * width * dims + col;
				b = data[index] & 0xff;
				g = data[index + 1] & 0xff;
				r = data[index + 2] & 0xff;

//				float[] cmyklist = ColorUtils.getCMYK(r, g, b);
//				if(cmyklist[3]>0&&cmyklist[0]==0&&cmyklist[1]==0&&cmyklist[2]==0){
//					System.out.println("什么颜色:"+r+","+g+","+b+"cmyk:"+cmyklist[0]+","+cmyklist[1]+","+cmyklist[2]+","+cmyklist[3]);
//					blackpoint++;
//				}
				
				if(r==255 && g==255 &&b==255){
					whitepoint++;
				}else{
					
					float[] hsb = rgb2hsb(r,g,b);
					if(hsb[1]==0 || hsb[2]==0 ||(hsb[1]<0.15 && hsb[2]<0.15)){
						blackpoint++;
					}else{
						colorpoint++;
					}
					
//					long colorsum = (long) (0.2126 * r + 0.7152 * g + 0.0722 * b);				
//					if(colorsum<40){
//						blackpoint++;
//					}else{
//						colorpoint++;
//					}
				}
				
//				if(r==255 && g==255 &&b==255){
//					whitepoint++;
//				}else
//				if(r==0 && g==0 &&b==0){
//					blackpoint++;
//				}else
//				if(r==255 && g==0 &&b==0){
//					redpoint++;
//				}else{
////					float[] cmyklist = ColorUtils.getCMYK(r, g, b);
////					System.out.println("什么颜色:"+r+","+g+","+b+"cmyk:"+cmyklist[0]+","+cmyklist[1]+","+cmyklist[2]+","+cmyklist[3]);
//				}
			}
		}
		BigDecimal allpoint_bd = new BigDecimal(allpoint);  
		BigDecimal whitepoint_bd = new BigDecimal(whitepoint);  
		BigDecimal blackpoint_bd = new BigDecimal(blackpoint);  
		BigDecimal colorpoint_bd = new BigDecimal(colorpoint);  
		BigDecimal redpoint_bd = new BigDecimal(redpoint);  
		System.out.println("总点数:"+allpoint_bd+"白色点数:"+whitepoint_bd+"黑色点数:"+blackpoint_bd+"彩色点数:"+colorpoint_bd);
		System.out.println("白色区域比例:"+whitepoint_bd.divide(allpoint_bd,10,RoundingMode.HALF_UP)+"黑色区域比例:"+blackpoint_bd.divide(allpoint_bd,10,RoundingMode.DOWN)+"彩色区域比例:"+colorpoint_bd.divide(allpoint_bd,10,RoundingMode.DOWN));
		
		image.put(0, 0, data);
		return image;
	}
	public static float[] rgb2hsb(int rgbR, int rgbG, int rgbB) {  
	    assert 0 <= rgbR && rgbR <= 255;  
	    assert 0 <= rgbG && rgbG <= 255;  
	    assert 0 <= rgbB && rgbB <= 255;  
	    int[] rgb = new int[] { rgbR, rgbG, rgbB };  
	    float[] hsb = new float[5];
	    Arrays.sort(rgb);  
	    int max = rgb[2];  
	    int min = rgb[0];  
	  
	    float hsbB = max / 255.0f;  
	    float hsbS = max == 0 ? 0 : (max - min) / (float) max;  
	  
	    float hsbH = 0;  
	    if (max == rgbR && rgbG >= rgbB) {  
	        hsbH = (rgbG - rgbB) * 60f / (max - min) + 0;  
	    } else if (max == rgbR && rgbG < rgbB) {  
	        hsbH = (rgbG - rgbB) * 60f / (max - min) + 360;  
	    } else if (max == rgbG) {  
	        hsbH = (rgbB - rgbR) * 60f / (max - min) + 120;  
	    } else if (max == rgbB) {  
	        hsbH = (rgbR - rgbG) * 60f / (max - min) + 240;  
	    }  
	    hsb[0] = hsbH;
	    hsb[1] = hsbS;
	    hsb[2] = hsbB;
	    return hsb;  
	}	
}