13
11

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でOpenCVを使う(続き)

Last updated at Posted at 2015-06-07

PImageとMatの変換について
前の記事ではProcessing上でOpenCVを使う方法を書いたけど
MatのままではProcessingで表示できないのでPImageへ変換します
この変換はライブラリ化している方がいたので"Mat(OpenCV)とBufferedImageとPImage(Procesing)を相互変換可能なライブラリを作ってみた"にあるImageTranslaterライブラリを使えば簡単にできる

下記のサンプルではOpenCVのVideoCaptureクラスでキャプチャを撮り
特徴点を検出してからMatからPImageへ変換して表示している
検出した特徴点はProcessingのellipseで描画している

TestOpenCV.pde
import imageTranslater.*;
import org.opencv.core.*;
import org.opencv.imgproc.*;
import org.opencv.highgui.*;
import org.opencv.features2d.*;

ImageTranslater imgTrans;
VideoCapture capture;
FeatureDetection feature;
Mat captureImage;
PImage img;

void setup() {
  imgTrans = new ImageTranslater(this);
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  capture = new VideoCapture(0);
  feature = new FeatureDetection(FeatureDetector.SIFT);
  captureImage = new Mat();
  size(640, 480);
}

void draw() {
  background(0);
  noStroke();
  fill(255, 255, 0, 100);
  if (capture.isOpened()) { 
    capture.read(captureImage);
    Size sz = new Size(width, height);
    Mat resizeimage = new Mat();
    Imgproc.resize(captureImage, resizeimage, sz);
    feature.setImage(resizeimage);
    feature.detection();
    image(imgTrans.MatToPImageRGB(this, resizeimage), 0, 0);
    for (Point p : feature.getPoints ()) {
      ellipse((float)p.x, (float)p.y, 10, 10);
    }
  }
}
FeatureDetection.pde
public class FeatureDetection {

  Mat image;
  FeatureDetector featuer;
  MatOfKeyPoint keyPoints;

  public FeatureDetection(int _type) {
    image = new Mat();
    featuer = FeatureDetector.create(_type);
    keyPoints = new MatOfKeyPoint();
  }

  public void detection() {
    featuer.detect(image, keyPoints);
  }

  public Point[] getPoints() {
    Point[] points = new Point[keyPoints.toArray().length];
    for (int i = 0; i < points.length; i++) {
      points[i] = keyPoints.toArray()[i].pt;
    }
    return points;
  }

  public void setImage(Mat _image) {
    image = _image;
  }
}

TestOpenCV.png

もしくは"OpenCV 2.4.4 and Processing"ではPImage->BufferedImage->Matのように変換している

参考 :
ImageBufferへの変換
http://www.magicandlove.com/blog/2013/04/04/opencv-2-4-4-and-processing/
変換ライブラリ
https://sites.google.com/site/gutugutu30/other/matopencvtobufferedimagetopimageprocesingwoxianghubianhuankenengnaraiburariwozuottemita

13
11
0

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
13
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?