在图像上绘制位置指示器

时间:2014-11-23 14:38:05

标签: java processing

所以,这是问题,我需要绘制与我的手位置对应的位置指示器,然后对图像执行一些操作

这是屏幕截图:http://i.stack.imgur.com/WZl26.jpg

屏幕的左半部分是图像,屏幕的右半部分是我的相机, 程序将绘制对应于我的手位置的位置指示器, 我的问题是光标不能消失,它会画很多次!

这是代码:

import gab.opencv.*;
import processing.video.*;
import java.awt.*;

PImage img;
PImage select;
PImage cur;

OpenCV opencv;
Capture cam;

int prevPositionX, prevPositionY, currPositionX, currPositionY;
int mode = -1; //mode 1 = s (select) mode 2 = c (copy) mode 3 = d (draw)
int select_ind = -1;

//store every dectected things
Rectangle[] hand;

//store the biggest hand
Rectangle bhand;


void setup() {
    size(1280, 480);

    img = loadImage("test.jpg");
    cur  = loadImage("cursor.png");
    stroke(255,10,0); 

    opencv = new OpenCV(this, 640, 480);
    opencv.loadCascade("aGest.xml");
    cam = new Capture(this, 640, 480);
    cam.start();

    image(img, 0, 0, img.width, img.height);
    }


  void draw(){  
  if (cam.available()==true) {
    cam.read();
  }

  opencv.loadImage(cam);
  hand = opencv.detect();

  pushMatrix();
  scale(-1.0, 1.0);
  image(cam, -1280, 0);
  popMatrix();

int handcount = -1;
int handsize = -1;

  //calculate the biggest hand
  for( int i=0; i < hand.length; i++ ) {
    if(handsize <  (hand[i].width * hand[i].height)){
      handsize = hand[i].width * hand[i].height;
      handcount = 1;
      bhand = hand[i];      
    }
  }

  if(handcount > 0){
      rect(1280 - bhand.x, bhand.y, -bhand.width, bhand.height);
      noFill(); 

      //draw the position indicator   
      image(cur, 480 - bhand.x, bhand.y, 16, 16);   

      prevPositionX = currPositionX;
      prevPositionY = currPositionY;
      currPositionX = 480 - bhand.x + 4;
      currPositionY = bhand.y;

      //select mode
      if (mode == 1){
      }

      //copy mode
      else if (mode == 2){
      }

      //draw mode
      else if (mode == 3){
        line(prevPositionX,prevPositionY,currPositionX,currPositionY);
      }
  }    
}


void keyPressed(){
    if(key=='s'||key=='S')
    mode = 1;

    else if(key=='c'||key=='C')
    mode = 2;

    else if(key=='d'||key=='D')  
    mode = 3;

    else if(key=='i'||key=='I')
    image(img, 0, 0, img.width, img.height);
    }



void keyReleased(){
    if(select_ind > -1 && mode == 2){ 
    //to be done

    }

    mode = -1;
    }

我正在使用绘图模式,即在图像上画一条线, 我知道问题,但我不知道如何解决它, 我需要添加:image(img,0,0,img.width,img.height);到第一个 draw()函数,但该行也将被删除。我想保留 线条像屏幕截图。 请帮帮我,对不好的英语抱歉。感谢

1 个答案:

答案 0 :(得分:0)

如果您需要仅保留部分绘制,则需要每帧重绘一次,同时仍然使用image(img, 0, 0, img.width, img.height)“清除”背景。这意味着存储行的坐标并每次重绘它,还要注意你可以隐藏光标...... SomeThing如下:

// YOU GOT ADD (CLICK) AT LEAST 2 POINTS TO SEE IT WORKING ;)

ArrayList<PVector> positions  = new ArrayList<PVector>();

void setup(){
  size(600, 600);

  //if you don't want to see the cursor...
  noCursor();    
}

void draw(){
  //clear screen
  background(255);

  //always draw at mousePosition
  //a "custom cursor"
  //that will not persist as screen is beeing cleared
  fill(200,80,220);
  noStroke();
  ellipse(mouseX, mouseY, 20, 20);

  stroke(80);
  PVector prevP = null;

  for (PVector p:positions) {
    if(prevP != null) {
      line(prevP.x, prevP.y, p.x, p.y);
    }

    prevP = p.get();
  }
}

void mouseClicked() {
  positions.add(new PVector(mouseX, mouseY));
}

修改 你也可以使用PGraphics作为图层来保留图形的一部分,而不是一遍又一遍地重绘所有的东西......)