Question - Changing color values for webcam stream
in
Core Library Questions
•
8 months ago
Hi everyone,
As a part of one of our university modules we have a video manipulation processing assignment. As my assignment I have chosen to create a simple RGB color corrector for a webcam stream. I have created most of my code, but I'm struggling with connecting the mapped sliders I have created to the loop that changes the colors (Excuse my primitive language but I'm quite a novice when it comes to Processing). Basically, I have been following the same procedure as if I was dealing with an image, and not surprisingly, this doesn't work with a video. I've looked in the libraries and got some pieces of code here and there but it doesn't seem to be working. Below this I'll post the specific part of the code which seems to be wrong, as well as the code in it's entirety.
I'm pretty sure I'm quite far away from where I want to be with this code, so any help would be greatly appreciated!
As a part of one of our university modules we have a video manipulation processing assignment. As my assignment I have chosen to create a simple RGB color corrector for a webcam stream. I have created most of my code, but I'm struggling with connecting the mapped sliders I have created to the loop that changes the colors (Excuse my primitive language but I'm quite a novice when it comes to Processing). Basically, I have been following the same procedure as if I was dealing with an image, and not surprisingly, this doesn't work with a video. I've looked in the libraries and got some pieces of code here and there but it doesn't seem to be working. Below this I'll post the specific part of the code which seems to be wrong, as well as the code in it's entirety.
I'm pretty sure I'm quite far away from where I want to be with this code, so any help would be greatly appreciated!
--------------------------------------------------------------------------------------------
The "bugged" part of the code:
if (cam.available() == true) {
cam.read();
loadPixels();
cam.loadPixels();
int x = int(random(cam.width));
int y = int(random(cam.height));
for (int i = 0; i < width*height; i++) {
cam.pixels[i] = color (red(cam.pixels[i])*rCC*lCC, green (cam.pixels[i])*gCC*lCC, blue (cam.pixels[i])*bCC*lCC);
}
updatePixels ();
}
Error message:
ArrayIndexOutOfBoundsException: 921600
-----------------------------------------------------------------------------------------------------
The current code in it's entirety:
import processing.video.*;
PImage img;
//Starting Slider positions
float lX = 1530;
float rX = 1530;
float gX = 1530;
float bX = 1530;
Capture cam;
//Load webcam and Background img
void setup() {
size(1800, 720, P2D);
img = loadImage ("Background.jpg");
String [] cameras = Capture.list();
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]);
}
//Start cam
cam = new Capture (this, cameras [0]);
cam.start();
}
}
void draw() {
//Mapping the sliders
float lCC = map(lX, 1330, 1730, 2, 0);
float rCC = map(rX, 1330, 1730, 2, 0);
float gCC = map(gX, 1330, 1730, 2, 0);
float bCC = map(bX, 1330, 1730, 2, 0);
if (cam.available() == true) {
cam.read();
loadPixels();
cam.loadPixels();
int x = int(random(cam.width));
int y = int(random(cam.height));
for (int i = 0; i < width*height; i++) {
cam.pixels[i] = color (red(cam.pixels[i])*rCC*lCC, green (cam.pixels[i])*gCC*lCC, blue (cam.pixels[i])*bCC*lCC);
}
updatePixels ();
}
image (img, 170, 0);
image(cam, 0, 0);
//Slider 1
stroke(0);
line(1330, 330, 1730, 330);
fill(150);
rectMode(CENTER);
rect(lX, 330, 20, 12);
if (mouseX > 1330 && mouseX < 1730 && mouseY > 325 && mouseY < 335) {
if (mousePressed==true) {
lX=mouseX;
}
if (lX <=1330) {
lX=1330;
}
if (lX >= 1730) {
lX=1730;
}
}
//Slider 2
stroke(0);
line(1330, 450, 1730, 450);
fill(150);
rectMode(CENTER);
rect(rX, 450, 20, 12);
if (mouseX > 1330 && mouseX < 1730 && mouseY > 445 && mouseY < 455) {
if (mousePressed==true) {
rX=mouseX;
}
if (rX <=1330) {
rX=1330;
}
if (rX >= 1730) {
rX=1730;
}
}
// Slider 3
stroke(0);
line(1330, 550, 1730, 550);
fill(150);
rectMode(CENTER);
rect(bX, 550, 20, 12);
if (mouseX > 1330 && mouseX < 1730 && mouseY > 545 && mouseY < 555) {
if (mousePressed==true) {
bX=mouseX;
}
if (bX <=1330) {
bX=1330;
}
if (bX >= 1730) {
bX=1730;
}
}
// Slider 4
stroke(0);
line(1330, 650, 1730, 650);
fill(150);
rectMode(CENTER);
rect(gX, 650, 20, 12);
if (mouseX > 1330 && mouseX < 1730 && mouseY > 645 && mouseY < 655) {
if (mousePressed==true) {
gX=mouseX;
}
if (gX <=1330) {
gX=1330;
}
if (gX >= 1730) {
gX=1730;
}
}
}
1