実現したいこと
以下のページで公開されているプログラムを実行したいです。
https://github.com/davidstutz/superpixel-benchmark
自分が行っている方法以外に実行するための環境やエラーの原因をご存じの方いましたら教えていただきたいです。
前提
やってみたいことができそうなプログラムをGitHubで見つけたのですが、どのような環境が必要で、どのようにこのプログラムを実行させるのかわかりません。
上記のページのプログラムをダウンロードし、vscodeやvisualstudioで実行しようと試みましたが、以下のようなエラーが出てしまいます。
実行するための環境やエラーの原因をご存じの方いましたら教えていただきたいです。
また、CMakeList.txtがある場合、CMakeでビルドしなければいけないのかなども不明です。
発生している問題・エラーメッセージ
ソース ファイルを開けません "opencv2/opencv_modules.hpp" (dependency of "C:\Users\〇〇\AppData\Local\Microsoft\vscode-cpptools\ipch\b163e7505d9cd06f\opencv2\opencv.hpp") [{ "resource": "/c:/Users/○○/SuperPixcel/superpixel-benchmark-master/ccs_cli/main.cpp", "owner": "cpptools", "severity": 8, "message": "opencv2/opencv.hpp: No such file or directory", "source": "gcc", "startLineNumber": 33, "startColumn": 10, "endLineNumber": 33, "endColumn": 10 }] opencv2/opencv.hpp: No such file or directory
該当のソースコード
C++
1/** 2 * Copyright (c) 2016, David Stutz 3 * Contact: david.stutz@rwth-aachen.de, davidstutz.de 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without modification, 7 * are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * 3. Neither the name of the copyright holder nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <fstream> 33#include <opencv2/opencv.hpp> 34#include <boost/filesystem.hpp> 35#include <boost/program_options.hpp> 36#include <boost/timer.hpp> 37#include "ccs_opencv.h" 38#include "io_util.h" 39#include "superpixel_tools.h" 40#include "visualization.h" 41 42/** \brief Command line tool for running CCS. 43 * Usage: 44 * \code{sh} 45 * $ ../bin/ccs_cli --help 46 * Allowed options: 47 * -h [ --help ] produce help message 48 * -i [ --input ] arg the folder to process 49 * -s [ --superpixels ] arg (=400) number of superpixels 50 * -c [ --compactness ] arg (=500) compactness weight 51 * -t [ --iterations ] arg (=20) number of iterations to perform 52 * -r [ --color-space ] arg (=0) 0 = RGB, >0 = Lab 53 * -o [ --csv ] arg save segmentation as CSV file 54 * -v [ --vis ] arg visualize contours 55 * -x [ --prefix ] arg output file prefix 56 * -w [ --wordy ] verbose/wordy/debug 57 * \endcode 58 * \author David Stutz 59 */ 60int main(int argc, const char** argv) { 61 62 boost::program_options::options_description desc("Allowed options"); 63 desc.add_options() 64 ("help,h", "produce help message") 65 ("input,i", boost::program_options::value<std::string>(), "the folder to process") 66 ("superpixels,s", boost::program_options::value<int>()->default_value(400), "number of superpixels") 67 ("compactness,c", boost::program_options::value<int>()->default_value(500), "compactness weight") 68 ("iterations,t", boost::program_options::value<int>()->default_value(20), "number of iterations to perform") 69 ("color-space,r", boost::program_options::value<int>()->default_value(0), "0 = RGB, >0 = Lab") 70 ("csv,o", boost::program_options::value<std::string>()->default_value(""), "save segmentation as CSV file") 71 ("vis,v", boost::program_options::value<std::string>()->default_value(""), "visualize contours") 72 ("prefix,x", boost::program_options::value<std::string>()->default_value(""), "output file prefix") 73 ("wordy,w", "verbose/wordy/debug"); 74 75 boost::program_options::positional_options_description positionals; 76 positionals.add("input", 1); 77 78 boost::program_options::variables_map parameters; 79 boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(positionals).run(), parameters); 80 boost::program_options::notify(parameters); 81 82 if (parameters.find("help") != parameters.end()) { 83 std::cout << desc << std::endl; 84 return 1; 85 } 86 87 boost::filesystem::path output_dir(parameters["csv"].as<std::string>()); 88 if (!output_dir.empty()) { 89 if (!boost::filesystem::is_directory(output_dir)) { 90 boost::filesystem::create_directories(output_dir); 91 } 92 } 93 94 boost::filesystem::path vis_dir(parameters["vis"].as<std::string>()); 95 if (!vis_dir.empty()) { 96 if (!boost::filesystem::is_directory(vis_dir)) { 97 boost::filesystem::create_directories(vis_dir); 98 } 99 } 100 101 boost::filesystem::path input_dir(parameters["input"].as<std::string>()); 102 if (!boost::filesystem::is_directory(input_dir)) { 103 std::cout << "Image directory not found ..." << std::endl; 104 return 1; 105 } 106 107 std::string prefix = parameters["prefix"].as<std::string>(); 108 109 bool wordy = false; 110 if (parameters.find("wordy") != parameters.end()) { 111 wordy = true; 112 } 113 114 int superpixels = parameters["superpixels"].as<int>(); 115 int compactness = parameters["compactness"].as<int>(); 116 int iterations = parameters["iterations"].as<int>(); 117 int color_space_int = parameters["color-space"].as<int>(); 118 119 bool lab = false; 120 if (color_space_int > 0) { 121 lab = true; 122 } 123 124 std::multimap<std::string, boost::filesystem::path> images; 125 std::vector<std::string> extensions; 126 IOUtil::getImageExtensions(extensions); 127 IOUtil::readDirectory(input_dir, extensions, images); 128 129 float total = 0; 130 for(std::multimap<std::string, boost::filesystem::path>::iterator it = images.begin(); 131 it != images.end(); ++it) { 132 133 cv::Mat image = cv::imread(it->first); 134 cv::Mat labels; 135 136 int region_size = SuperpixelTools::computeRegionSizeFromSuperpixels(image, 137 superpixels); 138 139 boost::timer timer; 140 CCS_OpenCV::computeSuperpixels(image, region_size, 141 iterations, compactness, lab, labels); 142 float elapsed = timer.elapsed(); 143 total += elapsed; 144 145 int unconnected_components = SuperpixelTools::relabelConnectedSuperpixels(labels); 146 int merged_components = SuperpixelTools::enforceMinimumSuperpixelSizeUpTo(image, labels, unconnected_components); 147 merged_components += SuperpixelTools::enforceMinimumSuperpixelSizeUpTo(image, labels, unconnected_components); 148 149 if (wordy) { 150 std::cout << SuperpixelTools::countSuperpixels(labels) << " superpixels for " << it->first 151 << " (" << unconnected_components << " not connected; " 152 << merged_components << " merged; " 153 << elapsed <<")." << std::endl; 154 } 155 156 if (!output_dir.empty()) { 157 boost::filesystem::path csv_file(output_dir 158 / boost::filesystem::path(prefix + it->second.stem().string() + ".csv")); 159 IOUtil::writeMatCSV<int>(csv_file, labels); 160 } 161 162 if (!vis_dir.empty()) { 163 boost::filesystem::path contours_file(vis_dir 164 / boost::filesystem::path(prefix + it->second.stem().string() + ".png")); 165 cv::Mat image_contours; 166 Visualization::drawContours(image, labels, image_contours); 167 cv::imwrite(contours_file.string(), image_contours); 168 } 169 } 170 171 if (wordy) { 172 std::cout << "Average time: " << total / images.size() << "." << std::endl; 173 } 174 175 if (!output_dir.empty()) { 176 std::ofstream runtime_file(output_dir.string() + "/" + prefix + "runtime.txt", 177 std::ofstream::out | std::ofstream::app); 178 179 runtime_file << total / images.size() << "\n"; 180 runtime_file.close(); 181 } 182 183 return 0; 184} 185
c_cpp_properties.json
1{ 2 "configurations": [ 3 { 4 "name": "Win32", 5 "includePath": [ 6 "${workspaceFolder}/**", 7 "C:\\opencv-4.7.0\\include", 8 "C:\\boost_1_81_0" 9 ], 10 "defines": [ 11 "_DEBUG", 12 "UNICODE", 13 "_UNICODE" 14 ], 15 "compilerPath": "C:\\MinGW\\bin\\gcc.exe", 16 "cStandard": "gnu17", 17 "cppStandard": "gnu++14", 18 "intelliSenseMode": "windows-gcc-x86" 19 } 20 ], 21 "version": 4 22} 23 24
試したこと
c_cpp_properties.jsonに「"C:\opencv\build\include","C:\boost_1_81_0"」
を追加し、#include <boost/〇〇.hpp>の同様のエラーはなくなったのですが、#include <opencv2/opencv.hpp>に対するエラーが消えません。
opencv2の中にopencv.hppがあることは確認できています。
補足情報(FW/ツールのバージョンなど)
VS Code, opencv-4.7.0, boost_1_81_0
