Keystone corner pin warp correction.

edited October 2013 in Library Questions

I'm attempting to use the keystone library for corner pin warps to correct a given trapezium to a square. Though I can do a pretty close job with my mouse in calibration mode I need a more precise and quicker option to occur automatically (given the parameters of the trapezium). Screen Shot 2013-10-25 at 10.37.43 pm Screen Shot 2013-10-25 at 10.38.08 pm

You should be able to see the intention there ^^

Someone was helping me before the forum updated and they were talking about a linear equation solution but I was struggling to implement it. My code so far:

import deadpixel.keystone.*;


Keystone ks;
CornerPinSurface surface;
PGraphics offscreen;

void setup() {
  size(1280, 720, P3D);
  smooth();
  ks = new Keystone(this);
  surface = ks.createCornerPinSurface(400, 400, 20);
  offscreen = createGraphics(400, 400, P3D);
}

void draw() {

  offscreen.beginDraw();
  offscreen.background(255); 
  offscreen.fill(#67cd47);
  offscreen.strokeWeight(2);
  offscreen.beginShape();
  offscreen.vertex(0, 50);
  offscreen.vertex(400, 0);
  offscreen.vertex(400, 400);
  offscreen.vertex(0, 250);
  offscreen.endShape(CLOSE);
  offscreen.endDraw();
  background(80);
  surface.render(offscreen);
}

void keyPressed() {
  switch (key) {
  case 'c':
    ks.toggleCalibration();
    break;
  case 't':
    surface.moveMeshPointBy(surface.BL, 0, 100); //The amout '100' needs to be replaced by the amount to move down.
    surface.moveMeshPointBy(surface.TL, 0, -100);
    break;
  }
}

Answers

  • that someone was me. hello.

    do you have the coords of the vertices of the green quad on the left?

  • edited October 2013

    Should we continue on the old post or here?

    I do have them: offscreen.vertex(0, 50); offscreen.vertex(400, 0); offscreen.vertex(400, 400); offscreen.vertex(0, 250);)

  • Answer ✓

    old forum is locked...

    PImage img;
    boolean normal = true;
    
    void setup() {
      size(700, 700, P2D);
      img = loadImage("keystone.jpg");
      textureMode(IMAGE);
    }
    
    void draw() {
      background(255);
      // centre the image
      translate((width / 2) - (img.width / 2), (height / 2) - (img.height / 2));
    
      // 0 - 1
      // |   |
      // 3 - 2
    
      float x0 = 0;    // top left
      float y0 = 0;
      float x1 = 400;  // top right
      float y1 = 0;
      float x2 = 400;  // borrom right
      float y2 = 400;
      float x3 = 0;    // bottom right
      float y3 = 400;
      if (normal) {
        // before
        beginShape(QUADS);
        texture(img);
        vertex(x0, y0, 0, 0);
        vertex(x1, y1, 400, 0);
        vertex(x2, y2, 400, 400);
        vertex(x3, y3, 0, 400);
        endShape();
    
      } else {
        // after
        float b = 50.0;
        float c = 250.0;
    
        float n = (y1 - y2) / (b - c);
        float m = y1 - (b * n);
        println("n = " + n);
        println("m = " + m);
    
        float ya = m + n * y0;
        float yd = m + n * y3;
        println("ya = " + ya);
        println("yd = " + yd);
    
        beginShape(QUADS);
        texture(img);
        vertex(x0, ya, 0, 0);
        vertex(x1, y1, 400, 0);
        vertex(x2, y2, 400, 400);
        vertex(x3, yd, 0, 400);
        endShape();
      }
      noLoop();
    }
    
    void keyReleased() {
      normal = !normal;
      loop();
    }
    

    keystone

    texture is a 400x400 image with the green bit drawn (crudely) on it as per coords given above. press any key and it'll redraw the shape with new coords so that the green bit of the texture fills the original square. press another and it'll pop back.

  • thinking about it, i may have just re-written lerp().

  • Any idea when this little bit is sticking out? Works perfectly well using the keystone corner pin warp but for me when I use textures I get this.

    Screen Shot 2013-10-31 at 9.43.05 PM

    Otherwise absolutely perfect: thank you so much!!

  • i think this is the usual texture problem that shows up from time to time, something to do with the shape being two triangles rather than one quad. see how the tip of the lump is in line with a line from bottom right and top left?

    the usual way around this is to split the shapes and textures up into smaller pieces, but that's a pain in this case.

    not sure why i didn't see that. maybe i wasn't looking hard enough? i'm using P2D and version 1.5.1...

Sign In or Register to comment.