Copying pixels from an image to the webcam - java

What I am trying to do is to convert a pixel from a video cam, into an image
to expalin it better imagine a 3d model so.. the pixels would be each polying, and i want to do is to conver each polyigon into an image.
What i have so far is this:
**
import processing.video.*;
PImage hoja;
Capture cam;
boolean uno, dos, tres, cuatro;
import ddf.minim.*;
Minim minim;
AudioPlayer audio;
float set;
void setup() {
//audio
minim = new Minim(this);
// audio = minim.loadFile("audio");
// audio.loop();
//
uno=false;
dos=false;
tres=false;
cuatro=true;
size(640, 480);
hoja=loadImage("hoja.gif");
cam = new Capture(this, width, height);
cam.start();
}
void draw() {
if (cam.available() == true) {
cam.read();
if (uno==true) {
filtroUno();
image(cam, 0, 0, 640, 480);
}
if (dos==true) {
filtroDos();
}
if(tres==true){
filtroTres();
}
if(cuatro==true){
filtroCuatro();
image(cam, set, 0,640,480);
}
}
// The following does the same, and is faster when just drawing the image
// without any additional resizing, transformations, or tint.
//set(0, 0, cam);
}
void filtroUno() {
cam.loadPixels();
hoja.loadPixels();
for (int i=0;i<cam.pixels.length;i++) {
if (brightness(cam.pixels[i])>110) {
cam.pixels[i]=color(0, 255, 255);
}
else {
cam.pixels[i]=color(255, 0, 0);
}
}
for (int i=0;i<cam.width;i+=10) {
for (int j=0;j<cam.height;j+=10) {
int loc=i+(j*cam.width);
if (cam.pixels[loc]==color(255, 0, 0)) {
for (int x=i;x<i+10;x++) {
for (int y=j;y<j+10;y++) {
// println("bla");
int locDos=i+(j*cam.width);
cam.pixels[locDos]=hoja.get(x, y);
}
}
}
}
}
cam.updatePixels();
}
**
The problem is that each pixel is creating me a matrix, so.. is not recreating what id that to do.
I had the method filtroUno but it wasn't showing ok..
and was the result
void filtroUno() {
cam.loadPixels();
hoja.loadPixels();
for (int i=0;i<cam.pixels.length;i++) {
if (brightness(cam.pixels[i])>110) {
cam.pixels[i]=color(0, 255, 255);
}
else {
cam.pixels[i]=color(255, 0, 0);
}
}
for (int i=0;i<cam.width;i+=10) {
for (int j=0;j<cam.height;j+=10) {
int loc=i+j*hoja.width*10;
if (cam.pixels[loc]==color(255, 0, 0)) {
for (int x=i;x<i+10;x++) {
for (int y=j;y<j+10;y++) {
// println("bla");
int locDos=x+y*hoja.height*10;
cam.pixels[locDos]=hoja.get(x, y);
}
}
}
}
}
cam.updatePixels();
}
i hope you can help me thanks
note: each red pixel should be the gif image the imge size is 10x10

I think what you are doing is looping through every 10th pixel in a webcam image and if the pixel is red you are placing the contents of a 10x10px gif over the webcam image with the top left corner of the gif positioned at the pixel that was red.
// loop through each 10th column in the camera
for (int i=0;i<cam.width;i+=10) {
// loop through each 10th row in the camera
for (int j=0;j<cam.height;j+=10) {
// calculate the pixel location at (i, j)
int loc=i+(j*cam.width);
// check the pixel is red
if (cam.pixels[loc]==color(255, 0, 0)) {
// loop through each column in the gif image
for (int x=0;x<10;x++) {
// loop through each row in the gif image
for (int y=0;y<10;y++) {
int locDos = (i + x) + ((j + y) * cam.width);
cam.pixels[locDos]=hoja.get(x, y);
}
}
}
}
}

Related

How to resize the image of kinect with Processing

The output image of using EasyOpenNI with an xbox360 kinect is a small 640 x 480 image, is there anyway to maybe scale it up to fullscreen, or near fullscreen.
I tried using image.resize() but it creates additional images on the top, and the expanded image does not refresh, it gives the initial frame at boot.
Code:
import SimpleOpenNI.*;
SimpleOpenNI kinect;
PImage depthCam;
PImage result;
void setup()
{
size(640, 480);
surface.setResizable(true);
background(0);
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
result = createImage(width,height,RGB);
}
void draw()
{
background(0);
kinect.update();
depthCam = kinect.depthImage();
int[] depthVals = kinect.depthMap();
result.loadPixels();
for (int y=0; y<depthCam.height; y=y+5)
{
for (int x =0; x<depthCam.width; x=x+5)
{
int loc = x+(y*depthCam.width);
if (depthVals[loc] <= 1200 && depthVals[loc] > 50) {
result.pixels[loc] = color(250);
}
else if (depthVals[loc] > 1200 && depthVals[loc]< 3000 )
{
result.pixels[loc] = color(102);
}
else {
//otherwise let the pixel value in the result image be white
result.pixels[loc] = color(0);
}
}
}
result.updatePixels();
result.resize(640*2,480*2);
image(result,0,0);
}
Here is a screenshot of what appears:
Everything is working correctly, except for the fact that there are two duplicated streams at the top (that do refresh), and the enlarged stream does not refresh.

Why it is showing only one box in export of processing.org java pdf?

Here is my code down below, when in run it shows multiple boxes as I wanted but when I try to export, it only shows a single box at that frame position.I tried generating output at particular frame but its same issue
import processing.pdf.*;
int ofs = 500;
boolean record;
void setup() {
size(1900, 1080, P3D);
}
void draw() {
if (record) {
beginRaw(PDF, "box.pdf");
}
background(255);
translate(width/2, height/2, -800);
rotateZ(0.3);
rotateY(frameCount/500.0);
float x = sin(radians(frameCount*0.1));
for ( int xo = -ofs; xo<=ofs; xo += 100) {
for ( int yo = -ofs; yo<=ofs; yo += 100) {
pushMatrix();
translate ( xo, yo, 0);
box(200*x);
fill(155,150,90);
strokeWeight(3*x);
popMatrix();
if (record) {
endRaw();
record = false;
}
}
}
}
void keyPressed() {
if (key == 'r') {
record = true;
}
}
So close! :)
Perhaps the formatting might have made it harder to spot the issue.
You're calling endRaw(); (which wraps up pdf recording) nested in the for loops.
This means it's only picking up the first box since record is set to false so the other boxes are ignored.
I recommend using Edit > Auto Format (CMD + T / Ctrl + T).
Simply move the if(record) after the nested for loops:
import processing.pdf.*;
int ofs = 500;
boolean record;
void setup() {
size(1400, 580, P3D);
}
void draw() {
if (record) {
beginRaw(PDF, "box.pdf");
}
background(255);
translate(width/2, height/2, -800);
rotateZ(0.3);
rotateY(frameCount/500.0);
float x = sin(radians(frameCount*0.1));
for ( int xo = -ofs; xo<=ofs; xo += 100) {
for ( int yo = -ofs; yo<=ofs; yo += 100) {
pushMatrix();
translate ( xo, yo, 0);
box(200*x);
fill(155, 150, 90);
strokeWeight(3*x);
popMatrix();
}
}
// finish recording to PDF only after everything was drawn
if (record) {
endRaw();
record = false;
}
}
void keyPressed() {
if (key == 'r') {
record = true;
}
}

Android rotate pixel in bitmap but the bitmap is turn to white/mising

I have a fingerprint image as a bitmap and then i rotate the pixel with this code :
public Bitmap rotateImage(Bitmap rotateBmp)
{
double radians=Math.toRadians(90);
double cos, sin;
cos=Math.cos(radians);
sin=Math.sin(radians);
boolean rotatePix[][]=new boolean[width][height];
for(int i=0;i<width;++i)
{
for(int j=0;j<height;++j)
{
int centerX=core.x, centerY=core.y;
int m=i - centerX;
int n=j - centerY;
int k=(int)(m * cos + n * sin);
int l=(int)(n * cos - m * sin);
k+=centerX;
l+=centerY;
if(!((k<0)||(k>width-1)||(k<0)||(k>height-1)))
{
try
{
rotatePix[k][l]=binaryMap[i][j];
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
for(int i=0;i<width;++i)
{
for(int j=0;j<height;++j)
{
if(rotatePix[i][j]==true)
{
rotateBmp.setPixel(i, j, Color.BLACK);
}
else
{
rotateBmp.setPixel(i, j, Color.WHITE);
}
}
}
return rotateBmp;
//}
}
but then when I check the result, the black pixel become less then before, I guess it turns to white because when I check the calculation in the X and Y coordinate, many of them has same X and Y new coordinate and maybe it change the black pixel to white. Please tell me how to rotate the pixel in an angle but with same color with before. I attach the result here for you to look. Many thanks for your help...
If what you want to do is just rotate the bitmap, you could use a Matrix like I have done below:
public Bitmap rotateBitmap (Bitmap rotateBmp) {
int rotationDegree = 90;
/* rotate the image based the rotation degree */
Matrix matrix = new Matrix();
matrix.postRotate(rotationDegree);
rotateBmp = Bitmap.createBitmap(rotateBmp, 0, 0, rotateBmp.getWidth(),
rotateBmp.getHeight(), matrix, true);
// rotateBmp is now rotated by 90 degrees
return rotateBmp;
}
I hope this helps.

How to Delete the white pixels in an image in java

How can I remove the white pixels of an image before loading it in the Panel
the method for loading image in panel is:
public void ajouterImage(File fichierImage) {
// desiiner une image à l'ecran
try {
monImage = ImageIO.read(fichierImage);
} catch (IOException e) {
e.printStackTrace();
}
repaint();
}
You can't remove a pixel for say from an image, but you sure can change the color of it, or even make it transparent.
Let's say you have a pixel array as a variable somewhere, and you can give it the RGB value of your BufferedImage. The pixel array will be called pixels:
try {
monImage = ImageIO.read(fichierImage);
int width = monImage.getWidth();
int height = monImage.getHeight();
pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++) {
// I used capital F's to indicate that it's the alpha value.
if (pixels[i] == 0xFFffffff) {
// We'll set the alpha value to 0 for to make it fully transparent.
pixels[i] = 0x00ffffff;
}
}
} catch (IOException e) {
e.printStackTrace();
}
Assuming that by removing pixels you mean setting them to transparent, you need to set the alpha value of the image to zero. Here is a function colorToAlpha(BufferedImage, Color) which takes a BufferedImage and a Color as input and returns another BufferedImage with the Color set to transparent.
public static BufferedImage colorToAlpha(BufferedImage raw, Color remove)
{
int WIDTH = raw.getWidth();
int HEIGHT = raw.getHeight();
BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
int pixels[]=new int[WIDTH*HEIGHT];
raw.getRGB(0, 0, WIDTH, HEIGHT, pixels, 0, WIDTH);
for(int i=0; i<pixels.length;i++)
{
if (pixels[i] == remove.getRGB())
{
pixels[i] = 0x00ffffff;
}
}
image.setRGB(0, 0, WIDTH, HEIGHT, pixels, 0, WIDTH);
return image;
}
Example usage:
BufferedImage processed = colorToAlpha(rawImage, Color.WHITE)

Lock graphice object in J2ME

it seems to be very stupid question, but I really need a help, I'm making a task with drawing a background transparent gradient image and then draw some objects over it, the problem is I want to draw this image once but the other objects will be drawn multi time to perform some animation
the code is as following, this is the code that i want to run once, and I have created a boolen variable = false and then set it to true
public void drawLockLayer(Graphics g) {
try {
lock = Image.createImage(Paths.lock);
g.drawImage(lock, 0, 0, LGBMainMidlet.width, LGBMainMidlet.height);
System.out.println("After Draw Image");
drawOnce = true;
} catch (Exception ex) {
ex.printStackTrace();
}
}
other code is as following
public void paint(Graphics g, Rectangle rect) {
synchronized (g) {
if (drawOnce == false) {
drawLockLayer(g);
}
}
pos = (int) ((System.currentTimeMillis() % 2700) / 300);
int i = 0;
g.setColor(bgColor);
g.fillRoundRect(startX, startY, width, height, 20, 20);
g.setColor(fgColor);
g.setFont(font);
g.drawString(loadMsg, startX + (spacing / 4), startY + (spacing / 4));
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
int thickness = 6;
if (i == pos) {
thickness = 14;
} else if (i == pos - 1) {
thickness = 10;
}
g.fillRect((startX + x * spacing - (thickness / 2)) + (width / 3), (startY + y * spacing - (thickness / 2)) + (height / 3), thickness, thickness);
i++;
}
}
}
it is enter the method but it is not draw the background image , What I want to do is to lock the graphics object until he finish drawing the image then continue with other code
can anyone help please
Several issues with the code:
synchronizing on a Graphics is generally a bad idea
paint is only supposed to be called from one thread. are you sure it isn't and you actually need to synchronize? where does rect come from?
to make it easier to maintain, you should set drawOnce to true in paint()
You need to set drawOnce to false before calling paint.
It looks like your code is based on GameCanvas. What do you think of the below approach?
class MainCanvas extends GameCanvas {
// rect is an attribute
private void updateScreen() {
drawOnce = false;
paint(getGraphics(), rect);
}
public void start() {
new DataThread().start();
new AnimationThread().start();
}
class DataThread extends Thread {
public void run() {
while (/*IO stuff not done*/) {
// save data
updateScreen();
}
}
}
class AnimationThread extends Thread {
public void run() {
while (/*IO stuff not done*/) {
// sleep a little
updateScreen();
}
}
}
// drawLockLayer and paint(Graphics, Rectangle) methods
} // end of MainCanvas class

Categories