| |
|
| |
|
| |
|
| |
|
| | #include "precomp.hpp"
|
| | #include <opencv2/imgproc.hpp>
|
| | #include <algorithm>
|
| |
|
| | using namespace cv;
|
| |
|
| | namespace
|
| | {
|
| | class ROISelector
|
| | {
|
| | public:
|
| | Rect select(const String &windowName, Mat img, bool showCrossair = true, bool fromCenter = true, bool printNotice = true)
|
| | {
|
| | if(printNotice)
|
| | {
|
| |
|
| | printf("Select a ROI and then press SPACE or ENTER button!\n");
|
| | printf("Cancel the selection process by pressing c button!\n");
|
| | }
|
| |
|
| | key = 0;
|
| | imageSize = img.size();
|
| |
|
| |
|
| | selectorParams.drawFromCenter = fromCenter;
|
| |
|
| |
|
| | imshow(windowName, img);
|
| |
|
| |
|
| | selectorParams.image = img.clone();
|
| |
|
| |
|
| | setMouseCallback(windowName, mouseHandler, (void*)this);
|
| |
|
| |
|
| | while (!(key == 32 || key == 27 || key == 13))
|
| | {
|
| |
|
| | rectangle(selectorParams.image, selectorParams.box, Scalar(255, 0, 0), 2, 1);
|
| |
|
| |
|
| | if (showCrossair)
|
| | {
|
| |
|
| | line(selectorParams.image,
|
| | Point((int)selectorParams.box.x,
|
| | (int)(selectorParams.box.y + selectorParams.box.height / 2)),
|
| | Point((int)(selectorParams.box.x + selectorParams.box.width),
|
| | (int)(selectorParams.box.y + selectorParams.box.height / 2)),
|
| | Scalar(255, 0, 0), 2, 1);
|
| |
|
| |
|
| | line(selectorParams.image,
|
| | Point((int)(selectorParams.box.x + selectorParams.box.width / 2),
|
| | (int)selectorParams.box.y),
|
| | Point((int)(selectorParams.box.x + selectorParams.box.width / 2),
|
| | (int)(selectorParams.box.y + selectorParams.box.height)),
|
| | Scalar(255, 0, 0), 2, 1);
|
| | }
|
| |
|
| |
|
| | imshow(windowName, selectorParams.image);
|
| |
|
| |
|
| | selectorParams.image = img.clone();
|
| |
|
| |
|
| | key = waitKey(30);
|
| |
|
| | if (key == 'c' || key == 'C')
|
| | {
|
| | selectorParams.box = Rect();
|
| | break;
|
| | }
|
| | }
|
| |
|
| |
|
| | setMouseCallback(windowName, emptyMouseHandler, NULL);
|
| |
|
| | return selectorParams.box;
|
| | }
|
| |
|
| | void select(const String &windowName, Mat img, std::vector<Rect> &boundingBoxes,
|
| | bool showCrosshair = true, bool fromCenter = true, bool printNotice = true)
|
| | {
|
| | if(printNotice)
|
| | {
|
| | printf("Finish the selection process by pressing ESC button!\n");
|
| | }
|
| | boundingBoxes.clear();
|
| | key = 0;
|
| |
|
| |
|
| | for (;;)
|
| | {
|
| | Rect temp = select(windowName, img, showCrosshair, fromCenter, printNotice);
|
| | if (key == 27)
|
| | break;
|
| | if (temp.width > 0 && temp.height > 0)
|
| | boundingBoxes.push_back(temp);
|
| | }
|
| | }
|
| |
|
| | struct handlerT
|
| | {
|
| |
|
| | bool isDrawing;
|
| | Rect2d box;
|
| | Mat image;
|
| | Point2f startPos;
|
| |
|
| |
|
| | bool drawFromCenter;
|
| |
|
| |
|
| | handlerT() : isDrawing(false), drawFromCenter(true){}
|
| | } selectorParams;
|
| |
|
| | private:
|
| | static void emptyMouseHandler(int, int, int, int, void*)
|
| | {
|
| | }
|
| |
|
| | static void mouseHandler(int event, int x, int y, int flags, void *param)
|
| | {
|
| | ROISelector *self = static_cast<ROISelector *>(param);
|
| | self->opencv_mouse_callback(event, x, y, flags);
|
| | }
|
| |
|
| | void opencv_mouse_callback(int event, int x, int y, int)
|
| | {
|
| | switch (event)
|
| | {
|
| |
|
| | case EVENT_MOUSEMOVE:
|
| | if (selectorParams.isDrawing)
|
| | {
|
| | if (selectorParams.drawFromCenter)
|
| | {
|
| |
|
| | float halfWidth = std::min(std::min(
|
| | std::abs(x - selectorParams.startPos.x),
|
| | selectorParams.startPos.x),
|
| | imageSize.width - selectorParams.startPos.x);
|
| | float halfHeight = std::min(std::min(
|
| | std::abs(y - selectorParams.startPos.y),
|
| | selectorParams.startPos.y),
|
| | imageSize.height - selectorParams.startPos.y);
|
| |
|
| | selectorParams.box.width = halfWidth * 2;
|
| | selectorParams.box.height = halfHeight * 2;
|
| | selectorParams.box.x = selectorParams.startPos.x - halfWidth;
|
| | selectorParams.box.y = selectorParams.startPos.y - halfHeight;
|
| |
|
| | }
|
| | else
|
| | {
|
| |
|
| | int lx = std::min(std::max(x, 0), imageSize.width);
|
| | int by = std::min(std::max(y, 0), imageSize.height);
|
| | selectorParams.box.width = std::abs(lx - selectorParams.startPos.x);
|
| | selectorParams.box.height = std::abs(by - selectorParams.startPos.y);
|
| | selectorParams.box.x = std::min((float)lx, selectorParams.startPos.x);
|
| | selectorParams.box.y = std::min((float)by, selectorParams.startPos.y);
|
| | }
|
| | }
|
| | break;
|
| |
|
| |
|
| | case EVENT_LBUTTONDOWN:
|
| | selectorParams.isDrawing = true;
|
| | selectorParams.box = Rect2d(x, y, 0, 0);
|
| | selectorParams.startPos = Point2f((float)x, (float)y);
|
| | break;
|
| |
|
| |
|
| | case EVENT_LBUTTONUP:
|
| | selectorParams.isDrawing = false;
|
| | if (selectorParams.box.width < 0)
|
| | {
|
| | selectorParams.box.x += selectorParams.box.width;
|
| | selectorParams.box.width *= -1;
|
| | }
|
| | if (selectorParams.box.height < 0)
|
| | {
|
| | selectorParams.box.y += selectorParams.box.height;
|
| | selectorParams.box.height *= -1;
|
| | }
|
| | break;
|
| | }
|
| | }
|
| |
|
| |
|
| | int key;
|
| | Size imageSize;
|
| | };
|
| | }
|
| |
|
| | Rect cv::selectROI(InputArray img, bool showCrosshair, bool fromCenter, bool printNotice)
|
| | {
|
| | ROISelector selector;
|
| | return selector.select("ROI selector", img.getMat(), showCrosshair, fromCenter, printNotice);
|
| | }
|
| |
|
| | Rect cv::selectROI(const String& windowName, InputArray img, bool showCrosshair, bool fromCenter, bool printNotice)
|
| | {
|
| | ROISelector selector;
|
| | return selector.select(windowName, img.getMat(), showCrosshair, fromCenter, printNotice);
|
| | }
|
| |
|
| | void cv::selectROIs(const String& windowName, InputArray img,
|
| | std::vector<Rect>& boundingBox, bool showCrosshair, bool fromCenter, bool printNotice)
|
| | {
|
| | ROISelector selector;
|
| | selector.select(windowName, img.getMat(), boundingBox, showCrosshair, fromCenter, printNotice);
|
| | }
|
| |
|