Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <Windows.h>
- #include <opencv2/core.hpp>
- #include <opencv2/highgui.hpp>
- #include <opencv2/imgproc.hpp>
- cv::Mat image1, image2;
- cv::Mat src, img, ROI;
- cv::Rect cropRect(0, 0, 0, 0);
- cv::Point P1(0, 0);
- cv::Point P2(0, 0);
- const std::string winName = "Crop Image";
- bool clicked = false;
- int saveIndex = 0;
- char imgName[15];
- void checkBoundary()
- {
- //check croping rectangle exceed image boundary
- if (cropRect.width > img.cols - cropRect.x)
- cropRect.width = img.cols - cropRect.x;
- if (cropRect.height > img.rows - cropRect.y)
- cropRect.height = img.rows - cropRect.y;
- if (cropRect.x < 0)
- cropRect.x = 0;
- if (cropRect.y < 0)
- cropRect.height = 0;
- }
- void showImage()
- {
- img = src.clone();
- checkBoundary();
- if (cropRect.width > 0 && cropRect.height > 0)
- {
- ROI = src(cropRect);
- imshow("cropped", ROI);
- }
- cv::rectangle(img, cropRect, cv::Scalar(0, 255, 0), 1, 8, 0);
- imshow(winName, img);
- }
- void onMouseCrop(int event, int x, int y, int f, void*)
- {
- switch (event)
- {
- case cv::EVENT_LBUTTONDOWN:
- clicked = true;
- P1.x = x;
- P1.y = y;
- P2.x = x;
- P2.y = y;
- break;
- case cv::EVENT_LBUTTONUP:
- P2.x = x;
- P2.y = y;
- clicked = false;
- break;
- case cv::EVENT_MOUSEMOVE:
- if (clicked)
- {
- P2.x = x;
- P2.y = y;
- }
- break;
- default:
- break;
- }
- if (clicked)
- {
- if (P1.x > P2.x)
- {
- cropRect.x = P2.x;
- cropRect.width = P1.x - P2.x;
- }
- else
- {
- cropRect.x = P1.x;
- cropRect.width = P2.x - P1.x;
- }
- if (P1.y > P2.y)
- {
- cropRect.y = P2.y;
- cropRect.height = P1.y - P2.y;
- }
- else
- {
- cropRect.y = P1.y;
- cropRect.height = P2.y - P1.y;
- }
- }
- showImage();
- }
- void displayRoiMessage()
- {
- std::cout << "Click and drag for Selection" << std::endl << std::endl;
- std::cout << "------> Press 's' to save" << std::endl << std::endl;
- std::cout << "------> Press '8' to move up" << std::endl;
- std::cout << "------> Press '2' to move down" << std::endl;
- std::cout << "------> Press '6' to move right" << std::endl;
- std::cout << "------> Press '4' to move left" << std::endl << std::endl;
- std::cout << "------> Press 'w' increas top" << std::endl;
- std::cout << "------> Press 'x' increas bottom" << std::endl;
- std::cout << "------> Press 'd' increas right" << std::endl;
- std::cout << "------> Press 'a' increas left" << std::endl << std::endl;
- std::cout << "------> Press 't' decrease top" << std::endl;
- std::cout << "------> Press 'b' decrease bottom" << std::endl;
- std::cout << "------> Press 'h' decrease right" << std::endl;
- std::cout << "------> Press 'f' decrease left" << std::endl << std::endl;
- std::cout << "------> Press 'r' to reset" << std::endl;
- std::cout << "------> Press 'Esc' to quit" << std::endl << std::endl;
- }
- void performOpcode(char c)
- {
- if (c == 's' && ROI.data)
- {
- std::sprintf(imgName, "%d.jpg", saveIndex++);
- cv::imwrite(imgName, ROI);
- std::cout << " Saved " << imgName << std::endl;
- }
- if (c == '6') cropRect.x++;
- if (c == '4') cropRect.x--;
- if (c == '8') cropRect.y--;
- if (c == '2') cropRect.y++;
- if (c == 'w')
- {
- cropRect.y--;
- cropRect.height++;
- }
- if (c == 'd') cropRect.width++;
- if (c == 'x') cropRect.height++;
- if (c == 'a')
- {
- cropRect.x--;
- cropRect.width++;
- }
- if (c == 't')
- {
- cropRect.y++;
- cropRect.height--;
- }
- if (c == 'h') cropRect.width--;
- if (c == 'b') cropRect.height--;
- if (c == 'f')
- {
- cropRect.x++;
- cropRect.width--;
- }
- if (c == 'r')
- {
- cropRect.x = 0;
- cropRect.y = 0;
- cropRect.width = 0;
- cropRect.height = 0;
- }
- if (c == 27)
- {
- std::cout << "Exit program" << std::endl << std::endl;
- std::exit(EXIT_FAILURE);
- }
- }
- int main(int argc, char* argv[])
- {
- cv::VideoCapture cap;
- cap.open(1);
- if (!cap.isOpened())
- {
- std::cerr << "cannot open camera for capture" << std::endl;
- std::exit(EXIT_FAILURE);
- }
- cv::waitKey(1000);
- // Let user crop a ROI from the input image
- displayRoiMessage();
- cv::namedWindow(winName, cv::WINDOW_NORMAL);
- cv::setMouseCallback(winName, onMouseCrop, nullptr);
- //imshow(winName, src);
- char c;
- do
- {
- cap >> src;
- showImage();
- c = cv::waitKey();
- performOpcode(c);
- } while (c != VK_RETURN);
- // Extract feature
- std::cout << "into feature detection" << std::endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment