im trying to map the screen size of a video, what am i missing?

JaiJai
edited March 2016 in Arduino
int x = 0;
int y = 0;
int valx;
int valy;

void setup() {
  size(200, 200);
  background(0);

}

void draw() {
valx = map(x, 0, 195, 0, 80);
valy = map(y, 0, 195, 0, 80);

  println("X" + " ", mouseX, "                          Y" + " ", mouseY);
}

i even try this and nothing

valx = map(x, mouseX, mouseX, 0, 80);
valy = map(y, mouseY, mouseY, 0, 80); 

Answers

  • edited March 2016
    1. When mapping you should use float (float valx, float valy)
    2. Both your x and y are set to 0. When you map them you'll get back 0.
    3. You are printing mouseX and mouseY, not valx, and valy. They are unrelated.

    Here's one way to use the function:

    float valx;
    float valy;
    float videoWidth = 800;
    float videoHeight = 600;
    
    void setup() {
      size(200, 200);
      background(0);
    
    }
    
    void draw() {
    valx = map(mouseX, 0, width, 0, videoWidth);
    valy = map(mouseY, 0, height, 0, videoHeight);
    
      println("X ", valx, "       Y ", valy);
    }
    
  • JaiJai
    edited March 2016

    @Eeyorelife where are you getting this values from?

    float videoWidth = 800;
    float videoHeight = 600;
    

    and more importantly why did you ignore my map value of 0, 80); ? this is the most important part of the map being used.

  • this is debugging

    println("X ", valx, " Y ", valy);

  • I didn't know which numbers were important or not, I just made an example where you have a video of height 600 and width 800.

    The thing is, you were displaying the mouseX and mouseY, which doesn't change whether you map some numbers or not.

  • he's mapping the range mouseX to mouseX onto the range 0 to 80

    but the range mouseX to mouseX is nonsensical, there is no range there as both values are the same.

    tbh i'm struggling with this question due to lack of details. 'map the screen size of a video'? what does that mean? map it to what?

  • @koogs what do you mean map it to what ?? lol it says right there map it to 80 im not sure why is that so hard to comprehend ? map the range of the screen to the range of 80 so 0 x 0 y map that to 0 range of x and 0 range of y so that when the mouse gets dragged across the entire screen we always have range of 0 through 80 no matter where in the screen you have gone, but obviously the more you travel outside of screen x and y the more you increment on the rang of 0-80

  • edited March 2016

    ... that when the mouse gets dragged across the entire screen we always have range of 0 through 80 no matter where in the screen you have gone, ...

    If you read map()'s reference: https://processing.org/reference/map_.html
    You'll see it doesn't clamp/constrain the resultant range in any way:

    ... numbers outside of the range are not clamped to the minimum and maximum parameters values, ...

    For that we need to use constrain(): https://Processing.org/reference/constrain_.html

  • JaiJai
    edited March 2016

    hmmm i come from arduino and in arduino this action im looking to do would do just that, i thought processing was just like arduino, though i must say im not looking to constrain anything i want to be able to move across the entire screen

  • edited March 2016

    AFaIK, when converting some range to another range via map(), we've gotta be "frank" about the actual range of the former.

    mouxeX's range is from 0 up to width - 1. mouxeY's range is from 0 up to height - 1.

    Let's say you wanna convert that range pair to vid.width, vid.height:

    float x = map(mouseX, 0, width,  0, vid.width);
    float y = map(mouseY, 0, height, 0, vid.height);
    

    That's pretty much like @Eeyorelife's former solution btW. :>

  • BUT AGAIN where is my 80 >? why do you guys keep taking my 80 out of the map THIS IS WHAT I WANT TO MAP IT TO ! lol do we need to do 2 diff map functions and then contain one map function inside a variable then use that variable to use it in the second map call ?

    mouseX and mouseY AND screen width & height should be the same thing?! the second should be that 0, 80);

    finally why is the screen datatype a float? there is no 0.0 is 0-what ever the size of the screen in pixels thats a int not a float even RGB's are int

  • lol it says right there map it to 80 im not sure why is that so hard to comprehend ?

    the lack of punctuation?

  • you still understood what i said right? is not like that dot was going to translate my msg into something else right? and is not like you had to hold your breathe till your done reading right?

    come on now what is this grammar academy ? if i had the same education as you i wouldn't be here asking question.

  • edited March 2016

    BUT AGAIN where is my 80 >?

    vid.height can be ANYTHING. You can switch it out with 80 if that's the perfect number!

    THIS IS WHAT I WANT TO MAP IT TO !

    What do you want to map? You want to map "it" to the range 0 - 80, but what is "it"?

    mouseX and mouseY AND screen width & height should be the same thing?!

    mouseX and mouseY are not one value, they can be any value from 0 to width/height-1. width and height, once they are set with size(), are one value.

    finally why is the screen datatype a float? there is no 0.0 is 0-what ever the size of the screen in pixels thats a int not a float even RGB's are int

    In a sketch with size(200,200) you have 200 pixels on the x-axis and 200 pixels on the y-axis. But you can place a point between two pixels (150.5). So in order to be able use those in-between points you will have to use a float.

    but obviously the more you travel outside of screen x and y the more you increment on the rang of 0-80

    Processing doesn't detect your mouse position outside of the window you are running, so you won't get a value larger than 79.6.

  • three people here don't understand the question. which suggests the question is unclear.

    and all their attempts at answers or asking you to clarify have been met with derision.

    you're the one wanting help. try and be clearer. and yes, punctuation and proper sentences do help.

    alternatively, draw us a picture.

  • edited March 2016

    But you can place a point between two pixels (150.5).

    Actually pixels are always integral values. When it comes the time to render them, coordinates are automatically round() 1st. ;)

    Three people here don't understand the question. Which suggests the question is unclear.

    My new theory is what he's actually looking for is mouse to rectangle intersection. >-)
    That is, he wants to know whether the mouse pointer is hovering above his Movie frame.

    // For rectMode(CORNER) & imageMode(CORNER):
    boolean isMouseOverRect(int x, int y, int w, int h) {
      return mouseX >= x & mouseX < x+w & mouseY >= y & mouseY < y+h;
    }
    
  • edited March 2016

    i think he just wants to map mouse position to 0-80 - map(mouseX, 0, width, 0, 80);

    but possibly with the added complication that he wasn't to use the full width of the screen, not just the processing window. which means he'll need to use java.awt.Robot (which i've never used. does it even work with processing3?)

    i think the 'video' in the subject line is irrelevant and has thrown us all off.

  • Guys first of thanks for trying without the attitude i get into huge fights with people over stack exchange because they want to be smart asses and they assume that everyone is as "smart as they are" (NOT!), you guys really try and i appreciate it a lot!

    NOW for @GoToLoop thanks for enforcing my point about the int in pixels i been really hard core on Daniel Shiftman youtube's so i think i knew what i was saying was true, as far as what im trying to do i think out of everyone @Eeyorelife should have a more clear idea of what im looking to do considering that i wrote him a personal msg being extremely detail with supporting example code, so im not sure why is he even asking?! oh well i guess ima have to say whats the dam 80 for lol specially since @Koogs keep reiterating what everyone's vote here is the not understanding where im going.

    Guys that 80 is the degrees in range of my servo over at arduino, im trying to do Visual Servoing i already am controlling the servo using the mouse so now i want to NOT use the mouse and use the face detection found in OpenCV in Processing as the mouse, "so rather then the mouse be hovering over the video screen and thus the servo moves across it following it.

    NOW as Koogs just stated about the throwing off THIS IS THE MAIN REASON why i did not want to mention anything about the 80 cuz i just wanted you guys to focus on the current issue which is the MAP FUNCTION, feel me? if i would of just chub the whole entire sketch in here which is in many tabs not all in one i would of been here all day just copy and pasting and then another entire day talking about my way of programming and another day talking about why my approach and THIS is what i did not want.

    Processing already has a servo mouse follow sketch which i been complaining about on the github for awhile now because they uploaded a sketch that does not work and they want to give me attitude because i called dem out on the sloppy job, like what da hell i thought feedback was how people get better not by ignoring it or fighting with those who found bugs or issues with their uploads

  • edited March 2016
    float valx = map(ocvX, 0, videoWidth, 0, 80);
    float valy = map(ocvY, 0, videoHeight, 0 , 80);
    

    This assumes that ocvX / ocvY represents a pixel position in the video frame and that the video size is videoWidth x videoHeight. This position is mapped to the range 0-80 for the arduino

  • JaiJai
    edited March 2016
    import gab.opencv.*;
    import processing.video.*;
    import java.awt.*;
    import processing.serial.*;       
    
    Serial port;
    Capture video;
    OpenCV opencv;
    
    void setup()
    {
      size(720, 720);
      video = new Capture(this, 640/2, 480/2, "Microsoft LifeCam Front");
      opencv = new OpenCV(this, 640/2, 480/2);
      opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
    
      frameRate(100);
      println(Serial.list());
      video.start();
    }
    
    void draw()
    {
      //  draw2();
      draw3();
    }
    
    void draw2() {
      scale(2);
      opencv.loadImage(video);
    
      image(video, 0, 0);
    
      noFill();
      stroke(0, 255, 0);
      strokeWeight(1);
      Rectangle[] faces = opencv.detect();
      println(faces.length);
    
      for (int i = 0; i < faces.length; i++) {
        println(faces[i].x + "," + faces[i].y);
        rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
      }
    }
    
    void draw3()
    {
    
      fill(255, 0, 0); //rgb value so RED
      rect(360, 350, mouseX-360, 20);
    
      fill(0, 255, 0); // and GREEN
      rect(350, 360, 20, mouseY-360);
    }
    
    void captureEvent(Capture c) {
      c.read();
    }
    
  • ...considering that i wrote him a personal msg being extremely detail with supporting example code...

    You sendt me the whole code and this: "thats what im looking to fix without the drawing of the bar"

    I am still confused, do you want to map the valuea of the mouse position or not? If you do then @koogs has already given you this: map(mouseX, 0, width, 0, 80);

  • why cant i get the draw3 to over lap the draw2 ? @Quark i had this issue before with merging one of your old game example and as you can see i never actually learned nothing about what you were trying to get me to do because im now faced with the same issue as before with another completely diff code

    star-fox-64_sep6-13_51_19

  • Which version of Processing are you using?

  • JaiJai
    edited March 2016

    2.2.1

  • @Eeyorelife i have already made myself clear in my long most recent msg

  • edited March 2016

    I can't run your code example above because it doesn't recognize the library imported with

    import gab.opencv.*;

  • JaiJai
    edited March 2016

    why is it so hard to map the area of the screen using pixel to send the values over to serial over to the Servo

      Rectangle[] faces = opencv.detect();
      println(faces.length);
      for (int i = 0; i < faces.length; i++) {
        println(faces[i].x + "," + faces[i].y);
        rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
      }
    

    why cant i just use

      port.write(xpos+"x");
      port.write(ypos+"y");
    

    so that the servo can just follow my face ?

    everything runs good with no issues, face detection works camera is obviously running and the arduino tx/rx is flickering fast implying that it is getting values. so WTF?

    if (faces.length > 0) {
    
      midFaceY = faces[0].y + (faces[0].height/2);
      midFaceX = faces[0].x + (faces[0].width/2);
      // map to 0-80
      float px = map(midFaceX, 0, video.width, 0, 80);
      float py = map(midFaceY, 0, video.height, 0, 80);
    
  • JaiJai
    edited March 2016

    thanks, i guess

  • everything runs good with no issues, face detection works camera is obviously running and the arduino tx/rx is flickering fast implying that it is getting values. so WTF?

    Have you checked the values being sent? Are they valid?

    For your mapping use.

      if (faces.length > 0) {
    
        midFaceY = faces[0].y + (faces[0].height/2);
        midFaceX = faces[0].x + (faces[0].width/2);
        // map to 0-80
        float px = map(midFaceX, 0, video.width, 0, 80);
        float py = map(midFaceY, 0, video.height, 0, 80);
    
  • i cant if i knew how i would of love to attack it that way but the thing is that while processing is using serial i cant use serial on the arduino else i would ,the same goes for arduino if i have terminal open i cant open serial on the processing

  • JaiJai
    edited March 2016

    is there a way ?

  • I have never used the serial or arduino so I can't help you there. Sorry.

  • i dont get it why ask if i checked the values unless you knew how to ? i dont think you would have considered something unless you knew how to right?

  • This thread went somewhere I wasn't expecting...

    Beware that the scale on line 29 will stay in effect for the rest of the draw(), including all of draw3(). You can stop this by putting pushMatrix() and popMatrix() around draw2.

  • JaiJai
    edited March 2016

    thank you @Koogs the issue is that i cant see draw 2 if im using draw3 so i have to comment out one to use the other one, so your saying that using pushMatrix() will solve that issue?

  • once i learn how to use one function on top of the other i will be satisfy and learn from that block of code for future sketches but as in right now i get video from draw2 and draw3 is somewhere in the background still working but not seen through out the draw() process and my objective is to display draw3 over draw2

  • The scale(2) in draw2 will double all the values in draw3 meaning they are off screen...

  • (I'll write something showing this when it's not 4am)

  • edited March 2016 Answer ✓

    this fills the screen in draw2 the same way your video does. then draws the bars like your draw3 does. all the numbers are identical to yours.

    if you run this as i pasted it you will see the bars but only at the very bottom.

    if you change the two commented lines in draw2 (11, 15) so they are no longer commented and run it again you'll see that they are now central.

    basically the scale(2) in draw2 applies to everything after it, even it it's in a different method, unless you surround it by pushMatrix/popMatrix...

    void setup() {
      size(720, 720);
    }
    
    void draw() {
      draw2();
      draw3();
    }
    
    void draw2() {
      //pushMatrix();
      scale(2);
      fill(0, 0, 255);  // blue
      rect(0, 0, 640 / 2, 480 / 2);  // your video
      //popMatrix();
    }
    
    void draw3() {
      fill(255, 0, 0);  // red
      rect(300, 350, mouseX - 360, 20); 
      fill(0, 255, 0); // green
      rect(350, 360, 20, mouseY-360);
    }
    
  • @koogs one last thing, how would i go about flipping the image or mirror the image ?

  • untested but...

    scale(-1, 1); // reflects x
    
    scale(1, -1); // reflects y 
    

    but beware that if you have a rectangle (0, 0, x, y) that'll end up at (0, 0, -x, y), which is probably off screen. so you have to change the coords too to compensate (or use rectMode center, or translate everything so that origin is in the center of the screen)

  • no i meant to mirror the video, because im using the video function twice in one screen but i want the right side to mirror the first video "left" you know?

  • JaiJai
    edited March 2016
    void vid1()
    {
      scale(1);
      opencv.loadImage(video);
      image(video, 0, 0);
    

    /********************************/

    void vid2()
    {
      scale(1);
      opencv.loadImage(video);
      image(video, 1280/4, 0);
    
  • yeah, that should just work. if you reflect an image it reflects the image. and a video is just an image.

    annoyingly i did exactly this for some other question a couple of weeks ago but can't find it now...

  • hmmm dam this is not working

    {
      scale(-1, 1);
      scale(1, -1);
      opencv.loadImage(video);
      image(video, 1280/4, 0);
    

    also i did just

    {
      scale(-1, 1);
      opencv.loadImage(video);
      image(video, 1280/4, 0);
    

    and

    {
      scale(1, -1);
      opencv.loadImage(video);
      image(video, 1280/4, 0);
    

    nothing adding those negative value just makes the second screen go away

  • Answer ✓

    i said it would unless you also changed the coords. it's printing the second image at -x or -y, offscreen.

    void setup() {
      size(280, 150);
      PImage img = loadImage("http:" + "//www.koogy.clara.co.uk/dexter_r.gif");
      image(img, 0, 0);
    
      pushMatrix();
      scale(-1, 1);
      image(img, -2 * img.width, 0);
      popMatrix();
    
      pushMatrix();
      scale(1, -1);
      image(img, 0, -2 * img.height);
      popMatrix();
    
      pushMatrix();
      scale(-1, -1);
      image(img, -2 * img.width, -2 * img.height);
      popMatrix();
    }
    
  • i own you a beer koogs thanks!

  • edited March 2016

    cool. we got there in the end 8)

Sign In or Register to comment.