bool HysteresisTest::_take_change_back()
{
	systemlib::Hysteresis hysteresis(false);
	hysteresis.set_hysteresis_time_from(false, 5000 * f);

	// Change to true.
	hysteresis.set_state_and_update(true);
	ut_assert_false(hysteresis.get_state());
	px4_usleep(3000 * f);
	hysteresis.update();
	ut_assert_false(hysteresis.get_state());
	// Change your mind to false.
	hysteresis.set_state_and_update(false);
	ut_assert_false(hysteresis.get_state());
	px4_usleep(6000 * f);
	hysteresis.update();
	ut_assert_false(hysteresis.get_state());

	// And true again
	hysteresis.set_state_and_update(true);
	ut_assert_false(hysteresis.get_state());
	px4_usleep(3000 * f);
	hysteresis.update();
	ut_assert_false(hysteresis.get_state());
	px4_usleep(3000 * f);
	hysteresis.update();
	ut_assert_true(hysteresis.get_state());

	// The other directory is immediate.
	hysteresis.set_state_and_update(false);
	ut_assert_false(hysteresis.get_state());

	return true;
}
Exemple #2
0
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Q_UNUSED(a)

    QImage inImage("lena.png");
    inImage = inImage.convertToFormat(QImage::Format_Grayscale8);
    QImage outImage(inImage.size(), inImage.format());

    QVector<int> gradient;
    QVector<int> direction;
    sobel(inImage, gradient, direction);
    QVector<int> thinned = thinning(inImage.width(), inImage.height(),
                                   gradient, direction);
    QVector<int> thresholded = threshold(75, 150, thinned);
    QVector<int> canny = hysteresis(inImage.width(), inImage.height(),
                                    thresholded);

    const int *iImg = canny.constData();
    quint8 *oImg = outImage.bits();

    int size = inImage.width() * inImage.height();

    for (int i = 0; i < size; i++)
        oImg[i] = qBound(0, iImg[i], 255);

    outImage.save("canny.png");

    return EXIT_SUCCESS;
}
bool HysteresisTest::_change_after_time()
{

	systemlib::Hysteresis hysteresis(false);
	hysteresis.set_hysteresis_time_from(false, 5000 * f);
	hysteresis.set_hysteresis_time_from(true, 3000 * f);

	// Change to true.
	hysteresis.set_state_and_update(true);
	ut_assert_false(hysteresis.get_state());
	px4_usleep(4000 * f);
	hysteresis.update();
	ut_assert_false(hysteresis.get_state());
	px4_usleep(2000 * f);
	hysteresis.update();
	ut_assert_true(hysteresis.get_state());

	// Change back to false.
	hysteresis.set_state_and_update(false);
	ut_assert_true(hysteresis.get_state());
	px4_usleep(1000 * f);
	hysteresis.update();
	ut_assert_true(hysteresis.get_state());
	px4_usleep(3000 * f);
	hysteresis.update();
	ut_assert_false(hysteresis.get_state());

	return true;
}
bool HysteresisTest::_init_true()
{
	systemlib::Hysteresis hysteresis(true);
	ut_assert_true(hysteresis.get_state());

	return true;
}
bool HysteresisTest::_change_after_multiple_sets()
{
	systemlib::Hysteresis hysteresis(false);
	hysteresis.set_hysteresis_time_from(true, 5000 * f);
	hysteresis.set_hysteresis_time_from(false, 5000 * f);

	// Change to true.
	hysteresis.set_state_and_update(true);
	ut_assert_false(hysteresis.get_state());
	usleep(3000 * f);
	hysteresis.set_state_and_update(true);
	ut_assert_false(hysteresis.get_state());
	usleep(3000 * f);
	hysteresis.set_state_and_update(true);
	ut_assert_true(hysteresis.get_state());

	// Change to false.
	hysteresis.set_state_and_update(false);
	ut_assert_true(hysteresis.get_state());
	usleep(3000 * f);
	hysteresis.set_state_and_update(false);
	ut_assert_true(hysteresis.get_state());
	usleep(3000 * f);
	hysteresis.set_state_and_update(false);
	ut_assert_false(hysteresis.get_state());

	return true;
}
bool HysteresisTest::_hysteresis_changed()
{
	systemlib::Hysteresis hysteresis(false);
	hysteresis.set_hysteresis_time_from(true, 2000 * f);
	hysteresis.set_hysteresis_time_from(false, 5000 * f);

	// Change to true.
	hysteresis.set_state_and_update(true);
	ut_assert_false(hysteresis.get_state());
	usleep(3000 * f);
	hysteresis.update();
	ut_assert_false(hysteresis.get_state());
	usleep(3000 * f);
	hysteresis.update();
	ut_assert_true(hysteresis.get_state());

	// Change hysteresis time.
	hysteresis.set_hysteresis_time_from(true, 10000 * f);

	// Change back to false.
	hysteresis.set_state_and_update(false);
	ut_assert_true(hysteresis.get_state());
	usleep(7000 * f);
	hysteresis.update();
	ut_assert_true(hysteresis.get_state());
	usleep(5000 * f);
	hysteresis.update();
	ut_assert_false(hysteresis.get_state());

	return true;
}
bool HysteresisTest::_zero_case()
{
	// Default is 0 hysteresis.
	systemlib::Hysteresis hysteresis(false);
	ut_assert_false(hysteresis.get_state());

	// Change and see result immediately.
	hysteresis.set_state_and_update(true);
	ut_assert_true(hysteresis.get_state());
	hysteresis.set_state_and_update(false);
	ut_assert_false(hysteresis.get_state());
	hysteresis.set_state_and_update(true);
	ut_assert_true(hysteresis.get_state());

	// A wait won't change anything.
	px4_usleep(1000 * f);
	hysteresis.update();
	ut_assert_true(hysteresis.get_state());

	return true;
}
Exemple #8
0
void HandController::updateHand(int n, const HandData& h) {
	int prospective = state[n];
	float coords[3];
	switch (state[n]) {
		case NONE:
			if (h.getData(n, coords)) prospective = NEUTRAL;
			break;
		case NEUTRAL:
			if (!h.getData(n, coords)) prospective = NONE;
			else {
				if (countFingers(h, n) == 2) prospective = TRACKING;
				int nf = countFingers(h,n);
			}
			break;
		case TRACKING:
			if (!h.getData(n, coords)) prospective = NONE;
			else {
				int nf = countFingers(h,n);
				if (nf <= 1) prospective = ENGAGED;
				else if (nf != 2) prospective = NEUTRAL;
			}
			break;
		case ENGAGED:
			if (!h.getData(n, coords)) prospective = NONE;
			else {
				int nf = countFingers(h,n);
				if (nf == 2) prospective = TRACKING;
				else if (nf > 2) prospective = NEUTRAL; 
			}
			break;
	}
	int oldstate = state[n];
	if (hysteresis(n, prospective)) {
		if (state[n] == ENGAGED) {
			// Record starting position
		}
	}
}
Exemple #9
0
static void
touchpad_update_state(struct touchpad_dispatch *touchpad, uint32_t time)
{
	int motion_index;
	int center_x, center_y;
	double dx = 0.0, dy = 0.0;
	struct libinput_device *base = &touchpad->device->base;

	if (touchpad->reset ||
	    touchpad->last_finger_state != touchpad->finger_state) {
		touchpad->reset = 0;
		touchpad->motion_count = 0;
		touchpad->event_mask = TOUCHPAD_EVENT_NONE;
		touchpad->event_mask_filter =
			TOUCHPAD_EVENT_ABSOLUTE_X | TOUCHPAD_EVENT_ABSOLUTE_Y;

		touchpad->last_finger_state = touchpad->finger_state;

		process_fsm_events(touchpad, time);

		return;
	}
	touchpad->last_finger_state = touchpad->finger_state;

	if (!(touchpad->event_mask & TOUCHPAD_EVENT_REPORT))
		return;
	else
		touchpad->event_mask &= ~TOUCHPAD_EVENT_REPORT;

	if ((touchpad->event_mask & touchpad->event_mask_filter) !=
	    touchpad->event_mask_filter)
		return;

	touchpad->event_mask_filter = TOUCHPAD_EVENT_ABSOLUTE_ANY;
	touchpad->event_mask = 0;

	/* Avoid noise by moving center only when delta reaches a threshold
	 * distance from the old center. */
	if (touchpad->motion_count > 0) {
		center_x = hysteresis(touchpad->hw_abs.x,
				      touchpad->hysteresis.center_x,
				      touchpad->hysteresis.margin_x);
		center_y = hysteresis(touchpad->hw_abs.y,
				      touchpad->hysteresis.center_y,
				      touchpad->hysteresis.margin_y);
	} else {
		center_x = touchpad->hw_abs.x;
		center_y = touchpad->hw_abs.y;
	}
	touchpad->hysteresis.center_x = center_x;
	touchpad->hysteresis.center_y = center_y;
	touchpad->hw_abs.x = center_x;
	touchpad->hw_abs.y = center_y;

	/* Update motion history tracker */
	motion_index = (touchpad->motion_index + 1) % TOUCHPAD_HISTORY_LENGTH;
	touchpad->motion_index = motion_index;
	touchpad->motion_history[motion_index].x = touchpad->hw_abs.x;
	touchpad->motion_history[motion_index].y = touchpad->hw_abs.y;
	if (touchpad->motion_count < 4)
		touchpad->motion_count++;

	if (touchpad->motion_count >= 4) {
		touchpad_get_delta(touchpad, &dx, &dy);

		filter_motion(touchpad, &dx, &dy, time);

		if (touchpad->finger_state == TOUCHPAD_FINGERS_ONE) {
			pointer_notify_motion(
				base,
				time,
				li_fixed_from_double(dx),
				li_fixed_from_double(dy));
		} else if (touchpad->finger_state == TOUCHPAD_FINGERS_TWO) {
			if (dx != 0.0)
				pointer_notify_axis(
					base,
					time,
					LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
					li_fixed_from_double(dx));
			if (dy != 0.0)
				pointer_notify_axis(
					base,
					time,
					LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
					li_fixed_from_double(dy));
		}
	}

	if (!(touchpad->state & TOUCHPAD_STATE_MOVE) &&
	    ((int)dx || (int)dy)) {
		touchpad->state |= TOUCHPAD_STATE_MOVE;
		push_fsm_event(touchpad, FSM_EVENT_MOTION);
	}

	process_fsm_events(touchpad, time);
}
Image * EdgeDetector::edgeDetectorCanny(Image* img, int thresholdHigh, int thresholdsDown)
{
    this->matrixImagenP=static_cast<ImagenPGM*>(img)->getMatrix();
    height=static_cast<ImagenPGM*>(img)->getHeight();
    width=static_cast<ImagenPGM*>(img)->getWidth();

    //int** dx = new int*[height];
    //int** dy = new int*[height];
    gradientMagnitude = new double*[height];
    //double** gradientDegree = new double*[height];
    gradientDegreeDiscret = new int*[height];
    edgeNonMaximumSuppression = new double*[height];
    edgeHysteresis = new int*[height];
    for (int i = 0; i < height; ++i) {
        //dx[i] = new int[width];
        //dy[i] = new int[width];
        gradientMagnitude[i] = new double[width];
        //gradientDegree[i] = new double[width];
        gradientDegreeDiscret[i] = new int[width];
        edgeNonMaximumSuppression[i] = new double[width];
        edgeHysteresis[i] = new int[width];
        for (int j = 0; j < width; ++j) {
            /*resultMatrix[i][j]= dx[i][j] = dy[i][j]=*/gradientDegreeDiscret[i][j]=
                    *matrixImagenP[i][j];
            edgeNonMaximumSuppression[i][j]=gradientMagnitude[i][j]/*=gradientDegree[i][j]*/=(double)*matrixImagenP[i][j];
            edgeHysteresis[i][j]=1;
        }
    }

    /*********************************************/
    /*//Filtro Gaussiano
    //gaussianaFilter(1,1);
    //gaussianaFilter(1,5);
    //Calculo del Gradiente (magnitud y angulo)

    //Calculo de los componentes usando los operadores de sobel
    //Image *temp1 = this->edgeDetectionSobel(0);

    ImagenPGM* temp = (static_cast<ImagenPGM*>(temp1));
    int *** dx ;
    dx= (temp->getMatrix());
    int ** dx=*((ImagenPGM*)(edgeDetectionSobel(0)))->getMatrix();

    temp = (static_cast<ImagenPGM*>(this->edgeDetectionSobel(1)));
    int *** dy ;
    dy = (temp->getMatrix());
    QTextStream (stdout) <<" mag! " << endl << endl << endl << endl;*/

    //****Calculo de la magnitud y angulo del gradiente a partir de lso componentes
    for(int i = 0; i < height; ++i) {
        for (int j = 0; j < width; ++j) {
            if(i==0 || j==0 || i==height-1 || j==width-1){// si son los bordes de la imagen completa
                //dx[i][j]=0;
                //dy[i][j]=0;
                gradientMagnitude[i][j]=0;
                gradientDegreeDiscret[i][j]=0;
                //gradientDegree[i][j]=0;
                edgeNonMaximumSuppression[i][j]=0;
                edgeHysteresis[i][j]=1;
            }else{
                double valueX = 0, valueY = 0, degree;
                valueX += *matrixImagenP[i-1][j-1]*gX[0][0];
                valueY += *matrixImagenP[i-1][j-1]*gY[0][0];


                valueX += *matrixImagenP[i-1][j]*gX[0][1];
                valueY += *matrixImagenP[i-1][j]*gY[0][1];

                valueX += *matrixImagenP[i-1][j+1]*gX[0][2];
                valueY += *matrixImagenP[i-1][j+1]*gY[0][2];

                valueX += *matrixImagenP[i][j-1]*gX[1][0];
                valueY += *matrixImagenP[i][j-1]*gY[1][0];

                valueX += *matrixImagenP[i][j]*gX[1][1];
                valueY += *matrixImagenP[i][j]*gY[1][1];

                valueX += *matrixImagenP[i][j+1]*gX[1][2];
                valueY += *matrixImagenP[i][j+1]*gY[1][2];

                valueX += *matrixImagenP[i+1][j-1]*gX[2][0];
                valueY += *matrixImagenP[i+1][j-1]*gY[2][0];

                valueX += *matrixImagenP[i+1][j]*gX[2][1];
                valueY += *matrixImagenP[i+1][j]*gY[2][1];

                valueX += *matrixImagenP[i+1][j+1]*gX[2][2];
                valueY += *matrixImagenP[i+1][j+1]*gY[2][2];


                if(valueX==0){
                    if(valueY<0){
                        degree = -PI/2;
                    }else{
                        degree = PI/2;
                    }
                }else{
                    degree = atan((double)(valueY/valueX));
                    if(degree==-0){
                        degree= fabs(degree);
                    }
                }
                //dx[i][j]=valueX;
                //dy[i][j]=valueY;
                gradientMagnitude[i][j]=fabs(valueX)+fabs(valueY);
                //gradientDegree[i][j]=degree;//solo para guardar los valores reales
                gradientDegreeDiscret[i][j]=this->discretDegree(degree);

            }
        }
    }

    //Non Maximum Suppression
    nonMaximumSuppression();

    //hysteresis
    hysteresis(thresholdHigh, thresholdsDown);


    /*********************************************/
    ImagenPGM *imageResult = new ImagenPGM (height, width, 1, edgeHysteresis); //OJO SE CAMBIO EL NIVEL DEL COLOR

        for (int i = 0; i < height; ++i) {

            //delete dx[i];
            //dx[i]=0;
            //delete dy[i];
            //dy[i]=0;
            delete gradientMagnitude[i];
            gradientMagnitude[i]=0;
            //delete gradientDegree[i];
            //gradientDegree[i]=0;
            delete gradientDegreeDiscret[i];
            gradientDegreeDiscret[i]=0;
            delete edgeNonMaximumSuppression[i];
            edgeNonMaximumSuppression[i]=0;
            delete edgeHysteresis[i];
            edgeHysteresis[i]=0;

        }
    //delete dx;
    //dx=0;
    //delete dy;
    //dy=0;
    delete gradientMagnitude;
    gradientMagnitude=0;
    //delete gradientDegree;
    //gradientDegree=0;
    delete gradientDegreeDiscret;
    gradientDegreeDiscret=0;

    delete edgeNonMaximumSuppression;
    edgeNonMaximumSuppression=0;
    delete edgeHysteresis;
    edgeHysteresis=0;

    return imageResult;
}
		void VDVSubscriptionService::_setFromParametersMap(const ParametersMap& map)
		{
			string content(map.getDefault<string>(PARAMETER_POST_DATA));

			XMLResults results;
			XMLNode allNode = XMLNode::parseString(content.c_str(), "AboAnfrage", &results);
			if(	results.error != eXMLErrorNone ||
				allNode.isEmpty()
			){
				_errorNumber = "100";
				_errorText = "Malformed XML";
				return;
			}

			try
			{
				string sender(allNode.getAttribute("Sender"));
				_client = &DataExchangeModule::GetVDVClient(sender);

				// Trace
				_client->trace("AboAnfrage", content);

				// Check if the client is declared as active
				if(!_client->get<Active>())
				{
					_errorNumber = "300";
					_errorText = "Sender is forbidden right now";
					return;
				}

				// Subscriptions cleaning
				XMLNode cleanNode(allNode.getChildNode("AboLoeschenAlle"));
				if(!cleanNode.isEmpty())
				{
					string cleanNodeStr(cleanNode.getText());
					if(	cleanNodeStr == "true" ||
						cleanNodeStr == "1"
					){
						_client->cleanSubscriptions();
					}
				}

				// New subscriptions
				size_t nbNodes(allNode.nChildNode("AboAZB"));
				for(size_t i(0); i<nbNodes; ++i)
				{
					XMLNode aboAZBNode(allNode.getChildNode("AboAZB", static_cast<int>(i)));
					string id(aboAZBNode.getAttribute("AboID"));
					if(id.empty())
					{
						continue;
					}

					boost::shared_ptr<VDVClientSubscription> subscription(new VDVClientSubscription(id, *_client));

					if(aboAZBNode.nChildNode("AZBID"))
					{
						XMLNode azbidNode(aboAZBNode.getChildNode("AZBID"));
						StopArea* stopArea(
							_client->get<DataSource>()->getObjectByCode<StopArea>(azbidNode.getText())
						);
						if(!stopArea)
						{
							continue;
						}
						subscription->setStopArea(stopArea);
					}

					if(aboAZBNode.nChildNode("LinienID"))
					{
						XMLNode linienNode(aboAZBNode.getChildNode("LinienID"));
						CommercialLine* line(
							_client->get<DataSource>()->getObjectByCode<CommercialLine>(linienNode.getText())
						);
						if(!line)
						{
							continue;
						}
						subscription->setLine(line);
					}

					// Direction filter
					if(aboAZBNode.nChildNode("RichtungsID"))
					{
						XMLNode linienNode(aboAZBNode.getChildNode("RichtungsID"));
						subscription->setDirectionFilter(linienNode.getText());
					}

					XMLNode durationNode(aboAZBNode.getChildNode("Vorschauzeit"));
					if(!durationNode.isEmpty())
					{
						try
						{
							time_duration timeSpan(
								minutes(
									lexical_cast<long>(durationNode.getText())
							)	);
							subscription->setTimeSpan(timeSpan);
						}
						catch(bad_lexical_cast&)
						{
						}
					}

					XMLNode hysteresisNode(aboAZBNode.getChildNode("Hysterese"));
					if(!hysteresisNode.isEmpty())
					{
						try
						{
							time_duration hysteresis(
								seconds(
									lexical_cast<long>(hysteresisNode.getText())
							)	);
							subscription->setHysteresis(hysteresis);
						}
						catch(bad_lexical_cast&)
						{
						}
					}
					
					_client->addSubscription(subscription);
				}

			}
			catch (Exception&)
			{
				_errorNumber = "200";
				_errorText = "Invalid sender";
				return;
			}

			// Error if the VDV server is inactive
			if(!DataExchangeModule::GetVDVServerActive())
			{
				_errorNumber = "400";
				_errorText = "Service temporary unavailable";
				return;
			}
		}
Exemple #12
0
void hysteresis(FileData &fd) {
	hysteresis(fd.getImage(),fd.ksize,fd.filename,fd);
}
Exemple #13
-1
ImageRAII canny( IplImage * image, std::pair< int, int > thresh, double sigma )
{
	const char * WINDOW_NAME = "Basic Canny Edge Detector";

	ImageRAII grayscale( cvCreateImage( cvGetSize( image ), image->depth, 1 ) );
	ImageRAII destination( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) );
	ImageRAII gaussian( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) );
	ImageRAII gradient_x( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) );
	ImageRAII gradient_y( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) );
	ImageRAII gradient( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) );
	ImageRAII orientation( cvCreateImage( cvGetSize( image ), image->depth, grayscale.image->nChannels ) );

	// convert image to grayscale
	cvCvtColor( image, grayscale.image, CV_BGR2GRAY );

	// gaussian smoothing
	cvSmooth( grayscale.image, gaussian.image, CV_GAUSSIAN, GAUSSIAN_X, GAUSSIAN_Y, sigma );
	// find edge strength
	cvSobel( gaussian.image, gradient_x.image, 1, 0, 3 );
	cvSobel( gaussian.image, gradient_y.image, 0, 1, 3 );
	// find edge orientation
	CvSize image_size = cvGetSize( gaussian.image );

	for( int i = 0; i < image_size.width; i++ )
	{
		for( int j = 0; j < image_size.height; j++ )
		{
			double x = cvGet2D( gradient_x.image, j, i ).val[0];
			double y = cvGet2D( gradient_y.image, j, i ).val[0];
			float angle;

			if( x == 0 )
			{
				if( y == 0 )
					angle = 0;
				else
					angle = 90;
			}
			else
				angle = cvFastArctan( y, x );

			CvScalar g;
			CvScalar a;
		   	g.val[0] = cvSqrt( pow( x, 2 ) + pow( y, 2 ) );
			a.val[0] = find_angle( angle );

			cvSet2D( destination.image, j, i, g );
			cvSet2D( orientation.image, j, i, a );
		}
	}

	ImageRAII suppressed_image = nonMaxSup( destination.image, orientation.image );
	ImageRAII hysteresis_image = hysteresis( suppressed_image.image, orientation.image, thresh );

	cvNamedWindow( WINDOW_NAME );
	cvShowImage( WINDOW_NAME, destination.image );
	cvMoveWindow( WINDOW_NAME, image_size.width, 0 );

	return hysteresis_image;
}