Add file read

添加了图片读取函数
This commit is contained in:
ZhenyeLi 2024-11-05 14:17:24 +08:00
parent 8e7a425222
commit 44c38dbbd2
2 changed files with 58 additions and 4 deletions

View File

@ -2,6 +2,8 @@
#include <iostream>
#include <map>
#include <string>
#include <windows.h>
#include <commdlg.h> // 包含文件对话框相关的函数
using namespace cv;
@ -52,12 +54,64 @@ void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map<st
}
string openFileDialog() {
// 初始化文件选择对话框
OPENFILENAME ofn; // 文件对话框结构
wchar_t szFile[260]; // 存储选择的文件路径
// 设置 OPENFILENAME 结构的默认值
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile) / sizeof(szFile[0]);
ofn.lpstrFilter = L"Image Files\0*.BMP;*.JPG;*.JPEG;*.PNG;*.GIF\0All Files\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = L"Select an image file";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// 打开文件选择对话框
if (GetOpenFileName(&ofn) == TRUE) {
// 将 wchar_t 转换为 string
wstring ws(szFile);
string filePath(ws.begin(), ws.end());
return filePath;
}
return ""; // 如果用户取消,返回空字符串
}
Mat readImage() {
// 读取输入图像
string imagePath = openFileDialog();
if (imagePath.empty()) {
cout << "No file selected or user cancelled." << endl;
return Mat();
}
// 使用 OpenCV 读取选中的图片
Mat image = imread(imagePath);
if (image.empty()) {
cout << "Error: Could not load image." << endl;
return Mat();
}
return image;
}
int main() {
// 读取输入图像
Mat inputImage = imread("C:\\Program Files\\Matrox Imaging\\Images\\test.bmp");
Mat inputImage = readImage();
if (inputImage.empty()) {
cout << "Error: Could not load image!" << endl;
cout << "Error: Could not load image." << endl;
return -1;
}
@ -78,4 +132,4 @@ int main() {
// 等待用户按键
waitKey(0);
return 0;
}
}

View File

@ -126,7 +126,7 @@
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\Users\ZLSDKJ\source\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>opencv_world4100.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Comdlg32.lib;opencv_world4100.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>