#include <opencv2/opencv.hpp>
CvCapture*camera;IplImage*frame;camera=cvCreateCameraCapture(-1);/* We need to call cvRetrieveFrame twice here to get the last frame.
* TODO: figure out the root cause and use correct method instead of
* the workaround.
*/cvGrabFrame(camera);frame=cvRetrieveFrame(camera);frame=cvRetrieveFrame(camera);if(frame){QImageimage(reinterpret_cast(frame->imageData),frame->width,frame->height,frame->widthStep,QImage::Format_RGB888);QImagecolorCorrectedImage(image.rgbSwapped());/* Show colorCorrectedImage which contains the picture. */...}cvReleaseCapture(&camera);
修改E:\Qt\Qt5.0.1\5.0.1\mingw47_32\bin\qtenv2.bat以便移除系統相依性。修改後如下所示:就是把磁碟機代號移除。 set PATH=\Qt\Qt5.0.1\5.0.1\mingw47_32\bin;\Qt\Qt5.0.1/Tools/MinGW\bin;%PATH%cd /D \Qt\Qt5.0.1\5.0.1\mingw4732_
/* Define states. */
enum STATE {STATE_RED, STATE_YELLOW, STATE_GREEN};
/* State flags are declared here. */
STATE state_before_change;
STATE state_prev;
STATE state;
bool state_changing;
/* The threshold or conditions for state machine are defined here. */
const int period_red = 40;
const int period_green = 60;
const int period_yellow_green_to_red = 10;
const int period_yellow_red_to_green = 5;
/* Reset state machine to initial state. */
void reset_state_machine()
{
state_before_change = STATE_RED;
state_prev = STATE_RED;
state = STATE_RED;
state_changing = false;
}
/* Execute state machine. reset_state_machine()
* should be called before the first time run_state_machine called().
*/
void run_state_machine()
{
/* State transition */
if (state_prev == STATE_RED)
{
if (timer >= period_red)
{
state = STATE_YELLOW;
}
}
else if (state_prev == STATE_YELLOW)
{
if (state_before_change == STATE_RED && timer >= period_yellow_red_to_green)
{
state = STATE_GREEN;
}
else if (state_before_change == STATE_GREEN && timer >= period_yellow_green_to_red)
{
state = STATE_RED;
}
}
else if (state_prev == STATE_GREEN)
{
if (timer >= period_green)
{
state = STATE_YELLOW;
}
}
else
{
// Non-exist state.
ASSERT(FALSE);
}
/* Conditions and flags for state transition. */
state_changing = (state != state_prev);
state_before_change = (state_changing) ? state_prev : state_before_change;
state_prev = state;
/* Tasks which are independent from states. */
if (state_changing)
{
RESET_TIMER();
}
/* Tasks which are related to specified state.*/
if (state == STATE_RED)
{
LED_RED(ON);
LED_YELLOW(OFF);
LED_GREEN(OFF);
}
else if (state == STATE_YELLOW)
{
LED_RED(OFF);
LED_YELLOW(ON);
LED_GREEN(OFF);
}
else if (state == STATE_GREEN)
{
LED_RED(OFF);
LED_YELLOW(ON);
LED_GREEN(OFF);
}
/* Tasks which are independant from states. */
// DO SOMETHING.
} /* void run_state_machine() */
...
/* Tasks which are related to specified state.*/
if (state == STATE_RED)
{
/* Tasks which are independant from states. */
if (state_changing)
{
RESET_TIMER();
}
LED_RED(ON);
LED_YELLOW(OFF);
LED_GREEN(OFF);
}
else if (state == STATE_YELLOW)
{
/* Tasks which are independant from states. */
if (state_changing)
{
RESET_TIMER();
}
LED_RED(OFF);
LED_YELLOW(ON);
LED_GREEN(OFF);
}
else if (state == STATE_GREEN)
{
/* Tasks which are independant from states. */
if (state_changing)
{
RESET_TIMER();
}
LED_RED(OFF);
LED_YELLOW(ON);
LED_GREEN(OFF);
}