unsigned int HaarDetector::search_faces()
{
        clear_detected_objects();

        //debug out only
        for (unsigned int i = 0; i < (unsigned int)m_object_maps.size(); i++) {
                ObjectMap* pom = m_object_maps[i];                
                pom->get_tmp_object_map().copy(pom->get_object_map());
        }
        //debug out only

        ObjectMap* pom = 0;
        int tmpx = 0, tmpy = 0;
        unsigned int x = 0, y = 0, index = 0;
        float max = 0.0f, tmpmx = 0.0f;        
        while (true) {
                float max = -FLT_MAX;
                index = 0;
                for (unsigned int i = 0; i < (unsigned int)m_object_maps.size(); i++) {
                        pom = m_object_maps[i];
                        unsigned int dx = pom->get_object_width() / 2;
                        unsigned int dy = pom->get_object_height() / 2;
                        pom->get_object_map().max_val(tmpmx, tmpx, tmpy, 5, 5, dx, dy);
                        if (tmpmx > max) {
                                index = i;
                                max = tmpmx;
                                x = tmpx;
                                y = tmpy;
                        }
                }
                
                pom = m_object_maps[index];                
                if (max > detection_sensitivity()) {
                        unsigned int w2 = pom->get_object_width() / 2;
                        unsigned int h2 = pom->get_object_height() / 2;
                        Rect rect;
                        rect.left = x - w2;
                        rect.top = y - h2;
                        rect.right = x + w2;
                        rect.bottom = y + h2;
                        ObjRect obj_rect;
                        obj_rect.rect = rect;
                        obj_rect.x = x;
                        obj_rect.y = y;
                        obj_rect.radius = sqrt(pow(float(w2), 2.0f) + pow(float(h2), 2.0f));
                        if (is_foundobject_overlaps(obj_rect) == false) {
                                obj_rect.object = new vec2Di(pom->get_object_height(), pom->get_object_width());
                                obj_rect.object->copy(*m_pimage, x - w2, y - h2);                                
                                m_detected_objects.push_back(obj_rect);
                                erase_object_rect(rect);            //erase found face region  [left, right)
                        } else {
                                erase_object_rect(x, y);                                                                
                        }
                }
                else
                        break;                
        }

        return (unsigned int)m_detected_objects.size();
}
void HaarDetector::erase_object_rect(unsigned int x, unsigned int y)
{                
        for (unsigned int i = 0; i < (unsigned int)m_object_maps.size(); i++) {
                ObjectMap* pom = m_object_maps[i];
                RECT zr;
                zr.left = x - 2;
                zr.top = y - 2;
                zr.right = x + 3;
                zr.bottom = y + 3;
                pom->get_object_map().set(0.0f, zr);                
        }
}
void HaarDetector::erase_object_rect(const Rect& r)
{
        RECT zr;
        zr.left = r.left;
        zr.top = r.top;
        zr.right = r.right + 1;
        zr.bottom = r.bottom + 1;
        for (unsigned int i = 0; i < (unsigned int)m_object_maps.size(); i++) {
                ObjectMap* pom = m_object_maps[i];
                pom->get_object_map().set(0.0f, zr);
        }
}