[ SM3117 ] - [ Class 09 ] - Colour

Class 09 - WebcamXtra Workshop

 

1
The getPixel command can obtain the colour information for any pixel in an image object.
2

To obtain the colour information from the webcam image.

function exitFrame() {
  var m = member("webCam").cameraImage;
  var p = point(10,20);
// the point can be changed according to your need.
  var c = m.getPixel(p);
  _movie.go(_movie.frame);
}

 

3

The colour object obtained can be of the form,

  • color(red, green, blue);
  • colour(paletteIndex); // for 8 bits of less colour depth.

 

4

Colour information is available from the webcamxtra. The block command can calculate an average colour value for a rectangle.

member("webcam").block(rect(10,10,30,30))

 

5

A sample application.

Suppose we divide the webcam image into a grid of 12 cells. Each cell will have a rectangle of rect(0,0,40,40).

0,0
1,0
2,0
3,0
0,1
1,1
2,1
3,1
0,2
1,2
2,2
3,2

 

6

We can calculate the average colour for each of the cell in the webcam image grid using the block command.

var r1 = rect(0,0,40,40);
for (var j=0;j<3;j++) {
  for (var i=0;i<4;i++) {
    var c = member("webcam").block(r1.offset(i*40,j*40));
  }
}

 

7

Divide the stage image into the same number of cells.

0,0
1,0
2,0
3,0
0,1
1,1
2,1
3,1
0,2
1,2
2,2
3,2

 

8

Fill the stage with the same colour you obtained from the webcam image by using the fill command. The following example fill the image (myImage) with colour black in the area specified by the rect(0,0,40,50).

var p = propList("shapeType",symbol("rect"),"color",
  color(0,0,0));
myImage.fill(rect(0,0,40,50),p);