【问题标题】:Contour position with "findcontour" opencv on processing处理时使用“findcontour”opencv 的轮廓位置
【发布时间】:2016-09-07 16:45:49
【问题描述】:

我正在做一个项目,我必须使用网络摄像头、arduino、覆盆子和红外接近传感器。我在谷歌的帮助下完成了一切。但我有一个大问题,我真的认为。 我正在使用 OpenCV 库进行处理,我希望网络摄像头获得的轮廓位于草图的中心。但是 All 只是为了移动视频而不是轮廓,这是我的代码。 我希望你能帮助我:)

一切顺利

亚历山大

////////////////////////////////////////////
////////////////////////////////// LIBRARIES
////////////////////////////////////////////

import processing.serial.*;
import gab.opencv.*;
import processing.video.*;




/////////////////////////////////////////////////
////////////////////////////////// INITIALIZATION
/////////////////////////////////////////////////

Movie mymovie;
Capture video;
OpenCV opencv;
Contour contour;




////////////////////////////////////////////
////////////////////////////////// VARIABLES
////////////////////////////////////////////

int lf = 10;    // Linefeed in ASCII
String myString = null;
Serial myPort;  // The serial port
int sensorValue = 0;
int x = 300;




/////////////////////////////////////////////
////////////////////////////////// VOID SETUP
/////////////////////////////////////////////

void setup() {
  size(1280, 1024);
  // List all the available serial ports
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
  opencv = new OpenCV(this, 720, 480);
  video = new Capture(this, 720, 480);
  mymovie = new Movie(this, "visage.mov");
  opencv.startBackgroundSubtraction(5, 3, 0.5);
  mymovie.loop();
}




////////////////////////////////////////////
////////////////////////////////// VOID DRAW
////////////////////////////////////////////

void draw() { 
  image(mymovie, 0, 0);
  image(video, 20, 20);
  //tint(150, 20);
  noFill();
  stroke(255, 0, 0);
  strokeWeight(1);



  // check if there is something new on the serial port
  while (myPort.available() > 0) {
    // store the data in myString 
    myString = myPort.readStringUntil(lf);
    // check if we really have something
    if (myString != null) {
      myString = myString.trim(); // let's remove whitespace characters
      // if we have at least one character...
      if (myString.length() > 0) {
        println(myString); // print out the data we just received
        // if we received a number (e.g. 123) store it in sensorValue, we sill use this to change the background color. 
        try {
          sensorValue = Integer.parseInt(myString);
        } 
        catch(Exception e) {
        }
      }
    }
  }
  if (x < sensorValue) {
    video.start();
    opencv.loadImage(video); 
  }

  if (x > sensorValue) {
    image(mymovie, 0, 0);
  }

  opencv.updateBackground();
  opencv.dilate();
  opencv.erode();

  for (Contour contour : opencv.findContours()) {
    contour.draw();
  }

}




//////////////////////////////////////////////
////////////////////////////////// VOID CUSTOM
//////////////////////////////////////////////

void captureEvent(Capture video) {
  video.read(); // affiche l'image de la webcam
}

void movieEvent(Movie myMovie) {
  myMovie.read();
}

【问题讨论】:

标签: opencv arduino processing webcam


【解决方案1】:

您可以使用的一种方法是在调用contour.draw() 之前调用translate() 函数来移动画布的原点。像这样的:

translate(moveX, moveY);

for (Contour contour : opencv.findContours()) {
    contour.draw();
}

您对moveXmoveY 的使用完全取决于您要执行的操作。您可以使用您用来绘制视频的任何位置(如果您希望在视频顶部显示轮廓),或者您可以使用width/2height/2(可能减去一点以使轮廓真正居中)。

更多信息请访问the reference。玩一堆不同的值,如果遇到困难,请发布MCVE。祝你好运。

【讨论】:

  • 嗨,凯文,非常感谢,使用“translate();”效果很好如您所说,使用 width/x 和 height/x 。再次感谢:)
猜你喜欢
  • 1970-01-01
  • 2019-07-18
  • 2011-12-30
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多