예제 #1
0
파일: ishara.cpp 프로젝트: Beenking/Ishara
void ishara::processFrameAndUpdateGUI() {
  int pos_x1 = 0;
  int pos_y1 = 0;
  int pos_x2 = 0;
  int pos_y2 = 0;

  hMin1 = ui->sliderHMin1->value();
  hMax1 = ui->sliderHMax1->value();
  sMin1 = ui->sliderSMin1->value();
  sMax1 = ui->sliderSMax1->value();
  vMin1 = ui->sliderVMin1->value();
  vMax1 = ui->sliderVMax1->value();

  hMin2 = ui->sliderHMin2->value();
  hMax2 = ui->sliderHMax2->value();
  sMin2 = ui->sliderSMin2->value();
  sMax2 = ui->sliderSMax2->value();
  vMin2 = ui->sliderVMin2->value();
  vMax2 = ui->sliderVMax2->value();

  smoothFac = ui->sliderSmoothFac->value();
  pinchR = ui->sliderPinchR->value();
  rightClickDealy = ui->sliderRCRC->value();

  capture.read(src);

  /*
   * flip the initial image and convert to RGB as opencv would read images in BGR format
   */
  cv::flip(src, frame, 1);
  cv::cvtColor(frame, frame, CV_BGR2RGB);

  /*
   * converting the image to HSV
   */
  cv::cvtColor(frame, imgHSV, CV_BGR2HSV);

  /*
   * identifying the color markers used in the index finger
   */
  cv::inRange(imgHSV,
              cv::Scalar(hMin1, sMin1, vMin1),
              cv::Scalar(hMax1, sMax1, vMax1),
              imgThresh1);
  openingOperation(&imgThresh1);
  marker1 = trackObject(&imgThresh1, &pos_x1, &pos_y1);

  /*
   * identifying the color markers used in the index thumb
   */
  cv::inRange(imgHSV,
              cv::Scalar(hMin2, sMin2, vMin2),
              cv::Scalar(hMax2, sMax2, vMax2),
              imgThresh2);
  openingOperation(&imgThresh2);
  marker2 = trackObject(&imgThresh2, &pos_x2, &pos_y2);

  mcorInit_X = pos_x1;
  mcorInit_Y = pos_y1;

  if(marker1 && startEmulation) {
      Display *display = XOpenDisplay(0);
      if(!display) {
          ui->statusBar->showMessage("Error opening display!");
          return;
        }

      Window root = DefaultRootWindow(display);
      if(!root) {
          ui->statusBar->showMessage("Root window not found!");
          return;
        }

      /*
       * map and move mouse pointer on screen
       */
      mouseMap();

      if(motionEnable == 0) {
          pre_x = msPoint_X;
          pre_y = msPoint_Y;

          motionEnable = 1;
        }
      else {
          dx = msPoint_X - pre_x;
          dy = msPoint_Y - pre_y;

          mcorFinal_X += dx * (xScreenWidth/frame.cols);
          mcorFinal_Y += dy * (xScreenHeight/frame.rows);

          if(mcorFinal_X > xScreenWidth)
            mcorFinal_X = xScreenWidth;
          if(mcorFinal_Y > xScreenHeight)
            mcorFinal_Y = xScreenHeight;

          pre_x = msPoint_X;
          pre_y = msPoint_Y;
        }

      XTestFakeMotionEvent(display, DefaultScreen(display), mcorFinal_X, mcorFinal_Y, 0);

      if(marker2) {
          if(tmpX == mcorFinal_X && tmpY == mcorFinal_Y) {
              ++waitCountRC;
            }
          else {
              tmpX = 0;
              tmpY = 0;
              waitCountRC = 0;
            }

          /*
           * right click
           */
          if(ui->chkEnableRightClick->isChecked() == true) {
              if(waitCountRC == rightClickDealy && (ifScrollUp != 1 && ifScrollDwn != 1) && pinch != 1) {
                  XTestFakeButtonEvent(display, 3, 1, 1);
                  XTestFakeButtonEvent(display, 3, 0, 1);
                  tmpX = 0;
                  tmpY = 0;
                  waitCountRC = 0;
                }
            }

          tmpX = mcorFinal_X;
          tmpY = mcorFinal_Y;

          preClick(&pos_x2, &pos_y2);

          /*
           * scroll
           */
          if(ui->chkEnableScroll->isChecked() == true) {
              scrollInit(&pos_x2, &pos_y2);
              if(ifScrollUp == 1) {
                  XTestFakeButtonEvent(display, 4, 1, 10);
                  XTestFakeButtonEvent(display, 4, 0, 10);
                }
              if(ifScrollDwn == 1) {
                  XTestFakeButtonEvent(display, 5, 1, 10);
                  XTestFakeButtonEvent(display, 5, 0, 10);
                }
            }

          /*
           * left click
           */
          if(ui->chkEnableLeftClick->isChecked() == true) {
              cv::circle(frame, cv::Point(mcorInit_X, mcorInit_Y), pinchR, cv::Scalar(255, 0, 0), 2);
              if(pinch == 1) {
                  if(btnPress == 1) {
                      XTestFakeButtonEvent(display, 1, 1, 1);
                      btnPress = 0;
                      btnRel = 1;
                    }
                }
              else{
                  if(btnRel == 1) {
                      XTestFakeButtonEvent(display, 1, 0, 1);
                      btnRel = 0;
                    }
                  else
                    btnPress = 1;
                }
            }
        }

      XFlush(display);
      XCloseDisplay(display);
    }
  else {
      motionEnable = 0;
    }

  /*
   * display images
   */
  QImage qImgDisplay((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
  ui->lblDisplayClean->setPixmap(imgDisplay.fromImage(qImgDisplay).scaledToHeight(480, Qt::FastTransformation));

  QImage qImgDisplayThresh1((uchar*)imgThresh1.data, imgThresh1.cols, imgThresh1.rows, imgThresh1.step, QImage::Format_Indexed8);
  ui->lblConfigureLeft->setPixmap(imgDisplay.fromImage(qImgDisplayThresh1).scaledToHeight(236, Qt::FastTransformation));

  QImage qImgDisplayThresh2((uchar*)imgThresh2.data, imgThresh2.cols, imgThresh2.rows, imgThresh2.step, QImage::Format_Indexed8);
  ui->lblConfigureRight->setPixmap(imgDisplay.fromImage(qImgDisplayThresh2).scaledToHeight(236, Qt::FastTransformation));
}
예제 #2
0
파일: ishara.cpp 프로젝트: impiza/Ishara
void ishara::processFrameAndUpdateGUI() {
    cv::Mat imgThresh1;
    cv::Mat imgThresh2;

    int pos_x1 = 0;
    int pos_y1 = 0;
    int pos_x2 = 0;
    int pos_y2 = 0;

    hMin1 = ui->sliderHMin1->value();
    hMax1 = ui->sliderHMax1->value();
    sMin1 = ui->sliderSMin1->value();
    sMax1 = ui->sliderSMax1->value();
    vMin1 = ui->sliderVMin1->value();
    vMax1 = ui->sliderVMax1->value();

    hMin2 = ui->sliderHMin2->value();
    hMax2 = ui->sliderHMax2->value();
    sMin2 = ui->sliderSMin2->value();
    sMax2 = ui->sliderSMax2->value();
    vMin2 = ui->sliderVMin2->value();
    vMax2 = ui->sliderVMax2->value();

    smoothFac = ui->sliderSmoothFac->value();
    pinchR = ui->sliderPinchR->value();
    rightClkCount = ui->sliderRCRC->value();

    capture.read(src);

    /**
     * Flip the initial image and convert to RGB as opencv would read images in BGR format.
     */
    cv::flip(src, frame, 1);
    cv::cvtColor(frame, frame, CV_BGR2RGB);

    /**
     * Converting the image to HSV and applying Gaussian Blur filter to reduce some noise.
     */
    cv::cvtColor(frame, imgHSV, CV_BGR2HSV);
    cv::GaussianBlur(imgHSV, imgHSV, cv::Size(11, 11), 0);

    /**
     * Identifying the color markers used in the index finger.
     */
    cv::inRange(imgHSV,
                cv::Scalar(hMin1, sMin1, vMin1),
                cv::Scalar(hMax1, sMax1, vMax1),
                imgThresh1);
    trackObject(&imgThresh1, &pos_x1, &pos_y1);

    /**
     * Identifying the color markers used in the index thumb.
     */
    cv::inRange(imgHSV,
                cv::Scalar(hMin2, sMin2, vMin2),
                cv::Scalar(hMax2, sMax2, vMax2),
                imgThresh2);
    trackObject(&imgThresh2, &pos_x2, &pos_y2);

    mcorInit_X = pos_x1;
    mcorInit_Y = pos_y1;

    if(mcorInit_X + mcorInit_Y) {
        if(startEmulation == 1) {
            Display *display = XOpenDisplay(0);
            if(!display) {
                ui->statusBar->showMessage("Error opening display!");
                return;
            }

            Window root = DefaultRootWindow(display);
            if(!root) {
                ui->statusBar->showMessage("Root window not found!");
                return;
            }

            /**
             * Map and move mouse pointer on screen.
             */
            getxScreenSize();
            mouseMap();
            XTestFakeMotionEvent(display, DefaultScreen(display), mcorFinal_X, mcorFinal_Y, 0);

            if(pos_x2 + pos_y2) {
                if(tmpX == mcorFinal_X && tmpY == mcorFinal_Y)
                    ++waitCount;
                else {
                    tmpX = 0;
                    tmpY = 0;
                    waitCount = 0;
                }

                /**
                 * Right click.
                 */
                if(ui->chkEnableRightClick->isChecked() == true) {
                    if(waitCount == rightClkCount && (ifScrollUp != 1 && ifScrollDwn != 1) && pinch != 1) {
                        XTestFakeButtonEvent(display, 3, 1, 1);
                        XTestFakeButtonEvent(display, 3, 0, 1);
                        tmpX = 0;
                        tmpY = 0;
                        waitCount = 0;
                    }
                }

                tmpX = mcorFinal_X;
                tmpY = mcorFinal_Y;

                preClick(&pos_x2, &pos_y2);

                /**
                 * Scroll.
                 */
                if(ui->chkEnableScroll->isChecked() == true) {
                    scrollInit(&pos_x2, &pos_y2);
                    if(ifScrollUp == 1) {
                        XTestFakeButtonEvent(display, 4, 1, 10);
                        XTestFakeButtonEvent(display, 4, 0, 10);
                    }
                    if(ifScrollDwn == 1) {
                        XTestFakeButtonEvent(display, 5, 1, 10);
                        XTestFakeButtonEvent(display, 5, 0, 10);
                    }
                }

                /**
                 * Left click.
                 */
                if(ui->chkEnableLeftClick->isChecked() == true) {
                    cv::circle(frame, cv::Point(mcorInit_X, mcorInit_Y), pinchR, cv::Scalar(0, 0, 255));
                    if(pinch == 1) {
                        if(btnPress == 1) {
                            XTestFakeButtonEvent(display, 1, 1, 1);
                            btnPress = 0;
                            btnRel = 1;
                        }
                    }
                    else{
                        if(btnRel == 1) {
                            XTestFakeButtonEvent(display, 1, 0, 1);
                            btnRel = 0;
                        }
                        else
                            btnPress = 1;
                    }
                }
            }

            XFlush(display);
            XCloseDisplay(display);
        }
    }

    /**
     * Display images.
     */
    QImage qImgDisplay((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
    ui->lblDisplayClean->setPixmap(imgDisplay.fromImage(qImgDisplay).scaledToHeight(480, Qt::FastTransformation));

    QImage qImgDisplayThresh1((uchar*)imgThresh1.data, imgThresh1.cols, imgThresh1.rows, imgThresh1.step, QImage::Format_Indexed8);
    ui->lblConfigureLeft->setPixmap(imgDisplay.fromImage(qImgDisplayThresh1).scaledToHeight(236, Qt::FastTransformation));

    QImage qImgDisplayThresh2((uchar*)imgThresh2.data, imgThresh2.cols, imgThresh2.rows, imgThresh2.step, QImage::Format_Indexed8);
    ui->lblConfigureRight->setPixmap(imgDisplay.fromImage(qImgDisplayThresh2).scaledToHeight(236, Qt::FastTransformation));

    /**
     * Clean up the crap.
     */
    imgThresh1.release();
    imgThresh2.release();
}