2017-08-26 9 views
0

J'ai des problèmes avec l'image de l'appareil photo. Je l'aide VideoCapture et lorsque je tente l'image d'affichage en niveaux de gris, il est fonctionne parfaitement, mais lorsque je tente l'image d'affichage en couleurs que je reçois quelque chose comme ça: link Une partie de mon code source:JavaCV Afficher l'image en couleur à partir de la capture vidéo

public void CaptureVideo() 
{ 
    VideoCapture videoCapture = new VideoCapture(0); 
    Mat frame = new Mat(); 

    while (videoCapture.isOpened() && _canWorking) 
    { 
     videoCapture.read(frame); 

     if (!frame.empty()) 
     { 
      Image img = MatToImage(frame); 
      videoView.setImage(img); 
     } 

     try { Thread.sleep(33); } catch (InterruptedException e) { e.printStackTrace(); } 
    } 
    videoCapture.release(); 
} 

private Image MatToImage(Mat original) 
{ 
    BufferedImage image = null; 
    int width = original.size().width(), height = original.size().height(), channels = original.channels(); 
    byte[] sourcePixels = MatToBytes(original, width, height, channels); 

    if (original.channels() > 1) 
    { 
     image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 
    } 
    else 
    { 
     image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 
    } 
    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 
    System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); 

    return SwingFXUtils.toFXImage(image, null); 
} 

private byte[] MatToBytes(Mat mat, int width, int height, int channels) 
{ 
    byte[] output = new byte[width * height * channels]; 
    UByteRawIndexer indexer = mat.createIndexer(); 

    int i = 0; 
    for (int j = 0; j < mat.rows(); j ++) 
    { 
     for (int k = 0; k < mat.cols(); k++) 
     { 
      output[i] = (byte)indexer.get(j,k); 
      i++; 
     } 
    } 
    return output; 
} 

Tout le monde peut me dire ce que je fais mal? Je suis nouveau dans le traitement d'image et je ne comprends pas pourquoi ça ne marche pas.

+0

S'il vous plaît ajouter votre solution dans une réponse distincte voir au lieu [tournée], vous solution est [ici] (https://stackoverflow.com/revisions/45897496/2) –

Répondre

0

Ok. Je résous ça. Solution:

byte[] output = new byte[_frame.size().width() * _frame.size().height() * _frame.channels()]; 
    UByteRawIndexer indexer = mat.createIndexer(); 

    int index = 0; 
    for (int i = 0; i < mat.rows(); i ++) 
    { 
     for (int j = 0; j < mat.cols(); j++) 
     { 
      for (int k = 0; k < mat.channels(); k++) 
      { 
       output[index] = (byte)indexer.get(i, j, k); 
       index++; 
      } 
     } 
    } 
    return output;