Commit f043d1fa326f052e3ae62abd8e79885ff32e4ad9
0 parents
first commit
Showing
4 changed files
with
220 additions
and
0 deletions
Show diff stats
1 | +++ a/CMakeLists.txt | |
1 | +#Specify the version being used aswell as the language | |
2 | +cmake_minimum_required(VERSION 3.12) | |
3 | +cmake_policy(SET CMP0074 NEW) | |
4 | + | |
5 | +#Name your project here | |
6 | +project(slideconvert) | |
7 | + | |
8 | +#set the module directory | |
9 | +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}") | |
10 | + | |
11 | +#default to release mode | |
12 | +if(NOT CMAKE_BUILD_TYPE) | |
13 | + set(CMAKE_BUILD_TYPE Release) | |
14 | +endif(NOT CMAKE_BUILD_TYPE) | |
15 | + | |
16 | +#build the executable in the binary directory on MS Visual Studio | |
17 | +if ( MSVC ) | |
18 | + SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}") | |
19 | + SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}") | |
20 | + SET( LIBRARY_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}") | |
21 | + SET( LIBRARY_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}") | |
22 | + add_definitions(-D_CRT_SECURE_NO_WARNINGS) | |
23 | + add_definitions(-D_SCL_SECURE_NO_WARNINGS) | |
24 | +endif ( MSVC ) | |
25 | +#MAYBE REMOVE----------------- | |
26 | +#set C++11 flags if using GCC | |
27 | +if( CMAKE_COMPILER_IS_GNUCC ) | |
28 | + SET( CMAKE_CXX_FLAGS "-std=c++11") | |
29 | + SET( CUDA_NVCC_FLAGS "-std=c++11") | |
30 | +endif( CMAKE_COMPILER_IS_GNUCC ) | |
31 | +#----------------------------- | |
32 | + | |
33 | +#find packages----------------------------------- | |
34 | + | |
35 | +find_package(OpenCV REQUIRED) | |
36 | +add_definitions(-DUSING_OPENCV) | |
37 | + | |
38 | +#find the pthreads package | |
39 | +find_package(Threads) | |
40 | + | |
41 | +#find the X11 package | |
42 | +find_package(X11) | |
43 | + | |
44 | +#find the STIM library | |
45 | +find_package(STIM REQUIRED) | |
46 | + | |
47 | +find_package(OpenSlide REQUIRED) | |
48 | + | |
49 | +add_executable(slideconvert | |
50 | + main.cpp | |
51 | +) | |
52 | + | |
53 | +#include include directories | |
54 | +include_directories(${OpenSlide_INCLUDE_DIRS} | |
55 | + ${OpenCV_INCLUDE_DIRS} | |
56 | + ${STIM_INCLUDE_DIRS} | |
57 | +) | |
58 | + | |
59 | +target_link_libraries(slideconvert | |
60 | + ${OpenSlide_LIBRARIES} | |
61 | + ${OpenCV_LIBS} | |
62 | +) | |
0 | 63 | \ No newline at end of file | ... | ... |
1 | +++ a/FindOpenSlide.cmake | |
1 | +# A CMake find module for the OpenSlide microscopy file reader library. | |
2 | +# | |
3 | +# http://openslide.org | |
4 | +# | |
5 | +# Once done, this module will define | |
6 | +# OpenSlide_FOUND - system has OpenSlide | |
7 | +# OpenSlide_INCLUDE_DIRS - the OpenSlide include directory | |
8 | +# OpenSlide_LIBRARIES - link to these to use OpenSlide | |
9 | + | |
10 | +set(OpenSlide_ROOT $ENV{OpenSlide_ROOT}) | |
11 | +IF(NOT OpenSlide_ROOT) | |
12 | + MESSAGE("ERROR: OpenSlide_ROOT must be set!") | |
13 | +ENDIF(NOT OpenSlide_ROOT) | |
14 | + | |
15 | +FIND_PATH(OpenSlide_INCLUDE_DIRS DOC "Path to OpenSlide include directory." | |
16 | + NAMES openslide/openslide.h | |
17 | + PATHS ${OpenSlide_ROOT}/include) | |
18 | + | |
19 | +FIND_LIBRARY(OpenSlide_LIBRARIES DOC "Absolute path to OpenSlide library." | |
20 | + NAMES libopenslide.lib | |
21 | + PATHS ${OpenSlide_ROOT}/lib) | |
22 | +include(FindPackageHandleStandardArgs) | |
23 | +find_package_handle_standard_args(OpenSlide DEFAULT_MSG OpenSlide_LIBRARIES OpenSlide_INCLUDE_DIRS) | |
24 | + | |
25 | +mark_as_advanced(OpenSlide_INCLUDE_DIRS OpenSlide_LIBRARIES) | |
0 | 26 | \ No newline at end of file | ... | ... |
1 | +++ a/FindSTIM.cmake | |
1 | +# finds the STIM library (downloads it if it isn't present) | |
2 | +# set STIMLIB_PATH to the directory containing the stim subdirectory (the stim repository) | |
3 | + | |
4 | +include(FindPackageHandleStandardArgs) | |
5 | + | |
6 | +set(STIM_ROOT $ENV{STIM_ROOT}) | |
7 | + | |
8 | +IF(NOT UNIX) | |
9 | + IF(NOT STIM_ROOT) | |
10 | + MESSAGE("ERROR: STIM_ROOT environment variable must be set!") | |
11 | + ENDIF(NOT STIM_ROOT) | |
12 | + | |
13 | + FIND_PATH(STIM_INCLUDE_DIRS DOC "Path to GLFW include directory." | |
14 | + NAMES stim/image/image.h | |
15 | + PATHS ${STIM_ROOT}) | |
16 | +ENDIF(NOT UNIX) | |
17 | + | |
18 | +find_package_handle_standard_args(STIM DEFAULT_MSG STIM_INCLUDE_DIRS) | |
19 | + | |
20 | +if(STIM_FOUND) | |
21 | + set(STIM_INCLUDE_DIRS ${STIM_INCLUDE_DIRS}) | |
22 | +elseif(STIM_FOUND) | |
23 | + message("STIM library not found. Set the STIM_ROOT environment variable to the STIM location.") | |
24 | + message("STIMLIB can be found here: https://git.stim.ee.uh.edu/codebase/stimlib") | |
25 | +endif(STIM_FOUND) | |
26 | + | |
27 | +find_package_handle_standard_args(STIM DEFAULT_MSG STIM_INCLUDE_DIRS) | ... | ... |
1 | +++ a/main.cpp | |
1 | +#include <iostream> | |
2 | +#include <string> | |
3 | +#include <openslide/openslide.h> | |
4 | +#include <opencv2/opencv.hpp> | |
5 | +#include <stim/parser/arguments.h> | |
6 | + | |
7 | +stim::arglist args; | |
8 | +int32_t level = 0; | |
9 | +int64_t x = 0; | |
10 | +int64_t y = 0; | |
11 | +int64_t sx, sy; | |
12 | +openslide_t* slide = NULL; | |
13 | + | |
14 | +std::string infile; | |
15 | +std::string outfile; | |
16 | + | |
17 | +void openSlide() { | |
18 | + slide = openslide_open(infile.c_str()); //open the slide file | |
19 | + if (slide == NULL) { //if the slide wasn't opened | |
20 | + throw std::runtime_error("ERROR: unable to open file"); | |
21 | + } | |
22 | +} | |
23 | +void closeSlide() { | |
24 | + openslide_close(slide); //close the slide file | |
25 | +} | |
26 | +//this function displays details about the specified image | |
27 | +void dispDetails() { | |
28 | + std::cout << "OpenSlide version: " << openslide_get_version() << std::endl; | |
29 | + openSlide(); | |
30 | + std::cout << "Details for slide file: " << infile << std::endl; | |
31 | + int32_t levels = openslide_get_level_count(slide); | |
32 | + int64_t w, h; | |
33 | + std::cout << "level sizes (in pixels):" << std::endl; | |
34 | + std::cout << "-------------------------------------" << std::endl; | |
35 | + for (int32_t l = 0; l < levels; l++) { | |
36 | + std::cout << "level " << l; | |
37 | + openslide_get_level_dimensions(slide, l, &w, &h); | |
38 | + std::cout << " [" << w << " x " << h << "]" << std::endl; | |
39 | + } | |
40 | + closeSlide(); | |
41 | +} | |
42 | + | |
43 | +void addArguments() { | |
44 | + args.add("help", "prints this help"); | |
45 | + args.add("level", "specify the level to output", "", "integer value between 0 and max"); | |
46 | + args.add("region", "region to save", "", "x-pos y-pos x-size y-size"); | |
47 | +} | |
48 | + | |
49 | +void readArguments() { | |
50 | + | |
51 | + if (args.nargs() > 0) infile = args.arg(0); //store the file name | |
52 | + | |
53 | + if (args.nargs() > 1) outfile = args.arg(1); //store the output file name | |
54 | + | |
55 | + if (args["level"]) { | |
56 | + level = args["level"].as_int(); //store the desired level to extract | |
57 | + } | |
58 | + else { //if the desired level isn't set, default to the smallest resolution image | |
59 | + openSlide(); | |
60 | + int32_t levels = openslide_get_level_count(slide); | |
61 | + level = levels - 1; | |
62 | + closeSlide(); | |
63 | + } | |
64 | + if (args["region"]) { | |
65 | + if (args["region"].nargs() == 4) { | |
66 | + x = args["region"].as_int(0); | |
67 | + y = args["region"].as_int(1); | |
68 | + sx = args["region"].as_int(2); | |
69 | + sy = args["region"].as_int(3); | |
70 | + } | |
71 | + else { | |
72 | + throw std::runtime_error("ERROR: --region requires 4 parameters"); | |
73 | + } | |
74 | + } | |
75 | + else { | |
76 | + openSlide(); | |
77 | + openslide_get_level_dimensions(slide, level, &sx, &sy); | |
78 | + closeSlide(); | |
79 | + } | |
80 | +} | |
81 | + | |
82 | +int main(int argc, char** argv) { | |
83 | + addArguments(); | |
84 | + args.parse(argc, argv); | |
85 | + readArguments(); | |
86 | + if (args.nargs() == 0 || args["help"]) { //if the user requests help or doesn't do anything, show usage | |
87 | + std::cout << "OpenSlide version: " << openslide_get_version() << std::endl; | |
88 | + std::cout << "Usage----------------" << std::endl; | |
89 | + std::cout << "save the full image at the lowest resolution: slideconvert slidefile.ndpi output.bmp" << std::endl; | |
90 | + std::cout << "save the level-3 (3x downsampling) image: slideconvert slidefile.ndpi output.bmp --level 3" << std::endl; | |
91 | + std::cout << "save a 1024x1024 region at full-resolution: slideconvert slidefile.ndpi output.bmp --level 0 --region 10000 15000 1024 1024" << std::endl; | |
92 | + std::cout << args.str(); //if no arguments are provided, show usage | |
93 | + return 0; | |
94 | + } | |
95 | + else if (args.nargs() == 1) { //if one argument is provided, display file information | |
96 | + dispDetails(); | |
97 | + return 0; | |
98 | + } | |
99 | + else if (args.nargs() == 2) { //if two arguments are provided, save the specified region | |
100 | + openSlide(); | |
101 | + cv::Mat I(sy, sx, CV_8UC4); //allocate an OpenCV image array | |
102 | + openslide_read_region(slide, (uint32_t*)I.data, x, y, level, sx, sy); | |
103 | + cv::imwrite(outfile, I); | |
104 | + closeSlide(); | |
105 | + } | |
106 | +} | |
0 | 107 | \ No newline at end of file | ... | ... |