Resizing a captured image?

edited November 2016 in Library Questions

I'm trying to make a program that captures an image from the webcam, then reloads the captured image in different places on the screen. I'll do more with it later, but I want to iron this problem out before I do any more. My problem is that I want to resize the image that I capture from the webcam when it loads into the program, but it justs crops the image. Is there any way to solve this? My current code is as follows:

import processing.video.*;

Capture cam;

PImage still;

void setup() {
  //window size
  size(1280, 720, P3D);
  noCursor();
  //webcam feed
  still = loadImage ("capture.jpg");
  imageMode (CENTER);

  String[] cameras = Capture.list();

  //webcam feed
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }

    cam = new Capture(this, cameras[0]);
    cam.start();
  }
}

void draw() {
  if (cam.available() == true) {
    cam.read();
  }
  //web cam feed position, size
  image(cam, 640, 360, 320, 180);
}

void mousePressed() {
  //image capture
  saveFrame("data/capture.jpg");
  cam.read();

  //loads image capture in
  image (still, 200, 200, 160, 90);
}

Answers

  • First comment is, I don't think it is proper to call cam.read() the way you do in line 43.

    This won't answer your question, but for clarification: do you want to load the image you saved in line 42? You are using the still object as a part of a test?

    I am not sure why you are having this problem:

    but it just crops the image

    Your line 46 shouldn't crop your image but resize it. A possible solution could be using PImage's copy function:

    https://processing.org/reference/PImage_copy_.html

    Here is the modified code:

    import processing.video.*;
    
    Capture cam;
    boolean showNow=false;     // ** CHANGED
    
    PImage still;
    
    void setup() {
      //window size
      size(1280, 720);
      //noCursor();
      //webcam feed
      //still = loadImage ("capture.jpg");   // ** CHANGED
    
      imageMode (CENTER);
    
      String[] cameras = Capture.list();
    
      //webcam feed
      if (cameras.length == 0) {
        println("There are no cameras available for capture.");
        exit();
      } else {
        println("Available cameras:");
        for (int i = 0; i < cameras.length; i++) {
          println(cameras[i]);
        }
    
        cam = new Capture(this, cameras[0]);
        cam.start();
        still=createImage(width, height, RGB);       // ** CHANGED
      }
    }
    
    void draw() {
      background(0);
      if (cam.available() == true) {
        cam.read();
      }
      if (showNow==false) {                  // ** CHANGED
        //web cam feed position, size
        image(cam, 640, 360, 320, 180);
      } else {
        image(still, 640,360, 320, 180);
      }
    }
    
    
    void mousePressed() {
    
      if (showNow==false) {
        cam.loadPixels();
        //still.set(width, height, cam);
        still=cam.copy();
        showNow=true;
      } else {
        showNow=false;
      }
    }
    

    I think the copping could be happening because your source image and your container image are not the same size. However i do not see this undesired effect in the previous code. If your image is being drawn in the corner of the screen, then you need to keep in mind you are working in CENTER mode when drawing images. I hope this helps,

    Kf

  • While I'm appreciative of your answer, it didn't really fix my problem. It only sent me a few steps back in my work. After I tried your solution, I went back to my original code and I see that I have the opposite of the problem I explained in this post (no idea how it happened). I started a new thread to address this new problem.

This discussion has been closed.