├── OpenCVEnv
├── ImageMatchesMain.cpp
├── Scissor.h
├── main.cpp
├── Scissor.cpp
├── OpenCVEnv.vcxproj.filters
├── Utils.h
├── OpenCVEnv.vcxproj
└── PriorityQueue.h
├── README.md
├── OpenCVEnv.sln
├── .gitattributes
└── .gitignore
/OpenCVEnv/ImageMatchesMain.cpp:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/OpenCVEnv/Scissor.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mapupcal/IntelligentScissor/HEAD/OpenCVEnv/Scissor.h
--------------------------------------------------------------------------------
/OpenCVEnv/main.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mapupcal/IntelligentScissor/HEAD/OpenCVEnv/main.cpp
--------------------------------------------------------------------------------
/OpenCVEnv/Scissor.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mapupcal/IntelligentScissor/HEAD/OpenCVEnv/Scissor.cpp
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 基于Opencv3实现智能剪刀算法
2 |
3 | 注:关于抠图算法,在我的代码实现上,并没有对凹多边形做凸多边形分解,是简(偷)单(懒)地使用一种“扫描线”算法的实现。所以凹多边形的抠图会出现如下问题。
4 |
5 | 注:项目代码基于Opencv3.2,主要用在图像卷积运算,CostMap的数据结构表示,以及图形界面的显示操作。
6 |
7 | 注:代码注重在功能的实现上,仍有较大的优化空间。
8 |
9 | 注:本代码并未实现有关training部分,如果你想知道详情,可以参考论文[2]自行实现。
10 |
11 | Reference:
12 | [1] Mortensen E N, Barrett W A. Intelligent scissors for image composition[C]//Proceedings of the 22nd annual conference on Computer graphics and interactive techniques. ACM, 1995: 191-198.
13 |
14 | [2] Mortensen E N, Barrett W A. Interactive segmentation with intelligent scissors[J]. Graphical models and image processing, 1998, 60(5): 349-384.
15 |
16 | [3] CS 4670/5670, Project 1: Image Scissors
17 |
18 | [4] Robert Sedgewick Kevin Wayne算法(中文第四版)
--------------------------------------------------------------------------------
/OpenCVEnv.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.26403.7
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenCVEnv", "OpenCVEnv\OpenCVEnv.vcxproj", "{EA2947D1-7380-40A7-823B-A668FCED7D02}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|x64 = Debug|x64
11 | Debug|x86 = Debug|x86
12 | Release|x64 = Release|x64
13 | Release|x86 = Release|x86
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {EA2947D1-7380-40A7-823B-A668FCED7D02}.Debug|x64.ActiveCfg = Debug|x64
17 | {EA2947D1-7380-40A7-823B-A668FCED7D02}.Debug|x64.Build.0 = Debug|x64
18 | {EA2947D1-7380-40A7-823B-A668FCED7D02}.Debug|x86.ActiveCfg = Debug|Win32
19 | {EA2947D1-7380-40A7-823B-A668FCED7D02}.Debug|x86.Build.0 = Debug|Win32
20 | {EA2947D1-7380-40A7-823B-A668FCED7D02}.Release|x64.ActiveCfg = Release|x64
21 | {EA2947D1-7380-40A7-823B-A668FCED7D02}.Release|x64.Build.0 = Release|x64
22 | {EA2947D1-7380-40A7-823B-A668FCED7D02}.Release|x86.ActiveCfg = Release|Win32
23 | {EA2947D1-7380-40A7-823B-A668FCED7D02}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/OpenCVEnv/OpenCVEnv.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Source Files
20 |
21 |
22 | Source Files
23 |
24 |
25 |
26 |
27 | Header Files
28 |
29 |
30 | Header Files
31 |
32 |
33 | Header Files
34 |
35 |
36 |
--------------------------------------------------------------------------------
/OpenCVEnv/Utils.h:
--------------------------------------------------------------------------------
1 | #ifndef SCISSOR_UTILS_H
2 | #define SCISSOR_UTILS_H
3 | #include
4 | #include"Scissor.h"
5 | void DigitizeCost(unsigned char *cst, double lCost)
6 | {
7 | cst[0] = cst[1] = cst[2] = (unsigned char)(floor(__max(0.0, __min(255.0, lCost*1000))));
8 | }
9 | void MakeCostGraph(cv::Mat &OutputCostMap, const PixelNode* nodes, const cv::Mat &img);
10 | void MakeCostGraphImpl(unsigned char*costGraph, const PixelNode *nodes, const unsigned char *img, int width, int height);
11 |
12 |
13 | void MakeCostGraph(cv::Mat &OutputCostMap, const PixelNode* nodes, const cv::Mat &img)
14 | {
15 | OutputCostMap.create(img.rows * 3, img.cols * 3, CV_8UC3);
16 | MakeCostGraphImpl(OutputCostMap.data, nodes, img.data, img.cols, img.rows);
17 | }
18 | void MakeCostGraphImpl(unsigned char *costGraph, const PixelNode *nodes, const unsigned char *img, int imgWidth, int imgHeight)
19 | {
20 | int graphWidth = imgWidth * 3;
21 | int graphHeight = imgHeight * 3;
22 | int dgX = 3;
23 | int dgY = 3 * graphWidth;
24 |
25 | int i, j;
26 | for (j = 0; jlinkCost[0]);
44 | DigitizeCost(cst - dgY + dgX, node->linkCost[1]);
45 | DigitizeCost(cst - dgY, node->linkCost[2]);
46 | DigitizeCost(cst - dgY - dgX, node->linkCost[3]);
47 | DigitizeCost(cst - dgX, node->linkCost[4]);
48 | DigitizeCost(cst + dgY - dgX, node->linkCost[5]);
49 | DigitizeCost(cst + dgY, node->linkCost[6]);
50 | DigitizeCost(cst + dgY + dgX, node->linkCost[7]);
51 | }
52 | }
53 | }
54 |
55 |
56 | #endif // !UTILS_H
57 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | project.fragment.lock.json
46 | artifacts/
47 |
48 | *_i.c
49 | *_p.c
50 | *_i.h
51 | *.ilk
52 | *.meta
53 | *.obj
54 | *.pch
55 | *.pdb
56 | *.pgc
57 | *.pgd
58 | *.rsp
59 | *.sbr
60 | *.tlb
61 | *.tli
62 | *.tlh
63 | *.tmp
64 | *.tmp_proj
65 | *.log
66 | *.vspscc
67 | *.vssscc
68 | .builds
69 | *.pidb
70 | *.svclog
71 | *.scc
72 |
73 | # Chutzpah Test files
74 | _Chutzpah*
75 |
76 | # Visual C++ cache files
77 | ipch/
78 | *.aps
79 | *.ncb
80 | *.opendb
81 | *.opensdf
82 | *.sdf
83 | *.cachefile
84 | *.VC.db
85 | *.VC.VC.opendb
86 |
87 | # Visual Studio profiler
88 | *.psess
89 | *.vsp
90 | *.vspx
91 | *.sap
92 |
93 | # TFS 2012 Local Workspace
94 | $tf/
95 |
96 | # Guidance Automation Toolkit
97 | *.gpState
98 |
99 | # ReSharper is a .NET coding add-in
100 | _ReSharper*/
101 | *.[Rr]e[Ss]harper
102 | *.DotSettings.user
103 |
104 | # JustCode is a .NET coding add-in
105 | .JustCode
106 |
107 | # TeamCity is a build add-in
108 | _TeamCity*
109 |
110 | # DotCover is a Code Coverage Tool
111 | *.dotCover
112 |
113 | # NCrunch
114 | _NCrunch_*
115 | .*crunch*.local.xml
116 | nCrunchTemp_*
117 |
118 | # MightyMoose
119 | *.mm.*
120 | AutoTest.Net/
121 |
122 | # Web workbench (sass)
123 | .sass-cache/
124 |
125 | # Installshield output folder
126 | [Ee]xpress/
127 |
128 | # DocProject is a documentation generator add-in
129 | DocProject/buildhelp/
130 | DocProject/Help/*.HxT
131 | DocProject/Help/*.HxC
132 | DocProject/Help/*.hhc
133 | DocProject/Help/*.hhk
134 | DocProject/Help/*.hhp
135 | DocProject/Help/Html2
136 | DocProject/Help/html
137 |
138 | # Click-Once directory
139 | publish/
140 |
141 | # Publish Web Output
142 | *.[Pp]ublish.xml
143 | *.azurePubxml
144 | # TODO: Comment the next line if you want to checkin your web deploy settings
145 | # but database connection strings (with potential passwords) will be unencrypted
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
150 | # checkin your Azure Web App publish settings, but sensitive information contained
151 | # in these scripts will be unencrypted
152 | PublishScripts/
153 |
154 | # NuGet Packages
155 | *.nupkg
156 | # The packages folder can be ignored because of Package Restore
157 | **/packages/*
158 | # except build/, which is used as an MSBuild target.
159 | !**/packages/build/
160 | # Uncomment if necessary however generally it will be regenerated when needed
161 | #!**/packages/repositories.config
162 | # NuGet v3's project.json files produces more ignoreable files
163 | *.nuget.props
164 | *.nuget.targets
165 |
166 | # Microsoft Azure Build Output
167 | csx/
168 | *.build.csdef
169 |
170 | # Microsoft Azure Emulator
171 | ecf/
172 | rcf/
173 |
174 | # Windows Store app package directories and files
175 | AppPackages/
176 | BundleArtifacts/
177 | Package.StoreAssociation.xml
178 | _pkginfo.txt
179 |
180 | # Visual Studio cache files
181 | # files ending in .cache can be ignored
182 | *.[Cc]ache
183 | # but keep track of directories ending in .cache
184 | !*.[Cc]ache/
185 |
186 | # Others
187 | ClientBin/
188 | ~$*
189 | *~
190 | *.dbmdl
191 | *.dbproj.schemaview
192 | *.jfm
193 | *.pfx
194 | *.publishsettings
195 | node_modules/
196 | orleans.codegen.cs
197 |
198 | # Since there are multiple workflows, uncomment next line to ignore bower_components
199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
200 | #bower_components/
201 |
202 | # RIA/Silverlight projects
203 | Generated_Code/
204 |
205 | # Backup & report files from converting an old project file
206 | # to a newer Visual Studio version. Backup files are not needed,
207 | # because we have git ;-)
208 | _UpgradeReport_Files/
209 | Backup*/
210 | UpgradeLog*.XML
211 | UpgradeLog*.htm
212 |
213 | # SQL Server files
214 | *.mdf
215 | *.ldf
216 |
217 | # Business Intelligence projects
218 | *.rdl.data
219 | *.bim.layout
220 | *.bim_*.settings
221 |
222 | # Microsoft Fakes
223 | FakesAssemblies/
224 |
225 | # GhostDoc plugin setting file
226 | *.GhostDoc.xml
227 |
228 | # Node.js Tools for Visual Studio
229 | .ntvs_analysis.dat
230 |
231 | # Visual Studio 6 build log
232 | *.plg
233 |
234 | # Visual Studio 6 workspace options file
235 | *.opt
236 |
237 | # Visual Studio LightSwitch build output
238 | **/*.HTMLClient/GeneratedArtifacts
239 | **/*.DesktopClient/GeneratedArtifacts
240 | **/*.DesktopClient/ModelManifest.xml
241 | **/*.Server/GeneratedArtifacts
242 | **/*.Server/ModelManifest.xml
243 | _Pvt_Extensions
244 |
245 | # Paket dependency manager
246 | .paket/paket.exe
247 | paket-files/
248 |
249 | # FAKE - F# Make
250 | .fake/
251 |
252 | # JetBrains Rider
253 | .idea/
254 | *.sln.iml
255 |
256 | # CodeRush
257 | .cr/
258 |
259 | # Python Tools for Visual Studio (PTVS)
260 | __pycache__/
261 | *.pyc
262 | *.tga
263 | *.png
264 | *.jpg
265 | *.dll
266 | *.dll
267 | *.jpg
268 | *.dll
269 |
--------------------------------------------------------------------------------
/OpenCVEnv/OpenCVEnv.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 15.0
23 | {EA2947D1-7380-40A7-823B-A668FCED7D02}
24 | OpenCVEnv
25 | 10.0.15063.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v141
32 | MultiByte
33 |
34 |
35 | Application
36 | false
37 | v141
38 | true
39 | MultiByte
40 |
41 |
42 | Application
43 | true
44 | v141
45 | MultiByte
46 |
47 |
48 | Application
49 | false
50 | v141
51 | true
52 | MultiByte
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | G:\Code\opencv\opencv3.2\build\include;$(IncludePath)
74 | G:\Code\opencv\opencv3.2\build\x64\vc14\lib;$(LibraryPath)
75 |
76 |
77 | G:\Code\opencv\opencv3.2\build\include;$(IncludePath)
78 | G:\Code\opencv\opencv3.2\build\x64\vc14\lib;$(LibraryPath)
79 |
80 |
81 |
82 | Level3
83 | Disabled
84 | true
85 |
86 |
87 |
88 |
89 | Level3
90 | Disabled
91 | true
92 |
93 |
94 | opencv_calib3d320d.lib;opencv_aruco320.lib;opencv_aruco320d.lib;opencv_bgsegm320.lib;opencv_bgsegm320d.lib;opencv_bioinspired320.lib;opencv_bioinspired320d.lib;opencv_calib3d320.lib;opencv_ccalib320.lib;opencv_ccalib320d.lib;opencv_core320.lib;opencv_core320d.lib;opencv_datasets320.lib;opencv_datasets320d.lib;opencv_dnn320.lib;opencv_dnn320d.lib;opencv_dpm320.lib;opencv_dpm320d.lib;opencv_face320.lib;opencv_face320d.lib;opencv_features2d320.lib;opencv_features2d320d.lib;opencv_flann320.lib;opencv_flann320d.lib;opencv_fuzzy320.lib;opencv_fuzzy320d.lib;opencv_hdf320.lib;opencv_hdf320d.lib;opencv_highgui320.lib;opencv_highgui320d.lib;opencv_imgcodecs320.lib;opencv_imgcodecs320d.lib;opencv_imgproc320.lib;opencv_imgproc320d.lib;opencv_line_descriptor320.lib;opencv_line_descriptor320d.lib;opencv_ml320.lib;opencv_ml320d.lib;opencv_objdetect320.lib;opencv_objdetect320d.lib;opencv_optflow320.lib;opencv_optflow320d.lib;opencv_phase_unwrapping320.lib;opencv_phase_unwrapping320d.lib;opencv_photo320.lib;opencv_photo320d.lib;opencv_plot320.lib;opencv_plot320d.lib;opencv_reg320.lib;opencv_reg320d.lib;opencv_rgbd320.lib;opencv_rgbd320d.lib;opencv_saliency320.lib;opencv_saliency320d.lib;opencv_shape320.lib;opencv_shape320d.lib;opencv_stereo320.lib;opencv_stereo320d.lib;opencv_stitching320.lib;opencv_stitching320d.lib;opencv_structured_light320.lib;opencv_structured_light320d.lib;opencv_superres320.lib;opencv_superres320d.lib;opencv_surface_matching320.lib;opencv_surface_matching320d.lib;opencv_text320.lib;opencv_text320d.lib;opencv_tracking320.lib;opencv_tracking320d.lib;opencv_video320.lib;opencv_video320d.lib;opencv_videoio320.lib;opencv_videoio320d.lib;opencv_videostab320.lib;opencv_videostab320d.lib;opencv_world320.lib;opencv_world320d.lib;opencv_xfeatures2d320.lib;opencv_xfeatures2d320d.lib;opencv_ximgproc320.lib;opencv_ximgproc320d.lib;opencv_xobjdetect320.lib;opencv_xobjdetect320d.lib;opencv_xphoto320.lib;opencv_xphoto320d.lib;%(AdditionalDependencies)
95 |
96 |
97 |
98 |
99 | Level3
100 | MaxSpeed
101 | true
102 | true
103 | true
104 |
105 |
106 | true
107 | true
108 |
109 |
110 |
111 |
112 | Level3
113 | MaxSpeed
114 | true
115 | true
116 | true
117 |
118 |
119 | true
120 | true
121 | opencv_aruco320.lib;opencv_aruco320d.lib;opencv_bgsegm320.lib;opencv_bgsegm320d.lib;opencv_bioinspired320.lib;opencv_bioinspired320d.lib;opencv_calib3d320.lib;opencv_calib3d320d.lib;opencv_ccalib320.lib;opencv_ccalib320d.lib;opencv_core320.lib;opencv_core320d.lib;opencv_datasets320.lib;opencv_datasets320d.lib;opencv_dnn320.lib;opencv_dnn320d.lib;opencv_dpm320.lib;opencv_dpm320d.lib;opencv_face320.lib;opencv_face320d.lib;opencv_features2d320.lib;opencv_features2d320d.lib;opencv_flann320.lib;opencv_flann320d.lib;opencv_fuzzy320.lib;opencv_fuzzy320d.lib;opencv_hdf320.lib;opencv_hdf320d.lib;opencv_highgui320.lib;opencv_highgui320d.lib;opencv_imgcodecs320.lib;opencv_imgcodecs320d.lib;opencv_imgproc320.lib;opencv_imgproc320d.lib;opencv_line_descriptor320.lib;opencv_line_descriptor320d.lib;opencv_ml320.lib;opencv_ml320d.lib;opencv_objdetect320.lib;opencv_objdetect320d.lib;opencv_optflow320.lib;opencv_optflow320d.lib;opencv_phase_unwrapping320.lib;opencv_phase_unwrapping320d.lib;opencv_photo320.lib;opencv_photo320d.lib;opencv_plot320.lib;opencv_plot320d.lib;opencv_reg320.lib;opencv_reg320d.lib;opencv_rgbd320.lib;opencv_rgbd320d.lib;opencv_saliency320.lib;opencv_saliency320d.lib;opencv_shape320.lib;opencv_shape320d.lib;opencv_stereo320.lib;opencv_stereo320d.lib;opencv_stitching320.lib;opencv_stitching320d.lib;opencv_structured_light320.lib;opencv_structured_light320d.lib;opencv_superres320.lib;opencv_superres320d.lib;opencv_surface_matching320.lib;opencv_surface_matching320d.lib;opencv_text320.lib;opencv_text320d.lib;opencv_tracking320.lib;opencv_tracking320d.lib;opencv_video320.lib;opencv_video320d.lib;opencv_videoio320.lib;opencv_videoio320d.lib;opencv_videostab320.lib;opencv_videostab320d.lib;opencv_world320.lib;opencv_world320d.lib;opencv_xfeatures2d320.lib;opencv_xfeatures2d320d.lib;opencv_ximgproc320.lib;opencv_ximgproc320d.lib;opencv_xobjdetect320.lib;opencv_xobjdetect320d.lib;opencv_xphoto320.lib;opencv_xphoto320d.lib;%(AdditionalDependencies)
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
--------------------------------------------------------------------------------
/OpenCVEnv/PriorityQueue.h:
--------------------------------------------------------------------------------
1 | #ifndef PRIORITYQUEUE_H
2 | #define PRIORITYQUEUE_H
3 |
4 | template < class T >
5 | class CTypedPtrArray
6 | {
7 | protected:
8 | T **m_pHead;
9 | int m_iSize;
10 | int m_iTail;
11 | public:
12 | CTypedPtrArray(void);
13 | ~CTypedPtrArray(void);
14 |
15 | bool IsEmpty(void) const { return m_iTail == 0; }
16 | int GetSize(void) const { return m_iTail; }
17 | int GetTrueSize(void) const { return m_iSize; }
18 | bool IsIndexValid(const int i) const { return (i >= 0) && (i < m_iTail); }
19 |
20 | void SetTailAt(const int i) { m_iTail = i; }
21 | void SetSize(int);
22 |
23 | int AddTail(T *);
24 | bool RemoveTail(void);
25 | void RemoveAll(void);
26 | bool SetAt(int, T *);
27 |
28 | T *&operator[] (const int index) { return m_pHead[index]; }
29 | T *ElementAt(const int index) const { return m_pHead[index]; }
30 | void FreePtrs(void);
31 | };
32 |
33 | template < class T >
34 | CTypedPtrArray< T >::CTypedPtrArray(void)
35 | {
36 | m_pHead = NULL;
37 | m_iSize = 0;
38 | m_iTail = 0;
39 | }
40 |
41 | template < class T >
42 | CTypedPtrArray< T >::~CTypedPtrArray(void)
43 | {
44 | if (m_pHead)
45 | delete[] m_pHead;
46 | }
47 |
48 | template < class T >
49 | void CTypedPtrArray< T >::SetSize(const int size)
50 | {
51 | if (size == 0)
52 | {
53 | RemoveAll();
54 | return;
55 | }
56 |
57 | T **oldArray = m_pHead;
58 | int oldSize = m_iSize;
59 |
60 | m_iSize = size;
61 | if (m_iTail > m_iSize)
62 | m_iTail = m_iSize;
63 | m_pHead = new T*[m_iSize];
64 | // Fixed by Loren. Was:
65 | // memset( m_pHead, NULL, sizeof( T * ) * m_iSize );
66 | memset(m_pHead, 0, sizeof(T *) * m_iSize);
67 |
68 | if (oldSize)
69 | {
70 | if (m_iTail)
71 | memcpy(m_pHead, oldArray, sizeof(T *) * m_iTail);
72 | delete[] oldArray;
73 | }
74 | }
75 |
76 | template < class T >
77 | int CTypedPtrArray< T >::AddTail(T *t)
78 | {
79 | if (m_iTail == m_iSize)
80 | SetSize(m_iSize ? m_iSize << 1 : 1);
81 |
82 | m_pHead[m_iTail] = t;
83 | int index = m_iTail;
84 | m_iTail++;
85 | return index;
86 | }
87 |
88 | template < class T >
89 | bool CTypedPtrArray< T >::RemoveTail(void)
90 | {
91 | if (IsEmpty())
92 | return false;
93 |
94 | m_iTail--;
95 | m_pHead[m_iTail] = NULL;
96 | if (m_iTail <= (m_iSize >> 2))
97 | SetSize(m_iSize >> 1);
98 | return true;
99 | }
100 |
101 | template < class T >
102 | void CTypedPtrArray< T >::RemoveAll(void)
103 | {
104 | if (m_pHead)
105 | {
106 | delete[] m_pHead;
107 | m_pHead = NULL;
108 | m_iSize = 0;
109 | m_iTail = 0;
110 | }
111 | }
112 |
113 | template < class T >
114 | bool CTypedPtrArray< T >::SetAt(int index, T *t)
115 | {
116 | if (IsIndexValid(index))
117 | {
118 | m_pHead[index] = t;
119 | return true;
120 | }
121 | else
122 | return false;
123 | }
124 |
125 | template < class T >
126 | void CTypedPtrArray< T >::FreePtrs(void)
127 | {
128 | int i;
129 | for (i = 0; i < m_iSize; i++)
130 | {
131 | if (m_pHead[i])
132 | {
133 | delete m_pHead[i];
134 | m_pHead[i] = NULL;
135 | }
136 | }
137 | }
138 |
139 | /////////////////////
140 | //definition of typed pointer Heap based on typed pointer array;
141 |
142 | template < class T >
143 | class CTypedPtrHeap : public CTypedPtrArray< T >
144 | {
145 | public:
146 | CTypedPtrHeap(void) : CTypedPtrArray< T >() {}
147 | ~CTypedPtrHeap(void) {}
148 |
149 | private:
150 | void Heapify(const int index);
151 | void BubbleUp(const int index);
152 |
153 | public:
154 | static int Parent(int i) { ++i; i >>= 1; return --i; }
155 | static int Left(int i) { ++i; i <<= 1; return --i; }
156 | static int Right(int i) { ++i; i <<= 1; return i; }
157 |
158 | void Insert(T *t);
159 | bool RemoveAt(const int index);
160 | bool Remove(const T *t);
161 | bool UpdateAt(const int index);
162 | bool Update(const T *t);
163 | void BuildHeap(void);
164 | T *ExtractMin(void);
165 | };
166 |
167 | template < class T >
168 | void CTypedPtrHeap< T >::Heapify(const int index)
169 | {
170 | if (!CTypedPtrArray::IsIndexValid(index))
171 | return;
172 |
173 | int i = index;
174 | while (true) {
175 | int least = i;
176 |
177 | int left = Left(i);
178 | if (left < CTypedPtrArray::m_iTail &&
179 | *(CTypedPtrArray::m_pHead[left]) < *(CTypedPtrArray::m_pHead[least]))
180 | least = left;
181 |
182 | int right = Right(i);
183 | if (right < CTypedPtrArray::m_iTail &&
184 | *(CTypedPtrArray::m_pHead[right]) < *(CTypedPtrArray::m_pHead[least]))
185 | least = right;
186 |
187 | if (least != i) {
188 | T *t = CTypedPtrArray::m_pHead[least];
189 | CTypedPtrArray::m_pHead[least] = CTypedPtrArray::m_pHead[i];
190 | CTypedPtrArray::m_pHead[least]->Index() = least;
191 | CTypedPtrArray::m_pHead[i] = t;
192 | CTypedPtrArray::m_pHead[i]->Index() = i;
193 | i = least;
194 | }
195 | else {
196 | break;
197 | }
198 | }
199 | }
200 |
201 | template < class T >
202 | void CTypedPtrHeap< T >::BubbleUp(const int index)
203 | {
204 | if (!CTypedPtrArray::IsIndexValid(index))
205 | return;
206 |
207 | int i = index;
208 | T *t = CTypedPtrArray::m_pHead[index];
209 |
210 | while (Parent(i) >= 0 && *t < *(CTypedPtrArray::m_pHead[Parent(i)])) {
211 | CTypedPtrArray::m_pHead[i] = CTypedPtrArray::m_pHead[Parent(i)];
212 | CTypedPtrArray::m_pHead[i]->Index() = i;
213 | i = Parent(i);
214 | }
215 | CTypedPtrArray::m_pHead[i] = t;
216 | CTypedPtrArray::m_pHead[i]->Index() = i;
217 | }
218 |
219 | template < class T >
220 | void CTypedPtrHeap< T >::Insert(T *t)
221 | {
222 | int index = AddTail(t);
223 | BubbleUp(index);
224 | }
225 |
226 | template < class T >
227 | bool CTypedPtrHeap< T >::RemoveAt(const int index)
228 | {
229 | if (!CTypedPtrArray::IsIndexValid(index))
230 | return false;
231 |
232 | if (index == CTypedPtrArray::m_iTail - 1)
233 | CTypedPtrArray::RemoveTail();
234 | else {
235 | CTypedPtrArray::m_pHead[index] = CTypedPtrArray::m_pHead[CTypedPtrArray::m_iTail - 1];
236 | CTypedPtrArray::m_pHead[index]->Index() = index;
237 | CTypedPtrArray::RemoveTail();
238 | UpdateAt(index);
239 | }
240 | return true;
241 | }
242 |
243 | template < class T >
244 | bool CTypedPtrHeap< T >::Remove(const T *t)
245 | {
246 | return RemoveAt(t->Index());
247 | }
248 |
249 | template < class T >
250 | bool CTypedPtrHeap< T >::UpdateAt(const int index)
251 | {
252 | if (!CTypedPtrArray::IsIndexValid(index))
253 | return false;
254 |
255 | T *t = CTypedPtrArray::m_pHead[index];
256 | Heapify(index);
257 |
258 | if (t == CTypedPtrArray::m_pHead[index])
259 | BubbleUp(index);
260 |
261 | return true;
262 | }
263 |
264 | template < class T >
265 | bool CTypedPtrHeap< T >::Update(const T *t)
266 | {
267 | return UpdateAt(t->Index());
268 | }
269 |
270 | template < class T >
271 | void CTypedPtrHeap< T >::BuildHeap(void)
272 | {
273 | if (CTypedPtrArray::IsEmpty())
274 | return;
275 |
276 | for (int i = Parent(CTypedPtrArray::m_iTail - 1); i >= 0; i--)
277 | Heapify(i);
278 | }
279 |
280 | template < class T >
281 | T *CTypedPtrHeap< T >::ExtractMin(void)
282 | {
283 | if (CTypedPtrArray::IsEmpty())
284 | return NULL;
285 |
286 | T *t = CTypedPtrArray::m_pHead[0];
287 | RemoveAt(0);
288 | return t;
289 | }
290 |
291 | ////////////////////
292 | //definition of CTypedPtrDblElement;
293 | template < class T >
294 | class CTypedPtrDblElement
295 | {
296 | protected:
297 | T *m_pData;
298 | CTypedPtrDblElement *m_pPrev, *m_pNext;
299 |
300 | public:
301 | CTypedPtrDblElement (void)
302 | {
303 | m_pData = NULL; m_pPrev = NULL; m_pNext = NULL;
304 | }
305 |
306 | void Data(T *pData) { m_pData = pData; }
307 | T *Data(void) const { return m_pData; }
308 |
309 | void Next(CTypedPtrDblElement *pNext) { m_pNext = pNext; }
310 | CTypedPtrDblElement *Next(void) const { return m_pNext; }
311 |
312 | void Prev(CTypedPtrDblElement *pPrev) { m_pPrev = pPrev; }
313 | CTypedPtrDblElement *Prev(void) const { return m_pPrev; }
314 | };
315 |
316 | ////////////////////
317 | //definition of doubly-linked list;
318 | template < class T >
319 | class CTypedPtrDblList
320 | {
321 | private:
322 | int m_iCircular;
323 |
324 | protected:
325 |
326 | CTypedPtrDblElement < T > *m_pSentinel; // this is a dummy element;
327 | int m_iCount;
328 |
329 | public:
330 |
331 | CTypedPtrDblList(void)
332 | {
333 | m_iCircular = 0;
334 |
335 | m_pSentinel = new CTypedPtrDblElement ;
336 | m_pSentinel->Prev(m_pSentinel);
337 | m_pSentinel->Next(m_pSentinel);
338 |
339 | m_iCount = 0;
340 | }
341 | ~CTypedPtrDblList(void)
342 | {
343 | RemoveAll();
344 | delete m_pSentinel;
345 | }
346 |
347 | bool IsEmpty(void) const { return m_iCount == 0; }
348 | int GetCount(void) const { return m_iCount; }
349 |
350 | void SetCircular(int c) { m_iCircular = c; }
351 | int IsCircular(void) const { return m_iCircular; }
352 |
353 | CTypedPtrDblElement *GetHeadPtr(void) const { return m_pSentinel->Next(); }
354 | CTypedPtrDblElement *GetTailPtr(void) const { return m_pSentinel->Prev(); }
355 |
356 | bool IsSentinel(const CTypedPtrDblElement *pEle) const { return pEle == m_pSentinel; }
357 |
358 | CTypedPtrDblElement < T > * AddPrev(CTypedPtrDblElement < T > *pEle, T *t)
359 | {
360 | CTypedPtrDblElement < T > *pNewEle = new CTypedPtrDblElement < T >;
361 | pNewEle->Data(t);
362 | pNewEle->Prev(pEle->Prev());
363 | pNewEle->Next(pEle);
364 | pNewEle->Prev()->Next(pNewEle);
365 | pNewEle->Next()->Prev(pNewEle);
366 | m_iCount++;
367 | return pNewEle;
368 | }
369 |
370 | CTypedPtrDblElement < T > * AddNext(CTypedPtrDblElement * pEle, T *t)
371 | {
372 | CTypedPtrDblElement < T > *pNewEle = new CTypedPtrDblElement < T >;
373 | pNewEle->Data(t);
374 | pNewEle->Prev(pEle);
375 | pNewEle->Next(pEle->Next());
376 | pNewEle->Prev()->Next(pNewEle);
377 | pNewEle->Next()->Prev(pNewEle);
378 | m_iCount++;
379 | return pNewEle;
380 | }
381 |
382 | T *Remove(CTypedPtrDblElement *pEle)
383 | {
384 | if (pEle == m_pSentinel) return NULL;
385 |
386 | pEle->Prev()->Next(pEle->Next());
387 | pEle->Next()->Prev(pEle->Prev());
388 |
389 | T *pData = pEle->Data();
390 | delete pEle;
391 | m_iCount--;
392 | return pData;
393 | }
394 |
395 | T *RemovePrev(CTypedPtrDblElement * pEle) { return Remove(pEle->Prev()); }
396 | T *RemoveNext(CTypedPtrDblElement * pEle) { return Remove(pEle->Next()); }
397 |
398 | CTypedPtrDblElement < T > * AddHead(T *t) { return AddNext(m_pSentinel, t); }
399 | CTypedPtrDblElement < T > * AddTail(T *t) { return AddPrev(m_pSentinel, t); }
400 |
401 | T *RemoveHead(void) { return Remove(m_pSentinel->Next()); }
402 | T *RemoveTail(void) { return Remove(m_pSentinel->Prev()); }
403 |
404 | void RemoveAll(void) { while (!IsEmpty()) RemoveHead(); }
405 |
406 | CTypedPtrDblElement < T > *Find(const T * t)
407 | {
408 | CTypedPtrDblElement < T > *result = GetHeadPtr();
409 | while (!IsSentinel(result))
410 | {
411 | if (result->Data() == t)
412 | break;
413 | else
414 | result = result->Next();
415 | }
416 | return result;
417 | }
418 |
419 | bool Remove(const T * t)
420 | {
421 | CTypedPtrDblElement *pEle = Find(t);
422 | Remove(pEle);
423 | return !IsSentinel(pEle);
424 | }
425 |
426 | void FreePtrs(void)
427 | {
428 | CTypedPtrDblElement < T > *pEle = GetHeadPtr();
429 | while (!IsSentinel(pEle))
430 | {
431 | if (pEle->Data()) delete pEle->Data();
432 | pEle = pEle->Next();
433 | }
434 | }
435 |
436 | void Do(void(*method) (T *)) const
437 | {
438 | CTypedPtrDblElement < T > *pEle = GetHeadPtr();
439 | while (!IsSentinel(pEle))
440 | {
441 | method(pEle->Data());
442 | pEle = pEle->Next();
443 | }
444 | }
445 |
446 | template
447 | void Do(Method method) const
448 | {
449 | CTypedPtrDblElement < T > *pEle = GetHeadPtr();
450 | while (!IsSentinel(pEle))
451 | {
452 | method(pEle->Data());
453 | pEle = pEle->Next();
454 | }
455 | }
456 | };
457 |
458 | #endif
459 |
--------------------------------------------------------------------------------