commit 19bf16cc8908f1d2ae8f95fa5c7734c4c17042ea Author: hssheng Date: Mon Dec 30 14:37:54 2024 +0800 Init: 首次提交, 功能完成, 裁剪锁眼地图. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aeaa683 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +################################################################################ +# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 +################################################################################ + +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1d40087 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,35 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.9) + +# project name +PROJECT(cutImg) + +# release debug +SET(CMAKE_BUILD_TYPE release) + +# head file +file(GLOB HEAD_LIST +${PROJECT_SOURCE_DIR}/src/*.h ${PROJECT_SOURCE_DIR}/src/*.hpp ) + +# direction of source file +file(GLOB SRC_LIST ${PROJECT_SOURCE_DIR}/src/*.cpp) + +# direction of head file +INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src) + +add_executable(${PROJECT_NAME} ${HEAD_LIST} ${SRC_LIST}) +target_link_libraries(${PROJECT_NAME} -lSDL) + +# openMP +find_package(OpenMP) +if (OPENMP_FOUND) + set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") + set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}") +endif() + + +# opencv +set(OpenCV_DIR "C:\\Toolkits\\OpenCV\\x64\\vc15\\lib") +find_package(OpenCV REQUIRED) +include_directories(${OpenCV_INCLUDE_DIRS}) +target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) diff --git a/cutImg.ico b/cutImg.ico new file mode 100644 index 0000000..ac604de Binary files /dev/null and b/cutImg.ico differ diff --git a/data/1.png b/data/1.png new file mode 100644 index 0000000..489eb2e Binary files /dev/null and b/data/1.png differ diff --git a/data/1_cut.png b/data/1_cut.png new file mode 100644 index 0000000..a536dd1 Binary files /dev/null and b/data/1_cut.png differ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..42e7157 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,257 @@ +#include +#include +#include +#include +#include +#include + +using namespace cv; +using namespace std; + +cv::Mat img; +bool select_flag = false; +cv::Rect m_select; +cv::Point origin; +int ROI_count; + +void onMouseRectPicking(int event, int x, int y, int, void*) +{ + if (select_flag) + { + m_select.x = MIN(origin.x, x);//һҪ굯żο򣬶Ӧ갴¿ʼʱʵʱѡο + m_select.y = MIN(origin.y, y); + m_select.width = abs(x - origin.x);//οȺ͸߶ + m_select.height = abs(y - origin.y); + m_select &= cv::Rect(0, 0, img.cols, img.rows);//֤ѡοƵʾ֮ + } + + if (event == CV_EVENT_LBUTTONDOWN) + { + select_flag = true; //갴µı־ֵ + origin = cv::Point(x, y); //׽ĵ + m_select = cv::Rect(x, y, 0, 0); //һҪʼ͸Ϊ(0,0)ΪopencvRectοڵĵǰϽǸģDz½Ǹ + } + else if (event == CV_EVENT_LBUTTONUP) + { + select_flag = false; + ROI_count++; + } +} + +int main(int argc, char* argv[]) +{ + //..................................................................... + // QT for open TIF image + //..................................................................... + OPENFILENAME ofn; + TCHAR szFile[MAX_PATH]; + ZeroMemory(&ofn, sizeof(OPENFILENAME)); + ofn.lStructSize = sizeof(OPENFILENAME); + ofn.hwndOwner = NULL; + ofn.lpstrFile = szFile; + ofn.lpstrFile[0] = '\0'; + ofn.nMaxFile = sizeof(szFile); + ofn.lpstrFilter = (LPCSTR)(LPCWSTR)"TIF Img(*.tif*)\0"; + ofn.nFilterIndex = 1; + ofn.lpstrFileTitle = NULL; + ofn.nMaxFileTitle = 0; + ofn.lpstrInitialDir = NULL; + ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; + + if (GetOpenFileName(&ofn)) + { + std::cout << szFile << std::endl; + } + + std::string filename; +#ifndef UNICODE + filename = szFile; +#else + std::wstring wStr = szFile; + filename = std::string(wStr.begin(), wStr.end()); +#endif + + // check filename + if (filename.length() < 1) + { + std::cout << "Sorry! No image!" << std::endl; + return -1; + } + + if (FILE* file = fopen(filename.c_str(), "r")) + { + fclose(file); + } + else + { + std::cout << "Sorry! No image!" << std::endl; + return -1; + } + + //std::string filePath = "../data/1.png"; + std::string filePath = filename; + img = imread(filePath); + // get file name prefix + int preFixId = filePath.find_last_of('.'); + int lastPoai = filePath.length() - 1; + std::string prfixPath = filePath.substr(0, preFixId); + std::string ptfixPath = filePath.substr(preFixId, filePath.length()); + std::string cutFilePath = prfixPath + "_cut" + ptfixPath; + // resample + int down_width = 1600; + int down_height = img.rows * down_width / img.cols; + cv::Size2i down_points{ down_width, down_height }; + cv::Mat imgR; + cv::resize(img, imgR, down_points, cv::INTER_LINEAR); + bool stop = false; + cv::namedWindow("capframe", CV_WINDOW_AUTOSIZE); + cv::setMouseCallback("capframe", onMouseRectPicking, 0); + char pic_name[40]; + ROI_count = 0; + cv::Mat imgLine; + + while (!stop) + { + imgLine = imgR.clone(); + cv::rectangle(imgLine, m_select, cv::Scalar(255, 0, 0), 2, 8, 0); // ο + cv::imshow("capframe", imgLine); + char key = cv::waitKey(30); + + if (key == 27) + stop = true; + } + + waitKey(0); + // orignal + double ratioW0 = m_select.x * 1.0 / down_width; + double ratioH0 = m_select.y * 1.0 / down_height; + cv::Rect m_selectOri; + m_selectOri.x = floor(ratioW0 * img.cols); + m_selectOri.y = floor(ratioH0 * img.rows); + m_selectOri.width = floor(m_select.width * img.cols * 1.0 / down_width); + m_selectOri.height = floor(m_select.height * img.rows * 1.0 / down_height); + Mat ROI = img(m_selectOri); + imwrite(cutFilePath, ROI); + printf("Write cut image successfully!!!"); + return 0; +} + + + +//#include +// +//#include"opencv2/opencv.hpp" +//#include"opencv2/highgui.hpp" +// +// +//// ¼ݽṹ +//struct onMouseData +//{ +//public: +// cv::Mat kSrcImg; // ԭʼͼ +// cv::Mat kDrawImg; // ͼͼ +// cv::Point kPointer; // ǰ +// cv::Point kRectTopLeft; // ROIϽ +// cv::Point kRectBottomRight; // ROI½ +// cv::Rect kRoi; // ROIοϢ +// cv::Scalar kLineColor; // οɫ +// int kLineWidth; // οͼ +// std::string kWiddget; // ͼ +// bool isDrawing; // ǰǷڻROI +// onMouseData() +// { +// kLineColor = cv::Scalar(0, 0, 255); +// kRectTopLeft = { -1, -1 }; +// kRectBottomRight = { -1, -1 }; +// kPointer = { -1, -1 }; +// isDrawing = false; +// kLineWidth = 3; +// } +//}; +// +//// ʵʱROI +//void drawRoi(onMouseData* kMouseData); +// +//// ¼ص +//void onMouseCallBack(int event, int x, int y, int flags, void* param); +// +//// ROIѡܺ +//void selectROI(const std::string& kWiddget, const cv::Mat& kSrcImg, onMouseData& kMouseData); +// +//int main() +//{ +// cv::Mat kSrcImg = cv::imread("../data/1.png"); +// std::string mainWidget = "ROIѡ"; +// cv::namedWindow(mainWidget, cv::WINDOW_KEEPRATIO); +// cv::resizeWindow(mainWidget, 1280, 800); +// onMouseData kMouseData; +// selectROI(mainWidget, kSrcImg, kMouseData); +// std::cout << kMouseData.kRoi << std::endl; +// system("pause"); +// cv::destroyAllWindows(); +// return 0; +//} +// +//void drawRoi(onMouseData* kMouseData) +//{ +// if (kMouseData->isDrawing) +// { +// kMouseData->kRoi.x = kMouseData->kRectTopLeft.x; +// kMouseData->kRoi.y = kMouseData->kRectTopLeft.y; +// kMouseData->kRoi.width = kMouseData->kPointer.x - kMouseData->kRectTopLeft.x; +// kMouseData->kRoi.height = kMouseData->kPointer.y - kMouseData->kRectTopLeft.y; +// cv::rectangle(kMouseData->kDrawImg, +// kMouseData->kRoi, +// kMouseData->kLineColor, +// kMouseData->kLineWidth, cv::LINE_8, 0); +// cv::imshow(kMouseData->kWiddget, kMouseData->kDrawImg); +// } +//} +//void onMouseCallBack(int event, int x, int y, int flags, void* param) +//{ +// onMouseData* kMouseData = reinterpret_cast(param); +// kMouseData->kDrawImg = kMouseData->kSrcImg.clone(); +// kMouseData->kPointer = { x, y }; +// +// switch (event) +// { +// case cv::EVENT_LBUTTONDOWN: +// kMouseData->kRectTopLeft = kMouseData->kPointer; +// kMouseData->isDrawing = !kMouseData->isDrawing; +// drawRoi(kMouseData); +// break; +// +// case cv::EVENT_LBUTTONUP: +// kMouseData->kRectBottomRight = kMouseData->kPointer; +// kMouseData->isDrawing = false; +// break; +// +// case cv::EVENT_MOUSEMOVE: +// kMouseData->kRectBottomRight = kMouseData->kPointer; +// drawRoi(kMouseData); +// break; +// +// case cv::EVENT_RBUTTONDOWN: +// kMouseData->isDrawing = false; +// +// default: +// break; +// } +//} +//void selectROI(const std::string& kWiddget, const cv::Mat& kSrcImg, onMouseData& kMouseData) +//{ +// kMouseData.kSrcImg = kSrcImg.clone(); +// kMouseData.kWiddget = kWiddget; +// cv::imshow(kMouseData.kWiddget, kMouseData.kSrcImg); +// cv::setMouseCallback(kMouseData.kWiddget, +// onMouseCallBack, +// reinterpret_cast(&kMouseData)); +// cv::waitKey(); +//} +// +// +// + + + +