How to fusion this two code together?
in
Core Library Questions
•
1 year ago
Hello everybody,
I'm just a 100% Freshfish in Processing, i've met some problems to realize my works.
I want make a Mimosa that when you detect the movement of Mimosa then launch SOMETHING(not defini yet). (when the finger touche the leaf of Mimosa, it close and fall down, and when the day light coming the leafs open, night fall the leafs close)....
here is a demostration of this plant and how it fonction.
There is two person helped me in different times and but in the end i think i need fusion this two code together.
Here is the first one that use color detection to detect the movement , it could choose the color and follow the movement, but in this code , there is just one point , my professor helped me and he said that need detect more points on the leaf.
Here is two code From one program that detect the color point,
----------------------------------------------Detect from Video----------------------------------------------------
import processing.video.*;
Capture video;
int numPixels; // number of pixels in the video
int rectDivide = 4; // the stage width/height divided by this number is the video width/height
int vidW; // video width
int vidH; // video height
int[][] colouredPixels; // the different colour references for each pixel
int[][] colourCompareData; // captured r, g and b colours
int currR; //
int currG; //
int currB; //
int[][] squareCoords; // x, y, w + h of the coloured areas
color[] colours; // captured colours
int colourRange = 25; // colour threshold
int[][] centrePoints; // centres of the coloured squares
color[] pixelColours;
boolean isShowPixels = false; // determines whether the square and coloured pixels are displayed
int colourMax = 2; // max amount of colours - also adjust the amount of colours added to pixelColours in setup()
int coloursAssigned = 0; // amount of cours currently assigned
CoordsCalc coordsCalc;
void setup()
{
size(640, 480);
vidW = width / rectDivide;
vidH = height / rectDivide;
video = new Capture(this, vidW, vidH, 30);
noStroke();
numPixels = vidW * vidH;
colouredPixels = new int[vidH][vidW];
colourCompareData = new int[colourMax][3];
squareCoords = new int[colourMax][4];
colours = new color[colourMax];
centrePoints = new int[colourMax][2];
color c1 = color(0, 255, 0);
color c2 = color(255, 0, 0);
pixelColours = new color[colourMax];
pixelColours[0] = color(0, 255, 0);
pixelColours[1] = color(255, 0, 0);
coordsCalc = new CoordsCalc();
}
void captureEvent(Capture video)
{
video.read();
}
void draw()
{
noStroke();
fill(255, 255, 255);
rect(0, 0, width, height);
drawVideo();
coordsCalc.update();
}
void drawVideo() {
image(video, 0, 0);
}
void mousePressed() {
// if outside video, forget the rest
if (mouseX >= video.width || mouseY >= video.height) return;
// ok, we're in the video
// get the index of the pixel we clicked on
int index = mouseX + mouseY*video.width;
color pixelColor = video.pixels[index];
int redValue = int(red(pixelColor));
int greenValue = int(green(pixelColor));
int blueValue = int(blue(pixelColor));
coloursAssigned = 1;
colourCompareData[0][0] = redValue;
colourCompareData[0][1] = greenValue;
colourCompareData[0][2] = blueValue;
colours[0] = color(redValue, greenValue, blueValue);
}
------------------------Coordcalc( work with the first code)----------------
import processing.video.*;
Capture video;
int numPixels; // number of pixels in the video
int rectDivide = 4; // the stage width/height divided by this number is the video width/height
int vidW; // video width
int vidH; // video height
int[][] colouredPixels; // the different colour references for each pixel
int[][] colourCompareData; // captured r, g and b colours
int currR; //
int currG; //
int currB; //
int[][] squareCoords; // x, y, w + h of the coloured areas
color[] colours; // captured colours
int colourRange = 25; // colour threshold
int[][] centrePoints; // centres of the coloured squares
color[] pixelColours;
boolean isShowPixels = false; // determines whether the square and coloured pixels are displayed
int colourMax = 2; // max amount of colours - also adjust the amount of colours added to pixelColours in setup()
int coloursAssigned = 0; // amount of cours currently assigned
CoordsCalc coordsCalc;
void setup()
{
size(640, 480);
vidW = width / rectDivide;
vidH = height / rectDivide;
video = new Capture(this, vidW, vidH, 30);
noStroke();
numPixels = vidW * vidH;
colouredPixels = new int[vidH][vidW];
colourCompareData = new int[colourMax][3];
squareCoords = new int[colourMax][4];
colours = new color[colourMax];
centrePoints = new int[colourMax][2];
color c1 = color(0, 255, 0);
color c2 = color(255, 0, 0);
pixelColours = new color[colourMax];
pixelColours[0] = color(0, 255, 0);
pixelColours[1] = color(255, 0, 0);
coordsCalc = new CoordsCalc();
}
void captureEvent(Capture video)
{
video.read();
}
void draw()
{
noStroke();
fill(255, 255, 255);
rect(0, 0, width, height);
drawVideo();
coordsCalc.update();
}
void drawVideo() {
image(video, 0, 0);
}
void mousePressed() {
// if outside video, forget the rest
if (mouseX >= video.width || mouseY >= video.height) return;
// ok, we're in the video
// get the index of the pixel we clicked on
int index = mouseX + mouseY*video.width;
color pixelColor = video.pixels[index];
int redValue = int(red(pixelColor));
int greenValue = int(green(pixelColor));
int blueValue = int(blue(pixelColor));
coloursAssigned = 1;
colourCompareData[0][0] = redValue;
colourCompareData[0][1] = greenValue;
colourCompareData[0][2] = blueValue;
colours[0] = color(redValue, greenValue, blueValue);
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is another person helped me to detect the pixels on the image,but my intention is replace those image to Video, that could catch the movement of the leaf.
=============================Detect from images===========================================
// Test détection de couleur dans une image
// Pierre Rossel 17.11.2011
String sFiles[] = {
"Mimosa001.jpg",
"Mimosa002.jpg",
"Mimosa003.jpg",
"Mimosa004.jpg",
"Mimosa005.jpg",
"Mimosa006.jpg"
};
PImage imgs[] = {
};
int iImg = 0;
color colSearch = 0;
int iTolerance = 50;
// Range in HSB mode
int iRangeH = 10;
int iRangeS = 25;
int iRangeB = 25;
final int MODE_RGB_3D = 0;
final int MODE_HSB = 1;
int iMode = MODE_HSB;
boolean bShowPixels = true;
void setup()
{
size (640, 480);
PImage img;
// Load images
for (String sName: sFiles)
{
img = loadImage(sName);
imgs = (PImage[])append(imgs, img);
}
// Default search color
setMode(MODE_HSB);
colSearch = color(70, 55, 70);
}
void draw()
{
background(0);
image(imgs[iImg], 0, 0);
fill(255);
if (iMode == MODE_RGB_3D)
{
text("mode (m): RGB 3D", 500, 220);
text(" tolerance: " + iTolerance + " (T/t)", 500, 240);
}
else
{
text("mode (m): range HSB", 500, 220);
text(" H: " + iRangeH + " (H/h)", 500, 240);
text(" S: " + iRangeS + " (S/s)", 500, 260);
text(" B: " + iRangeB + " (B/b)", 500, 280);
}
text("click image\nto select color", 500, 30);
text("#" + hex(colSearch, 6), 500, 140);
colorMode(RGB, 255, 255, 255);
text("RGB: " + (int)red(colSearch) + " " + (int)green(colSearch) + " " + (int)blue(colSearch), 500, 160);
colorMode(HSB, 255, 100, 100);
text("HSB: " + (int)hue(colSearch) + " " + (int)saturation(colSearch) + " " + (int)brightness(colSearch), 500, 180);
setMode(iMode);
// infos
text("This test app experiments two algoritms to detect a color in images.\n" +
"Type m to change mode or other keys in () to change different values.",
10, height - 100);
// quantité de pixels trouvés
int count;
if (iMode == MODE_RGB_3D)
count = countColor(imgs[iImg], colSearch, iTolerance);
else
count = countColor(imgs[iImg], colSearch, iRangeH, iRangeS, iRangeB);
text(100 * count / (imgs[iImg].width * imgs[iImg].height) + "% pixels. (p) to show/hide", 10, height - 20);
// numéro d'image
text("img " + (iImg + 1) + " / " + imgs.length, width - 100, height - 20);
// Color sample
fill(colSearch);
stroke(255);
rect(500, 50, 70, 70);
}
void setMode(int _iMode)
{
switch(_iMode)
{
case MODE_RGB_3D:
colorMode(RGB, 255, 255, 255);
break;
case MODE_HSB:
colorMode(HSB, 255, 100, 100);
break;
}
iMode = _iMode;
}
// count pixels of specific color in an image
// img: the image
// colSearch: the color to search
// iTolerance: tolerance (max distance between colors in 3D space)
int countColor(PImage img, color colSearch, int iTolerance)
{
int count = 0;
int nPixels = img.width * img.height;
img.loadPixels();
color col;
PVector vSearch = new PVector(red(colSearch), green(colSearch), blue(colSearch));
PVector vCur = new PVector();
boolean bMatch = false;
for (int i = 0; i < nPixels; i++)
{
col = img.pixels[i];
vCur.set(red(col), green(col), blue(col));
if (vSearch.dist(vCur) < iTolerance)
{
count++;
if (bShowPixels)
{
// light the pixel on stage
set(i % img.width, i / img.width, #FF0000);
}
}
}
return count;
}
// count pixels of specific color in an image
// img: the image
// colSearch: the color to search
// iRangeH: Hue range to match color
// iRangeS: Hue range to match color
// iRangeB: Hue range to match color
int countColor(PImage img, color colSearch, int iRangeH, int iRangeS, int iRangeB)
{
int count = 0;
int nPixels = img.width * img.height;
img.loadPixels();
color col;
boolean bMatch = false;
for (int i = 0; i < nPixels; i++)
{
col = img.pixels[i];
bMatch =
abs(hue(col) - hue(colSearch)) < iRangeH &&
abs(saturation(col) - saturation(colSearch)) < iRangeS &&
abs(brightness(col) - brightness(colSearch)) < iRangeB;
if (bMatch)
{
count++;
if (bShowPixels)
{
// light the pixel on stage
set(i % img.width, i / img.width, #FF0000);
}
}
}
return count;
}
void keyPressed()
{
println(keyCode);
switch (keyCode)
{
case 39: // droite
iImg = (iImg + 1) % imgs.length;
break;
case 37: // gauche
iImg = (iImg - 1 + imgs.length) % imgs.length;
break;
}
switch(key)
{
case 'T': // tolerance RGB_3D
iTolerance++;
break;
case 't': // tolerance RGB_3D
iTolerance--;
break;
case 'm': // mode
setMode((iMode + 1) % 2);
break;
case 'h': // Hue
iRangeH = (iRangeH + 255) % 256;
break;
case 'H': // Hue
iRangeH = (iRangeH + 1) % 256;
break;
case 's': // Saturation
iRangeS = (iRangeS + 100) % 101;
break;
case 'S': // Saturation
iRangeS = (iRangeS + 1) % 101;
break;
case 'b': // Brightness
iRangeB = (iRangeB + 100) % 101;
break;
case 'B': // Brightness
iRangeB = (iRangeB + 1) % 101;
break;
case 'p':
bShowPixels = !bShowPixels;
break;
}
}
void mousePressed()
{
mouseColor();
}
void mouseDragged()
{
mouseColor();
}
void mouseColor()
{
// cherche la couleur du point cliqué dans l'image
PImage img = imgs[iImg];
img.loadPixels();
if (mouseX > img.width || mouseY > img.height)
return;
colSearch = img.pixels[mouseX + mouseY * img.width];
}
=====================
So here is two codes and i would like to fusion together, that to detect the movement (use the color detection) on the video, then start another step.
So i hope you understand my poor english and help me a little bit !
Thanks !
1