1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Processingで内臓カメラを表示するだけのコード

Last updated at Posted at 2017-07-17

Processingというより、もはやただのjava

投稿してから気づいたが、Matの中身が負値になってしまうので、byteの配列ではなくintの配列を使うべきだった……
Mat.get()オーバーロードされてるじゃん……


import org.opencv.core.*;
import org.opencv.highgui.*;

VideoCapture capture;

void setup(){  
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  capture = new VideoCapture(0);
  fullScreen();
}

void draw(){
  background(0);
   if (capture.isOpened()) {
      Mat video = new Mat();
      capture.read(video);
      
      byte [] b;
      b = new byte[(int)video.size().height * (int)video.size().width*3];
      
      video.get(0,0,b);
      PImage img = createImage((int)video.size().width,(int)video.size().height,ARGB);
      
      for(int i=0;i<(int)video.size().width;i++){
        for(int j=0;j<(int)video.size().height;j++){
          int p = (i+j*(int)video.size().width)*3;
          img.set(i,j,color(fix(b[p+2]),fix(b[p+1]),fix(b[p+0])));
        }
      }
      image(img, 0, 0, (int)video.size().width, (int)video.size().height);
    } 
}

int fix(int val){
  if(val>-1)return val;
  else{
    return 256+val;
  }
}

参考
cv::Matの基本処理
QiitaProcessingでOpenCVを使う(続き)

1
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?