Helderste punt

Het helderste punt volgen

import gab.opencv.*;
import processing.video.*;
import java.awt.*;

Capture video;
OpenCV opencv;

void setup() {
  fullScreen();
  video = new Capture(this, width, height);
  opencv = new OpenCV(this, video);
  video.start();
}

void draw() {
  if (video.available()) {
    video.read();
    opencv.loadImage(video);
  }

  // Convert image to grayscale manually
  PImage gray = createImage(video.width, video.height, RGB);
  video.loadPixels();
  gray.loadPixels();

  float totalBrightness = 0;

  for (int i = 0; i < video.pixels.length; i++) {
    int col = video.pixels[i];
    float r = red(col);
    float g = green(col);
    float b = blue(col);
    float brightness = (r + g + b) / 3;
    gray.pixels[i] = color(brightness);
    totalBrightness += brightness;
  }
  gray.updatePixels();

  // Calculate average brightness
  float avgBrightness = totalBrightness / (gray.width * gray.height);

  // Set a threshold above which we consider a spot as "much brighter"
  float brightnessThreshold = avgBrightness * 1.5;  // You can adjust the multiplier

  // Find the brightest spot above the threshold
  gray.loadPixels();
  int brightestX = -1;
  int brightestY = -1;
  float maxBrightness = brightnessThreshold; // Start looking for anything above the threshold

  for (int y = 0; y < gray.height; y++) {
    for (int x = 0; x < gray.width; x++) {
      int index = x + y * gray.width;
      float bright = brightness(gray.pixels[index]);
      if (bright > maxBrightness) {
        maxBrightness = bright;
        brightestX = x;
        brightestY = y;
      }
    }
  }

  // Display the webcam image
  //image(video, 0, 0);

  // Draw an ellipse around the brightest spot if found
  if (brightestX != -1 && brightestY != -1) {
    // Mirror the X coordinate
    int mirroredX = video.width - brightestX;

    fill(255, 0, 0, 100);
    noStroke();
    ellipse(mirroredX, brightestY, 50, 50);
  }
}
Volgende pagina