├── COPYING ├── Makefile ├── README ├── include └── graph-segment │ ├── convolve.h │ ├── disjoint-set.h │ ├── filter.h │ ├── image.h │ ├── imconv.h │ ├── imutil.h │ ├── misc.h │ ├── pnmfile.h │ ├── segment-graph.h │ └── segment-image.h └── segment.cpp /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | INCDIR = -I. 2 | DBG = -g 3 | OPT = -O3 4 | CPP = g++ 5 | CFLAGS = $(DBG) $(OPT) $(INCDIR) 6 | LINK = -lm 7 | 8 | .cpp.o: 9 | $(CPP) $(CFLAGS) -c $< -o $@ 10 | 11 | all: segment 12 | 13 | segment: segment.cpp segment-image.h segment-graph.h disjoint-set.h 14 | $(CPP) $(CFLAGS) -o segment segment.cpp $(LINK) 15 | 16 | clean: 17 | /bin/rm -f segment *.o 18 | 19 | clean-all: clean 20 | /bin/rm -f *~ 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | 2 | Implementation of the segmentation algorithm described in: 3 | 4 | Efficient Graph-Based Image Segmentation 5 | Pedro F. Felzenszwalb and Daniel P. Huttenlocher 6 | International Journal of Computer Vision, 59(2) September 2004. 7 | 8 | http://cs.brown.edu/~pff/segment/ 9 | 10 | The program takes a color image (PPM format) and produces a segmentation 11 | with a random color assigned to each region. 12 | 13 | 1) Type "make" to compile "segment". 14 | 15 | 2) Run "segment sigma k min input output". 16 | 17 | The parameters are: (see the paper for details) 18 | 19 | sigma: Used to smooth the input image before segmenting it. 20 | k: Value for the threshold function. 21 | min: Minimum component size enforced by post-processing. 22 | input: Input image. 23 | output: Output image. 24 | 25 | Typical parameters are sigma = 0.5, k = 500, min = 20. 26 | Larger values for k result in larger components in the result. 27 | 28 | -------------------------------------------------------------------------------- /include/graph-segment/convolve.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | /* convolution */ 20 | 21 | #ifndef CONVOLVE_H 22 | #define CONVOLVE_H 23 | 24 | #include 25 | #include 26 | #include 27 | #include "image.h" 28 | 29 | /* convolve src with mask. dst is flipped! */ 30 | static void convolve_even(image *src, image *dst, 31 | std::vector &mask) { 32 | int width = src->width(); 33 | int height = src->height(); 34 | int len = mask.size(); 35 | 36 | for (int y = 0; y < height; y++) { 37 | for (int x = 0; x < width; x++) { 38 | float sum = mask[0] * imRef(src, x, y); 39 | for (int i = 1; i < len; i++) { 40 | sum += mask[i] * 41 | (imRef(src, std::max(x-i,0), y) + 42 | imRef(src, std::min(x+i, width-1), y)); 43 | } 44 | imRef(dst, y, x) = sum; 45 | } 46 | } 47 | } 48 | 49 | /* convolve src with mask. dst is flipped! */ 50 | static void convolve_odd(image *src, image *dst, 51 | std::vector &mask) { 52 | int width = src->width(); 53 | int height = src->height(); 54 | int len = mask.size(); 55 | 56 | for (int y = 0; y < height; y++) { 57 | for (int x = 0; x < width; x++) { 58 | float sum = mask[0] * imRef(src, x, y); 59 | for (int i = 1; i < len; i++) { 60 | sum += mask[i] * 61 | (imRef(src, std::max(x-i,0), y) - 62 | imRef(src, std::min(x+i, width-1), y)); 63 | } 64 | imRef(dst, y, x) = sum; 65 | } 66 | } 67 | } 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /include/graph-segment/disjoint-set.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifndef DISJOINT_SET 20 | #define DISJOINT_SET 21 | 22 | // disjoint-set forests using union-by-rank and path compression (sort of). 23 | 24 | typedef struct { 25 | int rank; 26 | int p; 27 | int size; 28 | } uni_elt; 29 | 30 | class universe { 31 | public: 32 | universe(int elements); 33 | ~universe(); 34 | int find(int x); 35 | void join(int x, int y); 36 | int size(int x) const { return elts[x].size; } 37 | int num_sets() const { return num; } 38 | 39 | private: 40 | uni_elt *elts; 41 | int num; 42 | }; 43 | 44 | universe::universe(int elements) { 45 | elts = new uni_elt[elements]; 46 | num = elements; 47 | for (int i = 0; i < elements; i++) { 48 | elts[i].rank = 0; 49 | elts[i].size = 1; 50 | elts[i].p = i; 51 | } 52 | } 53 | 54 | universe::~universe() { 55 | delete [] elts; 56 | } 57 | 58 | int universe::find(int x) { 59 | int y = x; 60 | while (y != elts[y].p) 61 | y = elts[y].p; 62 | elts[x].p = y; 63 | return y; 64 | } 65 | 66 | void universe::join(int x, int y) { 67 | if (elts[x].rank > elts[y].rank) { 68 | elts[y].p = x; 69 | elts[x].size += elts[y].size; 70 | } else { 71 | elts[x].p = y; 72 | elts[y].size += elts[x].size; 73 | if (elts[x].rank == elts[y].rank) 74 | elts[y].rank++; 75 | } 76 | num--; 77 | } 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /include/graph-segment/filter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | /* simple filters */ 20 | 21 | #ifndef FILTER_H 22 | #define FILTER_H 23 | 24 | #include 25 | #include 26 | #include "image.h" 27 | #include "misc.h" 28 | #include "convolve.h" 29 | #include "imconv.h" 30 | 31 | #define WIDTH 4.0 32 | 33 | /* normalize mask so it integrates to one */ 34 | static void normalize(std::vector &mask) { 35 | int len = mask.size(); 36 | float sum = 0; 37 | for (int i = 1; i < len; i++) { 38 | sum += fabs(mask[i]); 39 | } 40 | sum = 2*sum + fabs(mask[0]); 41 | for (int i = 0; i < len; i++) { 42 | mask[i] /= sum; 43 | } 44 | } 45 | 46 | /* make filters */ 47 | #define MAKE_FILTER(name, fun) \ 48 | static std::vector make_ ## name (float sigma) { \ 49 | sigma = std::max(sigma, 0.01F); \ 50 | int len = (int)ceil(sigma * WIDTH) + 1; \ 51 | std::vector mask(len); \ 52 | for (int i = 0; i < len; i++) { \ 53 | mask[i] = fun; \ 54 | } \ 55 | return mask; \ 56 | } 57 | 58 | MAKE_FILTER(fgauss, exp(-0.5*square(i/sigma))); 59 | 60 | /* convolve image with gaussian filter */ 61 | static image *smooth(image *src, float sigma) { 62 | std::vector mask = make_fgauss(sigma); 63 | normalize(mask); 64 | 65 | image *tmp = new image(src->height(), src->width(), false); 66 | image *dst = new image(src->width(), src->height(), false); 67 | convolve_even(src, tmp, mask); 68 | convolve_even(tmp, dst, mask); 69 | 70 | delete tmp; 71 | return dst; 72 | } 73 | 74 | /* convolve image with gaussian filter */ 75 | image *smooth(image *src, float sigma) { 76 | image *tmp = imageUCHARtoFLOAT(src); 77 | image *dst = smooth(tmp, sigma); 78 | delete tmp; 79 | return dst; 80 | } 81 | 82 | /* compute laplacian */ 83 | static image *laplacian(image *src) { 84 | int width = src->width(); 85 | int height = src->height(); 86 | image *dst = new image(width, height); 87 | 88 | for (int y = 1; y < height-1; y++) { 89 | for (int x = 1; x < width-1; x++) { 90 | float d2x = imRef(src, x-1, y) + imRef(src, x+1, y) - 91 | 2*imRef(src, x, y); 92 | float d2y = imRef(src, x, y-1) + imRef(src, x, y+1) - 93 | 2*imRef(src, x, y); 94 | imRef(dst, x, y) = d2x + d2y; 95 | } 96 | } 97 | return dst; 98 | } 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /include/graph-segment/image.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | /* a simple image class */ 20 | 21 | #ifndef IMAGE_H 22 | #define IMAGE_H 23 | 24 | #include 25 | 26 | template 27 | class image { 28 | public: 29 | /* create an image */ 30 | image(const int width, const int height, const bool init = true); 31 | 32 | /* delete an image */ 33 | ~image(); 34 | 35 | /* init an image */ 36 | void init(const T &val); 37 | 38 | /* copy an image */ 39 | image *copy() const; 40 | 41 | /* get the width of an image. */ 42 | int width() const { return w; } 43 | 44 | /* get the height of an image. */ 45 | int height() const { return h; } 46 | 47 | /* image data. */ 48 | T *data; 49 | 50 | /* row pointers. */ 51 | T **access; 52 | 53 | private: 54 | int w, h; 55 | }; 56 | 57 | /* use imRef to access image data. */ 58 | #define imRef(im, x, y) (im->access[y][x]) 59 | 60 | /* use imPtr to get pointer to image data. */ 61 | #define imPtr(im, x, y) &(im->access[y][x]) 62 | 63 | template 64 | image::image(const int width, const int height, const bool init) { 65 | w = width; 66 | h = height; 67 | data = new T[w * h]; // allocate space for image data 68 | access = new T*[h]; // allocate space for row pointers 69 | 70 | // initialize row pointers 71 | for (int i = 0; i < h; i++) 72 | access[i] = data + (i * w); 73 | 74 | if (init) 75 | memset(data, 0, w * h * sizeof(T)); 76 | } 77 | 78 | template 79 | image::~image() { 80 | delete [] data; 81 | delete [] access; 82 | } 83 | 84 | template 85 | void image::init(const T &val) { 86 | T *ptr = imPtr(this, 0, 0); 87 | T *end = imPtr(this, w-1, h-1); 88 | while (ptr <= end) 89 | *ptr++ = val; 90 | } 91 | 92 | 93 | template 94 | image *image::copy() const { 95 | image *im = new image(w, h, false); 96 | memcpy(im->data, data, w * h * sizeof(T)); 97 | return im; 98 | } 99 | 100 | #endif 101 | 102 | -------------------------------------------------------------------------------- /include/graph-segment/imconv.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | /* image conversion */ 20 | 21 | #ifndef CONV_H 22 | #define CONV_H 23 | 24 | #include 25 | #include "image.h" 26 | #include "imutil.h" 27 | #include "misc.h" 28 | 29 | #define RED_WEIGHT 0.299 30 | #define GREEN_WEIGHT 0.587 31 | #define BLUE_WEIGHT 0.114 32 | 33 | static image *imageRGBtoGRAY(image *input) { 34 | int width = input->width(); 35 | int height = input->height(); 36 | image *output = new image(width, height, false); 37 | 38 | for (int y = 0; y < height; y++) { 39 | for (int x = 0; x < width; x++) { 40 | imRef(output, x, y) = (uchar) 41 | (imRef(input, x, y).r * RED_WEIGHT + 42 | imRef(input, x, y).g * GREEN_WEIGHT + 43 | imRef(input, x, y).b * BLUE_WEIGHT); 44 | } 45 | } 46 | return output; 47 | } 48 | 49 | static image *imageGRAYtoRGB(image *input) { 50 | int width = input->width(); 51 | int height = input->height(); 52 | image *output = new image(width, height, false); 53 | 54 | for (int y = 0; y < height; y++) { 55 | for (int x = 0; x < width; x++) { 56 | imRef(output, x, y).r = imRef(input, x, y); 57 | imRef(output, x, y).g = imRef(input, x, y); 58 | imRef(output, x, y).b = imRef(input, x, y); 59 | } 60 | } 61 | return output; 62 | } 63 | 64 | static image *imageUCHARtoFLOAT(image *input) { 65 | int width = input->width(); 66 | int height = input->height(); 67 | image *output = new image(width, height, false); 68 | 69 | for (int y = 0; y < height; y++) { 70 | for (int x = 0; x < width; x++) { 71 | imRef(output, x, y) = imRef(input, x, y); 72 | } 73 | } 74 | return output; 75 | } 76 | 77 | static image *imageINTtoFLOAT(image *input) { 78 | int width = input->width(); 79 | int height = input->height(); 80 | image *output = new image(width, height, false); 81 | 82 | for (int y = 0; y < height; y++) { 83 | for (int x = 0; x < width; x++) { 84 | imRef(output, x, y) = imRef(input, x, y); 85 | } 86 | } 87 | return output; 88 | } 89 | 90 | static image *imageFLOATtoUCHAR(image *input, 91 | float min, float max) { 92 | int width = input->width(); 93 | int height = input->height(); 94 | image *output = new image(width, height, false); 95 | 96 | if (max == min) 97 | return output; 98 | 99 | float scale = UCHAR_MAX / (max - min); 100 | for (int y = 0; y < height; y++) { 101 | for (int x = 0; x < width; x++) { 102 | uchar val = (uchar)((imRef(input, x, y) - min) * scale); 103 | imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX); 104 | } 105 | } 106 | return output; 107 | } 108 | 109 | static image *imageFLOATtoUCHAR(image *input) { 110 | float min, max; 111 | min_max(input, &min, &max); 112 | return imageFLOATtoUCHAR(input, min, max); 113 | } 114 | 115 | static image *imageUCHARtoLONG(image *input) { 116 | int width = input->width(); 117 | int height = input->height(); 118 | image *output = new image(width, height, false); 119 | 120 | for (int y = 0; y < height; y++) { 121 | for (int x = 0; x < width; x++) { 122 | imRef(output, x, y) = imRef(input, x, y); 123 | } 124 | } 125 | return output; 126 | } 127 | 128 | static image *imageLONGtoUCHAR(image *input, long min, long max) { 129 | int width = input->width(); 130 | int height = input->height(); 131 | image *output = new image(width, height, false); 132 | 133 | if (max == min) 134 | return output; 135 | 136 | float scale = UCHAR_MAX / (float)(max - min); 137 | for (int y = 0; y < height; y++) { 138 | for (int x = 0; x < width; x++) { 139 | uchar val = (uchar)((imRef(input, x, y) - min) * scale); 140 | imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX); 141 | } 142 | } 143 | return output; 144 | } 145 | 146 | static image *imageLONGtoUCHAR(image *input) { 147 | long min, max; 148 | min_max(input, &min, &max); 149 | return imageLONGtoUCHAR(input, min, max); 150 | } 151 | 152 | static image *imageSHORTtoUCHAR(image *input, 153 | short min, short max) { 154 | int width = input->width(); 155 | int height = input->height(); 156 | image *output = new image(width, height, false); 157 | 158 | if (max == min) 159 | return output; 160 | 161 | float scale = UCHAR_MAX / (float)(max - min); 162 | for (int y = 0; y < height; y++) { 163 | for (int x = 0; x < width; x++) { 164 | uchar val = (uchar)((imRef(input, x, y) - min) * scale); 165 | imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX); 166 | } 167 | } 168 | return output; 169 | } 170 | 171 | static image *imageSHORTtoUCHAR(image *input) { 172 | short min, max; 173 | min_max(input, &min, &max); 174 | return imageSHORTtoUCHAR(input, min, max); 175 | } 176 | 177 | #endif 178 | -------------------------------------------------------------------------------- /include/graph-segment/imutil.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | /* some image utilities */ 20 | 21 | #ifndef IMUTIL_H 22 | #define IMUTIL_H 23 | 24 | #include "image.h" 25 | #include "misc.h" 26 | 27 | /* compute minimum and maximum value in an image */ 28 | template 29 | void min_max(image *im, T *ret_min, T *ret_max) { 30 | int width = im->width(); 31 | int height = im->height(); 32 | 33 | T min = imRef(im, 0, 0); 34 | T max = imRef(im, 0, 0); 35 | for (int y = 0; y < height; y++) { 36 | for (int x = 0; x < width; x++) { 37 | T val = imRef(im, x, y); 38 | if (min > val) 39 | min = val; 40 | if (max < val) 41 | max = val; 42 | } 43 | } 44 | 45 | *ret_min = min; 46 | *ret_max = max; 47 | } 48 | 49 | /* threshold image */ 50 | template 51 | image *threshold(image *src, int t) { 52 | int width = src->width(); 53 | int height = src->height(); 54 | image *dst = new image(width, height); 55 | 56 | for (int y = 0; y < height; y++) { 57 | for (int x = 0; x < width; x++) { 58 | imRef(dst, x, y) = (imRef(src, x, y) >= t); 59 | } 60 | } 61 | 62 | return dst; 63 | } 64 | 65 | #endif 66 | 67 | -------------------------------------------------------------------------------- /include/graph-segment/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | /* random stuff */ 20 | 21 | #ifndef MISC_H 22 | #define MISC_H 23 | 24 | #include 25 | 26 | #ifndef M_PI 27 | #define M_PI 3.141592653589793 28 | #endif 29 | 30 | typedef unsigned char uchar; 31 | 32 | typedef struct { uchar r, g, b; } rgb; 33 | 34 | inline bool operator==(const rgb &a, const rgb &b) { 35 | return ((a.r == b.r) && (a.g == b.g) && (a.b == b.b)); 36 | } 37 | 38 | template 39 | inline T abs(const T &x) { return (x > 0 ? x : -x); }; 40 | 41 | template 42 | inline int sign(const T &x) { return (x >= 0 ? 1 : -1); }; 43 | 44 | template 45 | inline T square(const T &x) { return x*x; }; 46 | 47 | template 48 | inline T bound(const T &x, const T &min, const T &max) { 49 | return (x < min ? min : (x > max ? max : x)); 50 | } 51 | 52 | template 53 | inline bool check_bound(const T &x, const T&min, const T &max) { 54 | return ((x < min) || (x > max)); 55 | } 56 | 57 | inline int vlib_round(float x) { return (int)(x + 0.5F); } 58 | 59 | inline int vlib_round(double x) { return (int)(x + 0.5); } 60 | 61 | inline double gaussian(double val, double sigma) { 62 | return exp(-square(val/sigma)/2)/(sqrt(2*M_PI)*sigma); 63 | } 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /include/graph-segment/pnmfile.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | /* basic image I/O */ 20 | 21 | #ifndef PNM_FILE_H 22 | #define PNM_FILE_H 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "image.h" 29 | #include "misc.h" 30 | 31 | #define BUF_SIZE 256 32 | 33 | class pnm_error { }; 34 | 35 | static void read_packed(unsigned char *data, int size, std::ifstream &f) { 36 | unsigned char c = 0; 37 | 38 | int bitshift = -1; 39 | for (int pos = 0; pos < size; pos++) { 40 | if (bitshift == -1) { 41 | c = f.get(); 42 | bitshift = 7; 43 | } 44 | data[pos] = (c >> bitshift) & 1; 45 | bitshift--; 46 | } 47 | } 48 | 49 | static void write_packed(unsigned char *data, int size, std::ofstream &f) { 50 | unsigned char c = 0; 51 | 52 | int bitshift = 7; 53 | for (int pos = 0; pos < size; pos++) { 54 | c = c | (data[pos] << bitshift); 55 | bitshift--; 56 | if ((bitshift == -1) || (pos == size-1)) { 57 | f.put(c); 58 | bitshift = 7; 59 | c = 0; 60 | } 61 | } 62 | } 63 | 64 | /* read PNM field, skipping comments */ 65 | static void pnm_read(std::ifstream &file, char *buf) { 66 | char doc[BUF_SIZE]; 67 | char c; 68 | 69 | file >> c; 70 | while (c == '#') { 71 | file.getline(doc, BUF_SIZE); 72 | file >> c; 73 | } 74 | file.putback(c); 75 | 76 | file.width(BUF_SIZE); 77 | file >> buf; 78 | file.ignore(); 79 | } 80 | 81 | static image *loadPBM(const char *name) { 82 | char buf[BUF_SIZE]; 83 | 84 | /* read header */ 85 | std::ifstream file(name, std::ios::in | std::ios::binary); 86 | pnm_read(file, buf); 87 | if (strncmp(buf, "P4", 2)) 88 | throw pnm_error(); 89 | 90 | pnm_read(file, buf); 91 | int width = atoi(buf); 92 | pnm_read(file, buf); 93 | int height = atoi(buf); 94 | 95 | /* read data */ 96 | image *im = new image(width, height); 97 | for (int i = 0; i < height; i++) 98 | read_packed(imPtr(im, 0, i), width, file); 99 | 100 | return im; 101 | } 102 | 103 | static void savePBM(image *im, const char *name) { 104 | int width = im->width(); 105 | int height = im->height(); 106 | std::ofstream file(name, std::ios::out | std::ios::binary); 107 | 108 | file << "P4\n" << width << " " << height << "\n"; 109 | for (int i = 0; i < height; i++) 110 | write_packed(imPtr(im, 0, i), width, file); 111 | } 112 | 113 | static image *loadPGM(const char *name) { 114 | char buf[BUF_SIZE]; 115 | 116 | /* read header */ 117 | std::ifstream file(name, std::ios::in | std::ios::binary); 118 | pnm_read(file, buf); 119 | if (strncmp(buf, "P5", 2)) 120 | throw pnm_error(); 121 | 122 | pnm_read(file, buf); 123 | int width = atoi(buf); 124 | pnm_read(file, buf); 125 | int height = atoi(buf); 126 | 127 | pnm_read(file, buf); 128 | if (atoi(buf) > UCHAR_MAX) 129 | throw pnm_error(); 130 | 131 | /* read data */ 132 | image *im = new image(width, height); 133 | file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar)); 134 | 135 | return im; 136 | } 137 | 138 | static void savePGM(image *im, const char *name) { 139 | int width = im->width(); 140 | int height = im->height(); 141 | std::ofstream file(name, std::ios::out | std::ios::binary); 142 | 143 | file << "P5\n" << width << " " << height << "\n" << UCHAR_MAX << "\n"; 144 | file.write((char *)imPtr(im, 0, 0), width * height * sizeof(uchar)); 145 | } 146 | 147 | static image *loadPPM(const char *name) { 148 | char buf[BUF_SIZE], doc[BUF_SIZE]; 149 | 150 | /* read header */ 151 | std::ifstream file(name, std::ios::in | std::ios::binary); 152 | pnm_read(file, buf); 153 | if (strncmp(buf, "P6", 2)) 154 | throw pnm_error(); 155 | 156 | pnm_read(file, buf); 157 | int width = atoi(buf); 158 | pnm_read(file, buf); 159 | int height = atoi(buf); 160 | 161 | pnm_read(file, buf); 162 | if (atoi(buf) > UCHAR_MAX) 163 | throw pnm_error(); 164 | 165 | /* read data */ 166 | image *im = new image(width, height); 167 | file.read((char *)imPtr(im, 0, 0), width * height * sizeof(rgb)); 168 | 169 | return im; 170 | } 171 | 172 | static void savePPM(image *im, const char *name) { 173 | int width = im->width(); 174 | int height = im->height(); 175 | std::ofstream file(name, std::ios::out | std::ios::binary); 176 | 177 | file << "P6\n" << width << " " << height << "\n" << UCHAR_MAX << "\n"; 178 | file.write((char *)imPtr(im, 0, 0), width * height * sizeof(rgb)); 179 | } 180 | 181 | template 182 | void load_image(image **im, const char *name) { 183 | char buf[BUF_SIZE]; 184 | 185 | /* read header */ 186 | std::ifstream file(name, std::ios::in | std::ios::binary); 187 | pnm_read(file, buf); 188 | if (strncmp(buf, "VLIB", 9)) 189 | throw pnm_error(); 190 | 191 | pnm_read(file, buf); 192 | int width = atoi(buf); 193 | pnm_read(file, buf); 194 | int height = atoi(buf); 195 | 196 | /* read data */ 197 | *im = new image(width, height); 198 | file.read((char *)imPtr((*im), 0, 0), width * height * sizeof(T)); 199 | } 200 | 201 | template 202 | void save_image(image *im, const char *name) { 203 | int width = im->width(); 204 | int height = im->height(); 205 | std::ofstream file(name, std::ios::out | std::ios::binary); 206 | 207 | file << "VLIB\n" << width << " " << height << "\n"; 208 | file.write((char *)imPtr(im, 0, 0), width * height * sizeof(T)); 209 | } 210 | 211 | #endif 212 | -------------------------------------------------------------------------------- /include/graph-segment/segment-graph.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifndef SEGMENT_GRAPH 20 | #define SEGMENT_GRAPH 21 | 22 | #include 23 | #include 24 | #include "disjoint-set.h" 25 | 26 | // threshold function 27 | #define THRESHOLD(size, c) (c/size) 28 | 29 | typedef struct { 30 | float w; 31 | int a, b; 32 | } edge; 33 | 34 | bool operator<(const edge &a, const edge &b) { 35 | return a.w < b.w; 36 | } 37 | 38 | /* 39 | * Segment a graph 40 | * 41 | * Returns a disjoint-set forest representing the segmentation. 42 | * 43 | * num_vertices: number of vertices in graph. 44 | * num_edges: number of edges in graph 45 | * edges: array of edges. 46 | * c: constant for treshold function. 47 | */ 48 | universe *segment_graph(int num_vertices, int num_edges, edge *edges, 49 | float c) { 50 | // sort edges by weight 51 | std::sort(edges, edges + num_edges); 52 | 53 | // make a disjoint-set forest 54 | universe *u = new universe(num_vertices); 55 | 56 | // init thresholds 57 | float *threshold = new float[num_vertices]; 58 | for (int i = 0; i < num_vertices; i++) 59 | threshold[i] = THRESHOLD(1,c); 60 | 61 | // for each edge, in non-decreasing weight order... 62 | for (int i = 0; i < num_edges; i++) { 63 | edge *pedge = &edges[i]; 64 | 65 | // components conected by this edge 66 | int a = u->find(pedge->a); 67 | int b = u->find(pedge->b); 68 | if (a != b) { 69 | if ((pedge->w <= threshold[a]) && 70 | (pedge->w <= threshold[b])) { 71 | u->join(a, b); 72 | a = u->find(a); 73 | threshold[a] = pedge->w + THRESHOLD(u->size(a), c); 74 | } 75 | } 76 | } 77 | 78 | // free up 79 | delete threshold; 80 | return u; 81 | } 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /include/graph-segment/segment-image.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #ifndef SEGMENT_IMAGE 20 | #define SEGMENT_IMAGE 21 | 22 | #include 23 | #include "image.h" 24 | #include "misc.h" 25 | #include "filter.h" 26 | #include "segment-graph.h" 27 | 28 | // random color 29 | rgb random_rgb(){ 30 | rgb c; 31 | double r; 32 | 33 | c.r = (uchar)rand(); 34 | c.g = (uchar)rand(); 35 | c.b = (uchar)rand(); 36 | 37 | return c; 38 | } 39 | 40 | // dissimilarity measure between pixels 41 | static inline float diff(image *r, image *g, image *b, 42 | int x1, int y1, int x2, int y2) { 43 | return sqrt(square(imRef(r, x1, y1)-imRef(r, x2, y2)) + 44 | square(imRef(g, x1, y1)-imRef(g, x2, y2)) + 45 | square(imRef(b, x1, y1)-imRef(b, x2, y2))); 46 | } 47 | 48 | /* 49 | * Segment an image 50 | * 51 | * Returns a color image representing the segmentation. 52 | * 53 | * im: image to segment. 54 | * sigma: to smooth the image. 55 | * c: constant for treshold function. 56 | * min_size: minimum component size (enforced by post-processing stage). 57 | * num_ccs: number of connected components in the segmentation. 58 | */ 59 | image *segment_image(image *im, float sigma, float c, int min_size, 60 | int *num_ccs) { 61 | int width = im->width(); 62 | int height = im->height(); 63 | 64 | image *r = new image(width, height); 65 | image *g = new image(width, height); 66 | image *b = new image(width, height); 67 | 68 | // smooth each color channel 69 | for (int y = 0; y < height; y++) { 70 | for (int x = 0; x < width; x++) { 71 | imRef(r, x, y) = imRef(im, x, y).r; 72 | imRef(g, x, y) = imRef(im, x, y).g; 73 | imRef(b, x, y) = imRef(im, x, y).b; 74 | } 75 | } 76 | image *smooth_r = smooth(r, sigma); 77 | image *smooth_g = smooth(g, sigma); 78 | image *smooth_b = smooth(b, sigma); 79 | delete r; 80 | delete g; 81 | delete b; 82 | 83 | // build graph 84 | edge *edges = new edge[width*height*4]; 85 | int num = 0; 86 | for (int y = 0; y < height; y++) { 87 | for (int x = 0; x < width; x++) { 88 | if (x < width-1) { 89 | edges[num].a = y * width + x; 90 | edges[num].b = y * width + (x+1); 91 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y); 92 | num++; 93 | } 94 | 95 | if (y < height-1) { 96 | edges[num].a = y * width + x; 97 | edges[num].b = (y+1) * width + x; 98 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1); 99 | num++; 100 | } 101 | 102 | if ((x < width-1) && (y < height-1)) { 103 | edges[num].a = y * width + x; 104 | edges[num].b = (y+1) * width + (x+1); 105 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1); 106 | num++; 107 | } 108 | 109 | if ((x < width-1) && (y > 0)) { 110 | edges[num].a = y * width + x; 111 | edges[num].b = (y-1) * width + (x+1); 112 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1); 113 | num++; 114 | } 115 | } 116 | } 117 | delete smooth_r; 118 | delete smooth_g; 119 | delete smooth_b; 120 | 121 | // segment 122 | universe *u = segment_graph(width*height, num, edges, c); 123 | 124 | // post process small components 125 | for (int i = 0; i < num; i++) { 126 | int a = u->find(edges[i].a); 127 | int b = u->find(edges[i].b); 128 | if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size))) 129 | u->join(a, b); 130 | } 131 | delete [] edges; 132 | *num_ccs = u->num_sets(); 133 | 134 | image *output = new image(width, height); 135 | 136 | // pick random colors for each component 137 | rgb *colors = new rgb[width*height]; 138 | for (int i = 0; i < width*height; i++) 139 | colors[i] = random_rgb(); 140 | 141 | for (int y = 0; y < height; y++) { 142 | for (int x = 0; x < width; x++) { 143 | int comp = u->find(y * width + x); 144 | imRef(output, x, y) = colors[comp]; 145 | } 146 | } 147 | 148 | delete [] colors; 149 | delete u; 150 | 151 | return output; 152 | } 153 | 154 | #endif 155 | -------------------------------------------------------------------------------- /segment.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2006 Pedro Felzenszwalb 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 2 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program; if not, write to the Free Software 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 | */ 18 | 19 | #include 20 | #include 21 | #include "image.h" 22 | #include "misc.h" 23 | #include "pnmfile.h" 24 | #include "segment-image.h" 25 | 26 | int main(int argc, char **argv) { 27 | if (argc != 6) { 28 | fprintf(stderr, "usage: %s sigma k min input(ppm) output(ppm)\n", argv[0]); 29 | return 1; 30 | } 31 | 32 | float sigma = atof(argv[1]); 33 | float k = atof(argv[2]); 34 | int min_size = atoi(argv[3]); 35 | 36 | printf("loading input image.\n"); 37 | image *input = loadPPM(argv[4]); 38 | 39 | printf("processing\n"); 40 | int num_ccs; 41 | image *seg = segment_image(input, sigma, k, min_size, &num_ccs); 42 | savePPM(seg, argv[5]); 43 | 44 | printf("got %d components\n", num_ccs); 45 | printf("done! uff...thats hard work.\n"); 46 | 47 | return 0; 48 | } 49 | 50 | --------------------------------------------------------------------------------