xxxxxxxxxx
143
let video;
let audio;
let level;
let pixelArray; // Array to hold grayscale values
let numPoints = 10000; // Number of random points to generate
let j = 150; // Width of the pixel array
let k = 150; // Height of the pixel array
function setup() {
createCanvas(windowWidth, windowHeight);
// Create a video capture from the webcam
video = createCapture(VIDEO, { flipped:true });
video.size(j, k); // Set the size of the video
// Create an audio from mic
audio = new p5.AudioIn();
audio.start();//start mic
}
function draw() {
background('#240035');
videoToGray(); // Convert video to gray scale
// Create array of only the first pixels in the 4-element group from video
pixelArray = video.pixels.filter((_, index) => index % 4 === 0);
// Map the audio level to contrast factor
let level = map(audio.getLevel(), 0, 1, 1.25, 30);
// Apply high contrast transformation
pixelArray = applyHighContrast(pixelArray, level);
noStroke();
//if (mouseIsPressed) {
// Generate and draw random points
for (let i = 0; i < numPoints; i++) {
let x = random(-windowWidth / 2, windowWidth / 2);
let y = random(-windowHeight / 2, windowHeight / 2);
if (shouldDrawPoint(x, y)) {
fill('#fbf2ff'); // Red color for the points
ellipse(x + windowWidth / 2, y + windowHeight / 2, 5, 5); // Draw the point
}
}
}
function videoToGray() {
// Load the pixels from the video
video.loadPixels();
// Check if the video has pixels loaded
if (video.pixels.length > 0) {
// Convert to grayscale
for (let i = 0; i < video.pixels.length; i += 4) {
let r = video.pixels[i]; // Red
let g = video.pixels[i + 1]; // Green
let b = video.pixels[i + 2]; // Blue
// Calculate grayscale value
let gray = (r + g + b) / 3;
// Set the pixel color to the grayscale value
video.pixels[i] = gray; // Red
video.pixels[i + 1] = gray; // Green
video.pixels[i + 2] = gray; // Blue
// pixels[i + 3] stays the same (Alpha)
}
// Update the video pixels
video.updatePixels();
}
}
function applyHighContrast(array, contrastFactor) {
// Stretch the grayscale values to increase contrast
let minVal = Math.min(array);
let maxVal = Math.max(array);
// Prevent division by zero if all values are the same
if (minVal === maxVal) {
return array.map(() => 255);
}
// Apply contrast stretching with a scaling factor
return array.map(value => {
// Apply contrast stretching
let stretchedValue = ((value - minVal) * (255 / (maxVal - minVal))) * contrastFactor;
// Clip the value to ensure it stays within bounds
return constrain(Math.round(stretchedValue), 0, 255);
});
}
function drawGrayscaleArray() {
// Draw the pixel array as a grid of rectangles
for (let y = 0; y < k; y++) {
for (let x = 0; x < j; x++) {
let index = x + y * j;
let grayValue = pixelArray[index];
// Set fill color based on the grayscale value
fill(grayValue);
noStroke();
rect((x / j) * windowWidth, (y / k) * windowHeight, windowWidth / j, windowHeight / k);
}
}
}
function mapToPixelIndex(x, y) {
// Map the y-coordinate to the pixel array
let pixelX = Math.floor((x + windowWidth / 2) * j / windowWidth);
let pixelY = Math.floor((y + windowHeight / 2) * k / windowHeight);
// Ensure the pixel indices are within bounds
pixelX = constrain(pixelX, 0, j - 1);
pixelY = constrain(pixelY, 0, k - 1);
// Convert 2D indices to 1D index
return pixelX + pixelY * j;
}
function shouldDrawPoint(x, y) {
let index = mapToPixelIndex(x, y);
// Ensure the index is valid before accessing pixelArray
if (index < 0 || index >= pixelArray.length) {
return false; // Prevent out-of-bounds access
}
let grayscaleValue = pixelArray[index];
// Invert the grayscale value to calculate probability
let probability = 1 - (grayscaleValue / 255); // 0 becomes 1, 255 becomes 0
// Decide to draw the point based on probability
return random() < probability; // Return true or false based on random chance
}