Repository navigation
Expand file tree
/
Copy pathdibview.cpp
More file actions
2369 lines (1966 loc) · 52.8 KB
/
Copy pathdibview.cpp
File metadata and controls
2369 lines (1966 loc) · 52.8 KB
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// dibview.cpp : implementation of the CDibView class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "diblook.h"
#include <afxdisp.h>
#include <algorithm>
#include <iostream>
#include "Tree.h"
#include "Knn.h"
using namespace std;
#include "dibdoc.h"
#include <fstream>
#include "dibview.h"
#include "dibapi.h"
#include "mainfrm.h"
#include <math.h>
#include <stack>
#include <list>
#include <vector>
#include "HRTimer.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#define BEGIN_PROCESSING() INCEPUT_PRELUCRARI()
#define END_PROCESSING(Title) SFARSIT_PRELUCRARI(Title)
#define INCEPUT_PRELUCRARI() \
CDibDoc* pDocSrc=GetDocument(); \
CDocTemplate* pDocTemplate=pDocSrc->GetDocTemplate(); \
CDibDoc* pDocDest=(CDibDoc*) pDocTemplate->CreateNewDocument(); \
BeginWaitCursor(); \
HDIB hBmpSrc=pDocSrc->GetHDIB(); \
HDIB hBmpDest = (HDIB)::CopyHandle((HGLOBAL)hBmpSrc); \
if ( hBmpDest==0 ) { \
pDocTemplate->RemoveDocument(pDocDest); \
return; \
} \
BYTE* lpD = (BYTE*)::GlobalLock((HGLOBAL)hBmpDest); \
BYTE* lpS = (BYTE*)::GlobalLock((HGLOBAL)hBmpSrc); \
int iColors = DIBNumColors((char *)&(((LPBITMAPINFO)lpD)->bmiHeader)); \
RGBQUAD *bmiColorsDst = ((LPBITMAPINFO)lpD)->bmiColors; \
RGBQUAD *bmiColorsSrc = ((LPBITMAPINFO)lpS)->bmiColors; \
BYTE * lpDst = (BYTE*)::FindDIBBits((LPSTR)lpD); \
BYTE * lpSrc = (BYTE*)::FindDIBBits((LPSTR)lpS); \
int dwWidth = ::DIBWidth((LPSTR)lpS);\
int dwHeight = ::DIBHeight((LPSTR)lpS);\
int w=WIDTHBYTES(dwWidth*((LPBITMAPINFOHEADER)lpS)->biBitCount); \
HRTimer my_timer; \
my_timer.StartTimer(); \
#define BEGIN_SOURCE_PROCESSING \
CDibDoc* pDocSrc=GetDocument(); \
BeginWaitCursor(); \
HDIB hBmpSrc=pDocSrc->GetHDIB(); \
BYTE* lpS = (BYTE*)::GlobalLock((HGLOBAL)hBmpSrc); \
int iColors = DIBNumColors((char *)&(((LPBITMAPINFO)lpS)->bmiHeader)); \
RGBQUAD *bmiColorsSrc = ((LPBITMAPINFO)lpS)->bmiColors; \
BYTE * lpSrc = (BYTE*)::FindDIBBits((LPSTR)lpS); \
int dwWidth = ::DIBWidth((LPSTR)lpS);\
int dwHeight = ::DIBHeight((LPSTR)lpS);\
int w=WIDTHBYTES(dwWidth*((LPBITMAPINFOHEADER)lpS)->biBitCount); \
#define END_SOURCE_PROCESSING \
::GlobalUnlock((HGLOBAL)hBmpSrc); \
EndWaitCursor(); \
/////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------
#define SFARSIT_PRELUCRARI(Titlu) \
double elapsed_time_ms = my_timer.StopTimer(); \
CString Title; \
Title.Format("%s - Proc. time = %.2f ms", Titlu, elapsed_time_ms); \
::GlobalUnlock((HGLOBAL)hBmpDest); \
::GlobalUnlock((HGLOBAL)hBmpSrc); \
EndWaitCursor(); \
pDocDest->SetHDIB(hBmpDest); \
pDocDest->InitDIBData(); \
pDocDest->SetTitle((LPCSTR)Title); \
CFrameWnd* pFrame=pDocTemplate->CreateNewFrame(pDocDest,NULL); \
pDocTemplate->InitialUpdateFrame(pFrame,pDocDest); \
/////////////////////////////////////////////////////////////////////////////
// CDibView
IMPLEMENT_DYNCREATE(CDibView, CScrollView)
BEGIN_MESSAGE_MAP(CDibView, CScrollView)
//{{AFX_MSG_MAP(CDibView)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
ON_COMMAND(ID_PROCESSING_PARCURGERESIMPLA, OnProcessingParcurgereSimpla)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
ON_COMMAND(ID_LAB1_RANSAC, &CDibView::OnLab1Ransac)
ON_COMMAND(ID_LAB1_RANSAC32776, &CDibView::OnLab1Ransac32776)
ON_COMMAND(ID_LAB2_RANSAC, &CDibView::OnLab2Ransac)
ON_COMMAND(ID_LAB3_HOUGH, &CDibView::OnLab3Hough)
ON_COMMAND(ID_LAB3_DRAWHOUGH, &CDibView::OnLab3Drawhough)
ON_COMMAND(ID_LAB4_DT, &CDibView::OnLab4Dt)
ON_COMMAND(ID_LAB4_PATTERNMATCHING, &CDibView::OnLab4Patternmatching)
ON_COMMAND(ID_LAB5_MAGNITUDEANDGRADIENT, &CDibView::OnLab5Magnitudeandgradient)
ON_COMMAND(ID_LAB5_INTERVALCOLORING, &CDibView::OnLab5Intervalcoloring)
ON_COMMAND(ID_LAB5_CELLDIVISION, &CDibView::OnLab5Celldivision)
ON_COMMAND(ID_LAB5_HOG, &CDibView::OnLab5Hog)
ON_COMMAND(ID_LAB5_DEGREEOFSIMILARITY, &CDibView::OnLab5Degreeofsimilarity)
ON_COMMAND(ID_LAB6_LOAD400, &CDibView::OnLab6Load400)
ON_COMMAND(ID_LAB6_CORELATIONCOEFFICIENTS, &CDibView::OnLab6Corelationcoefficients)
ON_COMMAND(ID_LAB6_CORE, &CDibView::OnLab6Core)
ON_COMMAND(ID_LAB7_ASS1, &CDibView::OnLab7Ass1)
ON_COMMAND(ID_LAB7_NORMALDISTR, &CDibView::OnLab7Normaldistr)
ON_COMMAND(ID_LAB7_HISTOGRAMPOINTS, &CDibView::OnLab7Histogrampoints)
ON_COMMAND(ID_LAB7_BIVARIATEDISTRIBUTION, &CDibView::OnLab7Bivariatedistribution)
ON_COMMAND(ID_LAB5_LOAD2, &CDibView::OnLab5Load2)
ON_COMMAND(ID_LAB7_LOADTRAINNONFACE, &CDibView::OnLab7Loadtrainnonface)
ON_COMMAND(ID_LAB9_PERCEPTRON, &CDibView::OnLab9Perceptron)
ON_COMMAND(ID_LAB10_LDA, &CDibView::OnLab10Lda)
ON_COMMAND(ID_LAB11_ADABOOST, &CDibView::OnLab11Adaboost)
ON_COMMAND(ID_PRS_PROIECT, &CDibView::OnPrsProiect)
ON_COMMAND(ID_PRS_PROIECT2, &CDibView::OnPrsProiect2)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDibView construction/destruction
CDibView::CDibView()
{
}
CDibView::~CDibView()
{
}
/////////////////////////////////////////////////////////////////////////////
// CDibView drawing
void CDibView::OnDraw(CDC* pDC)
{
CDibDoc* pDoc = GetDocument();
HDIB hDIB = pDoc->GetHDIB();
if (hDIB != NULL)
{
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
::GlobalUnlock((HGLOBAL) hDIB);
CRect rcDIB;
rcDIB.top = rcDIB.left = 0;
rcDIB.right = cxDIB;
rcDIB.bottom = cyDIB;
CRect rcDest;
if (pDC->IsPrinting()) // printer DC
{
// get size of printer page (in pixels)
int cxPage = pDC->GetDeviceCaps(HORZRES);
int cyPage = pDC->GetDeviceCaps(VERTRES);
// get printer pixels per inch
int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
//
// Best Fit case -- create a rectangle which preserves
// the DIB's aspect ratio, and fills the page horizontally.
//
// The formula in the "->bottom" field below calculates the Y
// position of the printed bitmap, based on the size of the
// bitmap, the width of the page, and the relative size of
// a printed pixel (cyInch / cxInch).
//
rcDest.top = rcDest.left = 0;
rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
/ ((double)cxDIB * cxInch));
rcDest.right = cxPage;
}
else // not printer DC
{
rcDest = rcDIB;
}
::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
&rcDIB, pDoc->GetDocPalette());
}
}
/////////////////////////////////////////////////////////////////////////////
// CDibView printing
BOOL CDibView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CDibView commands
LRESULT CDibView::OnDoRealize(WPARAM wParam, LPARAM)
{
ASSERT(wParam != NULL);
CDibDoc* pDoc = GetDocument();
if (pDoc->GetHDIB() == NULL)
return 0L; // must be a new document
CPalette* pPal = pDoc->GetDocPalette();
if (pPal != NULL)
{
CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
ASSERT_KINDOF(CMainFrame, pAppFrame);
CClientDC appDC(pAppFrame);
// All views but one should be a background palette.
// wParam contains a handle to the active view, so the SelectPalette
// bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
if (oldPalette != NULL)
{
UINT nColorsChanged = appDC.RealizePalette();
if (nColorsChanged > 0)
pDoc->UpdateAllViews(NULL);
appDC.SelectPalette(oldPalette, TRUE);
}
else
{
TRACE0("\tSelectPalette failed in CDibView::OnPaletteChanged\n");
}
}
return 0L;
}
void CDibView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
ASSERT(GetDocument() != NULL);
SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
}
void CDibView::OnActivateView(BOOL bActivate, CView* pActivateView,
CView* pDeactiveView)
{
CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
if (bActivate)
{
ASSERT(pActivateView == this);
OnDoRealize((WPARAM)m_hWnd, 0); // same as SendMessage(WM_DOREALIZE);
}
}
void CDibView::OnEditCopy()
{
CDibDoc* pDoc = GetDocument();
// Clean clipboard of contents, and copy the DIB.
if (OpenClipboard())
{
BeginWaitCursor();
EmptyClipboard();
SetClipboardData (CF_DIB, CopyHandle((HANDLE) pDoc->GetHDIB()) );
CloseClipboard();
EndWaitCursor();
}
}
void CDibView::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
}
void CDibView::OnEditPaste()
{
HDIB hNewDIB = NULL;
if (OpenClipboard())
{
BeginWaitCursor();
hNewDIB = (HDIB) CopyHandle(::GetClipboardData(CF_DIB));
CloseClipboard();
if (hNewDIB != NULL)
{
CDibDoc* pDoc = GetDocument();
pDoc->ReplaceHDIB(hNewDIB); // and free the old DIB
pDoc->InitDIBData(); // set up new size & palette
pDoc->SetModifiedFlag(TRUE);
SetScrollSizes(MM_TEXT, pDoc->GetDocSize());
OnDoRealize((WPARAM)m_hWnd,0); // realize the new palette
pDoc->UpdateAllViews(NULL);
}
EndWaitCursor();
}
}
void CDibView::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));
}
void CDibView::OnProcessingParcurgereSimpla()
{
// TODO: Add your command handler code here
BEGIN_PROCESSING();
// Makes a grayscale image by equalizing the R, G, B components from the LUT
for (int k=0; k < iColors ; k++)
bmiColorsDst[k].rgbRed=bmiColorsDst[k].rgbGreen=bmiColorsDst[k].rgbBlue=k;
// Goes through the bitmap pixels and performs their negative
for (int i=0;i<dwHeight;i++)
for (int j=0;j<dwWidth;j++)
{
lpDst[i*w+j]= 255 - lpSrc[i*w+j]; //makes image negative
}
END_PROCESSING("Negativ imagine");
}
void CDibView::OnLab1Ransac()
{
}
typedef struct Point
{
int X;
int Y;
};
double ComputeDistance(double a, double b, double c, Point p)
{
double up = abs(a * p.X + b * p.Y + c);
double down = sqrt(a*a + b * b);
return up / down;
}
struct StructureLine{
float a;
float b;
float c;
};
Point Points[999];
Point resultP1;
Point resultP2;
int initPoints(BYTE* lpSrc, int dwHeight,int dwWidth,int w){
int index = 0;
for (int i=0;i<dwHeight;i++)
for (int j=0;j<dwWidth;j++)
{
if(lpSrc[i*w+j]== 0)
{
Points[index].X = j;
Points[index].Y = i;
index++;
}
}
return index;
}
void RANSAC(int N, int counter,int t, int T, int maxInliers){
int rand1;
int rand2;
for(int i=0; i<N; i++)
{
rand1 = rand() % counter;
rand2 = rand() % counter;
while(rand1 == rand2)
{
rand1 = rand() % counter;
rand2 = rand() % counter;
}
double a = Points[rand1].Y - Points[rand2].Y;
double b = Points[rand2].X - Points[rand1].X;
double c = Points[rand1].X * Points[rand2].Y - Points[rand2].X * Points[rand1].Y;
int nrInliners = 0;
for (int k =0; k < counter; k++)
{
if ( ComputeDistance(a,b,c, Points[k]) < t)
nrInliners++;
}
if(nrInliners >= T)
{
resultP1 = Points[rand1];
resultP2 = Points[rand2];
break;
}
else
{
if(nrInliners > maxInliers)
{
maxInliers = nrInliners;
resultP1 = Points[rand1];
resultP2 = Points[rand2];
}
}
}
}
Point centerResult;
double radiusResult;
void RANSACCircle(int N, int counter,int t, int T){
int maxInliers = 0;
int rand1=0;
int rand2=0;
int rand3=0;
double ma=0,mb=0;
Point center;
double radius=0;
for(int i=0; i < N; i++)
{
do{
rand1 = rand() % counter;
rand2 = rand() % counter;
rand3 = rand() % counter;
ma = ((double)(Points[rand2].Y - Points[rand1].Y)) / ((double)(Points[rand2].X - Points[rand1].X));
mb = ((double)(Points[rand3].Y - Points[rand2].Y)) / ((double)(Points[rand3].X - Points[rand2].X));
}
while (rand1 == rand2 || rand2 == rand3 || rand1 == rand3
|| Points[rand1].X == Points[rand2].X
|| Points[rand2].X == Points[rand3].X
|| ma == 0
|| mb == 0
|| ma == mb);
center.X = (ma*mb*(Points[rand1].Y - Points[rand3].Y) + mb*(Points[rand1].X + Points[rand2].X) - ma*(Points[rand2].X + Points[rand3].X)) / (float) (2*(mb-ma));
center.Y = (-1/(float)ma) * (center.X - (Points[rand1].X + Points[rand2].X)/(float)2) + (Points[rand1].Y + Points[rand2].Y)/(float)2;
radius = sqrt ((double) (center.X - Points[rand1].X)*(center.X - Points[rand1].X) + (center.Y - Points[rand1].Y)*(center.Y - Points[rand1].Y) );
int nrInliers = 0;
for (int k =0; k < counter; k++)
{
double d = abs ((sqrt ((double) (center.X - Points[k].X) * (center.X - Points[k].X) + (center.Y - Points[k].Y) * (center.Y - Points[k].Y) )) - radius );
if ( d < t)
nrInliers++;
}
if(nrInliers >= T)
{
centerResult = center;
radiusResult = radius;
break;
}
else
{
if(nrInliers > maxInliers)
{
maxInliers = nrInliers;
centerResult = center;
radiusResult = radius;
}
}
}
}
void CDibView::OnLab1Ransac32776()
{
BEGIN_PROCESSING();
int t = 10;
double p = 0.99;
double W = 0.3;
int s = 2;
int counter = initPoints(lpSrc,dwHeight,dwWidth,w);
int limit = log(1-p) / log(1-W*W);
double T = W * counter;
int maxLiners = 0;
RANSAC(limit, counter,t, T, maxLiners);
CDC dc;
dc.CreateCompatibleDC(0);
CBitmap ddBitmap;
HBITMAP hDDBitmap = CreateDIBitmap(::GetDC(0),
&((LPBITMAPINFO)lpS)->bmiHeader, CBM_INIT, lpSrc,
(LPBITMAPINFO) lpS, DIB_RGB_COLORS);
ddBitmap.Attach(hDDBitmap);
CBitmap* pTempBmp = dc.SelectObject(&ddBitmap);
CPen pen(PS_SOLID, 1, RGB(255,0,0));
CPen *pTempPen = dc.SelectObject(&pen);
dc.MoveTo(resultP1.X,dwHeight-1-resultP1.Y);
dc.LineTo(resultP2.X,dwHeight-1-resultP2.Y);
dc.SelectObject(pTempPen);
dc.SelectObject(pTempBmp);
GetDIBits(dc.m_hDC, ddBitmap, 0, dwHeight, lpDst,
(LPBITMAPINFO)lpD, DIB_RGB_COLORS);
END_PROCESSING("RASAC Line");
}
void CDibView::OnLab2Ransac()
{
BEGIN_PROCESSING();
int t = 10;
double p = 0.99;
double W = 0.5;
int counter = initPoints(lpSrc,dwHeight,dwWidth,w);
int N = log(1-p) / log(1-W*W*W);
double T = W * counter;
radiusResult = 0;
RANSACCircle(N, counter,t, T);
CDC dc;
dc.CreateCompatibleDC(0);
CBitmap ddBitmap;
HBITMAP hDDBitmap = CreateDIBitmap(::GetDC(0),
&((LPBITMAPINFO)lpS)->bmiHeader, CBM_INIT, lpSrc,
(LPBITMAPINFO)lpS, DIB_RGB_COLORS);
ddBitmap.Attach(hDDBitmap);
CBitmap* pTempBmp = dc.SelectObject(&ddBitmap);
CPen pen(PS_SOLID, 1, RGB(255,0,0));
CPen *pTempPen = dc.SelectObject(&pen);
/* draw a circle having radius r and center a point of
coordinates (x,y)*/
dc.MoveTo ( (int)(centerResult.X + radiusResult), dwHeight-1-centerResult.Y );
dc.AngleArc(centerResult.X, dwHeight-1-centerResult.Y, radiusResult, 0, 360);
dc.SelectObject(pTempPen);
dc.SelectObject(pTempBmp);
GetDIBits(dc.m_hDC, ddBitmap, 0, dwHeight, lpDst,
(LPBITMAPINFO)lpD, DIB_RGB_COLORS);
END_PROCESSING("RANSAC Line");
}
double HoughAccumulator[1000][1000];
int hsize;
void CDibView::OnLab3Hough()
{
BEGIN_PROCESSING();
int dd=1;
int p=1;
int dmax = 360;
double diagonal = sqrt ((double) dwHeight*dwHeight + dwWidth*dwWidth);
int size = 360 * (diagonal + 1);
double **accumulator = (double **)malloc((diagonal+1) * sizeof(double*));
for(int i = 0; i < diagonal+1; i++) accumulator[i] = (double *)malloc(360 * sizeof(double));
for (int i=0;i < diagonal+1; i++)
for (int j=0; j<360;j++)
accumulator[i][j] = 0;
for (int i=0;i < dwHeight; i++)
for (int j=0; j<dwWidth;j++){
if (lpSrc[i*w+j] == 255){
for (int k=0;k<360;k+=dd){
double radiani = k * 3.1415 / 180.0;
int ro =(int) (i*cos(radiani) + j*sin(radiani));
if (ro > 0 && ro<diagonal + 1)
accumulator[ro][k] ++;
}
}
}
for (int i=0;i<diagonal+1;i++)
for (int j=0;j<360;j++){
HoughAccumulator[i][j] = accumulator[i][j]*3;
//lpDst[i*w+j] = accumulator[i][j];
}
hsize = diagonal + 1;
END_PROCESSING("RANSAC Line");
}
struct hafShit{
int i;
int j;
int val;
};
hafShit peaks[1000];
hafShit peaksSorted[1000];
Point intersectsWith(int ro, int theta){
int y = 0;
double radiani = theta * 3.1415 / 180.0;
int x = ro/cos(radiani);
Point dr;
dr.X = x;
dr.Y = y;
return dr;
}
void CDibView::OnLab3Drawhough()
{
BEGIN_PROCESSING();
int windowSize=3;
int margin = windowSize/2;
for (int i=0;i < hsize; i++){
for (int j=0; j<360;j++){
lpSrc[i*w+j] = HoughAccumulator[i][j];
}
}
bool isMax=true;
int peakCounter=0;
for (int i=margin;i<hsize-margin;i++){
for (int j=margin;j<360-margin;j++){
isMax = true;
int max = HoughAccumulator[i][j];
for(int k = -margin; k<margin+1; k++){
for(int l = -margin; l<margin+1; l++){
if(HoughAccumulator[i][j] < HoughAccumulator[k+margin][l+margin]){
isMax = false;
}
}
}
if(isMax){
peaks[peakCounter].val = max;
peaks[peakCounter].i = i;
peaks[peakCounter].j = j;
peakCounter++;
}
}
}
hafShit aux;
for(int i=0;i<peakCounter-1;i++){
for(int j=i+1;j<peakCounter;j++){
if(peaks[i].val < peaks[j].val){
aux = peaks[j];
peaks[j]=peaks[i];
peaks[i]=aux;
}
}
}
int k=10;
CDC dc;
dc.CreateCompatibleDC(0);
CBitmap ddBitmap;
HBITMAP hDDBitmap = CreateDIBitmap(::GetDC(0),
&((LPBITMAPINFO)lpS)->bmiHeader, CBM_INIT, lpSrc,
(LPBITMAPINFO) lpS, DIB_RGB_COLORS);
ddBitmap.Attach(hDDBitmap);
CBitmap* pTempBmp = dc.SelectObject(&ddBitmap);
CPen pen(PS_SOLID, 1, RGB(255,0,0));
CPen *pTempPen = dc.SelectObject(&pen);
for (int i=k;i>0;i--){
Point p = intersectsWith(peaks[i].i, peaks[i].j);
dc.MoveTo(p.X,p.Y);
dc.LineTo(peaks[i].i,peaks[i].j);
}
dc.SelectObject(pTempPen);
dc.SelectObject(pTempBmp);
GetDIBits(dc.m_hDC, ddBitmap, 0, dwHeight, lpDst,
(LPBITMAPINFO)lpD, DIB_RGB_COLORS);
END_PROCESSING("RANSAC Line");
}
int dt[3000][3000];
void CDibView::OnLab4Dt()
{
BEGIN_PROCESSING();
for (int i=0;i < dwHeight; i++){
for (int j=0; j<dwWidth;j++){
dt[i][j] = 0;
}
}
for (int i=1;i < dwHeight-1; i++){
for (int j=1; j<dwWidth-1;j++){
if (lpSrc[i*w+j] == 0)
dt[i][j] = 0;
else
dt[i][j] = 255;
}
}
int mask[3][3] = {{3,2,3},{2,0,2},{3,2,3}};
for (int i = 1; i < dwHeight; i++)
{
for (int j = 1; j < dwWidth-1; j++){
int val1 = dt[(i-1)][(j-1)] + mask[0][0];
int val2 = dt[(i-1)][(j+0)] + mask[0][1];
int val3 = dt[(i-1)][(j+1)] + mask[0][2];
int val4 = dt[(i+0)][(j-1)] + mask[1][0];
int min1 = val1 > val2 ? val2 : val1;
int min2 = val3 > val4 ? val4 : val3;
int min = min1 > min2 ? min2 : min1;
if (lpSrc[i*w+j] > min)
dt[i][j] = min;
}
}
for (int i = dwHeight - 2; i >=0; i--){
for (int j = dwWidth - 2; j >= 1; j--) {
int val1 = dt[(i+1)][(j+1)] + mask[2][2];
int val2 = dt[(i+1)][(j+0)] + mask[2][1];
int val3 = dt[(i+1)][(j-1)] + mask[2][0];
int val4 = dt[(i+0)][(j+1)] + mask[1][2];
int min1 = val1 > val2 ? val2 : val1;
int min2 = val3 > val4 ? val4 : val3;
int min = min1 > min2 ? min2 : min1;
if (min < dt[i][j])
dt[i][j] = min;
}
}
for (int i = 0; i < dwHeight; i++)
{
for (int j = 0; j < dwWidth; j++){
lpDst[i*w+j] = dt[i][j];
}
}
END_PROCESSING("RANSAC Line");
}
int patternMatches[1000];
int patternCounter = -1;
void CDibView::OnLab4Patternmatching()
{
BEGIN_PROCESSING();
for (int i=0;i < dwHeight; i++){
for (int j=0; j<dwWidth;j++){
if (lpSrc[i*w+j] == 0){
patternMatches[patternCounter++] = dt[i][j];
}
}
}
int average = 0;
int sum = 0;
for (int i=0;i<patternCounter;i++){
sum += patternMatches[i];
}
average = sum / patternCounter;
CString str ;
str.Format("%d",average);
AfxMessageBox(_T(str));
END_PROCESSING("RANSAC Line");
}
struct rgb{
int r;
int g;
int b;
};
int getColorForAngle(double angle,RGBQUAD *bmiColorsDst){
if (angle >= 0 && angle < 40){
bmiColorsDst[0].rgbRed = 255;
bmiColorsDst[0].rgbGreen = 0;
bmiColorsDst[0].rgbBlue = 0;
return 0;
}
if (angle >= 40 && angle < 80){
bmiColorsDst[1].rgbRed = 255;
bmiColorsDst[1].rgbGreen = 128;
bmiColorsDst[1].rgbBlue = 0;
return 1;
}
if (angle >= 80 && angle < 120){
bmiColorsDst[2].rgbRed = 255;
bmiColorsDst[2].rgbGreen = 255;
bmiColorsDst[2].rgbBlue = 0;
return 2;
}
if (angle >= 120 && angle < 160){
bmiColorsDst[3].rgbRed = 0;
bmiColorsDst[3].rgbGreen = 255;
bmiColorsDst[3].rgbBlue = 0;
return 3;
}
if (angle >= 160 && angle < 200){
bmiColorsDst[4].rgbRed = 0;
bmiColorsDst[4].rgbGreen = 255;
bmiColorsDst[4].rgbBlue = 128;
return 4;
}
if (angle >= 200 && angle < 240){
bmiColorsDst[5].rgbRed = 0;
bmiColorsDst[5].rgbGreen = 255;
bmiColorsDst[5].rgbBlue = 255;
return 5;
}
if (angle >= 240 && angle < 280){
bmiColorsDst[6].rgbRed = 0;
bmiColorsDst[6].rgbGreen = 0;
bmiColorsDst[6].rgbBlue = 255;
return 6;
}
if (angle >= 280 && angle < 320){
bmiColorsDst[7].rgbRed = 128;
bmiColorsDst[7].rgbGreen = 0;
bmiColorsDst[7].rgbBlue = 255;
return 7;
}
if (angle >= 320 && angle < 360){
bmiColorsDst[8].rgbRed = 0;
bmiColorsDst[8].rgbGreen = 0;
bmiColorsDst[8].rgbBlue = 255;
return 8;
}
}
BYTE *InitialImage;
int numberOfDivisions;
int numberOfDivisions2;
BYTE *CellDividedImage;
void CDibView::OnLab5Magnitudeandgradient()
{
BEGIN_PROCESSING();
//InitialImage = lpSrc;
int dx[3] = {-1,0,1};
int dy[3] = {1,0,1};
int ix,iy;
for(int i=1;i < dwHeight-1; i++){
for (int j=2; j<dwWidth-2;j++){
ix = abs((double)(-lpSrc[i*w+j-1] + lpSrc[i*w+j+1])/2);
if (ix > 255)
ix = 255;
if (ix < 0)
ix = 0;
iy = abs((double)(-lpSrc[(i-1)*w+j] + lpSrc[(i+1)*w+j])/2);
if (iy > 255)
iy = 255;
if (iy < 0)
iy = 0;
int gradient = sqrt (ix*ix + iy*iy);
lpDst[i*w+j] = gradient;
double theta = atan2(iy,ix);
double alfa = theta * 180 / 3.1415;
double alfasigned;
if (alfa >= 0)
alfasigned = alfa;
else
alfasigned = alfa + 360;
}
}
END_PROCESSING("RANSAC Line");
}
void CDibView::OnLab5Intervalcoloring()
{
BEGIN_PROCESSING();
int dx[3] = {-1,0,1};
int dy[3] = {1,0,1};
int ix,iy;
for(int i=1;i < dwHeight-1; i++){
for (int j=2; j<dwWidth-2;j++){
ix = ((double)(-lpSrc[i*w+j-1] + lpSrc[i*w+j+1]));
iy = ((double)(-lpSrc[(i-1)*w+j] + lpSrc[(i+1)*w+j]));
int gradient = sqrt (ix*ix + iy*iy);
double theta = atan2(iy,ix);
double alfa = theta * 180 / 3.1415;
double alfasigned;
if (alfa >= 0)
alfasigned = alfa;
else
alfasigned = alfa + 360;
int color = getColorForAngle(alfasigned,bmiColorsDst);
lpDst[i*w+j] = color;
}
}
END_PROCESSING("RANSAC Line");
}
int cellWidth = 16;
int cellHeight = 16;
int numberOfRuns = 0;
int nrCell1 = 0;
int nrCell2 = 0;
void CDibView::OnLab5Celldivision()
{
BEGIN_PROCESSING();
int dx[3] = {-1,0,1};
int dy[3] = {1,0,1};
int ix,iy;
int colors[500];
for (int i=0;i<500;i++)
colors[i]= 0;
int count = 0;
BYTE *aux;
aux = lpDst;
int hist[9];
for (int i=0;i<9;i++)
hist[i] = 0;
for(int i=1;i < dwHeight-1; i++){