Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | PointedEar | 1 | /*------------------------------------------------------------------------------------------*\ |
2 | This file contains material supporting chapter 5 of the cookbook: |
||
3 | Computer Vision Programming using the OpenCV Library. |
||
4 | by Robert Laganiere, Packt Publishing, 2011. |
||
5 | |||
6 | This program is free software; permission is hereby granted to use, copy, modify, |
||
7 | and distribute this source code, or portions thereof, for any purpose, without fee, |
||
8 | subject to the restriction that the copyright notice may not be removed |
||
9 | or altered from any source or altered source distribution. |
||
10 | The software is released on an as-is basis and without any warranties of any kind. |
||
11 | In particular, the software is not guaranteed to be fault-tolerant or free from failure. |
||
12 | The author disclaims all warranties with regard to this software, any use, |
||
13 | and any consequent failure, is purely the responsibility of the user. |
||
14 | |||
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name |
||
16 | \*------------------------------------------------------------------------------------------*/ |
||
17 | |||
18 | #include <iostream> |
||
19 | #include <opencv2/core/core.hpp> |
||
20 | #include <opencv2/imgproc/imgproc.hpp> |
||
21 | #include <opencv2/highgui/highgui.hpp> |
||
22 | #include "watershedSegmentation.h" |
||
23 | |||
24 | |||
25 | int main() |
||
26 | { |
||
27 | // Read input image |
||
28 | cv::Mat image= cv::imread("../group.jpg"); |
||
29 | if (!image.data) |
||
30 | return 0; |
||
31 | |||
32 | // Display the image |
||
33 | cv::namedWindow("Original Image"); |
||
34 | cv::imshow("Original Image",image); |
||
35 | |||
36 | // Get the binary map |
||
37 | cv::Mat binary; |
||
38 | binary= cv::imread("../binary.bmp",0); |
||
39 | |||
40 | // Display the binary image |
||
41 | cv::namedWindow("Binary Image"); |
||
42 | cv::imshow("Binary Image",binary); |
||
43 | |||
44 | // Eliminate noise and smaller objects |
||
45 | cv::Mat fg; |
||
46 | cv::erode(binary,fg,cv::Mat(),cv::Point(-1,-1),6); |
||
47 | |||
48 | // Display the foreground image |
||
49 | cv::namedWindow("Foreground Image"); |
||
50 | cv::imshow("Foreground Image",fg); |
||
51 | |||
52 | // Identify image pixels without objects |
||
53 | cv::Mat bg; |
||
54 | cv::dilate(binary,bg,cv::Mat(),cv::Point(-1,-1),6); |
||
55 | cv::threshold(bg,bg,1,128,cv::THRESH_BINARY_INV); |
||
56 | |||
57 | // Display the background image |
||
58 | cv::namedWindow("Background Image"); |
||
59 | cv::imshow("Background Image",bg); |
||
60 | |||
61 | // Show markers image |
||
62 | cv::Mat markers(binary.size(),CV_8U,cv::Scalar(0)); |
||
63 | markers= fg+bg; |
||
64 | cv::namedWindow("Markers"); |
||
65 | cv::imshow("Markers",markers); |
||
66 | |||
67 | // Create watershed segmentation object |
||
68 | WatershedSegmenter segmenter; |
||
69 | |||
70 | // Set markers and process |
||
71 | segmenter.setMarkers(markers); |
||
72 | segmenter.process(image); |
||
73 | |||
74 | // Display segmentation result |
||
75 | cv::namedWindow("Segmentation"); |
||
76 | cv::imshow("Segmentation",segmenter.getSegmentation()); |
||
77 | |||
78 | // Display watersheds |
||
79 | cv::namedWindow("Watersheds"); |
||
80 | cv::imshow("Watersheds",segmenter.getWatersheds()); |
||
81 | |||
82 | // Open another image |
||
83 | image= cv::imread("../tower.jpg"); |
||
84 | |||
85 | // Identify background pixels |
||
86 | cv::Mat imageMask(image.size(),CV_8U,cv::Scalar(0)); |
||
87 | cv::rectangle(imageMask,cv::Point(5,5),cv::Point(image.cols-5,image.rows-5),cv::Scalar(255),3); |
||
88 | // Identify foreground pixels (in the middle of the image) |
||
89 | cv::rectangle(imageMask,cv::Point(image.cols/2-10,image.rows/2-10), |
||
90 | cv::Point(image.cols/2+10,image.rows/2+10),cv::Scalar(1),10); |
||
91 | |||
92 | // Set markers and process |
||
93 | segmenter.setMarkers(imageMask); |
||
94 | segmenter.process(image); |
||
95 | |||
96 | // Display the image with markers |
||
97 | cv::rectangle(image,cv::Point(5,5),cv::Point(image.cols-5,image.rows-5),cv::Scalar(255,255,255),3); |
||
98 | cv::rectangle(image,cv::Point(image.cols/2-10,image.rows/2-10), |
||
99 | cv::Point(image.cols/2+10,image.rows/2+10),cv::Scalar(1,1,1),10); |
||
100 | cv::namedWindow("Image with marker"); |
||
101 | cv::imshow("Image with marker",image); |
||
102 | |||
103 | // Display watersheds |
||
104 | cv::namedWindow("Watersheds of foreground object"); |
||
105 | cv::imshow("Watersheds of foreground object",segmenter.getWatersheds()); |
||
106 | |||
107 | // Open another image |
||
108 | image= cv::imread("../tower.jpg"); |
||
109 | |||
110 | // define bounding rectangle |
||
111 | cv::Rect rectangle(50,70,image.cols-150,image.rows-180); |
||
112 | |||
113 | cv::Mat result; // segmentation result (4 possible values) |
||
114 | cv::Mat bgModel,fgModel; // the models (internally used) |
||
115 | // GrabCut segmentation |
||
116 | cv::grabCut(image, // input image |
||
117 | result, // segmentation result |
||
118 | rectangle,// rectangle containing foreground |
||
119 | bgModel,fgModel, // models |
||
120 | 1, // number of iterations |
||
121 | cv::GC_INIT_WITH_RECT); // use rectangle |
||
122 | |||
123 | // Get the pixels marked as likely foreground |
||
124 | cv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ); |
||
125 | // Generate output image |
||
126 | cv::Mat foreground(image.size(),CV_8UC3,cv::Scalar(255,255,255)); |
||
127 | image.copyTo(foreground,result); // bg pixels not copied |
||
128 | |||
129 | // draw rectangle on original image |
||
130 | cv::rectangle(image, rectangle, cv::Scalar(255,255,255),1); |
||
131 | cv::namedWindow("Image"); |
||
132 | cv::imshow("Image",image); |
||
133 | |||
134 | // display result |
||
135 | cv::namedWindow("Segmented Image"); |
||
136 | cv::imshow("Segmented Image",foreground); |
||
137 | |||
138 | // Open another image |
||
139 | image= cv::imread("../group.jpg"); |
||
140 | |||
141 | // define bounding rectangle |
||
142 | cv::Rect rectangle2(10,100,380,180); |
||
143 | |||
144 | cv::Mat bkgModel,fgrModel; // the models (internally used) |
||
145 | // GrabCut segmentation |
||
146 | cv::grabCut(image, // input image |
||
147 | result, // segmentation result |
||
148 | rectangle2,bkgModel,fgrModel,5,cv::GC_INIT_WITH_RECT); |
||
149 | // Get the pixels marked as likely foreground |
||
150 | // cv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ); |
||
151 | result= result&1; |
||
152 | foreground.create(image.size(),CV_8UC3); |
||
153 | foreground.setTo(cv::Scalar(255,255,255)); |
||
154 | image.copyTo(foreground,result); // bg pixels not copied |
||
155 | |||
156 | // draw rectangle on original image |
||
157 | cv::rectangle(image, rectangle2, cv::Scalar(255,255,255),1); |
||
158 | cv::namedWindow("Image 2"); |
||
159 | cv::imshow("Image 2",image); |
||
160 | |||
161 | // display result |
||
162 | cv::namedWindow("Foreground objects"); |
||
163 | cv::imshow("Foreground objects",foreground); |
||
164 | |||
165 | cv::waitKey(); |
||
166 | return 0; |
||
167 | } |