コード例 #1
0
ファイル: mser.cpp プロジェクト: Buanderie/mfw
    void MSERNode::tick( double dt )
    {
        Timer t;
        t.start();
        Pin* outPin = findPinFromLabel("out");
        Pin* inPin = findPinFromLabel("in");

        if( !inPin->isConnected() )
            waitForConnection();

        if( !outPin->isConnected() )
            waitForConnection();

        if( outPin->isConnected() && inPin->isConnected() )
        {
            vector<ObjectBlob*> b = inPin->read();
            if( b.size() > 0 )
            {
                for( int k = 0; k < b.size(); ++k )
                {
                    if( b[k]->getTypeName() == "Image" )
                    {
                        monadic::Image img;
                        img.deserialize(b[k]);
                        cv::Mat m( img.getHeight(), img.getWidth(), CV_8UC3, img.ptr() );
                        cv::Mat res( img.getHeight(), img.getWidth(), CV_8UC3 );
                        res = m.clone();

                        cv::cvtColor( m, m, CV_RGB2GRAY );

                        vector< vector< cv::Point > > ret;
                        cv::MSER* det = (cv::MSER*)(detector);
                        (*det)(m, ret);

                        for( size_t i = 0; i < ret.size(); ++i )
                        {
                            //cv::circle( res, kps[i].pt, 3, CV_RGB(255,0,0) );
                            cv::Rect r = cv::boundingRect(ret[i]);
                            cv::rectangle( res, r, CV_RGB(255,0,0), 3 );
                        }

                        monadic::Image imgout;
                        imgout.create( res.cols, res.rows, 8, res.channels() );
                        size_t bufferSize = res.cols * res.rows * res.channels();
                        imgout.copyFrom( (char*)res.data, bufferSize );
                        ObjectBlob* bout = imgout.serialize();
                        outPin->write( bout );
                        delete bout;
                    }
                    delete b[k];
                }
            }
        }
        t.stop();
    }
コード例 #2
0
void VibrationSensor2::run()
{
    // Take a reading.
    int lastReading = reading;
    Pin* pin = Esp8266::getInstance()->getPin(pinId);
    if (pin)
    {
        reading = pin->read();
    }

    // Each change shall be recorded as a discrete vibration.
    if (reading != lastReading)
    {
        vibrationCount++;
    }

    // Periodically update the server with the overall vibration state.
    if (updateTimer.isExpired())
    {
        // Send an update to the server.
        if (serverId != "")
        {
            SensorUpdateMsg message(getId(), state, ((millis() - stateChangeTime) / MILLISECONDS_PER_SECOND));
            message.address(getId(), serverId);
            MessageRouter::getInstance()->sendMessage(message);
        }

        updateTimer.start();
    }

    // Periodically calculate the overall vibration state.
    if (intervalTimer.isExpired())
    {
        // Increment the position in the circular queue.
        queuePosition++;
        if (queuePosition >= getIntervalCount())
        {
            // Wraparound.
            queuePosition = 0;
        }

        // Record the vibrating state for this interval.
        queue[queuePosition] = (vibrationCount > getVibrationThreshold()) ? VIBRATING : NOT_VIBRATING;

        // Calculate the total number of detected vibrations over all the observed intervals.
        int total = 0;
        for (int i = 0; i < getIntervalCount(); i++)
        {
            total += queue[i];
        }

        // Calculate the new overall vibration state.
        bool prevState = state;
        state = (total > (getIntervalCount() / 2));

#if 0
        String debugString = "";
        for (int i = 0; i < getIntervalCount(); i++)
        {
            debugString += "[" + String(queue[i]) + "]";
        }
        debugString += "\n";
        debugString += "vibration count = " + String(vibrationCount) + ", threshold = " + String(getVibrationThreshold());
        debugString += "\n";
        debugString += "total = " + String(total) + ", state = " + String(state);
        debugString += "\n";
        debugString += "\n";
        Logger::logDebug(debugString);
#endif

        // Respond to state changes.
        if (prevState != state)
        {
            // Send an update to the server.
            if (serverId != "")
            {
                SensorUpdateMsg message(getId(), state, 0);
                message.address(getId(), serverId);
                MessageRouter::getInstance()->sendMessage(message);
            }

            // Record the state change time.
            stateChangeTime = millis();
        }

        // Reset for the next interval.
        vibrationCount = 0;
        intervalTimer.start();
    }
}