【解決方法】opencv ライブラリを使用した C++ CLR winform (.NET フレームワーク) での画像強化

プログラミングQA


CLR winforms(.net Framework) C++ プロジェクトを作成しました。SelectImageBtn と EnhanceBn の 2 つのボタンがあります。
SelectImageBtnをクリックしてぼかし画像が保存されているフォルダを選択し、すべてのぼかし画像をシャープにして強調し、強調画像の新しいフォルダを保存したい。
私は何かを試しましたが、エラーが発生し、天気がうまくいくかどうかわかりません!!

取得エラー: E0725 name must be namespace name, on 行:

using namespace filesystem;

およびエラー: E0276 名前の後に ‘::’ が続く行は、クラス名または名前空間名でなければなりません:

for (const auto& file : filesystem::directory_iterator(folderPath))

注:私はC++の初心者なので、C++についてあまり知りません
コードを見て、私は正しいか間違っているか? 提案をいただければ幸いです。よろしく!!!

私が試したこと:

#include <filesystem>
#include <exception>
#include <opencv2/opencv.hpp>
#include <msclr/marshal_cppstd.h>
#include <string>



namespace ImageEnhancer {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO;
	using namespace System::Drawing::Imaging;
	using namespace cv;
	using namespace std;
	using namespace filesystem;


	/// <summary>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
	FolderBrowserDialog^ dialog = gcnew FolderBrowserDialog;
	private: System::Windows::Forms::Button^ SelectImageBtn;
	private: System::Windows::Forms::Button^ EnhanceBn;
	protected:

	protected:

	protected:

	protected:


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container^ components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->SelectImageBtn = (gcnew System::Windows::Forms::Button());
			this->EnhanceBn = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// SelectImageBtn
			// 
			this->SelectImageBtn->Location = System::Drawing::Point(134, 152);
			this->SelectImageBtn->Name = L"SelectImageBtn";
			this->SelectImageBtn->Size = System::Drawing::Size(75, 45);
			this->SelectImageBtn->TabIndex = 0;
			this->SelectImageBtn->Text = L"Select";
			this->SelectImageBtn->UseVisualStyleBackColor = true;
			this->SelectImageBtn->Click += gcnew System::EventHandler(this, &MyForm::SelectImageBtn_Click);
			// 
			// EnhanceBn
			// 
			this->EnhanceBn->Location = System::Drawing::Point(277, 152);
			this->EnhanceBn->Name = L"EnhanceBn";
			this->EnhanceBn->Size = System::Drawing::Size(75, 45);
			this->EnhanceBn->TabIndex = 1;
			this->EnhanceBn->Text = L"Enhance";
			this->EnhanceBn->UseVisualStyleBackColor = true;
			this->EnhanceBn->Click += gcnew System::EventHandler(this, &MyForm::EnhanceBn_Click);
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(522, 393);
			this->Controls->Add(this->EnhanceBn);
			this->Controls->Add(this->SelectImageBtn);
			this->Name = L"MyForm";
			this->Text = L"ImageEnhancer";
			this->ResumeLayout(false);

		}
#pragma endregion

		// Function to enhance a single image
		void enhanceImage(Mat& inputImage, Mat& outputImage)
		{
			// Code to enhance image using OpenCV functions goes here
			// For example, sharpening the image:
			Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
			filter2D(inputImage, outputImage, -1, kernel);
		}

	private: System::Void SelectImageBtn_Click(System::Object^ sender, System::EventArgs^ e)
	{
		try
		{
			MessageBox::Show("clicked");
			// Create FolderBrowserDialog to select folder
			
			dialog->ShowDialog();

			// Store selected folder path
			string folderPath = msclr::interop::marshal_as<string>(dialog->SelectedPath);

		}
		catch (const std::exception& exx)
		{
			
		}
	}
	private: System::Void EnhanceBn_Click(System::Object^ sender, System::EventArgs^ e)
	{
		string folderPath = msclr::interop::marshal_as<string>(dialog->SelectedPath);
		
		// Loop through all files in the selected folder
		for (const auto& file : filesystem::directory_iterator(folderPath))
		{
			// Load image using OpenCV
			Mat inputImage = imread(file.path().string());

			// Create output image
			Mat outputImage;

			// Enhance image
			enhanceImage(inputImage, outputImage);

			// Save enhanced image to new folder
			string outputPath = "output/" + file.path().filename().string();
			imwrite(outputPath, outputImage);
		}

	}
		  

	};
}

解決策 1

さて、エラーが発生するための解決策を見つけました
私が使っていた

ISO C++14 Standard (/std:c++14)

バージョンアップの代わりに

ISO C++17 Standard (/std:c++17)

次の手順で問題を修正します

C++17 以降に切り替えるには、コンパイラ オプションを変更する必要があります。 Visual Studio で C++17 に切り替える手順は次のとおりです。

-> 解決策 エクスプローラーでプロジェクトを右クリックし、[プロパティ]を選択します。

->左側のメニューから「C/C++」を選択し、次に「言語」を選択します。

->「C++ 言語標準」ドロップダウンで、「ISO C++17 標準 (/std:c++17)」を選択します。

->「適用」をクリックしてから「OK」をクリックして、変更を保存します。

別のコンパイラを使用している場合は、そのコンパイラのドキュメントを参照して、C++17 に切り替える方法を確認する必要があります。

コメント

タイトルとURLをコピーしました