import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.image.WritableRaster;
/**Provides some simple methods for getting playing with the pixels within the video.
*/
public class PixelOperater extends VideoWindow {
BufferedImage myBuffer = new BufferedImage(kWidth, kHeight, BufferedImage.TYPE_INT_RGB);;
Graphics myGraphics;
WritableRaster myRaster ;
/** A ConvolveOp object for blurring.*/
public ConvolveOp blur ;
/** A ConvolveOp object for finding edges.*/
public ConvolveOp edge;
int threshold = 200;
Graphics sampleImageGraphics;
PixelOperater() {
super();
setTitle("PixelOperator");
//you have to form this buffer after you start the video because it has to match the width you actually got back
myBuffer = new BufferedImage(kWidth, kHeight, BufferedImage.TYPE_INT_RGB);
myGraphics = myBuffer.getGraphics();
myRaster = (WritableRaster) myBuffer.getRaster();
float[] edgeMatrix = { 0.0f, -1.0f, 0.0f,
-1.0f, 4.1f, -1.0f,
0.0f, -1.0f, 0.0f};
setEdge( edgeMatrix,3);
setBlur(5);
}
/**Notice I start video now in main
*/
public static void main(String[] args) {
PixelOperater myOp = new PixelOperater();
myOp.startVideo();
}
/**This is the classice nested video loops for scanning through each row and then each column with the row
* of vdeo.
*/
public void newFrame() {
ps.grabFrame();
liveImage = ps.getImage();
for (int row = 0; row < kHeight; row++){
for (int col = 0; col < kWidth; col++){
performPixelOp(col,row);
}
}
arrayToImage(ps.getPixelArray(),myBuffer);
repaint();
}
/**I farmed this out to another method because this is the most likely thing
* you would want to overide
*/
public void performPixelOp(int _x, int _y){
int[] rgb = ps.getPixel(_x,_y);
if ((rgb[1] + rgb[2] + rgb[3]) < threshold){
ps.setPixel(_x,_y,255,0,0,0);
}
}
public void paint(Graphics g) {
g.drawImage(liveImage, 0, 0, null);
g.drawImage(myBuffer, kWidth, 0, null);
}
public void keyReleased(KeyEvent e) {
if (e.getKeyChar() == '-') {
threshold--;
put ("Down" + threshold);
} else if (e.getKeyChar() == '=') {
threshold++;
put ("Up" + threshold);
}else{
super.keyReleased(e); //pass the buck
}
}
/**Set up the edge detection. This sets the kernel in case you know what that is.
You never really have to set this.
The default matrix is,
{ 0.0f, -1.0f, 0.0f,
-1.0f, 4.0f, -1.0f,
0.0f, -1.0f, 0.0f };
always a classic. The size is 3 as in 3x3.
*/
public void setEdge( float[] matrix, int size) {
edge = new ConvolveOp(new Kernel( size, size, matrix ));
//ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
}
/**Sets the degree of blur. This takes an integer that describes how many pixels
around each pixel to take into account when averaging. The default is 5
and bigger numbers will really slow things down.
*/
public void setBlur(int size) {
float th = 1.0f/((float) (size*size));
float[] blurMatrix = new float[size*size];
for (int i = 0;i< size*size; i++){
blurMatrix[i] = th;
}
blur = new ConvolveOp(new Kernel(size,size, blurMatrix ));
}
/** You give me an image and I will blur it and return it in your destination image
*/
public BufferedImage blurImage(BufferedImage inImage){
return blur.filter(inImage,null);
}
/** You give me an image and I will find edges in it and return it in your destination image
*/
public BufferedImage edgeImage(BufferedImage inImage){
return edge.filter(inImage,null);
}
/** You give me an image and some pixels and I will transfer the image into the array.
*/
public void imageToArray(BufferedImage inImage,int[] outPixis){
myRaster = (WritableRaster) inImage.getData();
myRaster.getDataElements(0,0,kWidth,kHeight,outPixis);
}
/**You give me an array of pixels, I give you back and image.
*/
public BufferedImage arrayToImage(int[] pixis){
myRaster.setDataElements( 0, 0, kWidth, kHeight, pixis) ;
myBuffer.setData(myRaster);
return myBuffer;
}
/**You give me an array of pixels, and the image you want them returned in and I will blur them.
*/
public void arrayToImage(int[] pixis,BufferedImage outImage){
myRaster.setDataElements( 0, 0, kWidth, kHeight, pixis) ;
outImage.setData(myRaster);
}
public void blurArray(int[] pixis){
myRaster.setDataElements( 0, 0, kWidth, kHeight, pixis) ;
myBuffer.setData(myRaster);
myBuffer = blur.filter(myBuffer,null);
myRaster = (WritableRaster) myBuffer.getData();
myRaster.getDataElements(0,0,kWidth,kHeight,pixis);
}
/**You give me an array of pixels, I give you them back blurred in an image.
*/
public BufferedImage blurArrayToImage(int[] pixis){
myRaster.setDataElements( 0, 0, kWidth, kHeight, pixis) ;
myBuffer.setData(myRaster);
myBuffer = blur.filter(myBuffer,null);
return myBuffer;
}
/**You give me an array of pixels, I find the edges and give it back in an image
*/
public BufferedImage edgeArrayToImage(int[] myArray){
myRaster.setDataElements( 0, 0, kWidth, kHeight, myArray) ;
myBuffer.setData(myRaster);
myBuffer = edge.filter(myBuffer,null);
return myBuffer;
}
}