Transforming color image from webcam to grayscale image for analyse

Preformatted text

`Hello,
I need to analyse a grayscale image from webcam.
I know that **filter(GRAY)** would do it for the display... but i also need to transform all the pixels. 
Could anyone tell me what is the best way for this ? the faster way ?
Thank you for your help.
import processing.video.*;
Capture cam;

void setup() {
  size(500, 400, P3D);
  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]);
    }
    cam = new Capture(this, 200, 200, cameras[0]);//0
    cam.start();
  }
}

void captureEvent(Capture video) {
  video.read();
}

void draw() {
  textureMode(NORMAL);
  image(cam, 0, 0, 100, 100);
  filter(GRAY);
  //how to Split the original webcam image onto 4 vidéos on screen
  /////////////
  ////A - B////
  ////D - C////
  /////////////
  //shape A
  beginShape();
  texture(cam);
  vertex(100, 0, 0, 0, 0);
  vertex(300, 0, 0, 0.5, 0);
  vertex(300, 200, 0, 0.5, 0.5);
  vertex(100, 200, 0, 0, 0.5);
  endShape();
  //tester les superpositions vidéos avec couche alpha
  //shape B
  beginShape();
  texture(cam);
  vertex(300, 0, 0, 0.5, 0);
  vertex(500, 0, 0, 1, 0);
  vertex(500, 200, 0, 1, 0.5);
  vertex(300, 200, 0, 0.5, 0.5);
  endShape();
  //shape C
  beginShape();
  texture(cam);
  vertex(300, 200, 0, 0.5, 0.5);
  vertex(500, 200, 0, 1, 0.5);
  vertex(500, 400, 0, 1, 1);
  vertex(300, 400, 0, 0.5, 1);
  endShape();
  //shape D
  beginShape();
  texture(cam);
  vertex(100, 200, 0, 0, 0.5);
  vertex(300, 200, 0, 0.5, 0.5);
  vertex(300, 400, 0, 0.5, 1);
  vertex(100, 400, 0, 0, 1);
  endShape();
  //
}

Capture d’écran 2023-09-11 à 17.14.09

Hi @animalfrommars,

if you use cam.filter(GRAY) processing converts the image into grayscale, but you can only save it to 24b grayscale for bmp/jpg/png. (there is an ugly way to go via tga though)

if you really need a 8-bit grayscale using a common image extension you need to do it via java …

Hope that helps…

Cheers
— mnse

PS: here’s a little example of how it could be done. For your case replace the loadImage thing with your cam object. the filter isn’t needed for saving and happens under the hood…

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

void setup() {
  surface.setVisible(false);
  saveGrayScale8Bit(loadImage("image.bmp"), "image_gray.bmp");
  exit();  
}

void saveGrayScale8Bit(PImage img, String name) {
  BufferedImage bi = (BufferedImage) img.getNative();
  BufferedImage gray8Bit = new BufferedImage(img.width, img.height, BufferedImage.TYPE_BYTE_GRAY);
  gray8Bit.getGraphics().drawImage(bi, 0, 0, null);
  try {
    File outputfile = new File(dataPath(name));
    ImageIO.write(gray8Bit, name.substring(name.lastIndexOf('.') + 1), outputfile);
    println("image saved!");
  }
  catch (IOException e) {
    println("Error saving image! : " + e.getMessage());
  }
}
1 Like

Hello @animalfrommars,

:)