forked from wangdl2005/Resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResizeView.cpp
438 lines (400 loc) · 11 KB
/
ResizeView.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// ResizeView.cpp : implementation of the CResizeView class
//
#include "stdafx.h"
#include "Resize.h"
#include "ResizeDoc.h"
#include "ResizeView.h"
#include <afxwin.h>
#include "InputDlg.h"
#include <cxcore.h>
#include "SeamCarveResize.h"
#include <time.h>
#include <mmsystem.h>
//tiemGetTime()调用
#pragma comment(lib,"winmm.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CResizeView
IMPLEMENT_DYNCREATE(CResizeView, CView)
BEGIN_MESSAGE_MAP(CResizeView, CView)
//{{AFX_MSG_MAP(CResizeView)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_PIC_RESIZE_INSIDE, OnPicResizeInside)
ON_COMMAND(ID_PIC_SEAMCARVE, OnPicSeamcarve)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CResizeView construction/destruction
CResizeView::CResizeView()
{
// TODO: add construction code here
proc = 0;
procNew = 0;
}
CResizeView::~CResizeView()
{
delete proc;
delete procNew;
}
BOOL CResizeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CResizeView drawing
void CResizeView::OnDraw(CDC* pDC)
{
CResizeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CResizeView printing
BOOL CResizeView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CResizeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CResizeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CResizeView diagnostics
#ifdef _DEBUG
void CResizeView::AssertValid() const
{
CView::AssertValid();
}
void CResizeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CResizeDoc* CResizeView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CResizeDoc)));
return (CResizeDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CResizeView message handlers
void CResizeView::ShowOnDc(const IplImage*img,int idx /* = 0 */,int idy /* =0 */,const char * title /* = */ )
{
CClientDC dc(this);
OnPrepareDC(&dc);
CImage image;
//image.Load(path);
image.CopyOf((IplImage *)img);
//HDC hdc = ::GetDC(this->m_hWnd);
//image.Draw(hdc, 300, 200);
RECT * tmpRect = new RECT();
tmpRect->right = image.Width() + idx;
tmpRect->left = 0 + idx;
tmpRect->top = 0 + idy;
tmpRect->bottom = image.Height() + idy;
image.DrawToHDC(dc,tmpRect);
dc.TextOut(idx,image.Height() + idy,title);
//UpdateData(FALSE);
OnDraw(&dc);
}
void CResizeView::OpenPic()
{
CFileDialog dlg(TRUE,_T("*.bmp"),"",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY
,"image files(*.bmp;*.jpg;*.png;*.gif)|*.bmp;*.jpg;*.png;*.gif|ALLFiles(*.*)||"
,NULL);
char title[] =
{
"Open Image"
};
dlg.m_ofn.lpstrTitle = title;
if(IDOK == dlg.DoModal())
{
CString path = dlg.GetPathName();
if(proc != 0)
{
delete proc;
}
proc = new ImageProcessor (path,false);
CClientDC dc(this);
Invalidate(TRUE);
UpdateWindow();
ShowOnDc(proc->getImg());
}
}
void CResizeView::PicResizeInside()
{
if(proc != 0)
{
CInputDlg dlg;
//不能使用char * h_origin (=null);
char h_origin[5];
char wid_origin[5];
//int height = proc->getImg()->height;
//int width = proc->getImg()->width;
int h_to = 0;
int wid_to = 0;
itoa(proc->getImg()->height,h_origin,10);
itoa(proc->getImg()->width,wid_origin,10);
//dlg.m_h_origin = *proc->getImg()->height;
//dlg.m_wid_origin = *proc->getImg()->width;
dlg.m_h_origin = (CString)h_origin;
dlg.m_wid_origin = (CString)wid_origin;
if(IDOK == dlg.DoModal())
{
h_to = dlg.m_h_to;
wid_to = dlg.m_wid_to;
if(procNew != 0)
{
delete procNew;
}
IplImage* desc = cvCreateImage(cvSize(wid_to,h_to),proc->getImg()->depth,proc->getImg()->nChannels);
time_t tt_start;
//ctime(&tt_start);//放弃,s级不准确
//返回当前系统已运行时间ms级
tt_start = (time_t) timeGetTime();
cvResize(proc->getImg(),desc,CV_INTER_CUBIC);
/*
插值方法:
CV_INTER_NN - 最近邻插值,
CV_INTER_LINEAR - 双线性插值 (缺省使用)
CV_INTER_AREA - 使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。当图像放大时,类似于 CV_INTER_NN 方法..
CV_INTER_CUBIC - 立方插值.
*/
time_t tt_end;
//ctime(&tt_end);
tt_end = (time_t) timeGetTime();
procNew = new ImageProcessor(desc,"转换后的图片(内置方法)",false);
ShowOnDc(procNew->getImg(),proc->getImg()->width + 10, 0,"转换后的图片(内置方法)");
char tempString[20];
//char *tempString = new char(20);
sprintf(tempString,"花费时间为%d毫秒",(tt_end-tt_start));
cvReleaseImage(&desc);
}
}
else
{
MessageBox("请先打开图片");
}
}
void CResizeView::PicResizeSeamCarve()
{
if(proc != 0)
{
CInputDlg dlg;
//不能使用char * h_origin (=null);
char h_origin[5];
char wid_origin[5];
//int height = proc->getImg()->height;
//int width = proc->getImg()->width;
int h_to = 0;
int wid_to = 0;
int h_from = 0;
int wid_from = 0;
h_from = proc->getImg()->height;
wid_from = proc->getImg()->width;
itoa(h_from,h_origin,10);
itoa(wid_from,wid_origin,10);
//dlg.m_h_origin = *proc->getImg()->height;
//dlg.m_wid_origin = *proc->getImg()->width;
dlg.m_h_origin = (CString)h_origin;
dlg.m_wid_origin = (CString)wid_origin;
if(IDOK == dlg.DoModal())
{
h_to = dlg.m_h_to;
wid_to = dlg.m_wid_to;
if(procNew!=0)
{
delete procNew;
}
IplImage *img = NULL;
IplImage *mask = NULL;
IplImage *mask2 = NULL;
IplImage *gra = NULL;
IplImage *dis = NULL;
int wid_toSet = 0;
int h_toSet = 0;
int depth_toSet = 0;
int nChan_toSet = 0;
wid_toSet = wid_from < wid_to ? wid_to : proc->getImg()->width;
h_toSet = h_from < h_to ? h_to : proc->getImg()->height;
depth_toSet = proc->getImg()->depth;
nChan_toSet = proc->getImg()->nChannels;
//预设图片空间,防止算法中重复申请空间
img = cvCreateImage(cvSize(wid_toSet,h_toSet),depth_toSet,nChan_toSet);
mask = cvCreateImage(cvSize(wid_toSet,h_toSet),depth_toSet,nChan_toSet);
mask2 = cvCreateImage(cvSize(wid_toSet,h_toSet),depth_toSet,nChan_toSet);
gra = cvCreateImage(cvSize(wid_toSet,h_toSet),depth_toSet,nChan_toSet);
dis = cvCreateImage(cvSize(wid_toSet,h_toSet),depth_toSet,nChan_toSet);
cvZero(img);
for(int i=0;i<wid_from;i++)
for(int j=0;j<h_from;j++)
{
getPointImg8U3C(img,i,j,0)=getPointImg8U3C(proc->getImg(),i,j,0);
getPointImg8U3C(img,i,j,1)=getPointImg8U3C(proc->getImg(),i,j,1);
getPointImg8U3C(img,i,j,2)=getPointImg8U3C(proc->getImg(),i,j,2);
}
cvZero(mask);
cvZero(mask2);
cvZero(gra);
cvZero(dis);
time_t tt_start;
//ctime(&tt_start);//放弃,s级不准确
//返回当前系统已运行时间ms级
SeamCarve::neoBrightGradient(img,gra);
tt_start = (time_t) timeGetTime();
while(h_from!=h_to)
{
if(h_from>h_to)
{
SeamCarve::nSeamCarving(img,mask,mask2,gra,dis);
h_from = img->height ;
}
else if(h_from<h_to)
{
SeamCarve::nSeamCarvingLarge(img,mask,mask2,gra,dis,h_from,wid_from);
h_from ++;
}
}
SeamCarve::neoBrightGradient(img,gra);
//proc = new ImageProcessor(img,"hello",true);
while(wid_from!=wid_to)
{
if(wid_from>wid_to)
{
SeamCarve::nSeamCarvingVertical(img,mask,mask2,gra,dis);
wid_from = img->width;
}
else if(wid_from<wid_to)
{
SeamCarve::nSeamCarvingLargeVertical(img,mask,mask2,gra,dis,h_from,wid_from);
wid_from ++;
}
}
time_t tt_end;
//ctime(&tt_end);
tt_end = (time_t) timeGetTime();
procNew = new ImageProcessor(img,"转换后的图片(SeamCarv)",false);
ShowOnDc(procNew->getImg(),0, proc->getImg()->height + 30,"转换后的图片(SeamCarv)");
char tempString[20];
//char *tempString = new char(20);
sprintf(tempString,"花费时间为%d毫秒",(tt_end-tt_start));
MessageBox(tempString);
cvReleaseImage(&img);
cvReleaseImage(&mask);
cvReleaseImage(&mask2);
cvReleaseImage(&dis);
cvReleaseImage(&gra);
}
}
else
{
MessageBox("请先打开图片");
}
}
void CResizeView::SavePic()
{
if(procNew != 0)
{
CFileDialog dlgSave(FALSE,_T("*.bmp"),"",OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY
,"image files(*.bmp;*.jpg;*.png;*.gif)|*.bmp;*.jpg;*.png;*.gif|ALLFiles(*.*)||"
,NULL);
char title[] =
{
"Save Image"
};
dlgSave.m_ofn.lpstrTitle = title;
if(IDOK == dlgSave.DoModal())
{
CString path = dlgSave.GetPathName();
cvSaveImage(path,procNew->getImg());
}
}
else
{
MessageBox("请先处理图片");
}
}
/*
void CResizeView::cairResize(const IplImage* src,const IplImage* dst,int newWidth, int newHeight)
{
int width = src->width;
int height = src->height;
//cvCML
//CAIR( &source, &source_weights, newWidth, newHeight, conv, ener, &dest_weights, &dest, updateCallback );
}
void CResizeView::IMG_to_CML( IplImage * Source, CML_color * Dest )
{
(*Dest).D_Resize( Source->width, Source->height );
for( int y = 0; y < Source->width; y++ )
{
for( int x = 0; x < Source->height; x++ )
{
//间接访问,效率较低
CvScalar p;//B,G,R
p = cvGet2D(Source,x,y);//
(*Dest)(x,y).alpha = 0;//idx0 : height;idx1 : width
(*Dest)(x,y).red = (unsigned char)p.val[2];
(*Dest)(x,y).green = (unsigned char)p.val[1];
(*Dest)(x,y).blue = (unsigned char)p.val[0];
}
}
}
//=========================================================================================================//
void CResizeView::CML_to_IMG( CML_color * Source, IplImage * Dest )
{
Dest = cvCreateImage(cvSize(Source->Width(),Source->Height()),IPL_DEPTH_16U,3);
for( int y = 0; y < Source->Height(); y++ )
{
for( int x = 0; x < Source->Width(); x++ )
{
// = (*Source)(x,y).alpha;
CvScalar p;//B,G,R
p.val[0] = (*Source)(x,y).red;
p.val[1] = (*Source)(x,y).green;
p.val[2] = (*Source)(x,y).blue;
cvSet2D(Dest,y,x,p);
}
}
}
*/
void CResizeView::OnFileOpen()
{
// TODO: Add your command handler code here
OpenPic();
}
void CResizeView::OnPicResizeInside()
{
// TODO: Add your command handler code here
PicResizeInside();
}
void CResizeView::OnFileSaveAs()
{
// TODO: Add your command handler code here
SavePic();
}
void CResizeView::OnPicSeamcarve()
{
// TODO: Add your command handler code here
PicResizeSeamCarve();
}
void CResizeView::OnFileSave()
{
// TODO: Add your command handler code here
SavePic();
}