We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everyone I am working on a video colour identifier that can distinguish different colours. I am able to read colour difference but I am stuck in one of the functions in opencv library "erode and dilate" what's happening is I am getting a lot of noise from the colour separation, I was hoping to treat that with erode and dilate but it looks like it creating a lot of noise so I was wondering if there is a way to control the size of the erosion and dilation function I was wondering if someone can guide me to the correct direction.
//library
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;
// initlization
Capture video;
OpenCV opencv;
Histogram histogram;
// variables
int erosion_size = 5;
int dilation_size = 5;
int lowerb = 50;
int upperb = 100;
PImage src, dilated, eroded, both;
ArrayList<Contour> contours;
ArrayList<Contour> polygons;
void setup() {
//opencv setup
video = new Capture(this, 640, 480);
opencv = new OpenCV(this, video);
video.start();
size(opencv.width, opencv.height);
morphOps();
}
void draw() {
//open cv image convertions
opencv.loadImage(video);
image(video, 0, 0);
opencv.useColor(HSB);
opencv.setGray(opencv.getH().clone());
opencv.inRange(lowerb, upperb);
opencv.threshold(100);
opencv.invert();
readcolor();
//
//
// contours = opencv.findContours();
// println("found " + contours.size() + " contours");
//
// for (Contour contour : contours) {
// stroke(0, 255, 0);
// contour.draw();
//
// stroke(255, 0, 0);
// beginShape();
// for (PVector point : contour.getPolygonApproximation().getPoints()) {
// vertex(point.x, point.y);
// }
// endShape();
// }
}
void mouseMoved() {
if (keyPressed) {
upperb += mouseX - pmouseX;
} else {
if (upperb < 255 || (mouseX - pmouseX) < 0) {
lowerb += mouseX - pmouseX;
}
if (lowerb > 0 || (mouseX - pmouseX) > 0) {
upperb += mouseX - pmouseX;
}
}
upperb = constrain(upperb, lowerb, 255);
lowerb = constrain(lowerb, 0, upperb-1);
}
void morphOps() {
// reload un-eroded image and dilate it
opencv.loadImage(video);
opencv.dilate();
// save dilated version for display
dilated = video;
// now erode on top of dilated version to close holes
opencv.erode();
eroded = video;
}
void captureEvent(Capture c) {
c.read();
}
void readcolor() {
histogram = opencv.findHistogram(opencv.getH(), 255);
image(opencv.getOutput(), 3*width/4, 3*height/4, width/4, height/4);
noStroke();
fill(0);
histogram.draw(10, height - 230, 400, 200);
noFill();
stroke(0);
line(10, height-30, 410, height-30);
text("Hue", 10, height - (textAscent() + textDescent()));
float lb = map(lowerb, 0, 255, 0, 400);
float ub = map(upperb, 0, 255, 0, 400);
stroke(255, 0, 0);
fill(255, 0, 0);
strokeWeight(2);
line(lb + 10, height-30, ub +10, height-30);
ellipse(lb+10, height-30, 3, 3 );
text(lowerb, lb-10, height-15);
ellipse(ub+10, height-30, 3, 3 );
text(upperb, ub+10, height-15);
}