Multiple colored objects detection


#1

Hi,

I am trying to detect multiple colored objects. Actually I succeeded a bit detecting different color’s objects at once.

The problem is that I just compared each hsv values, so there are some objects not related detected.

So is there any detection algorithm that not just compares the hsv values?

I posted my codes for feedback.

thanks.

#include "ofApp.h"
#include <iostream>
using namespace std;

//--------------------------------------------------------------
void ofApp::setup(){
    _start = false;
    nObjects = 0;
    objects[0][0] = 300;
    w = 640;
    h = 480;
    totalPixels = w*h*3;
    targetHue = 255;
    tolerance = 10;
    
    grabber.setDeviceID(1);
    grabber.initGrabber(w, h);
    
    // Allocate
    rgb.allocate(w, h);
    hsv.allocate(w, h);
    red.allocate(w, h);
    green.allocate(w, h);
    blue.allocate(w, h);
    hueImage.allocate(w, h);
    satImage.allocate(w, h);
    brightImage.allocate(w, h);
    hueFiltered.allocate(w, h);
}

//--------------------------------------------------------------
void ofApp::update(){
    grabber.update();
    
    if (grabber.isFrameNew()){
        
        
        rgb.setFromPixels(grabber.getPixels());
        
        rgb.mirror(false, true);
        
        hsv = rgb;
        
        hsv.convertRgbToHsv();
        
        
        
        hsv.convertToGrayscalePlanarImages(hueImage, satImage, brightImage);
        //Smoothing original image
        hueFiltered = hsv;
        //hueFiltered = rgb;//Copy the image
        hueFiltered.blurGaussian( 9 );      //Gaussian blurring
        //with window size 9x9
        //heufiltered.draw( 0, h+10, w, h );  //Draw
        
        //Thresholding smoothed image
        hueFiltered.threshold( 128 );     //Thresholding smoothed image
        //using thresold 128
        
        
        
        
            for(int j=0; j<nObjects; j++){
                for (int i = 0; i < w * h; i++){
                    
                        if (ofInRange(hueImage.getPixels()[i], objects[j][0], objects[j][0]+10) && ofInRange(satImage.getPixels()[i], objects[j][1], objects[j][1]+10) && ofInRange(brightImage.getPixels()[i], objects[j][2], objects[j][2]+10)){
                            hueFiltered.getPixels()[i] = 255;
                           
                        } else {
                            if(hueFiltered.getPixels()[i] == 255){
                                continue;
                            }
                            hueFiltered.getPixels()[i] = 0;
                        }
                    
                }
            }
        
        
        hueFiltered.flagImageChanged();
        
        contourFinder.findContours(hueFiltered, 100, w*h/2, 10, false);
        
        
        
}
    for(int i=0; i<nObjects; i++){
        dString += objects[i][0];
    }
}


//--------------------------------------------------------------
void ofApp::draw(){
    
    //hsv.draw(0, 0);
    hueFiltered.draw(640, 0);
    rgb.draw(0,0);
    //contourFinder.draw(0,0);
    contourFinder.draw(640,0);
    contourFinder.draw(0,0);
    
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    for(int i=0; i<nObjects; i++){
        cout<<objects[i][0];
    }
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
    
    tempHue = hueImage.getPixels()[w*y + x+1];
    tempSat = satImage.getPixels()[w*y + x+1];
    tempVal = brightImage.getPixels()[w*y + x+1];
    objects[nObjects][0] = tempHue;
    objects[nObjects][1] = tempSat;
    objects[nObjects][2] = tempVal;
    nObjects++;
    _start = true;
    cout<<contourFinder.blobs.size();
    
    
}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------


//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}