【问题标题】:Converting an RGB image to a YCbCr color space image将 RGB 图像转换为 YCbCr 颜色空间图像
【发布时间】:2018-08-26 17:18:46
【问题描述】:

谁能帮助我使用 opencv Android 将 RGB 颜色空间图像转换为 YCbCr 颜色空间图像?

【问题讨论】:

  • 你搜索过这个网站吗?您使用什么语言?
  • 是的,我已经搜索过它,但没有得到任何有意义的东西。我正在使用java语言。
  • 我已将图像从 drawable 转换为 YCrCb 颜色空间。但实际上我想将捕获的图像转换为 YCrCb 颜色空间。
  • public void onClick(View v) { Bitmap img = BitmapFactory.decodeResource(getResources(),R.drawable.android);垫源 = 新垫();垫 dest = 新垫();实用程序。 bitmapToMat(img, 源);图像处理。 cvtColor(源,目标,Imgproc.COLOR_BGR2YCrCb);位图 btmp = Bitmap.createBitmap(dest.width(),dest.height(),Bitmap.Config.ARGB_8888);实用程序。 matToBitmap(dest,btmp);图像视图1。设置图像位图(btmp); }
  • 我如何在运行时使用它(通过相机捕获的图像)。我对 android 完全陌生。

标签: android image opencv image-processing


【解决方案1】:

有一个很好的例子来说明如何将 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());
      }
   }
}

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 2018-04-09
    • 1970-01-01
    • 2019-01-01
    • 2021-03-01
    • 1970-01-01
    • 2014-11-28
    • 2014-06-17
    • 2011-03-02
    相关资源
    最近更新 更多