2017-09-29 22 views
0

画像はグレースケールにカバーされますが、BufferedImage.TYPE_3BYTE_BGRのために3つのタイルに表示されます。openCVをグレースケール画像に変換する

.TYPE_BYTE_GRAYは機能しません。

グレースケールイメージを1つだけ表示するにはどうすればよいですか?

public void matToBufferedImage(Mat matRGB){ 
    Mat mGray = new Mat(); 
    Imgproc.cvtColor(matRGB, mGray, Imgproc.COLOR_BGR2GRAY); 
    int width = mGray.width(), height = mGray.height(), channels = mGray.channels(); 
    byte[] source = new byte[width * height * channels]; 
    mGray.get(0, 0, source); 

    image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 

    final byte[] target = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 
    System.arraycopy(source, 0, target, 0, source.length); 
} 

答えて

0

これらの2つの方法を使用します。だから、これらの2つのメソッドを追加した後、あなたのようなそれらを使用することになり

public static BufferedImage Mat2BufferedImage(Mat matrix) throws IOException { 
    MatOfByte mob=new MatOfByte(); 
    Imgcodecs.imencode(".jpg", matrix, mob); 
    return ImageIO.read(new ByteArrayInputStream(mob.toArray())); 
} 

public static Mat BufferedImage2Mat(BufferedImage image) throws IOException { 
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    ImageIO.write(image, "jpg", byteArrayOutputStream); 
    byteArrayOutputStream.flush(); 
    return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED); 
} 

Imgproc.cvtColor(matRGB, mGray, Imgproc.COLOR_BGR2GRAY); 

image = Mat2BufferedImage(mGray); 
+0

それはビデオキャプチャとの認識のためです。そのコードはそれでも動作するのでしょうか、それとも静止画像だけでしょうか? –

+0

ビデオは画像の集まりに過ぎません。それが動作します。どうしてあなたは自分を見ないの? – Ultraviolet

関連する問題