๐ฃ ๋ชฉ์ : CPP ํ์ผ์ Build ํ ํ object file์ ์คํ์ํฌ ๊ฒฝ์ฐ args๋ฅผ ๋ฐ์(์
๋ ฅ ์ด๋ฏธ์ง, ์ถ๋ ฅ์ด๋ฏธ์ง) ์คํ์ํค๊ธฐ!
๐ฃ ๋ฐฉ๋ฒ 1 : CMakefile๊ณผ cpp ํ์ผ์ ์์ฑํ๊ณ clion์ผ๋ก ์คํ์ํฌ ๋ args๋ฅผ ๋ฃ๊ธฐ
๐ฃ ๋ฐฉ๋ฒ 2 : CMakefile๊ณผ cpp ํ์ผ์ ์์ฑํ๊ณ shell ์ฐฝ์ ํตํด ์คํ ์ํค๊ธฐ
๐ ๊ณตํต๊ณผ์
- ์๋์ ๊ฐ์ ๊ตฌ์กฐ๋ก ํด๋๋ฅผ ๊ตฌ์ฑํด ์ค๋ค
.
โโโ CMakeLists.txt #1
โโโ examples
โ โโโ lenna.bmp
โ โโโ imgread.cpp
โโโ main.cpp
โโโ modules
โ โโโ CMakeLists.txt #2
โ โโโ module1
โ โโโ CMakeLists.txt #3
โ โโโ include > module1 > ClassMat.hpp
โ โโโ src > ClassMat.cpp
โ
โโโ thirdparty
โ
โโโ OpenCV
โโโ build
โโโ install
โโโ opencv
2023.06.01 - [Done] - [Ubuntu CMake] OpenCV ์ํ๋ ํด๋์ build & Install
์ด ๋ถ๋ถ์ ์ฐธ๊ณ ํด์ thirdparty๋ด์ ์ค์นํ๋ฉด ๋๋ค (๋จ -DCMAKE_BUILD_TYPE=Debug ๋ชจ๋๋ก ์งํ)
์ด 3๊ฐ์ง์ CMakeLists.txt๋ฅผ ์์ฑํด์ผํ๋๋ฐ
#1์ ์ ์ฒด ํด๋์์ ๋งํฌ๋ฅผ ์ฐ๊ฒฐํด์ค๋ค๊ณ ์๊ฐํ๋ฉด ๋๊ณ ,
2๋ฒ์ module1๊ณผ #1์ ์ฐ๊ฒฐ module1 ๋ด๋ถ๋ include๋ฅผ ์ฐพ์ ์ ์๋๋ก ํด์ค๋ค
CMakeLists.txt ๐งก #1
cmake_minimum_required(VERSION 3.16)
project(<project_name> LANGUAGES CXX) # CXX -> C++
set(CMAKE_CXX_STANDARD 14) # CMAKE_ : Depends on Cmake
set(CMAKE_CXX_STANDARD_REQUIRED ON) # only do 14 versions of cmake
add_subdirectory(modules)
add_executable(<project_name> main.cpp) # (project-name project.cpp)
# add_excutable์ ํตํด์ ์คํํ ํ์ผ์ ์์ฑ
add_executable(read_img examples/imgread.cpp)
# target_link_libraries : target libraries ์ฐ๊ฒฐ
target_link_libraries(read_img PRIVATE
module1
)
CMakeLists.txt ๐ #2
add_subdirectory(module1) // ํ์ directory ์ฐ๊ฒฐ์ ์ํ ์ง๊ฒ๋ค๋ฆฌ!
CMakeLists.txt ๐ #3
cmake_minimum_required(VERSION 3.16)
project(module1 LANGUAGES CXX) # CXX -> C++
set(CMAKE_CXX_STANDARD 14) # CMAKE_ : Depends on Cmake
set(CMAKE_CXX_STANDARD_REQUIRED ON) # only do 14 versions of cmake
set(MODULE1_SOURCE_FILES
src/ClassMat.cpp
) # Create a variable : MODULE1_SOURCE_FILES
add_library(module1
${MODULE1_SOURCE_FILES}
) # ์ ๋ณ์์ ์๋ ๋ชจ๋ ์์คํ์ผ๋ค์ ๋ฃ์ผ๋ ค๊ณ ํจ
# REQUIRED : ์์ผ๋ฉด build ์ ํ๊ฒ ๋ค
find_package(OpenCV REQUIRED HINTS ${CMAKE_SOURCE_DIR}/thirdparty/OpenCV/install/lib/cmake/opencv4)
if (OpenCV_FOUND)
message(STATUS "OpenCV Found! - ${OpenCV_DIR}")
endif()
#include_directories() # global ํ์ cmake directory
target_include_directories(module1 PUBLIC
include
${OpenCV_INCLUDE_DIRS}
) # target header files folder
target_link_libraries(module1 PUBLIC
${OpenCV_LIBS} # link libraries ํ๊ณ ์ถ์ด์!
)
์ดํ์ hpp, cpp file๋ค์ ์์ฑํด ์ฃผ์
๐ClassMat.hpp๐
# include "opencv2/opencv.hpp"
class ClassMat
{
public:
ClassMat() = default;
private:
cv::Mat cv_mat_;
};
๐ฆClassMat.cpp๐ฆ
#include "module1/ClassMat.hpp"
๐ฌimgread.cpp๐ฌ
#include <iostream>
#include <typeinfo> // type information
#include <unistd.h> // dir
#include "opencv2/opencv.hpp"
int main(int argc, char* argv[]) // argc : input num, argv : input char s // char* argv[]์ char **argv๋ ๋ชจ๋ ๋ฌธ์์ด ํฌ์ธํฐ ๋ฐฐ์ด์ ๋ํ
{
std::string input_img;
std::string output_img;
// argc ๊ฐ ๋ช ๊ฐ์ธ์ง ํ์ธํ ํ ๊ฐ์ ์ฌ์ฉํ ๊ฒ์ธ์ง ์๋์ง ํ๋จ
if (argc == 2){
input_img = argv[1];
output_img = "./examples/result.png";
std::cout << "here!" << std::endl;
}
else if(argc == 3){
input_img = argv[1];
output_img = argv[2];
}
else{
input_img = "./examples/lenna.bmp";
output_img = "./examples/result.png";
}
// directory๊ฐ ์ด๋์ธ์ง ๊ถ๊ธํด์ ๋ฃ์ด๋ ๋ถ๋ถ ์๋ฏธ์์
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != nullptr) {
std::cout << "Current working directory: " << cwd << std::endl;
} else {
std::cerr << "Failed to get current working directory\n";
return 1;
}
// 1. imread
cv::Mat src = cv::imread(input_img, cv::IMREAD_GRAYSCALE); // cmake-build-debug์์ ์คํ๋จ์ผ๋ก,,,, ์์๋จ์ผ๋ก ์ฌ๋ผ๊ฐ์ ๋ค์ exaple๋ก,,
// 2. file check
if (src.empty()) {
std::cerr << "Image laod failed!" << std::endl;
return -1;
}
// 3. img ์กฐ์
cv::Mat dst = 255 - src;
// 4. ์กฐ์๋ ์ด๋ฏธ์ง ์ ์ฅ imwrite
cv::imwrite(output_img, dst);
// 5. ๊ฒฐ๊ณผํ์ธ
cv::imshow("src", src);
cv::imshow("dst", dst);
cv::waitKey();
cv::destroyAllWindows();
}
๐ช ๋ฐฉ๋ฒ 1
- ๋ง์น๋ชจ์์ ํตํด ๋น๋ํ ํ ์คํ ์ํฌ ๊ฒฝ์ฐ Run > Edit Configurations
๐ช ๋ฐฉ๋ฒ 2
- build ํ shell ์ฐฝ์ ์
๋ ฅ
./cmake-build-debug/read_img ./examples/lenna.bmp
# <์คํ ํ์ผ์ ๊ฒฝ๋ก> <read image arg> <write image arg>
'Done > C++_Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[OpenCV C++] ์ปฌ๋ฌ์์์ ๊ทธ๋ ์ด์ค์ผ์ผ ์ด๋ฏธ์ง๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ(cvtColor & ์ฐ์ฐ์ ์ด์ฉํ ๋ฐฉ๋ฒ & sol.) (0) | 2023.06.01 |
---|---|
[OpenCV C++] ์ด๋ฏธ์ง ์ ์ฅํ๊ณ ๋ถ๋ฌ์ค๊ธฐ , ์ฐฝ๋ซ๊ธฐ (CV ํจ์ ์ค๋ช - 1) (0) | 2023.06.01 |
[Python] Visualization tool (0) | 2023.01.31 |
[Pytorch] Randomness ๊ณ ์ cuda (0) | 2022.09.14 |
[Python-OpenCV] Window ์ฐฝ ๊ด๋ จ ํจ์ ์ ๋ฆฌ (0) | 2021.10.19 |