-
Notifications
You must be signed in to change notification settings - Fork 0
/
ud_imgchoose.cpp
344 lines (267 loc) · 10.3 KB
/
ud_imgchoose.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// ud_imgchoose.cpp: 实现文件
//
#include "pch.h"
#include "UnSuperviseDeploy.h"
#include "ud_imgchoose.h"
#include "afxdialogex.h"
#include <iostream>
#include <filesystem>
namespace fs = std::experimental::filesystem; // C++11的文件系统命名空间
// CImgChoose 对话框
IMPLEMENT_DYNAMIC(CImgChoose, CDialogEx)
BEGIN_MESSAGE_MAP(CImgChoose, CDialogEx)
ON_EN_CHANGE(IDC_EDITOPENFOLD, &CImgChoose::OnEnChangeEditopenfold)
ON_EN_CHANGE(IDC_EDITSAVEGOOD, &CImgChoose::OnEnChangeEditsavegood)
ON_EN_CHANGE(IDC_EDITSAVEBAD, &CImgChoose::OnEnChangeEditsavebad)
//ON_BN_CLICKED(IDC_OPENFILEBUTTON, &CImgChoose::OnBnClickedOpenfilebutton)
ON_STN_CLICKED(IDC_DstImg, &CImgChoose::OnStnClickedDstimg)
//ON_BN_CLICKED(IDC_PREVIOUSBUTTON, &CImgChoose::OnBnClickedPreviousbutton)
//ON_BN_CLICKED(IDC_NEXTBUTTON, &CImgChoose::OnBnClickedNextbutton)
//ON_BN_CLICKED(IDC_GOODBUTTON, &CImgChoose::OnBnClickedGoodbutton)
//ON_BN_CLICKED(IDC_BADBUTTON, &CImgChoose::OnBnClickedBadbutton)
ON_EN_CHANGE(IDC_EDITGOODNUM, &CImgChoose::OnEnChangeEditgoodnum)
ON_EN_CHANGE(IDC_EDITBADNUM, &CImgChoose::OnEnChangeEditbadnum)
ON_EN_CHANGE(IDC_EDITALLNUM, &CImgChoose::OnEnChangeEditallnum)
ON_EN_CHANGE(IDC_EDITFOLDNUM, &CImgChoose::OnEnChangeEditfoldnum)
ON_BN_CLICKED(IDC_MFCBUTTON1, &CImgChoose::OnBnClickedMfcbutton1)
//ON_BN_CLICKED(IDC_MFCBUTTON3, &CImgChoose::OnBnClickedMfcbutton3)
//ON_BN_CLICKED(IDC_MFCBUTTON4, &CImgChoose::OnBnClickedMfcbutton4)
ON_BN_CLICKED(IDC_MFCBUTTON7, &CImgChoose::OnBnClickedMfcbutton7)
ON_BN_CLICKED(IDC_MFCBUTTON6, &CImgChoose::OnBnClickedMfcbutton6)
ON_BN_CLICKED(IDC_BUTTON1, &CImgChoose::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CImgChoose::OnBnClickedButton2)
END_MESSAGE_MAP()
CImgChoose::CImgChoose(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_DIALOG1, pParent)
{
//m_editOpenFold.SubclassDlgItem(IDC_EDITOPENFOLD, this);
//m_editOpenFold.SetWindowText(strOpenFoldPth);
//m_editSaveGood.SubclassDlgItem(IDC_EDITSAVEGOOD, this);
//m_editSaveGood.SetWindowText(strSaveGoodPth);
//m_editSaveBad.SubclassDlgItem(IDC_EDITSAVEBAD, this);
//m_editSaveBad.SetWindowText(strSaveBadPth);
m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDC_BUTTON2));
}
CImgChoose::~CImgChoose()
{
}
void CImgChoose::DoDataExchange(CDataExchange* pDX)
{
//初始化函数
CDialogEx::DoDataExchange(pDX);
m_editOpenFold.SubclassDlgItem(IDC_EDITOPENFOLD, this);
m_editOpenFold.SetWindowText(strOpenFoldPth);
m_editFoldNum.SubclassDlgItem(IDC_EDITFOLDNUM, this);
m_editFoldNum.SetWindowText(to_string(0).c_str());
m_editSaveGood.SubclassDlgItem(IDC_EDITSAVEGOOD, this);
m_editSaveGood.SetWindowText(strSaveGoodPth);
m_editSaveBad.SubclassDlgItem(IDC_EDITSAVEBAD, this);
m_editSaveBad.SetWindowText(strSaveBadPth);
m_editGoodNUm.SubclassDlgItem(IDC_EDITGOODNUM, this);
m_editGoodNUm.SetWindowText(to_string(0).c_str());
m_editBadNum.SubclassDlgItem(IDC_EDITBADNUM, this);
m_editBadNum.SetWindowText(to_string(0).c_str());
m_editAllNum.SubclassDlgItem(IDC_EDITALLNUM, this);
m_editAllNum.SetWindowText(to_string(0).c_str());
m_btnPrevious.SubclassDlgItem(IDC_BUTTON1, this);
m_btnNext.SubclassDlgItem(IDC_BUTTON2, this);
nSaveImgNum = 0;
//上一张 可选/不选
if (nImgIndex == 0)
{
m_btnPrevious.EnableWindow(0);
}
}
// CImgChoose 消息处理程序
void CImgChoose::OnEnChangeEditopenfold()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CDialogEx::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
void CImgChoose::OnEnChangeEditsavegood()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CDialogEx::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
void CImgChoose::OnEnChangeEditsavebad()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CDialogEx::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
void CImgChoose::OnBnClickedOpenfilebutton()
{
std::string folderPath = strOpenFoldPth.GetBuffer();
if (isDirectoryEmpty(folderPath))
{
AfxMessageBox("文件夹为空,请确保抠图完成");
}
else
{
CImage cImage;
CWnd* pOpenWnd = NULL;
pOpenWnd = GetDlgItem(IDC_DstImg);//获取控件句柄
cv::String pattern = folderPath; // 定义图像文件的模式(这里假设图像为.jpg格式)
//std::vector<cv::String> imageFiles;
for (const auto &i : stdfs::directory_iterator(stdfs::path(pattern))) {
imageFiles.push_back(i.path().generic_string());
}
m_editFoldNum.SetWindowText(to_string(imageFiles.size()).c_str());
dlgImage = cv::imread(imageFiles[0]); // 读取第一张图像
m_pcTools->MatToCImage(dlgImage, cImage);
m_pcTools->ShowImg(cImage, pOpenWnd);
nImgIndex = 0;
}
}
//判断文件夹是否为空
bool CImgChoose::isDirectoryEmpty(const std::string& folderPath)
{
if (!fs::is_directory(folderPath))
{
std::cout << "指定路径不是文件夹" << std::endl;
return false;
}
fs::directory_iterator it(folderPath);
return it == fs::directory_iterator(); // 如果迭代器等于结束迭代器,则文件夹为空
}
void CImgChoose::OnStnClickedDstimg()
{
// TODO: 在此添加控件通知处理程序代码
}
void CImgChoose::OnEnChangeEditgoodnum()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CDialogEx::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
void CImgChoose::OnEnChangeEditbadnum()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CDialogEx::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
void CImgChoose::OnEnChangeEditallnum()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CDialogEx::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
void CImgChoose::OnEnChangeEditfoldnum()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CDialogEx::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
}
void CImgChoose::OnBnClickedMfcbutton1()
{
std::string folderPath = strOpenFoldPth.GetBuffer();
if (isDirectoryEmpty(folderPath))
{
AfxMessageBox("文件夹为空,请确保抠图完成");
}
else
{
CImage cImage;
CWnd* pOpenWnd = NULL;
pOpenWnd = GetDlgItem(IDC_DstImg);//获取控件句柄
cv::String pattern = folderPath; // 定义图像文件的模式(这里假设图像为.jpg格式)
//std::vector<cv::String> imageFiles;
for (const auto& i : stdfs::directory_iterator(stdfs::path(pattern))) {
imageFiles.push_back(i.path().generic_string());
}
m_editFoldNum.SetWindowText(to_string(imageFiles.size()).c_str());
dlgImage = cv::imread(imageFiles[0]); // 读取第一张图像
m_pcTools->MatToCImage(dlgImage, cImage);
m_pcTools->ShowImg(cImage, pOpenWnd);
nImgIndex = 0;
}
}
void CImgChoose::OnBnClickedMfcbutton3()
{
nImgIndex--;
CWnd* pPreviousWnd = NULL;
pPreviousWnd = GetDlgItem(IDC_DstImg);//获取控件句柄
CImage cImage;
dlgImage = cv::imread(imageFiles[nImgIndex]); // 读取第一张图像
m_pcTools->MatToCImage(dlgImage, cImage);
m_pcTools->ShowImg(cImage, pPreviousWnd);
if (nImgIndex == 0)
{
m_btnPrevious.EnableWindow(0);
}
}
void CImgChoose::OnBnClickedMfcbutton4()
{
nImgIndex++;
CWnd* pNextWnd = NULL;
pNextWnd = GetDlgItem(IDC_DstImg);//获取控件句柄
CImage cImage;
dlgImage = cv::imread(imageFiles[nImgIndex]); // 读取第一张图像
m_pcTools->MatToCImage(dlgImage, cImage);
m_pcTools->ShowImg(cImage, pNextWnd);
if (nImgIndex != 0) m_btnPrevious.EnableWindow(1);
else if (nImgIndex == imageFiles.size() - 1) m_btnNext.EnableWindow(0);
}
void CImgChoose::OnBnClickedMfcbutton7()
{
static int nGoodIndex = 0;
std::string ImgPath = strSaveGoodPth.GetBuffer();
if (!dlgImage.empty()) cv::imwrite(ImgPath + to_string(nGoodIndex) + ".jpg", dlgImage);
nGoodIndex++;
nSaveImgNum++;
m_editGoodNUm.SetWindowText(to_string(nGoodIndex).c_str());
m_editAllNum.SetWindowText(to_string(nSaveImgNum).c_str());
}
void CImgChoose::OnBnClickedMfcbutton6()
{
static int nBadIndex = 0;
std::string ImgPath = strSaveBadPth.GetBuffer();
if (!dlgImage.empty()) cv::imwrite(ImgPath + to_string(nBadIndex) + ".jpg", dlgImage);
nBadIndex++;
nSaveImgNum++;
m_editBadNum.SetWindowText(to_string(nBadIndex).c_str());
m_editAllNum.SetWindowText(to_string(nSaveImgNum).c_str());
}
void CImgChoose::OnBnClickedButton1()
{
nImgIndex--;
CWnd* pPreviousWnd = NULL;
pPreviousWnd = GetDlgItem(IDC_DstImg);//获取控件句柄
CImage cImage;
dlgImage = cv::imread(imageFiles[nImgIndex]); // 读取第一张图像
m_pcTools->MatToCImage(dlgImage, cImage);
m_pcTools->ShowImg(cImage, pPreviousWnd);
if (nImgIndex == 0)
{
m_btnPrevious.EnableWindow(0);
}
}
void CImgChoose::OnBnClickedButton2()
{
nImgIndex++;
CWnd* pNextWnd = NULL;
pNextWnd = GetDlgItem(IDC_DstImg);//获取控件句柄
CImage cImage;
dlgImage = cv::imread(imageFiles[nImgIndex]); // 读取第一张图像
m_pcTools->MatToCImage(dlgImage, cImage);
m_pcTools->ShowImg(cImage, pNextWnd);
if (nImgIndex != 0) m_btnPrevious.EnableWindow(1);
else if (nImgIndex == imageFiles.size() - 1) m_btnNext.EnableWindow(0);
}