将RGB图像转换为YCbCr颜色空间图像

时间:2018-03-17 19:31:32

标签: android image opencv image-processing

任何人都可以帮我使用opencv Android将RGB色彩空间图像转换为YCbCr色彩空间图像吗?

1 个答案:

答案 0 :(得分:0)

有一个很好的例子,说明如何将RGB图像转换为HSV颜色空间here。  使用该代码并将颜色空间字段转换值更改为COLOR_RGB2YCrCb:

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;

import java.io.File;
import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class Main {
   public static void main( String[] args ) {

      try {
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         File input = new File("digital_image_processing.jpg");
         BufferedImage image = ImageIO.read(input); 
         byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
         Mat mat = new Mat(image.getHeight(),image.getWidth(), CvType.CV_8UC3);
         mat.put(0, 0, data);

         Mat mat1 = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
         Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2YCrCb)//original Imgproc.COLOR_RGB2HSV);

         byte[] data1 = new byte[mat1.rows()*mat1.cols()*(int)(mat1.elemSize())];
         mat1.get(0, 0, data1);
         BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), 5);
         image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);

         File ouptut = new File("hsv.jpg");
         ImageIO.write(image1, "jpg", ouptut);

      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}