A utility class which tracks the movement of hands. The camera image is the input.
The output can be used for hand gesture analysis.
Revisión | f2cb08846f6f780ebb45a93b639f902305e88237 (tree) |
---|---|
Tiempo | 2013-07-30 08:35:06 |
Autor | tkawata <takuji.kawata@gmai...> |
Commiter | tkawata |
[NDNGestureVision] Fixed a crash bug.
@@ -589,30 +589,63 @@ void NDNGestureVision::calculateFlow(const cv::Mat &newImage, cv::Mat *pGestureV | ||
589 | 589 | { |
590 | 590 | float dx = m_focusedFlowRegion.direction.x * m_focusedFlowRegion.distance; |
591 | 591 | float dy = m_focusedFlowRegion.direction.y * m_focusedFlowRegion.distance; |
592 | - cv::Rect prevRect = cv::Rect(m_focusedFlowRegion.xmin - dx, m_focusedFlowRegion.ymin - dy, | |
593 | - m_focusedFlowRegion.xmax - m_focusedFlowRegion.xmin + 1, m_focusedFlowRegion.ymax - m_focusedFlowRegion.ymax + 1); | |
594 | - | |
595 | - cv::Mat tempMat(m_backgroundImage.size(), CV_8UC3); | |
596 | - cv::Mat tempROIMat(tempMat, prevRect); | |
597 | - m_backgroundImage.copyTo(tempROIMat); | |
592 | + int xmin_prev = m_focusedFlowRegion.xmin - dx; | |
593 | + if (xmin_prev < 0) xmin_prev = 0; | |
594 | + else if (xmin_prev >= m_imageWidth) xmin_prev = m_imageWidth - 1; | |
595 | + int width_prev = m_focusedFlowRegion.xmax - m_focusedFlowRegion.xmin; | |
596 | + if (width_prev + xmin_prev > m_imageWidth ) | |
597 | + { | |
598 | + width_prev = m_imageWidth - xmin_prev; | |
599 | + } | |
600 | + int ymin_prev = m_focusedFlowRegion.ymin - dy; | |
601 | + if (ymin_prev < 0) ymin_prev = 0; | |
602 | + else if (ymin_prev >= m_imageHeight) ymin_prev = m_imageHeight - 1; | |
603 | + int height_prev = m_focusedFlowRegion.ymax - m_focusedFlowRegion.ymin; | |
604 | + if (height_prev + ymin_prev > m_imageHeight) | |
605 | + { | |
606 | + height_prev = m_imageHeight - ymin_prev; | |
607 | + } | |
598 | 608 | |
599 | - for (int y = 0; y < m_imageHeight; y++) | |
609 | + if (width_prev > 0 && height_prev > 0) | |
600 | 610 | { |
601 | - for (int x = 0; x < m_imageWidth; x++) | |
611 | + cv::Rect prevRect = cv::Rect(xmin_prev, ymin_prev, width_prev, height_prev); | |
612 | + | |
613 | + cv::Mat tempMat(m_backgroundImage.size(), CV_8UC3); | |
614 | + cv::Mat tempROIMat(tempMat, prevRect); | |
615 | + m_backgroundImage.copyTo(tempROIMat); | |
616 | + | |
617 | + for (int y = 0; y < m_imageHeight; y++) | |
602 | 618 | { |
603 | - if (m_focusedFlowRegion.xmin == x) | |
619 | + int py = y - dy; | |
620 | + if (m_focusedFlowRegion.ymin > y || m_focusedFlowRegion.ymax < y || py < 0 || py >= m_imageHeight) | |
604 | 621 | { |
605 | - int xx = x; | |
606 | - for (; xx <= m_focusedFlowRegion.xmax; xx++) | |
622 | + for (int x = 0; x < m_imageWidth; x++) | |
607 | 623 | { |
608 | - m_backgroundImage.at<cv::Vec3b>(y,xx) = tempROIMat.at<cv::Vec3b>(y - dy, xx - dx); | |
624 | + m_backgroundImage.at<cv::Vec3b>(y,x) = newImage.at<cv::Vec3b>(y,x); | |
625 | + m_backgroundImageAge.at<uchar>(y,x) = 1; | |
609 | 626 | } |
610 | - x = xx - 1; | |
611 | 627 | } |
612 | 628 | else |
613 | 629 | { |
614 | - m_backgroundImage.at<cv::Vec3b>(y,x) = newImage.at<cv::Vec3b>(y,x); | |
615 | - m_backgroundImageAge.at<uchar>(y,x) = 1; | |
630 | + for (int x = 0; x < m_imageWidth; x++) | |
631 | + { | |
632 | + if (m_focusedFlowRegion.xmin == x) | |
633 | + { | |
634 | + int xx = x; | |
635 | + for (; xx < m_focusedFlowRegion.xmax && xx < m_imageWidth; xx++) | |
636 | + { | |
637 | + int px = xx -dx; | |
638 | + if (px >= 0 && px < m_imageWidth) | |
639 | + m_backgroundImage.at<cv::Vec3b>(y,xx) = tempROIMat.at<cv::Vec3b>(py, px); | |
640 | + } | |
641 | + x = xx - 1; | |
642 | + } | |
643 | + else | |
644 | + { | |
645 | + m_backgroundImage.at<cv::Vec3b>(y,x) = newImage.at<cv::Vec3b>(y,x); | |
646 | + m_backgroundImageAge.at<uchar>(y,x) = 1; | |
647 | + } | |
648 | + } | |
616 | 649 | } |
617 | 650 | } |
618 | 651 | } |