├── .gitignore ├── 000015.jpg ├── BlobAverageBestOverlap.m ├── BlobBestOverlap.m ├── BlobStruct2HierarchicalGrouping.p ├── BlobStructColourHist.p ├── BlobStructTextureHist.p ├── BoxAverageBestOverlap.m ├── ChangeEdges.p ├── Dependencies ├── Blob2Image.p ├── Blob2Vector.p ├── BlobAddSizes.p ├── BlobAddTextureHists.p ├── Blobs2Boxes.p ├── BoxBestOverlap.m ├── BoxIntersection.m ├── BoxRemoveDuplicates.m ├── BoxSize.m ├── BoxUnion.m ├── CountVisualWordsIndex.m ├── FelzenSegment │ ├── COPYING │ ├── Makefile │ ├── README │ ├── convolve.h │ ├── disjoint-set.h │ ├── filter.h │ ├── image.h │ ├── imconv.h │ ├── imutil.h │ ├── mexFelzenSegmentIndex.cpp │ ├── misc.h │ ├── pnmfile.h │ ├── segment-graph.h │ ├── segment-image.h │ └── segment.cpp ├── FilterBoxesWidth.m ├── GetPascalOverlap.m ├── Image2ColourSpace.p ├── Image2OrientedGradients.p ├── NormalizeArray.m ├── NormalizeRows.m ├── PascalOverlap.m ├── Rgb2C.p ├── Rgb2Ooo.p ├── Rgb2Rg.p ├── Rgb2Rgi.p ├── SegmentIndices2Blobs.p ├── ShowBlobs.m ├── ShowImageCell.m ├── ShowRectsWithinImage.m ├── Vector2Hist.p ├── anigaussm │ ├── anigauss.c │ ├── anigauss.m │ └── anigauss_mex.c ├── gaussianFilter.p └── mexCountWordsIndex.cpp ├── GroundTruthVOC2007test.mat ├── Image2HierarchicalGrouping.m ├── License.txt ├── MergeBlobs.p ├── README.md ├── RecreateBlobHierarchy.m ├── RecreateBlobHierarchyIndIm.m ├── SSSimBoxFill.p ├── SSSimBoxFillOrig.p ├── SSSimBoxFillOrigSize.p ├── SSSimBoxFillSize.p ├── SSSimColour.p ├── SSSimColourSize.p ├── SSSimColourTextureSizeFill.p ├── SSSimColourTextureSizeFillOrig.p ├── SSSimSize.m ├── SSSimTexture.p ├── SSSimTextureSize.p ├── SSSimTextureSizeFill.p ├── __init__.py ├── cat.jpg ├── demo.m ├── demoPascal2007.m ├── selective_search.m ├── selective_search.py └── selective_search_rcnn.m /.gitignore: -------------------------------------------------------------------------------- 1 | *.mex* 2 | -------------------------------------------------------------------------------- /000015.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/000015.jpg -------------------------------------------------------------------------------- /BlobAverageBestOverlap.m: -------------------------------------------------------------------------------- 1 | function [abo mabo boScores avgNumSegments] = BlobAverageBestOverlap(gtBlobs, gtNrs, blobIndIm, blobBoxes, hierarchy, minWidth) 2 | % [abo mabo boScores avgNumSegments] = BlobAverageBestOverlap(gtBlobs, 3 | % gtNrs, blobIndIm, blobBoxes, hierarchy, minWidth) 4 | % 5 | % Calculate Average Best Overlap scores 6 | % 7 | % gtBlobs: Cell array of ground truth segments per class (see 8 | % GetAllObjectBoxes) 9 | % gtNrs: Cell array with image nrs corresponding to ground truth. 10 | % blobIndIm: Image with indices per blob (mexFelzenSegmentIndex) 11 | % blobBoxes: Boxes corresponding to blobs in blobIndIm 12 | % hierarchy: Hierarchy necessary to reconstruct all blobs in grouping 13 | % minWidth: (optional) Filter out blobs with a width smaller than minWidth. 14 | % 15 | % abo: Average Best Overlap per class (Pascal Overlap criterion) 16 | % mabo: Mean Average Best Overlap (mean(abo)) 17 | % boScores: Best Overlap Score per GT segment. 18 | % avgNumBlobs: Average number of blobs per image 19 | % 20 | % Jasper Uijlings - 2013 21 | 22 | if ~exist('minWidth', 'var') 23 | minWidth = 0; 24 | end 25 | 26 | nClasses = length(gtBlobs); 27 | 28 | % Memory initialization 29 | numSegments = zeros(length(blobIndIm), 1); 30 | boScores = cell(1, nClasses); 31 | for cI = 1:nClasses 32 | boScores{cI} = length(gtBlobs{cI}); 33 | end 34 | 35 | % indices per class 36 | classIdx = ones(1, nClasses); 37 | 38 | for cI=1:length(gtBlobs) 39 | for i=1:length(gtBlobs{cI}) 40 | testImNr = gtNrs{cI}(i); 41 | 42 | % the hierarchy here contains possibly multiple groupings with 43 | % different initial measures 44 | testBlobsT = cell(length(hierarchy{testImNr}), 1); 45 | testBlobsT{1} = RecreateBlobHierarchyIndIm(blobIndIm{testImNr}, blobBoxes{testImNr}, hierarchy{testImNr}{1}); 46 | for j=2:length(hierarchy{testImNr}) % Without initial blobs here 47 | [aa bb testBlobsT{j}] = RecreateBlobHierarchyIndIm(blobIndIm{testImNr}, blobBoxes{testImNr}, hierarchy{testImNr}{j}); 48 | end 49 | testBlobs = cat(1, testBlobsT{:}); 50 | 51 | % Get rid of too small blobs 52 | testBlobs = FilterBlobsWidth(testBlobs, minWidth); 53 | numSegments(testImNr) = length(testBlobs); 54 | 55 | % Calculate overlap scores 56 | boScores{cI}(classIdx(cI)) = BlobBestOverlap(testBlobs, gtBlobs{cI}(i)); 57 | 58 | classIdx(cI) = classIdx(cI) + 1; 59 | end 60 | end 61 | 62 | abo = zeros(nClasses, 1); 63 | 64 | for cI = 1:nClasses 65 | abo(cI) = mean(boScores{cI}); 66 | end 67 | 68 | mabo = mean(abo); 69 | 70 | % Average of numSegments. Make sure that only images for which the 71 | % numSegments are actually calculated are taken into account. 72 | avgNumSegments = mean(numSegments(numSegments > 0)); 73 | -------------------------------------------------------------------------------- /BlobBestOverlap.m: -------------------------------------------------------------------------------- 1 | function [scores index] = BlobBestOverlap(gtBlobs, testBlobs) 2 | % [scores index] = BlobBestOverlap(gtBlobs, testBlobs) 3 | % 4 | % Get overlap scores (Pascal-wise) for test blobs 5 | % 6 | % groundTruthBlob: ground truth blobs 7 | % test: Test blobs 8 | % 9 | % scores: Highest overlap scores for each test blob. 10 | % index: Index for each test blob which ground truth blob 11 | % is best 12 | % 13 | % Jasper Uijlings - 2013 14 | 15 | numTarget = length(gtBlobs); 16 | numTest = length(testBlobs); 17 | 18 | scoreM = zeros(numTest, numTarget); 19 | 20 | for i=1:numTest 21 | for j=1:numTarget 22 | scoreM(i,j) = PascalOverlapBlob(gtBlobs{j}, testBlobs{i}); 23 | end 24 | end 25 | 26 | [scores index] = max(scoreM, [], 2); 27 | 28 | -------------------------------------------------------------------------------- /BlobStruct2HierarchicalGrouping.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/BlobStruct2HierarchicalGrouping.p -------------------------------------------------------------------------------- /BlobStructColourHist.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/BlobStructColourHist.p -------------------------------------------------------------------------------- /BlobStructTextureHist.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/BlobStructTextureHist.p -------------------------------------------------------------------------------- /BoxAverageBestOverlap.m: -------------------------------------------------------------------------------- 1 | function [abo mabo boScores avgNumBoxes] = BoxAverageBestOverlap(gtBoxes, gtNrs, testBoxes) 2 | % [abo mabo boScores avgNumBoxes] = BoxAverageBestOverlap(gtBoxes, gtNrs, testBoxes) 3 | % 4 | % Calculate Average Best Overlap scores 5 | % 6 | % gtBoxes: Cell array of ground truth boxes per class (see 7 | % GetAllObjectBoxes) 8 | % gtNrs: Cell array with image nrs corresponding to ground truth. 9 | % testBoxes: Cell array of testboxes per image. 10 | % 11 | % abo: Average Best Overlap per class (Pascal Overlap criterion) 12 | % mabo: Mean Average Best Overlap (mean(abo)) 13 | % boScores: Best Overlap Score per GT box. 14 | % avgNumBoxes: Average number of boxes per image 15 | % 16 | % Jasper Uijlings - 2013 17 | 18 | % Check nr of gt elements 19 | nClasses = length(gtBoxes); 20 | 21 | boScores = cell(1, nClasses); 22 | for cI = 1:nClasses 23 | boScores{cI} = zeros(size(gtBoxes{cI}, 1),1); 24 | end 25 | 26 | % indices per class 27 | classIdx = ones(1, nClasses); 28 | 29 | for cI = 1:length(gtBoxes) 30 | for i = 1:size(gtBoxes{cI}, 1) 31 | boScores{cI}(classIdx(cI)) = ... 32 | BoxBestOverlap(gtBoxes{cI}(i,:), testBoxes{gtNrs{cI}(i)}); 33 | classIdx(cI) = classIdx(cI) + 1; 34 | end 35 | end 36 | 37 | % Calculation abo and mabo measures 38 | abo = zeros(nClasses, 1); 39 | for cI = 1:nClasses 40 | abo(cI) = mean(boScores{cI}); 41 | end 42 | mabo = mean(abo); 43 | 44 | % Calculation avgNumBoxes 45 | numBoxes = zeros(length(testBoxes), 1); 46 | for i=1:length(testBoxes) 47 | numBoxes(i) = size(testBoxes{i}, 1); 48 | end 49 | avgNumBoxes = mean(numBoxes); -------------------------------------------------------------------------------- /ChangeEdges.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/ChangeEdges.p -------------------------------------------------------------------------------- /Dependencies/Blob2Image.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Blob2Image.p -------------------------------------------------------------------------------- /Dependencies/Blob2Vector.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Blob2Vector.p -------------------------------------------------------------------------------- /Dependencies/BlobAddSizes.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/BlobAddSizes.p -------------------------------------------------------------------------------- /Dependencies/BlobAddTextureHists.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/BlobAddTextureHists.p -------------------------------------------------------------------------------- /Dependencies/Blobs2Boxes.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Blobs2Boxes.p -------------------------------------------------------------------------------- /Dependencies/BoxBestOverlap.m: -------------------------------------------------------------------------------- 1 | function [scores index] = BoxBestOverlap(gtBoxes, testBoxes) 2 | % [scores index] = BoxBestOverlap(gtBox, testBoxes) 3 | % 4 | % Get overlap scores (Pascal-wise) for testBoxes bounding boxes 5 | % 6 | % gtBoxes: Ground truth bounding boxes 7 | % testBoxes: Test bounding boxes 8 | % 9 | % scores: Highest overlap scores for each testBoxes bbox. 10 | % index: Index for each testBoxes box which ground truth box is best 11 | % 12 | % Jasper Uijlings - 2013 13 | 14 | numGT = size(gtBoxes,1); 15 | numTest = size(testBoxes,1); 16 | 17 | scoreM = zeros(numGT, numTest); 18 | 19 | 20 | for j=1:numGT 21 | scoreM(j,:) = PascalOverlap(gtBoxes(j,:), testBoxes); 22 | end 23 | 24 | 25 | [scores index] = max(scoreM, [], 2); 26 | 27 | 28 | -------------------------------------------------------------------------------- /Dependencies/BoxIntersection.m: -------------------------------------------------------------------------------- 1 | function intersection = BoxIntersection(a, b) 2 | % intersection = BoxIntersection(a, b) 3 | % 4 | % Creates the intersection of two bounding boxes. Returns minus ones if 5 | % there is no intersection 6 | % 7 | % a: Input bonding box "a" 8 | % b: Input bounding box "b" 9 | % 10 | % intersection: Intersection of box a and b 11 | % 12 | % Jasper Uijlings - 2013 13 | 14 | intersection = [max(a(:,1),b(:,1)) max(a(:,2),b(:,2)) ... 15 | min(a(:,3),b(:,3)) min(a(:,4),b(:,4))]; 16 | 17 | [numRows numColumns] = BoxSize(intersection); 18 | 19 | % There is no intersection box 20 | negIds = numRows < 1 | numColumns < 1; 21 | intersection(negIds,:) = -1; 22 | 23 | 24 | -------------------------------------------------------------------------------- /Dependencies/BoxRemoveDuplicates.m: -------------------------------------------------------------------------------- 1 | function [boxesOut uniqueIdx] = BoxRemoveDuplicates(boxesIn) 2 | % function boxOut = BoxRemoveDuplicates(boxIn) 3 | % 4 | % Removes duplicate boxes. Leaves the boxes in the same order 5 | % Keeps the first box of each kind. 6 | % 7 | % boxesIn: N x 4 array containing boxes 8 | % 9 | % boxexOut: M x 4 array of boxes witout duplicates. M <= N 10 | % uniqueIdx: Indices of retained boxes from boxesIn 11 | % 12 | % Jasper Uijlings - 2013 13 | 14 | [dummy uniqueIdx] = unique(boxesIn, 'rows', 'first'); 15 | uniqueIdx = sort(uniqueIdx); 16 | boxesOut = boxesIn(uniqueIdx,:); 17 | -------------------------------------------------------------------------------- /Dependencies/BoxSize.m: -------------------------------------------------------------------------------- 1 | function [numRows numColumns area] = BoxSize(bbox) 2 | % [numRows numColumns Surface] = BoxSize(bbox) 3 | % 4 | % Retrieves number of rows, columns, and surface area from bounding box 5 | % 6 | % bbox: 4 x N Bounding box as [rowBegin colBegin rowEnd colEnd] 7 | % 8 | % numRows: Number of rows of boxes 9 | % numColumns: Number of columns of boxes 10 | % area: Area of boxes 11 | % 12 | % Jasper Uijlings - 2013 13 | 14 | % Box is empty 15 | if isempty(bbox) 16 | numRows = 0; 17 | numColumns = 0; 18 | area = 0; 19 | return 20 | end 21 | 22 | numRows = bbox(:,3) - bbox(:,1) + 1; 23 | numColumns = bbox(:,4) - bbox(:,2) + 1; 24 | area = numRows .* numColumns; 25 | 26 | -------------------------------------------------------------------------------- /Dependencies/BoxUnion.m: -------------------------------------------------------------------------------- 1 | function union = BoxUnion(a, b) 2 | % union = BoxUnion(a, b) 3 | % 4 | % Creates the union box of two bounding boxes. 5 | % 6 | % a: Input bonding box "a" 7 | % b: Input bounding box "b" 8 | % 9 | % union: Intersection of box a and b 10 | % 11 | % Jasper Uijlings - 2013 12 | 13 | union = [min(a(:,1),b(:,1)) min(a(:,2),b(:,2)) ... 14 | max(a(:,3),b(:,3)) max(a(:,4),b(:,4))]; 15 | -------------------------------------------------------------------------------- /Dependencies/CountVisualWordsIndex.m: -------------------------------------------------------------------------------- 1 | function [cb counts] = CountVisualWordsIndex(indexIm, wordIm, numIndex, numWords) 2 | % cb = CountVisualWordsIndex(indexIm, wordIm, numIndex, numWords) 3 | % 4 | % Counts the number of visual words for the visual words in wordIm. 5 | % wordIm is an array with visual word identities. Zeros will be ignored. 6 | % indexIm is an array with regions to which visual words belong. 7 | % 8 | % WARNING: VERY FEW CHECKS FOR INTEGRETY. WRONG INPUT WILL CRASH THE SYSTEM 9 | % 10 | % indexIm: Array with indices. Range: [1,numIndex] 11 | % wordIm: Array with visual word identities. Range: [0,numWords] 12 | % numIndex: Number of indices in indexIm. 13 | % numWords: Number of visual words. 14 | % 15 | % cb: numIndex x numWords array with histogram counts 16 | % counts: numIndex x 1 array with counts per row of cb. 17 | % 18 | % Jasper Uijlings - 2013 19 | 20 | if size(indexIm,1) ~= size(wordIm,1) | size(indexIm,2) ~= size(wordIm,2) 21 | error('First two input arguments should have the same 2D dimension'); 22 | end 23 | 24 | wordIm = double(wordIm); 25 | 26 | [cb counts] = mexCountWordsIndex(indexIm, wordIm, numIndex, numWords); 27 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | The program takes a color image (PPM format) and produces a segmentation 9 | with a random color assigned to each region. 10 | 11 | 1) Type "make" to compile "segment". 12 | 13 | 2) Run "segment sigma k min input output". 14 | 15 | The parameters are: (see the paper for details) 16 | 17 | sigma: Used to smooth the input image before segmenting it. 18 | k: Value for the threshold function. 19 | min: Minimum component size enforced by post-processing. 20 | input: Input image. 21 | output: Output image. 22 | 23 | Typical parameters are sigma = 0.5, k = 500, min = 20. 24 | Larger values for k result in larger components in the result. 25 | 26 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/mexFelzenSegmentIndex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mex.h" 3 | #include "segment-image.h" 4 | 5 | #define UInt8 char 6 | 7 | 8 | /* 9 | * Segment an image 10 | * 11 | * Matlab Wrapper around the code of Felzenszwalb and Huttenlocher created 12 | * by Jasper Uijlings, 2012 13 | * 14 | * Returns a color image representing the segmentation. 15 | * JASPER: Random is replaced by just an index. 16 | * 17 | * im: image to segment. 18 | * sigma: to smooth the image. 19 | * c: constant for treshold function. 20 | * min_size: minimum component size (enforced by post-processing stage). 21 | * num_ccs: number of connected components in the segmentation. 22 | */ 23 | double *segment_image_index(image *im, float sigma, float c, int min_size, 24 | int *num_ccs) { 25 | int width = im->width(); 26 | int height = im->height(); 27 | 28 | image *r = new image(width, height); 29 | image *g = new image(width, height); 30 | image *b = new image(width, height); 31 | 32 | // smooth each color channel 33 | for (int y = 0; y < height; y++) { 34 | for (int x = 0; x < width; x++) { 35 | imRef(r, x, y) = imRef(im, x, y).r; 36 | imRef(g, x, y) = imRef(im, x, y).g; 37 | imRef(b, x, y) = imRef(im, x, y).b; 38 | } 39 | } 40 | image *smooth_r = smooth(r, sigma); 41 | image *smooth_g = smooth(g, sigma); 42 | image *smooth_b = smooth(b, sigma); 43 | delete r; 44 | delete g; 45 | delete b; 46 | 47 | // build graph 48 | edge *edges = new edge[width*height*4]; 49 | int num = 0; 50 | for (int y = 0; y < height; y++) { 51 | for (int x = 0; x < width; x++) { 52 | if (x < width-1) { 53 | edges[num].a = y * width + x; 54 | edges[num].b = y * width + (x+1); 55 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y); 56 | num++; 57 | } 58 | 59 | if (y < height-1) { 60 | edges[num].a = y * width + x; 61 | edges[num].b = (y+1) * width + x; 62 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x, y+1); 63 | num++; 64 | } 65 | 66 | if ((x < width-1) && (y < height-1)) { 67 | edges[num].a = y * width + x; 68 | edges[num].b = (y+1) * width + (x+1); 69 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y+1); 70 | num++; 71 | } 72 | 73 | if ((x < width-1) && (y > 0)) { 74 | edges[num].a = y * width + x; 75 | edges[num].b = (y-1) * width + (x+1); 76 | edges[num].w = diff(smooth_r, smooth_g, smooth_b, x, y, x+1, y-1); 77 | num++; 78 | } 79 | } 80 | } 81 | delete smooth_r; 82 | delete smooth_g; 83 | delete smooth_b; 84 | 85 | // segment 86 | universe *u = segment_graph(width*height, num, edges, c); 87 | 88 | // post process small components 89 | for (int i = 0; i < num; i++) { 90 | int a = u->find(edges[i].a); 91 | int b = u->find(edges[i].b); 92 | if ((a != b) && ((u->size(a) < min_size) || (u->size(b) < min_size))) 93 | u->join(a, b); 94 | } 95 | delete [] edges; 96 | *num_ccs = u->num_sets(); 97 | 98 | //image *output = new image(width, height); 99 | 100 | // pick random colors for each component 101 | double *colors = new double[width*height]; 102 | for (int i = 0; i < width*height; i++) 103 | colors[i] = 0; 104 | 105 | int idx = 1; 106 | double* indexmap = new double[width * height]; 107 | for (int y = 0; y < height; y++) { 108 | for (int x = 0; x < width; x++) { 109 | int comp = u->find(y * width + x); 110 | if (!(colors[comp])){ 111 | colors[comp] = idx; 112 | idx = idx + 1; 113 | } 114 | 115 | //imRef(output, x, y) = colors[comp]; 116 | indexmap[x * height + y] = colors[comp]; 117 | } 118 | } 119 | //mexPrintf("indexmap 0: %f\n", indexmap[0]); 120 | //mexPrintf("indexmap 1: %f\n", indexmap[1]); 121 | 122 | delete [] colors; 123 | delete u; 124 | 125 | return indexmap; 126 | } 127 | 128 | void mexFunction(int nlhs, mxArray *out[], int nrhs, const mxArray *input[]) 129 | { 130 | // Checking number of arguments 131 | if(nlhs > 3){ 132 | mexErrMsgTxt("Function has three return values"); 133 | return; 134 | } 135 | 136 | if(nrhs != 4){ 137 | mexErrMsgTxt("Usage: mexFelzenSegment(UINT8 im, double sigma, double c, int minSize)"); 138 | return; 139 | } 140 | 141 | if(!mxIsClass(input[0], "uint8")){ 142 | mexErrMsgTxt("Only image arrays of the UINT8 class are allowed."); 143 | return; 144 | } 145 | 146 | // Load in arrays and parameters 147 | UInt8* matIm = (UInt8*) mxGetPr(input[0]); 148 | int nrDims = (int) mxGetNumberOfDimensions(input[0]); 149 | int* dims = (int*) mxGetDimensions(input[0]); 150 | double* sigma = mxGetPr(input[1]); 151 | double* c = mxGetPr(input[2]); 152 | double* minSize = mxGetPr(input[3]); 153 | int min_size = (int) *minSize; 154 | 155 | int height = dims[0]; 156 | int width = dims[1]; 157 | int imSize = height * width; 158 | 159 | // Convert to image. 160 | int idx; 161 | image* theIm = new image(width, height); 162 | for (int x = 0; x < width; x++){ 163 | for (int y = 0; y < height; y++){ 164 | idx = x * height + y; 165 | imRef(theIm, x, y).r = matIm[idx]; 166 | imRef(theIm, x, y).g = matIm[idx + imSize]; 167 | imRef(theIm, x, y).b = matIm[idx + 2 * imSize]; 168 | } 169 | } 170 | 171 | // KOEN: Disable randomness of the algorithm 172 | srand(12345); 173 | 174 | // Call Felzenswalb segmentation algorithm 175 | int num_css; 176 | //image* segIm = segment_image(theIm, *sigma, *c, min_size, &num_css); 177 | double* segIndices = segment_image_index(theIm, *sigma, *c, min_size, &num_css); 178 | //mexPrintf("numCss: %d\n", num_css); 179 | 180 | // The segmentation index image 181 | out[0] = mxCreateDoubleMatrix(dims[0], dims[1], mxREAL); 182 | double* outSegInd = mxGetPr(out[0]); 183 | 184 | // Keep track of minimum and maximum of each blob 185 | out[1] = mxCreateDoubleMatrix(num_css, 4, mxREAL); 186 | double* minmax = mxGetPr(out[1]); 187 | for (int i=0; i < num_css; i++) 188 | minmax[i] = dims[0]; 189 | for (int i= num_css; i < 2 * num_css; i++) 190 | minmax[i] = dims[1]; 191 | 192 | // Keep track of neighbouring blobs using square matrix 193 | out[2] = mxCreateDoubleMatrix(num_css, num_css, mxREAL); 194 | double* nn = mxGetPr(out[2]); 195 | 196 | // Copy the contents of segIndices 197 | // Keep track of neighbours 198 | // Get minimum and maximum 199 | // These actually comprise of the bounding boxes 200 | double currDouble; 201 | int mprev, curr, prevHori, mcurr; 202 | for(int x = 0; x < width; x++){ 203 | mprev = segIndices[x * height]-1; 204 | for(int y=0; y < height; y++){ 205 | //mexPrintf("x: %d y: %d\n", x, y); 206 | idx = x * height + y; 207 | //mexPrintf("idx: %d\n", idx); 208 | //currDouble = segIndices[idx]; 209 | //mexPrintf("currDouble: %d\n", currDouble); 210 | curr = segIndices[idx]; 211 | //mexPrintf("curr: %d\n", curr); 212 | outSegInd[idx] = curr; // copy contents 213 | //mexPrintf("outSegInd: %f\n", outSegInd[idx]); 214 | mcurr = curr-1; 215 | 216 | // Get neighbours (vertical) 217 | //mexPrintf("idx: %d", curr * num_css + mprev); 218 | //mexPrintf(" %d\n", curr + num_css * mprev); 219 | //mexPrintf("mprev: %d\n", mprev); 220 | nn[(mcurr) * num_css + mprev] = 1; 221 | nn[(mcurr) + num_css * mprev] = 1; 222 | 223 | // Get horizontal neighbours 224 | //mexPrintf("Get horizontal neighbours\n"); 225 | if (x > 0){ 226 | prevHori = outSegInd[(x-1) * height + y] - 1; 227 | nn[mcurr * num_css + prevHori] = 1; 228 | nn[mcurr + num_css * prevHori] = 1; 229 | } 230 | 231 | // Keep track of min and maximum index of blobs 232 | //mexPrintf("Keep track of min and maximum index\n"); 233 | if (minmax[mcurr] > y) 234 | minmax[mcurr] = y; 235 | if (minmax[mcurr + num_css] > x) 236 | minmax[mcurr + num_css] = x; 237 | if (minmax[mcurr + 2 * num_css] < y) 238 | minmax[mcurr + 2 * num_css] = y; 239 | if (minmax[mcurr + 3 * num_css] < x) 240 | minmax[mcurr + 3 * num_css] = x; 241 | 242 | //mexPrintf("Mprev = mcurr"); 243 | mprev = mcurr; 244 | } 245 | } 246 | 247 | // Do minmax plus one for Matlab 248 | for (int i=0; i < 4 * num_css; i++) 249 | minmax[i] += 1; 250 | 251 | delete theIm; 252 | delete [] segIndices; 253 | 254 | return; 255 | } 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 | -------------------------------------------------------------------------------- /Dependencies/FelzenSegment/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 22 | #include 23 | #include 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 | -------------------------------------------------------------------------------- /Dependencies/FilterBoxesWidth.m: -------------------------------------------------------------------------------- 1 | function [outBoxes idsGood]= FilterBoxesWidth(inBoxes, minLen) 2 | % [outBoxes idsGood]= FilterBoxesWidth(inBoxes, minLen) 3 | % 4 | % Filters out small boxes. Boxes have to have a width and height 5 | % larger than minLen 6 | % 7 | % inBoxes: M x 4 array of boxes 8 | % minLen: Minimum width and height of boxes 9 | % 10 | % outBoxes: N x 4 array of boxes, N < M 11 | % idsGood: M x 1 logical array denoting boxes kept 12 | % 13 | % Jasper Uijlings - 2013 14 | 15 | [nr nc] = BoxSize(inBoxes); 16 | 17 | idsGood = (nr >= minLen) & (nc >= minLen); 18 | outBoxes = inBoxes(idsGood,:); -------------------------------------------------------------------------------- /Dependencies/GetPascalOverlap.m: -------------------------------------------------------------------------------- 1 | function score = GetPascalOverlap(bb, bbgt) 2 | % Directly copied from Pascal code 3 | % 4 | % Gets the overlap measure according to Pascal 5 | % 6 | % bb: Bounding Box 7 | % bbgt: Ground truth bounding box 8 | % 9 | % score: Score between 0 and 1. 1 is complete overlap. 10 | 11 | score = 0; 12 | 13 | % intersection bbox 14 | bi=[max(bb(1),bbgt(1)) ; max(bb(2),bbgt(2)) ; min(bb(3),bbgt(3)) ; min(bb(4),bbgt(4))]; 15 | iw=bi(3)-bi(1)+1; 16 | ih=bi(4)-bi(2)+1; 17 | if iw>0 & ih>0 % intersection should be non-zero 18 | % compute overlap as area of intersection / area of union 19 | ua=(bb(3)-bb(1)+1)*(bb(4)-bb(2)+1)+... 20 | (bbgt(3)-bbgt(1)+1)*(bbgt(4)-bbgt(2)+1)-... 21 | iw*ih; 22 | score=iw*ih/ua; 23 | end -------------------------------------------------------------------------------- /Dependencies/Image2ColourSpace.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Image2ColourSpace.p -------------------------------------------------------------------------------- /Dependencies/Image2OrientedGradients.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Image2OrientedGradients.p -------------------------------------------------------------------------------- /Dependencies/NormalizeArray.m: -------------------------------------------------------------------------------- 1 | function b = NormalizeArray(a) 2 | % Normalizes array a. This means that the minimum value will become 0 and 3 | % the maximum value 1. 4 | % 5 | % a: Input array. 6 | % 7 | % b: Normalized output array 8 | % 9 | % Jasper Uijlings - 2013 10 | 11 | minVal = min(a(:)); 12 | maxVal = max(a(:)); 13 | 14 | diffVal = maxVal - minVal; 15 | 16 | b = a - minVal; 17 | if diffVal ~= 0 18 | b = b ./ diffVal; 19 | end 20 | -------------------------------------------------------------------------------- /Dependencies/NormalizeRows.m: -------------------------------------------------------------------------------- 1 | function b = NormalizeRows(a, n) 2 | % Normalizes the rows of a. Makes sure there is no division by zero: b will 3 | % not contain any NaN entries. 4 | % 5 | % a: data with row vectors 6 | % n: The rows will sum to n. By default n = 1 7 | % 8 | % b: normalized data with row vecors. All rows sum to one except 9 | % the ones that are zero in the first place: these remain 10 | % zero. 11 | % 12 | % Jasper Uijlings - 2013 13 | 14 | % Get sums 15 | sumA = sum(a,2); 16 | 17 | % Make sure there is no division by zero 18 | sumA(sumA == 0) = 1; 19 | 20 | % Do the normalization 21 | if nargin == 1 22 | b = bsxfun(@rdivide, a, sumA); 23 | else 24 | b = bsxfun(@rdivide, a, sumA / n); 25 | end 26 | 27 | % Do the normalization 28 | % if nargin == 1 29 | % b = a ./ repmat(sumA, 1, size(a,2)); 30 | % else 31 | % b = a .* n ./ repmat(sumA, 1, size(a,2)); 32 | % end -------------------------------------------------------------------------------- /Dependencies/PascalOverlap.m: -------------------------------------------------------------------------------- 1 | function scores = PascalOverlap(targetBox, testBoxes) 2 | % scores = PascalOverlap(targetBox, testBoxes) 3 | % 4 | % Function obtains the pascal overlap scores between the targetBox and 5 | % all testBoxes 6 | % 7 | % targetBox: 1 x 4 array containing target box 8 | % testBoxes: N x 4 array containing test boxes 9 | % 10 | % scores: N x 1 array containing for each testBox the pascal 11 | % overlap score. 12 | % 13 | % Jasper Uijlings - 2013 14 | 15 | intersectBoxes = BoxIntersection(targetBox, testBoxes); 16 | overlapI = intersectBoxes(:,1) ~= -1; % Get which boxes overlap 17 | 18 | % Intersection size 19 | [nr nc intersectionSize] = BoxSize(intersectBoxes(overlapI,:)); 20 | 21 | % Union size 22 | [nr nc testBoxSize] = BoxSize(testBoxes(overlapI,:)); 23 | [nr nc targetBoxSize] = BoxSize(targetBox); 24 | unionSize = testBoxSize + targetBoxSize - intersectionSize; 25 | 26 | scores = zeros(size(testBoxes,1),1); 27 | scores(overlapI) = intersectionSize ./ unionSize; 28 | -------------------------------------------------------------------------------- /Dependencies/Rgb2C.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Rgb2C.p -------------------------------------------------------------------------------- /Dependencies/Rgb2Ooo.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Rgb2Ooo.p -------------------------------------------------------------------------------- /Dependencies/Rgb2Rg.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Rgb2Rg.p -------------------------------------------------------------------------------- /Dependencies/Rgb2Rgi.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Rgb2Rgi.p -------------------------------------------------------------------------------- /Dependencies/SegmentIndices2Blobs.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/SegmentIndices2Blobs.p -------------------------------------------------------------------------------- /Dependencies/ShowBlobs.m: -------------------------------------------------------------------------------- 1 | function ShowBlobs(blobs, numRow, numCol, image, minSize, imNames) 2 | % PlotBlobs(blobs, numRow, numCol, image, minSize) plots all blobs in numCol columns 3 | % 4 | % Jasper Uijlings - 2013 5 | 6 | if nargin == 4 7 | minSize = 0; 8 | end 9 | 10 | doNames = exist('imNames', 'var'); 11 | 12 | if doNames 13 | if ~iscell(imNames) 14 | imageNamesC = cell(size(imNames)); 15 | for i=1:length(imNames) 16 | imageNamesC{i} = sprintf('%g', imNames(i)); 17 | end 18 | imNames = imageNamesC; 19 | end 20 | end 21 | 22 | % Convert to images 23 | idx = 1; 24 | for i=1:length(blobs) 25 | if not(isfield(blobs{i}, 'size')) 26 | blobs{i}.size = sum(sum(blobs{i}.mask)); 27 | end 28 | if blobs{i}.size > minSize 29 | images{idx} = Blob2Image(blobs{i}, image); 30 | if doNames 31 | iiNames{idx} = imNames{i}; 32 | end 33 | idx = idx + 1; 34 | end 35 | end 36 | 37 | 38 | if doNames; 39 | ShowImageCell(images, numRow, numCol, '', iiNames); 40 | else 41 | ShowImageCell(images, numRow, numCol); 42 | end 43 | 44 | % totImages = idx - 1; 45 | % 46 | % numFigures = ceil(totImages / (numCol * numRow)) 47 | % 48 | % n = 1; 49 | % screenSize = get(0, 'ScreenSize'); 50 | % 51 | % for i=1:numFigures 52 | % figure('Position', [1, 1, screenSize(3)/2, screenSize(4)]); 53 | % clf; 54 | % for j = 1:numCol * numRow 55 | % if(n <= totImages) 56 | % subplot(numRow, numCol, j); 57 | % imshow(images{n}); 58 | % n = n + 1; 59 | % end 60 | % end 61 | % end 62 | -------------------------------------------------------------------------------- /Dependencies/ShowImageCell.m: -------------------------------------------------------------------------------- 1 | function ShowImageCell(imageCell, n, m, figurename, imageNames) 2 | % ShowImageCell(imageCell, n, m, figurename, imageNames) 3 | % 4 | % Generate a figure with thumbnails of the images in the imageCell. 5 | % 6 | % imageCell: Cell array with images which can be displayed 7 | % with imshow. 8 | % n: number of thumbnail rows per figure. 9 | % m: number of thumbnail columns per figure. 10 | % figurename: Name of the figures (optional). 11 | % 12 | % Jasper Uijlings - 2013 13 | 14 | totImages = length(imageCell); 15 | numFigures = ceil(totImages / (n * m)); 16 | 17 | if nargin < 4 18 | figurename = 'untitled'; 19 | end 20 | 21 | if nargin < 5 22 | imageNames = cell(length(imageCell)); 23 | end 24 | 25 | if ~iscell(imageNames) 26 | imageNamesC = cell(length(imageNames)); 27 | for i=1:length(imageNames) 28 | imageNamesC{i} = sprintf('%g', imageNames(i)); 29 | end 30 | imageNames = imageNamesC; 31 | end 32 | 33 | idx = 1; 34 | screenSize = get(0, 'ScreenSize'); 35 | 36 | for i=1:numFigures 37 | if ispc 38 | figure('Position', [1, 1, screenSize(3), screenSize(4)], 'Name', figurename); 39 | % figure('Position', [1 49 1920 946] , 'Name', figurename); 40 | else 41 | figure('Position', [1, 1, screenSize(3)/2, screenSize(4)], 'Name', figurename); 42 | end 43 | clf; 44 | for j = 1:n * m 45 | if(idx <= totImages) 46 | subplot(n, m, j); 47 | imshow(imageCell{idx}); 48 | xlabel(imageNames{idx}); 49 | idx = idx + 1; 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /Dependencies/ShowRectsWithinImage.m: -------------------------------------------------------------------------------- 1 | function ShowRectsWithinImage(rects, numRow, numCol, image, imageNames) 2 | % ShowRects(Rects, numRow, numCol, image) 3 | % 4 | % Shows only the rectangles of the image 5 | % 6 | % Jasper Uijlings - 2013 7 | 8 | if ~exist('imageNames', 'var') 9 | imageNames = cell(size(rects,1), 1); 10 | for i=1:size(rects,1) 11 | imageNames{i} = sprintf('%d', i); 12 | end 13 | end 14 | 15 | % Convert to images 16 | idx = 1; 17 | images = cell(size(rects,1),1); 18 | for i=1:size(rects,1) 19 | bbox = rects(i,:); 20 | images{idx} = image(bbox(1):bbox(3),bbox(2):bbox(4),:); 21 | idx = idx + 1; 22 | end 23 | 24 | ShowImageCell(images, numRow, numCol, 'rects', imageNames); -------------------------------------------------------------------------------- /Dependencies/Vector2Hist.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/Vector2Hist.p -------------------------------------------------------------------------------- /Dependencies/anigaussm/anigauss.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | 6 | #ifdef COPYRIGHT_NOTICE 7 | 8 | Copyright University of Amsterdam, 2002-2004. All rights reserved. 9 | 10 | Contact person: 11 | Jan-Mark Geusebroek (mark@science.uva.nl, http://www.science.uva.nl/~mark) 12 | Intelligent Systems Lab Amsterdam 13 | Informatics Institute, Faculty of Science, University of Amsterdam 14 | Kruislaan 403, 1098 SJ Amsterdam, The Netherlands. 15 | 16 | 17 | This software is being made available for individual research use only. 18 | Any commercial use or redistribution of this software requires a license from 19 | the University of Amsterdam. 20 | 21 | You may use this work subject to the following conditions: 22 | 23 | 1. This work is provided "as is" by the copyright holder, with 24 | absolutely no warranties of correctness, fitness, intellectual property 25 | ownership, or anything else whatsoever. You use the work 26 | entirely at your own risk. The copyright holder will not be liable for 27 | any legal damages whatsoever connected with the use of this work. 28 | 29 | 2. The copyright holder retain all copyright to the work. All copies of 30 | the work and all works derived from it must contain (1) this copyright 31 | notice, and (2) additional notices describing the content, dates and 32 | copyright holder of modifications or additions made to the work, if 33 | any, including distribution and use conditions and intellectual property 34 | claims. Derived works must be clearly distinguished from the original 35 | work, both by name and by the prominent inclusion of explicit 36 | descriptions of overlaps and differences. 37 | 38 | 3. The names and trademarks of the copyright holder may not be used in 39 | advertising or publicity related to this work without specific prior 40 | written permission. 41 | 42 | 4. In return for the free use of this work, you are requested, but not 43 | legally required, to do the following: 44 | 45 | - If you become aware of factors that may significantly affect other 46 | users of the work, for example major bugs or 47 | deficiencies or possible intellectual property issues, you are 48 | requested to report them to the copyright holder, if possible 49 | including redistributable fixes or workarounds. 50 | 51 | - If you use the work in scientific research or as part of a larger 52 | software system, you are requested to cite the use in any related 53 | publications or technical documentation. The work is based upon: 54 | 55 | J. M. Geusebroek, A. W. M. Smeulders, and J. van de Weijer. 56 | Fast anisotropic gauss filtering. IEEE Trans. Image Processing, 57 | vol. 12, no. 8, pp. 938-943, 2003. 58 | 59 | related work: 60 | 61 | I.T. Young and L.J. van Vliet. Recursive implementation 62 | of the Gaussian filter. Signal Processing, vol. 44, pp. 139-151, 1995. 63 | 64 | B. Triggs and M. Sdika. Boundary conditions for Young-van Vliet 65 | recursive filtering. IEEE Trans. Signal Processing, 66 | vol. 54, pp. 2365-2367, 2006. 67 | 68 | This copyright notice must be retained with all copies of the software, 69 | including any modified or derived versions. 70 | 71 | #endif /* COPYRIGHT_NOTICE */ 72 | 73 | 74 | #ifndef PI 75 | #ifdef M_PI 76 | #define PI M_PI 77 | #else 78 | #define PI 3.14159265358979323846 79 | #endif 80 | #endif 81 | 82 | 83 | /* define the input buffer type, e.g. "float" */ 84 | #define SRCTYPE double 85 | 86 | /* define the output buffer type, should be at least "float" */ 87 | #define DSTTYPE double 88 | 89 | 90 | /* the function prototypes */ 91 | void anigauss(SRCTYPE *input, DSTTYPE *output, int sizex, int sizey, 92 | double sigmav, double sigmau, double phi, int orderv, int orderu); 93 | void YvVfilterCoef(double sigma, double *filter); 94 | void TriggsM(double *filter, double *M); 95 | 96 | static void f_iir_xline_filter(SRCTYPE *src, DSTTYPE *dest, int sx, int sy, 97 | double *filter); 98 | static void f_iir_yline_filter(DSTTYPE *src, DSTTYPE *dest, int sx, int sy, 99 | double *filter); 100 | static void f_iir_tline_filter(DSTTYPE *src, DSTTYPE *dest, int sx, int sy, 101 | double *filter, double tanp); 102 | static void f_iir_derivative_filter(DSTTYPE *src, DSTTYPE *dest, int sx, int sy, 103 | double phi, int order); 104 | 105 | 106 | 107 | /* 108 | * the main function: 109 | * anigauss(inbuf, outbuf, bufwidth, bufheight, sigma_v, sigma_u, phi, 110 | * derivative_order_v, derivative_order_u); 111 | * 112 | * v-axis = short axis 113 | * u-axis = long axis 114 | * phi = orientation angle in degrees 115 | * 116 | * for example, anisotropic data smoothing: 117 | * anigauss(inptr, outptr, 512, 512, 3.0, 7.0, 30.0, 0, 0); 118 | * 119 | * or, anisotropic edge detection: 120 | * anigauss(inptr, outptr, 512, 512, 3.0, 7.0, 30.0, 1, 0); 121 | * 122 | * or, anisotropic line detection: 123 | * anigauss(inptr, outptr, 512, 512, 3.0, 7.0, 30.0, 2, 0); 124 | * 125 | * or, in-place anisotropic data smoothing: 126 | * anigauss(bufptr, bufptr, 512, 512, 3.0, 7.0, 30.0, 0, 0); 127 | * 128 | */ 129 | 130 | 131 | void anigauss(SRCTYPE *input, DSTTYPE *output, int sizex, int sizey, 132 | double sigmav, double sigmau, double phi, int orderv, int orderu) 133 | { 134 | double filter[7]; 135 | double sigmax, sigmay, tanp; 136 | double su2, sv2; 137 | double phirad; 138 | double a11, a21, a22; 139 | int i; 140 | 141 | su2 = sigmau*sigmau; 142 | sv2 = sigmav*sigmav; 143 | phirad = phi*PI/180.; 144 | 145 | a11 = cos(phirad)*cos(phirad)*su2 + sin(phirad)*sin(phirad)*sv2; 146 | a21 = cos(phirad)*sin(phirad)*(su2-sv2); 147 | a22 = cos(phirad)*cos(phirad)*sv2 + sin(phirad)*sin(phirad)*su2; 148 | 149 | sigmax = sqrt(a11-a21*a21/a22); 150 | tanp = a21/a22; 151 | sigmay = sqrt(a22); 152 | 153 | /* calculate filter coefficients of x-direction*/ 154 | YvVfilterCoef(sigmax, filter); 155 | 156 | /* filter in the x-direction */ 157 | f_iir_xline_filter(input,output,sizex,sizey,filter); 158 | 159 | /* calculate filter coefficients in tanp-direction */ 160 | YvVfilterCoef(sigmay, filter); 161 | 162 | if (tanp != 0.0) { 163 | /* filter in the tanp-direction */ 164 | f_iir_tline_filter(output,output,sizex,sizey,filter, tanp); 165 | } 166 | else { 167 | /* isotropic filter or anisotropic filter aligned with grid */ 168 | f_iir_yline_filter(output,output,sizex,sizey,filter); 169 | } 170 | 171 | /* do the derivative filter: [-1,0,1] rotated over phi */ 172 | for(i=0; i= 0; j--) { 304 | pix = sum * *(--dest) + b1*p1 + b2*p2 + b3*p3; 305 | *dest = pix; 306 | p3 = p2; p2 = p1; p1 = pix; 307 | } 308 | dest += sx; 309 | } 310 | } 311 | 312 | static void 313 | f_iir_yline_filter(DSTTYPE *src, DSTTYPE *dest, int sx, int sy, double *filter) 314 | { 315 | double *p0, *p1, *p2, *p3, *pswap; 316 | double *buf0, *buf1, *buf2, *buf3; 317 | double *uplusbuf; 318 | int i, j; 319 | double b1, b2, b3; 320 | double pix; 321 | double sum, sumsq; 322 | double uplus, vplus; 323 | double unp, unp1, unp2; 324 | double M[9]; 325 | 326 | sumsq = filter[3]; 327 | sum = sumsq*sumsq; 328 | 329 | uplusbuf = malloc(sx*sizeof(*uplusbuf)); 330 | 331 | buf0 = malloc(sx*sizeof(*buf0)); 332 | buf1 = malloc(sx*sizeof(*buf1)); 333 | buf2 = malloc(sx*sizeof(*buf2)); 334 | buf3 = malloc(sx*sizeof(*buf3)); 335 | 336 | p0 = buf0; p1 = buf1; p2 = buf2; p3 = buf3; 337 | 338 | /* causal filter */ 339 | b1 = filter[2]; b2 = filter[1]; b3 = filter[0]; 340 | 341 | /* border first line*/ 342 | for (j = 0; j < sx; j++) { 343 | pix = *src++/sumsq; 344 | p1[j] = pix; p2[j] = pix; p3[j] = pix; 345 | } 346 | /* calc last line for Triggs boundary condition */ 347 | src += (sy-2)*sx; 348 | for (j = 0; j < sx; j++) 349 | uplusbuf[j] = *src++/(1.0-b1-b2-b3); 350 | src -= sy*sx; 351 | 352 | for (i = 0; i < sy; i++) { 353 | for (j = 0; j < sx; j++) { 354 | pix = *src++ + b1*p1[j] + b2*p2[j] + b3*p3[j]; 355 | *dest++ = pix; 356 | p0[j] = pix; 357 | } 358 | 359 | /* shift history */ 360 | pswap = p3; p3 = p2; p2 = p1; p1 = p0; p0 = pswap; 361 | } 362 | 363 | 364 | /* anti-causal filter */ 365 | 366 | /* apply Triggs border condition */ 367 | b1 = filter[4]; b2 = filter[5]; b3 = filter[6]; 368 | TriggsM(filter, M); 369 | 370 | /* first line */ 371 | p0 = uplusbuf; 372 | for (j = sx-1; j >= 0; j--) { 373 | uplus = p0[j]; 374 | vplus = uplus/(1.0-b1-b2-b3); 375 | 376 | unp = p1[j]-uplus; 377 | unp1 = p2[j]-uplus; 378 | unp2 = p3[j]-uplus; 379 | pix = M[0]*unp+M[1]*unp1+M[2]*unp2 + vplus; 380 | pix *= sum; 381 | *(--dest) = pix; 382 | p1[j] = pix; 383 | pix = M[3]*unp+M[4]*unp1+M[5]*unp2 + vplus; 384 | p2[j] = pix*sum; 385 | pix = M[6]*unp+M[7]*unp1+M[8]*unp2 + vplus; 386 | p3[j] = pix*sum; 387 | } 388 | 389 | for (i = sy-2; i >= 0; i--) { 390 | for (j = sx-1; j >= 0; j--) { 391 | pix = sum * *(--dest) + b1*p1[j] + b2*p2[j] + b3*p3[j]; 392 | *dest = pix; 393 | p0[j] = pix; 394 | } 395 | 396 | /* shift history */ 397 | pswap = p3; p3 = p2; p2 = p1; p1 = p0; p0 = pswap; 398 | } 399 | 400 | free(buf0); 401 | free(buf1); 402 | free(buf2); 403 | free(buf3); 404 | free(uplusbuf); 405 | } 406 | 407 | 408 | static void 409 | f_iir_tline_filter(DSTTYPE *src, DSTTYPE *dest, int sx, int sy, 410 | double *filter, double tanp) 411 | { 412 | double *p0, *p1, *p2, *p3; 413 | double *buf0, *buf1, *buf2, *buf3; 414 | double *uplusbuf; 415 | int i, j; 416 | double b1, b2, b3; 417 | double sum, sumsq; 418 | double uplus, vplus; 419 | double unp, unp1, unp2; 420 | double M[9]; 421 | double pix, prev, val; 422 | double res, prevres; 423 | double xf; 424 | int x; 425 | double c, d; 426 | double e, f; 427 | int q4 = 0; 428 | 429 | /* check filter direction towards first or fourth quadrant */ 430 | if (tanp <= 0.0) { 431 | q4 = 1; 432 | tanp = -tanp; 433 | } 434 | 435 | /* alloc buffer for Triggs boundary condition */ 436 | uplusbuf = malloc(sx*sizeof(*uplusbuf)); 437 | 438 | /* alloc recursion line buffers */ 439 | buf0 = malloc((sx+sy*tanp+2)*sizeof(*buf0)); 440 | buf1 = malloc((sx+sy*tanp+2)*sizeof(*buf1)); 441 | buf2 = malloc((sx+sy*tanp+2)*sizeof(*buf2)); 442 | buf3 = malloc((sx+sy*tanp+2)*sizeof(*buf3)); 443 | 444 | if (q4) { 445 | buf0 += (int)(sy*tanp+1); 446 | buf1 += (int)(sy*tanp+1); 447 | buf2 += (int)(sy*tanp+1); 448 | buf3 += (int)(sy*tanp+1); 449 | } 450 | 451 | sumsq = filter[3]; 452 | sum = sumsq*sumsq; 453 | 454 | /* causal filter */ 455 | b1 = filter[2]; b2 = filter[1]; b3 = filter[0]; 456 | 457 | /* first line */ 458 | p1 = buf1; p2 = buf2; p3 = buf3; 459 | for (j = 0; j < sx; j++) { 460 | pix = *src++; 461 | *dest++ = pix; *p1++ = pix; *p2++ = pix; *p3++ = pix; 462 | } 463 | 464 | /* calc last line for Triggs boundary condition */ 465 | src += (sy-2)*sx; 466 | for (j = 0; j < sx; j++) 467 | uplusbuf[j] = *src++ * sumsq/(1.0-b1-b2-b3); 468 | src -= (sy-1)*sx; 469 | 470 | x = 0; 471 | for (i = 1; i < sy; i++) { 472 | xf = i*tanp; 473 | 474 | /* border handling at image corner */ 475 | if (q4) { 476 | p1 = buf1-x; p2 = buf2-x; p3 = buf3-x; 477 | for (j=1; j <= (int)(xf)-x; j++) { 478 | p1[-j] = p1[0]; 479 | p2[-j] = p2[0]; 480 | p3[-j] = p3[0]; 481 | } 482 | } 483 | else { 484 | p1 = buf1+x; p2 = buf2+x; p3 = buf3+x; 485 | for (j=1; j <= (int)(xf)-x; j++) { 486 | p1[sx+j-1] = p1[sx-1]; 487 | p2[sx+j-1] = p2[sx-1]; 488 | p3[sx+j-1] = p3[sx-1]; 489 | } 490 | } 491 | 492 | /* calc interpolation coefficients */ 493 | x = (int)xf; 494 | c = xf-(double)x; 495 | d = 1.0-c; 496 | 497 | e = c; f = d; 498 | if (!q4) { 499 | res = d; d = c; c = res; 500 | res = f; f = e; e = res; 501 | } 502 | 503 | c *= sumsq; d *= sumsq; 504 | 505 | /* set buffers at start */ 506 | if (q4) { 507 | p0 = buf0-x; p1 = buf1-x; p2 = buf2-x; p3 = buf3-x; 508 | } 509 | else { 510 | p0 = buf0+x; p1 = buf1+x; p2 = buf2+x; p3 = buf3+x; 511 | } 512 | prev = *src; 513 | prevres = sumsq*prev + b1 * *p1 + b2 * *p2 + b3 * *p3; 514 | 515 | /* run the filter */ 516 | for (j = 0; j < sx; j++) { 517 | pix = *src++; 518 | val = c*pix+d*prev; 519 | prev = pix; 520 | 521 | res = val + b1 * *p1++ + b2 * *p2++ + b3 * *p3++; 522 | *p0++ = res; 523 | *dest++ = f*res+e*prevres; 524 | prevres = res; 525 | } 526 | 527 | /* shift history */ 528 | p0 = buf3; buf3 = buf2; buf2 = buf1; buf1 = buf0; buf0 = p0; 529 | } 530 | 531 | /* anti-causal */ 532 | 533 | /* apply Triggs border condition */ 534 | b1 = filter[4]; b2 = filter[5]; b3 = filter[6]; 535 | TriggsM(filter, M); 536 | 537 | /* first line */ 538 | x = (int)((sy-1)*tanp); 539 | if (q4) { 540 | p1 = buf1+sx-x; p2 = buf2+sx-x; p3 = buf3+sx-x; 541 | } 542 | else { 543 | p1 = buf1+sx+x; p2 = buf2+sx+x; p3 = buf3+sx+x; 544 | } 545 | p0 = uplusbuf+sx; 546 | for (j = 0; j < sx; j++) { 547 | uplus = *(--p0); 548 | vplus = uplus/(1.0-b1-b2-b3); 549 | 550 | unp = *(--p1)-uplus; 551 | unp1 = *(--p2)-uplus; 552 | unp2 = *(--p3)-uplus; 553 | pix = M[0]*unp+M[1]*unp1+M[2]*unp2 + vplus; 554 | pix *= sumsq; 555 | *(--dest) = pix; *p1 = pix; 556 | pix = M[3]*unp+M[4]*unp1+M[5]*unp2 + vplus; 557 | *p2 = pix*sumsq; 558 | pix = M[6]*unp+M[7]*unp1+M[8]*unp2 + vplus; 559 | *p3 = pix*sumsq; 560 | } 561 | 562 | for (i = sy-2; i >= 0; i--) { 563 | xf = i*tanp; 564 | 565 | /* border handling at image corner */ 566 | if (q4) { 567 | p1 = buf1-x; p2 = buf2-x; p3 = buf3-x; 568 | for (j=1; j <= x-(int)(xf); j++) { 569 | p1[sx+j-1] = p1[sx-1]; 570 | p2[sx+j-1] = p2[sx-1]; 571 | p3[sx+j-1] = p3[sx-1]; 572 | } 573 | } 574 | else { 575 | p1 = buf1+x; p2 = buf2+x; p3 = buf3+x; 576 | for (j=1; j <= x-(int)(xf); j++) { 577 | p1[-j] = p1[0]; 578 | p2[-j] = p2[0]; 579 | p3[-j] = p3[0]; 580 | } 581 | } 582 | 583 | /* calc interpolation coefficients */ 584 | x = (int)xf; 585 | c = xf-(double)x; 586 | d = 1.0-c; 587 | 588 | e = c; f = d; 589 | c *= sumsq; d *= sumsq; 590 | 591 | if (!q4) { 592 | res = d; d = c; c = res; 593 | res = f; f = e; e = res; 594 | } 595 | 596 | /* set buffers at start */ 597 | if (q4) { 598 | p0 = buf0+sx-x; p1 = buf1+sx-x; p2 = buf2+sx-x; p3 = buf3+sx-x; 599 | } 600 | else { 601 | p0 = buf0+sx+x; p1 = buf1+sx+x; p2 = buf2+sx+x; p3 = buf3+sx+x; 602 | } 603 | prev = *(dest-1); 604 | prevres = sumsq*prev + b1 * *(p1-1) + b2 * *(p2-1) + b3 * *(p3-1); 605 | 606 | /* run the filter */ 607 | for (j = 0; j < sx; j++) { 608 | pix = *(--dest); 609 | val = d*pix+c*prev; 610 | prev = pix; 611 | 612 | res = val + b1 * *(--p1) + b2 * *(--p2) + b3 * *(--p3); 613 | *(--p0) = res; 614 | *dest = e*res+f*prevres; 615 | prevres = res; 616 | } 617 | 618 | /* shift history */ 619 | p0 = buf3; buf3 = buf2; buf2 = buf1; buf1 = buf0; buf0 = p0; 620 | } 621 | 622 | if (q4) { 623 | buf0 -= (int)(sy*tanp+1); 624 | buf1 -= (int)(sy*tanp+1); 625 | buf2 -= (int)(sy*tanp+1); 626 | buf3 -= (int)(sy*tanp+1); 627 | } 628 | free(buf0); 629 | free(buf1); 630 | free(buf2); 631 | free(buf3); 632 | free(uplusbuf); 633 | } 634 | 635 | /* rotated [-1,0,1] derivative filter */ 636 | static void 637 | f_iir_derivative_filter(DSTTYPE *src, DSTTYPE *dest, int sx, int sy, 638 | double phi, int order) 639 | { 640 | int i, j; 641 | DSTTYPE *prev, *center, *next; 642 | DSTTYPE *buf, *pstore; 643 | double pn, pc, pp; 644 | double cn, cc, cp; 645 | double nn, nc, np; 646 | double cosp, sinp; 647 | 648 | buf = malloc(sx*sizeof(*buf)); 649 | 650 | sinp = 0.5*sin(phi); cosp = 0.5*cos(phi); 651 | 652 | center = src; prev = src; next = src+sx; 653 | for (i = 0; i < sy; i++) { 654 | pstore = buf; 655 | pn = *prev++; cn = *center++; nn = *next++; 656 | pp = pn; pc = pn; 657 | cp = cn; cc = cn; 658 | np = pn; nc = nn; 659 | *pstore++ = cc; 660 | for (j = 1; j < sx; j++) { 661 | pn = *prev++; 662 | cn = *center++; 663 | nn = *next++; 664 | *dest++ = sinp*(pc-nc)+cosp*(cn-cp); 665 | pp = pc; pc = pn; 666 | cp = cc; cc = cn; 667 | np = pc; nc = nn; 668 | *pstore++ = cc; 669 | } 670 | *dest++ = sinp*(pc-nc)+cosp*(cn-cp); 671 | prev = buf; 672 | if (i==sy-2) 673 | next -= sx; 674 | } 675 | 676 | free(buf); 677 | } 678 | -------------------------------------------------------------------------------- /Dependencies/anigaussm/anigauss.m: -------------------------------------------------------------------------------- 1 | % anigauss - Recursive anisotropic Gauss filtering 2 | % Usage: 3 | % out = anigauss(in, sigma_v, sigma_u, phi, 4 | % derivative_order_v, derivative_order_u); 5 | % 6 | % v-axis = short axis 7 | % u-axis = long axis 8 | % phi = orientation angle in degrees 9 | % 10 | % parameters sigma_u, phi, and derivative_order_{v,w} are optional. 11 | % sigma_u defaults to the value of sigma_v (isotropic filtering), 12 | % phi defaults to zero degrees, 13 | % derivative orders default to 0 (no differentiation, only smooth data). 14 | % 15 | % Note that for isotropic filtering a slightly faster algorithm is used than 16 | % for anisotropic filtering. Furthermore, execution time depends on the order 17 | % of differentiation. Note that the execution time is independend of the 18 | % values for sigma. 19 | % 20 | % Examples: 21 | % 22 | % isotropic filtering: 23 | % a=zeros(512,512); 24 | % a(256,256)=1; 25 | % tic;c=anigauss(a,10);toc 26 | % elapsed_time = 27 | % 0.0500 28 | % 29 | % anisotropic filtering: 30 | % a=zeros(512,512); 31 | % a(256,256)=1; 32 | % tic;c=anigauss(a,10,3,30);toc 33 | % elapsed_time = 34 | % 0.0600 35 | % 36 | % Usage: 37 | % 38 | % isotropic data smoothing: 39 | % out = anigauss(in, 3.0); 40 | % 41 | % isotropic data differentiation along y-axis: 42 | % out = anigauss(in, 3.0, 3.0, 0.0, 0, 1); 43 | % 44 | % anisotropic data smoothing: 45 | % out = anigauss(in, 3.0, 7.0, 30.0); 46 | % 47 | % anisotropic edge detection: 48 | % out = anigauss(in, 3.0, 7.0, 30.0, 1, 0); 49 | % 50 | % anisotropic line detection: 51 | % out = anigauss(in, 3.0, 7.0, 30.0, 2, 0); 52 | % 53 | % 54 | % 55 | % Copyright University of Amsterdam, 2002-2004. All rights reserved. 56 | % 57 | % Contact person: 58 | % Jan-Mark Geusebroek (mark@science.uva.nl, http://www.science.uva.nl/~mark) 59 | % Intelligent Systems Lab Amsterdam 60 | % Informatics Institute, Faculty of Science, University of Amsterdam 61 | % Kruislaan 403, 1098 SJ Amsterdam, The Netherlands. 62 | % 63 | % 64 | % This software is being made available for individual research use only. 65 | % Any commercial use or redistribution of this software requires a license from 66 | % the University of Amsterdam. 67 | % 68 | % You may use this work subject to the following conditions: 69 | % 70 | % 1. This work is provided "as is" by the copyright holder, with 71 | % absolutely no warranties of correctness, fitness, intellectual property 72 | % ownership, or anything else whatsoever. You use the work 73 | % entirely at your own risk. The copyright holder will not be liable for 74 | % any legal damages whatsoever connected with the use of this work. 75 | % 76 | % 2. The copyright holder retain all copyright to the work. All copies of 77 | % the work and all works derived from it must contain (1) this copyright 78 | % notice, and (2) additional notices describing the content, dates and 79 | % copyright holder of modifications or additions made to the work, if 80 | % any, including distribution and use conditions and intellectual property 81 | % claims. Derived works must be clearly distinguished from the original 82 | % work, both by name and by the prominent inclusion of explicit 83 | % descriptions of overlaps and differences. 84 | % 85 | % 3. The names and trademarks of the copyright holder may not be used in 86 | % advertising or publicity related to this work without specific prior 87 | % written permission. 88 | % 89 | % 4. In return for the free use of this work, you are requested, but not 90 | % legally required, to do the following: 91 | % 92 | % - If you become aware of factors that may significantly affect other 93 | % users of the work, for example major bugs or 94 | % deficiencies or possible intellectual property issues, you are 95 | % requested to report them to the copyright holder, if possible 96 | % including redistributable fixes or workarounds. 97 | % 98 | % - If you use the work in scientific research or as part of a larger 99 | % software system, you are requested to cite the use in any related 100 | % publications or technical documentation. The work is based upon: 101 | % 102 | % J. M. Geusebroek, A. W. M. Smeulders, and J. van de Weijer. 103 | % Fast anisotropic gauss filtering. IEEE Trans. Image Processing, 104 | % vol. 12, no. 8, pp. 938-943, 2003. 105 | % 106 | % related work: 107 | % 108 | % I.T. Young and L.J. van Vliet. Recursive implementation 109 | % of the Gaussian filter. Signal Processing, vol. 44, pp. 139-151, 1995. 110 | % 111 | % B. Triggs and M. Sdika. Boundary conditions for Young-van Vliet 112 | % recursive filtering. IEEE Trans. Signal Processing, 113 | % vol. 54, pp. 2365-2367, 2006. 114 | % 115 | % This copyright notice must be retained with all copies of the software, 116 | % including any modified or derived versions. 117 | -------------------------------------------------------------------------------- /Dependencies/anigaussm/anigauss_mex.c: -------------------------------------------------------------------------------- 1 | /* 2 | The Matlab mex function. 3 | If necessary to recompile, type: 4 | mex -v -g anigauss_mex.c anigauss.c 5 | from within matlab. 6 | For windows platforms, you may want to use the provided "anigauss.dll" file. 7 | */ 8 | 9 | 10 | #include "mex.h" 11 | 12 | extern void anigauss(double *input, double *output, int sizex, int sizey, 13 | double sigmav, double sigmau, double phi, int orderv, int orderu); 14 | 15 | void mexFunction(int nlhs,mxArray *plhs[],int nrhs, const mxArray *prhs[]) 16 | { 17 | double *in, *out; 18 | double sigmav, sigmau, phi = 0.0; 19 | int orderv = 0, orderu = 0; 20 | int m, n; 21 | 22 | /* 23 | * Check the input arguments and the output argument 24 | */ 25 | if ((nrhs<2) || (nrhs>6) || (nrhs==5) || (nlhs!=1)) 26 | mexErrMsgTxt( 27 | "use: out = anigauss(in, sigmav, sigmau, phi, orderv, orderu);"); 28 | 29 | if ( mxGetNumberOfDimensions(prhs[0]) != 2 ) 30 | { mexErrMsgTxt("anigauss: input array should be of dimension 2"); } 31 | 32 | if (nrhs>=2) { 33 | in = mxGetPr(prhs[0]); 34 | sigmav = mxGetScalar(prhs[1]); 35 | sigmau = sigmav; 36 | } 37 | if (nrhs>=3) 38 | sigmau = mxGetScalar(prhs[2]); 39 | if (nrhs>=4) 40 | phi = mxGetScalar(prhs[3]); 41 | if (nrhs==6) { 42 | orderv = (int)(mxGetScalar(prhs[4])+0.5); 43 | orderu = (int)(mxGetScalar(prhs[5])+0.5); 44 | } 45 | 46 | if ((orderv<0) || (orderu<0)) 47 | { mexErrMsgTxt("anigauss: derivative orders should be positive"); } 48 | 49 | m = mxGetM(prhs[0]); 50 | n = mxGetN(prhs[0]); 51 | 52 | /* pointers to output array */ 53 | 54 | plhs[0]=mxCreateDoubleMatrix(m, n, mxREAL ); 55 | if ( plhs[0] == NULL ) 56 | { mexErrMsgTxt("No more memory for out array"); } 57 | out = (double *)mxGetPr( plhs[0] ); 58 | 59 | anigauss(in, out, m, n, sigmav, sigmau, phi-90.0, orderv, orderu); 60 | } 61 | -------------------------------------------------------------------------------- /Dependencies/gaussianFilter.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/Dependencies/gaussianFilter.p -------------------------------------------------------------------------------- /Dependencies/mexCountWordsIndex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mex.h" 3 | void mexFunction(int nlhs, mxArray *out[], int nrhs, const mxArray *input[]) 4 | { 5 | // Checking number of arguments 6 | if (nlhs > 2){ 7 | mexErrMsgTxt("Error: function has only two output parameters"); 8 | return; 9 | } 10 | 11 | if (nrhs != 4){ 12 | mexErrMsgTxt("Error: Needs exactly two four input parameters"); 13 | return; 14 | } 15 | 16 | int numWords = (int) mxGetScalar(input[3]); 17 | int numIndices = (int) mxGetScalar(input[2]); 18 | 19 | // Load in arrays 20 | double* indices = mxGetPr(input[0]); 21 | double* a = mxGetPr( input[1] ); 22 | int aNum = (int) mxGetNumberOfElements(input[1]); 23 | int totIndices = (int) mxGetNumberOfElements(input[0]); // number of elements. Not confuse with max 24 | int numLoops = aNum / totIndices; 25 | 26 | // Create output histogram 27 | out[0] = mxCreateDoubleMatrix(numIndices, numWords, mxREAL); 28 | double* histogram = mxGetPr(out[0]); 29 | //histogram = histogram - 1; 30 | 31 | out[1] = mxCreateDoubleMatrix(numIndices, 1, mxREAL); 32 | double* count = mxGetPr(out[1]); 33 | 34 | double* aP = a; 35 | int iPval; 36 | for(int j=0; j < numLoops; j++){ 37 | double* iP = indices; 38 | for(int i=0;i < totIndices; i++){ 39 | //mexPrintf("%d\n", i); 40 | if (*aP){ 41 | //(*(histogram + (((int) *aP) -1) * numIndices + ((int) *iP - 1)))++; 42 | //count++; 43 | iPval = ((int) *iP) -1; 44 | histogram[(((int) *aP) - 1) * numIndices + iPval]++; 45 | count[iPval]++; 46 | } 47 | 48 | //arrayI = (int) *aP; 49 | //histogram[arrayI]++; 50 | aP++; 51 | iP++; 52 | } 53 | } 54 | 55 | return; 56 | } 57 | -------------------------------------------------------------------------------- /GroundTruthVOC2007test.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/GroundTruthVOC2007test.mat -------------------------------------------------------------------------------- /Image2HierarchicalGrouping.m: -------------------------------------------------------------------------------- 1 | function [boxes blobIndIm blobBoxes hierarchy priority] = Image2HierarchicalGrouping(im, sigma, k, minSize, colourType, functionHandles) 2 | % function [boxes blobIndIm blobBoxes hierarchy] = Image2HierarchicalGrouping 3 | % (im, sigma, k, minSize, colourType, functionHandles) 4 | % 5 | % Creates hierarchical grouping from an image 6 | % 7 | % im: Image 8 | % sigma (= 0.8): Smoothing for initial segmentation (Felzenszwalb 2004) 9 | % k (= 100): Threshold for initial segmentation 10 | % minSize (= 100): Minimum size of segments for initial segmentation 11 | % colourType: ColourType in which to do grouping (see Image2ColourSpace) 12 | % functionHandles: Similarity functions which are called. Function 13 | % creates as many hierarchies as there are functionHandles 14 | % 15 | % boxes: N x 4 array with boxes of all hierarchical groupings 16 | % blobIndIm: Index image with the initial segmentation 17 | % blobBoxes: Boxes belonging to the indices in blobIndIm 18 | % hierarchy: M x 1 cell array with hierarchies. M = 19 | % length(functionHandles) 20 | % 21 | % Jasper Uijlings - 2013 22 | 23 | % Change colour space 24 | [colourIm imageToSegment] = Image2ColourSpace(im, colourType); 25 | 26 | % Get initial segmentation, boxes, and neighbouring blobs 27 | [blobIndIm blobBoxes neighbours] = mexFelzenSegmentIndex(imageToSegment, sigma, k, minSize); 28 | numBlobs = size(blobBoxes,1); 29 | 30 | % Skip hierarchical grouping if segmentation results in single region only 31 | if numBlobs == 1 32 | warning('Oversegmentation results in a single region only'); 33 | boxes = blobBoxes; 34 | hierarchy = []; 35 | priority = 1; % priority is legacy 36 | return; 37 | end 38 | 39 | %%% Calculate histograms and sizes as prerequisite for grouping procedure 40 | 41 | % Get colour histogram 42 | [colourHist blobSizes] = BlobStructColourHist(blobIndIm, colourIm); 43 | 44 | % Get texture histogram 45 | textureHist = BlobStructTextureHist(blobIndIm, colourIm); 46 | % textureHist = BlobStructTextureHistLBP(blobIndIm, colourIm); 47 | 48 | % Allocate memory for complete hierarchy. 49 | blobStruct.colourHist = zeros(size(colourHist,2), numBlobs * 2 - 1); 50 | blobStruct.textureHist = zeros(size(textureHist,2), numBlobs * 2 - 1); 51 | blobStruct.size = zeros(numBlobs * 2 -1, 1); 52 | blobStruct.boxes = zeros(numBlobs * 2 - 1, 4); 53 | 54 | % Insert calculated histograms, sizes, and boxes 55 | blobStruct.colourHist(:,1:numBlobs) = colourHist'; 56 | blobStruct.textureHist(:,1:numBlobs) = textureHist'; 57 | blobStruct.size(1:numBlobs) = blobSizes ./ 3; 58 | blobStruct.boxes(1:numBlobs,:) = blobBoxes; 59 | 60 | blobStruct.imSize = size(im,1) * size(im,2); 61 | 62 | %%% If you want to use original blobs in similarity functions, uncomment 63 | %%% these lines. 64 | % blobStruct.blobs = cell(numBlobs * 2 - 1, 1); 65 | % initialBlobs = SegmentIndices2Blobs(blobIndIm, blobBoxes); 66 | % blobStruct.blobs(1:numBlobs) = initialBlobs; 67 | 68 | 69 | % Loop over all merging strategies. Perform them one by one. 70 | boxes = cell(1, length(functionHandles)+1); 71 | priority = cell(1, length(functionHandles) + 1); 72 | hierarchy = cell(1, length(functionHandles)); 73 | for i=1:length(functionHandles) 74 | [boxes{i} hierarchy{i} blobStructT mergeThreshold] = BlobStruct2HierarchicalGrouping(blobStruct, neighbours, numBlobs, functionHandles{i}); 75 | boxes{i} = boxes{i}(numBlobs+1:end,:); 76 | priority{i} = (size(boxes{i}, 1):-1:1)'; 77 | end 78 | 79 | % Also save the initial boxes 80 | i = i+1; 81 | boxes{i} = blobBoxes; 82 | priority{i} = ones(size(boxes{i}, 1), 1) * (size(boxes{1}, 1)+1); 83 | 84 | % Concatenate boxes and priorities resulting from the different merging 85 | % strategies 86 | boxes = cat(1, boxes{:}); 87 | priority = cat(1, priority{:}); 88 | [priority ids] = sort(priority, 'ascend'); 89 | boxes = boxes(ids,:); 90 | 91 | 92 | -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | Copyright University of Amsterdam. All rights reserved. 2 | 3 | Contact persons: 4 | Jasper Uijlings (jrr disi.unitn.it) 5 | Koen van de Sande (ksande uva.nl) 6 | 7 | This software is being made available for individual research use only. 8 | Any commercial use or redistribution of this software requires a license from 9 | the University of Amsterdam. 10 | 11 | You may use this work subject to the following conditions: 12 | 13 | 1. This work is provided "as is" by the copyright holder, with 14 | absolutely no warranties of correctness, fitness, intellectual property 15 | ownership, or anything else whatsoever. You use the work 16 | entirely at your own risk. The copyright holder will not be liable for 17 | any legal damages whatsoever connected with the use of this work. 18 | 19 | 2. The copyright holder retain all copyright to the work. All copies of 20 | the work and all works derived from it must contain (1) this copyright 21 | notice, and (2) additional notices describing the content, dates and 22 | copyright holder of modifications or additions made to the work, if 23 | any, including distribution and use conditions and intellectual property 24 | claims. Derived works must be clearly distinguished from the original 25 | work, both by name and by the prominent inclusion of explicit 26 | descriptions of overlaps and differences. 27 | 28 | 3. The names and trademarks of the copyright holder may not be used in 29 | advertising or publicity related to this work without specific prior 30 | written permission. 31 | 32 | 4. In return for the free use of this work, you are requested, but not 33 | legally required, to do the following: 34 | 35 | * If you become aware of factors that may significantly affect other 36 | users of the work, for example major bugs or 37 | deficiencies or possible intellectual property issues, you are 38 | requested to report them to the copyright holder, if possible 39 | including redistributable fixes or workarounds. 40 | 41 | * If you use the work in scientific research or as part of a larger 42 | software system, you are requested to cite the use in any related 43 | publications or technical documentation. The work is based upon: 44 | 45 | J.R.R. Uijlings, K.E.A. van de Sande, T. Gevers, and A.W.M. Smeulders. 46 | Selective Search for Object Recognition 47 | IJCV, 2013. 48 | 49 | and uses 50 | 51 | J. M. Geusebroek, A. W. M. Smeulders, and J. van de Weijer. 52 | Fast anisotropic gauss filtering. IEEE Trans. Image Processing, 53 | vol. 12, no. 8, pp. 938-943, 2003. 54 | 55 | P. Felzenszwalb and D. Huttenlocher. 56 | Efficient graph-based image segmentation, 57 | International Journal of Computer Vision, 2004. 58 | 59 | 60 | This copyright notice must be retained with all copies of the software, 61 | including any modified or derived versions. 62 | 63 | -------------------------------------------------------------------------------- /MergeBlobs.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/MergeBlobs.p -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is mostly the code from [Segmentation as Selective Search for Object Recognition](http://koen.me/research/selectivesearch/), downloaded November 2013. 2 | I simply needed a way to call this stuff from Python: `selective_search.py` and `selective_search.m` are the only new files. 3 | 4 | import selective_search_ijcv_with_python as selective_search 5 | windows = selective_search.get_windows(image_filenames) 6 | 7 | To make sure this works, simply `python selective_search.py`. 8 | 9 | Sergey Karayev 10 | 25 Nov 2013 11 | -------------------------------------------------------------------------------- /RecreateBlobHierarchy.m: -------------------------------------------------------------------------------- 1 | function hBlobs = RecreateBlobHierarchy(blobs, hierarchy) 2 | % [blobs hierarchy] = RecreateBlobHierarchy(blobs, hierarchy) 3 | % 4 | % Recreates the hierarchical grouping using the starting blobs and the 5 | % resulting hierarchy. This allows one to save the grouping using 6 | % relatively small disk space while still being able to fastly recreate the 7 | % complete grouping. 8 | % 9 | % blobs: Input cell array with blobs 10 | % hierarchy: Hierarchy of the blobs as created by 11 | % HierarchicalGrouping.m 12 | % 13 | % hBlobs: All segments of the hierarchical grouping. 14 | % 15 | % Jasper Uijlings - 2013 16 | 17 | hBlobs = cell(length(hierarchy) + 1,1); 18 | 19 | hBlobs(1:length(blobs)) = blobs; 20 | 21 | for i=length(blobs)+1:length(hBlobs) 22 | n = find(hierarchy == i); 23 | 24 | if length(n) ~= 2 25 | error('One can not merge more than 2 blobs!'); 26 | end 27 | 28 | hBlobs{i} = MergeBlobs(hBlobs{n(1)}, hBlobs{n(2)}); 29 | end -------------------------------------------------------------------------------- /RecreateBlobHierarchyIndIm.m: -------------------------------------------------------------------------------- 1 | function [hBlobs blobsInit blobsRest] = RecreateBlobHierarchyIndIm(blobIndIm, blobBoxes, hierarchy) 2 | % function hBlobs = RecreateBlobHierarchyIndIm(blobIndIm, boxes, hierarchy) 3 | % 4 | % Recreate hierarchy from the initial segmentation image 5 | % 6 | % blobIndIm: Image with indices denoting segments 7 | % blobBoxes: Boxes belonging to blobs in blobIndIm 8 | % hierarchy: Hierarchy denoting hierarchical merging 9 | % 10 | % hBlobs: All blobs in the hierarchy 11 | % blobsInit: The initial blobs 12 | % blobsRest: All blobs but the initial blobs 13 | % 14 | % Jasper Uijlings - 2013 15 | 16 | % Get blobs of initial segmentation 17 | blobsInit = SegmentIndices2Blobs(blobIndIm, blobBoxes); 18 | 19 | % Add sizes 20 | blobsInit = BlobAddSizes(blobsInit); 21 | 22 | % Reconstruct hierarchy 23 | hBlobs = RecreateBlobHierarchy(blobsInit, hierarchy); 24 | 25 | if nargout == 3 26 | blobsRest = hBlobs(length(blobsInit)+1:end); 27 | end -------------------------------------------------------------------------------- /SSSimBoxFill.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimBoxFill.p -------------------------------------------------------------------------------- /SSSimBoxFillOrig.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimBoxFillOrig.p -------------------------------------------------------------------------------- /SSSimBoxFillOrigSize.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimBoxFillOrigSize.p -------------------------------------------------------------------------------- /SSSimBoxFillSize.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimBoxFillSize.p -------------------------------------------------------------------------------- /SSSimColour.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimColour.p -------------------------------------------------------------------------------- /SSSimColourSize.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimColourSize.p -------------------------------------------------------------------------------- /SSSimColourTextureSizeFill.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimColourTextureSizeFill.p -------------------------------------------------------------------------------- /SSSimColourTextureSizeFillOrig.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimColourTextureSizeFillOrig.p -------------------------------------------------------------------------------- /SSSimSize.m: -------------------------------------------------------------------------------- 1 | function [similarity indSim] = SSSimSize(a, b, blobStruct) 2 | % function similarity = SSSimSize(a, b, blobStruct) 3 | % 4 | % Calculate size similarity 5 | 6 | similarity = (blobStruct.imSize - blobStruct.size(a) - blobStruct.size(b)) ... 7 | ./ blobStruct.imSize; 8 | 9 | indSim = similarity; -------------------------------------------------------------------------------- /SSSimTexture.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimTexture.p -------------------------------------------------------------------------------- /SSSimTextureSize.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimTextureSize.p -------------------------------------------------------------------------------- /SSSimTextureSizeFill.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/SSSimTextureSizeFill.p -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | from selective_search import get_windows 2 | -------------------------------------------------------------------------------- /cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sergeyk/selective_search_ijcv_with_python/2ff50894aea8f49a5697354c779d3a6f9aad7d1d/cat.jpg -------------------------------------------------------------------------------- /demo.m: -------------------------------------------------------------------------------- 1 | % This demo shows how to use the software described in our IJCV paper: 2 | % Selective Search for Object Recognition, 3 | % J.R.R. Uijlings, K.E.A. van de Sande, T. Gevers, A.W.M. Smeulders, IJCV 2013 4 | %% 5 | addpath('Dependencies'); 6 | 7 | fprintf('Demo of how to run the code for:\n'); 8 | fprintf(' J. Uijlings, K. van de Sande, T. Gevers, A. Smeulders\n'); 9 | fprintf(' Segmentation as Selective Search for Object Recognition\n'); 10 | fprintf(' IJCV 2013\n\n'); 11 | 12 | % Compile anisotropic gaussian filter 13 | if(~exist('anigauss')) 14 | fprintf('Compiling the anisotropic gauss filtering of:\n'); 15 | fprintf(' J. Geusebroek, A. Smeulders, and J. van de Weijer\n'); 16 | fprintf(' Fast anisotropic gauss filtering\n'); 17 | fprintf(' IEEE Transactions on Image Processing, 2003\n'); 18 | fprintf('Source code/Project page:\n'); 19 | fprintf(' http://staff.science.uva.nl/~mark/downloads.html#anigauss\n\n'); 20 | mex Dependencies/anigaussm/anigauss_mex.c Dependencies/anigaussm/anigauss.c -output anigauss 21 | end 22 | 23 | if(~exist('mexCountWordsIndex')) 24 | mex Dependencies/mexCountWordsIndex.cpp 25 | end 26 | 27 | % Compile the code of Felzenszwalb and Huttenlocher, IJCV 2004. 28 | if(~exist('mexFelzenSegmentIndex')) 29 | fprintf('Compiling the segmentation algorithm of:\n'); 30 | fprintf(' P. Felzenszwalb and D. Huttenlocher\n'); 31 | fprintf(' Efficient Graph-Based Image Segmentation\n'); 32 | fprintf(' International Journal of Computer Vision, 2004\n'); 33 | fprintf('Source code/Project page:\n'); 34 | fprintf(' http://www.cs.brown.edu/~pff/segment/\n'); 35 | fprintf('Note: A small Matlab wrapper was made.\n'); 36 | % fprintf(' 37 | mex Dependencies/FelzenSegment/mexFelzenSegmentIndex.cpp -output mexFelzenSegmentIndex; 38 | end 39 | 40 | %% 41 | % Parameters. Note that this controls the number of hierarchical 42 | % segmentations which are combined. 43 | colorTypes = {'Hsv', 'Lab', 'RGI', 'H', 'Intensity'}; 44 | colorType = colorTypes{1}; % Single color space for demo 45 | 46 | % Here you specify which similarity functions to use in merging 47 | simFunctionHandles = {@SSSimColourTextureSizeFillOrig, @SSSimTextureSizeFill, @SSSimBoxFillOrig, @SSSimSize}; 48 | simFunctionHandles = simFunctionHandles(1:2); % Two different merging strategies 49 | 50 | % Thresholds for the Felzenszwalb and Huttenlocher segmentation algorithm. 51 | % Note that by default, we set minSize = k, and sigma = 0.8. 52 | k = 200; % controls size of segments of initial segmentation. 53 | minSize = k; 54 | sigma = 0.8; 55 | 56 | % As an example, use a single image 57 | images = {'000015.jpg'}; 58 | im = imread(images{1}); 59 | 60 | % Perform Selective Search 61 | [boxes blobIndIm blobBoxes hierarchy] = Image2HierarchicalGrouping(im, sigma, k, minSize, colorType, simFunctionHandles); 62 | boxes = BoxRemoveDuplicates(boxes); 63 | 64 | % Show boxes 65 | ShowRectsWithinImage(boxes, 5, 5, im); 66 | 67 | % Show blobs which result from first similarity function 68 | hBlobs = RecreateBlobHierarchyIndIm(blobIndIm, blobBoxes, hierarchy{1}); 69 | ShowBlobs(hBlobs, 5, 5, im); -------------------------------------------------------------------------------- /demoPascal2007.m: -------------------------------------------------------------------------------- 1 | % This demo shows how to use the software described in our IJCV paper: 2 | % Selective Search for Object Recognition, 3 | % J.R.R. Uijlings, K.E.A. van de Sande, T. Gevers, A.W.M. Smeulders, IJCV 2013 4 | %% 5 | addpath('Dependencies'); 6 | 7 | fprintf('Demo of how to run the code for:\n'); 8 | fprintf(' J. Uijlings, K. van de Sande, T. Gevers, A. Smeulders\n'); 9 | fprintf(' Segmentation as Selective Search for Object Recognition\n'); 10 | fprintf(' IJCV 2013\n\n'); 11 | 12 | % Compile anisotropic gaussian filter 13 | if(~exist('anigauss')) 14 | fprintf('Compiling the anisotropic gauss filtering of:\n'); 15 | fprintf(' J. Geusebroek, A. Smeulders, and J. van de Weijer\n'); 16 | fprintf(' Fast anisotropic gauss filtering\n'); 17 | fprintf(' IEEE Transactions on Image Processing, 2003\n'); 18 | fprintf('Source code/Project page:\n'); 19 | fprintf(' http://staff.science.uva.nl/~mark/downloads.html#anigauss\n\n'); 20 | mex Dependencies/anigaussm/anigauss_mex.c Dependencies/anigaussm/anigauss.c -output anigauss 21 | end 22 | 23 | if(~exist('mexCountWordsIndex')) 24 | mex Dependencies/mexCountWordsIndex.cpp 25 | end 26 | 27 | % Compile the code of Felzenszwalb and Huttenlocher, IJCV 2004. 28 | if(~exist('mexFelzenSegmentIndex')) 29 | fprintf('Compiling the segmentation algorithm of:\n'); 30 | fprintf(' P. Felzenszwalb and D. Huttenlocher\n'); 31 | fprintf(' Efficient Graph-Based Image Segmentation\n'); 32 | fprintf(' International Journal of Computer Vision, 2004\n'); 33 | fprintf('Source code/Project page:\n'); 34 | fprintf(' http://www.cs.brown.edu/~pff/segment/\n'); 35 | fprintf('Note: A small Matlab wrapper was made. See demo.m for usage\n\n'); 36 | % fprintf(' 37 | mex Dependencies/FelzenSegment/mexFelzenSegmentIndex.cpp -output mexFelzenSegmentIndex; 38 | end 39 | 40 | %% 41 | % Parameters. Note that this controls the number of hierarchical 42 | % segmentations which are combined. 43 | colorTypes = {'Hsv', 'Lab', 'RGI', 'H', 'Intensity'}; 44 | 45 | % Here you specify which similarity functions to use in merging 46 | simFunctionHandles = {@SSSimColourTextureSizeFillOrig, @SSSimTextureSizeFill, @SSSimBoxFillOrig, @SSSimSize}; 47 | 48 | % Thresholds for the Felzenszwalb and Huttenlocher segmentation algorithm. 49 | % Note that by default, we set minSize = k, and sigma = 0.8. 50 | ks = [50 100 150 300]; % controls size of segments of initial segmentation. 51 | sigma = 0.8; 52 | 53 | % After segmentation, filter out boxes which have a width/height smaller 54 | % than minBoxWidth (default = 20 pixels). 55 | minBoxWidth = 20; 56 | 57 | % Comment the following three lines for the 'quality' version 58 | % colorTypes = colorTypes(1:2); % 'Fast' uses HSV and Lab 59 | % simFunctionHandles = simFunctionHandles(1:2); % Two different merging strategies 60 | % ks = ks(1:2); 61 | 62 | % Test the boxes 63 | load('GroundTruthVOC2007test.mat'); % Load ground truth boxes and images and image names 64 | VOCImgPath = '/media/Data/Databases/VOCdevkit/VOC2007/JPEGImages/%s.jpg' 65 | fprintf('After box extraction, boxes smaller than %d pixels will be removed\n', minBoxWidth); 66 | fprintf('Obtaining boxes for Pascal 2007 test set:\n'); 67 | totalTime = 0; 68 | for i=1:length(testIms) 69 | fprintf('%d ', i); 70 | 71 | % VOCopts.img 72 | im = imread(sprintf(VOCImgPath, testIms{i})); 73 | idx = 1; 74 | for j=1:length(ks) 75 | k = ks(j); % Segmentation threshold k 76 | minSize = k; % We set minSize = k 77 | for n = 1:length(colorTypes) 78 | colorType = colorTypes{n}; 79 | tic; 80 | [boxesT{idx} blobIndIm blobBoxes hierarchy priorityT{idx}] = Image2HierarchicalGrouping(im, sigma, k, minSize, colorType, simFunctionHandles); 81 | totalTime = totalTime + toc; 82 | idx = idx + 1; 83 | end 84 | end 85 | boxes{i} = cat(1, boxesT{:}); % Concatenate boxes from all hierarchies 86 | priority = cat(1, priorityT{:}); % Concatenate priorities 87 | 88 | % Do pseudo random sorting as in paper 89 | priority = priority .* rand(size(priority)); 90 | [priority sortIds] = sort(priority, 'ascend'); 91 | boxes{i} = boxes{i}(sortIds,:); 92 | end 93 | fprintf('\n'); 94 | 95 | %% 96 | tic 97 | for i=1:length(boxes) 98 | boxes{i} = FilterBoxesWidth(boxes{i}, minBoxWidth); 99 | boxes{i} = BoxRemoveDuplicates(boxes{i}); 100 | end 101 | totalTime = totalTime + toc; 102 | 103 | fprintf('Time per image: %.2f\nNow evaluating the boxes on Pascal 2007...\n', totalTime ./ length(testIms)); 104 | 105 | %% 106 | [boxAbo boxMabo boScores avgNumBoxes] = BoxAverageBestOverlap(gtBoxes, gtImIds, boxes); 107 | 108 | fprintf('Mean Average Best Overlap for the box-based locations: %.3f\n', boxMabo); -------------------------------------------------------------------------------- /selective_search.m: -------------------------------------------------------------------------------- 1 | function all_boxes = selective_search(image_filenames, output_filename) 2 | 3 | addpath('Dependencies'); 4 | 5 | if(~exist('anigauss')) 6 | mex Dependencies/anigaussm/anigauss_mex.c Dependencies/anigaussm/anigauss.c -output anigauss 7 | end 8 | 9 | if(~exist('mexCountWordsIndex')) 10 | mex Dependencies/mexCountWordsIndex.cpp 11 | end 12 | 13 | if(~exist('mexFelzenSegmentIndex')) 14 | mex Dependencies/FelzenSegment/mexFelzenSegmentIndex.cpp -output mexFelzenSegmentIndex; 15 | end 16 | 17 | colorTypes = {'Hsv', 'Lab', 'RGI', 'H', 'Intensity'}; 18 | colorType = colorTypes{1}; % Single color space for demo 19 | 20 | % Here you specify which similarity functions to use in merging 21 | simFunctionHandles = {@SSSimColourTextureSizeFillOrig, @SSSimTextureSizeFill, @SSSimBoxFillOrig, @SSSimSize}; 22 | simFunctionHandles = simFunctionHandles(1:2); % Two different merging strategies 23 | 24 | % Thresholds for the Felzenszwalb and Huttenlocher segmentation algorithm. 25 | % Note that by default, we set minSize = k, and sigma = 0.8. 26 | k = 200; % controls size of segments of initial segmentation. 27 | minSize = k; 28 | sigma = 0.8; 29 | 30 | % Process all images. 31 | all_boxes = {}; 32 | for i=1:length(image_filenames) 33 | im = imread(image_filenames{i}); 34 | [boxes blobIndIm blobBoxes hierarchy] = Image2HierarchicalGrouping(im, sigma, k, minSize, colorType, simFunctionHandles); 35 | all_boxes{i} = BoxRemoveDuplicates(boxes); 36 | end 37 | 38 | if nargin > 1 39 | all_boxes 40 | save(output_filename, 'all_boxes', '-v7'); 41 | end 42 | -------------------------------------------------------------------------------- /selective_search.py: -------------------------------------------------------------------------------- 1 | import tempfile 2 | import subprocess 3 | import shlex 4 | import os 5 | import numpy as np 6 | import scipy.io 7 | 8 | script_dirname = os.path.abspath(os.path.dirname(__file__)) 9 | 10 | 11 | def get_windows(image_fnames, cmd='selective_search'): 12 | """ 13 | Run MATLAB Selective Search code on the given image filenames to 14 | generate window proposals. 15 | 16 | Parameters 17 | ---------- 18 | image_filenames: strings 19 | Paths to images to run on. 20 | cmd: string 21 | selective search function to call: 22 | - 'selective_search' for a few quick proposals 23 | - 'selective_seach_rcnn' for R-CNN configuration for more coverage. 24 | """ 25 | # Form the MATLAB script command that processes images and write to 26 | # temporary results file. 27 | f, output_filename = tempfile.mkstemp(suffix='.mat') 28 | os.close(f) 29 | fnames_cell = '{' + ','.join("'{}'".format(x) for x in image_fnames) + '}' 30 | command = "{}({}, '{}')".format(cmd, fnames_cell, output_filename) 31 | print(command) 32 | 33 | # Execute command in MATLAB. 34 | mc = "matlab -nojvm -r \"try; {}; catch; exit; end; exit\"".format(command) 35 | pid = subprocess.Popen( 36 | shlex.split(mc), stdout=open('/dev/null', 'w'), cwd=script_dirname) 37 | retcode = pid.wait() 38 | if retcode != 0: 39 | raise Exception("Matlab script did not exit successfully!") 40 | 41 | # Read the results and undo Matlab's 1-based indexing. 42 | all_boxes = list(scipy.io.loadmat(output_filename)['all_boxes'][0]) 43 | subtractor = np.array((1, 1, 0, 0))[np.newaxis, :] 44 | all_boxes = [boxes - subtractor for boxes in all_boxes] 45 | 46 | # Remove temporary file, and return. 47 | os.remove(output_filename) 48 | if len(all_boxes) != len(image_fnames): 49 | raise Exception("Something went wrong computing the windows!") 50 | return all_boxes 51 | 52 | if __name__ == '__main__': 53 | """ 54 | Run a demo. 55 | """ 56 | import time 57 | 58 | image_filenames = [ 59 | script_dirname + '/000015.jpg', 60 | script_dirname + '/cat.jpg' 61 | ] * 4 62 | t = time.time() 63 | boxes = get_windows(image_filenames) 64 | print(boxes[:2]) 65 | print("Processed {} images in {:.3f} s".format( 66 | len(image_filenames), time.time() - t)) 67 | -------------------------------------------------------------------------------- /selective_search_rcnn.m: -------------------------------------------------------------------------------- 1 | function all_boxes = selective_search_rcnn(image_filenames, output_filename) 2 | 3 | % Based on the demo.m file included in the Selective Search 4 | % IJCV code, and on selective_search_boxes.m from R-CNN. 5 | 6 | % Load dependencies and compile if needed. 7 | 8 | addpath('Dependencies'); 9 | 10 | if(~exist('anigauss')) 11 | mex Dependencies/anigaussm/anigauss_mex.c Dependencies/anigaussm/anigauss.c -output anigauss 12 | end 13 | 14 | if(~exist('mexCountWordsIndex')) 15 | mex Dependencies/mexCountWordsIndex.cpp 16 | end 17 | 18 | if(~exist('mexFelzenSegmentIndex')) 19 | mex Dependencies/FelzenSegment/mexFelzenSegmentIndex.cpp -output mexFelzenSegmentIndex; 20 | end 21 | 22 | % Configure 23 | im_width = 500; 24 | 25 | % Parameters. Note that this controls the number of hierarchical 26 | % segmentations which are combined. 27 | colorTypes = {'Hsv', 'Lab', 'RGI', 'H', 'Intensity'}; 28 | 29 | % Here you specify which similarity functions to use in merging 30 | simFunctionHandles = {@SSSimColourTextureSizeFillOrig, ... 31 | @SSSimTextureSizeFill, ... 32 | @SSSimBoxFillOrig, ... 33 | @SSSimSize}; 34 | 35 | % Thresholds for the Felzenszwalb and Huttenlocher segmentation algorithm. 36 | % Note that by default, we set minSize = k, and sigma = 0.8. 37 | % controls size of segments of initial segmentation. 38 | ks = [50 100 150 300]; 39 | sigma = 0.8; 40 | 41 | % After segmentation, filter out boxes which have a width/height smaller 42 | % than minBoxWidth (default = 20 pixels). 43 | minBoxWidth = 20; 44 | 45 | % Comment the following three lines for the 'quality' version 46 | colorTypes = colorTypes(1:2); % 'Fast' uses HSV and Lab 47 | simFunctionHandles = simFunctionHandles(1:2); % Two different merging strategies 48 | ks = ks(1:2); 49 | 50 | % Process all images. 51 | all_boxes = {}; 52 | for i=1:length(image_filenames) 53 | im = imread(image_filenames{i}); 54 | % Resize image to canonical dimensions since proposals aren't scale invariant. 55 | scale = size(im, 2) / im_width; 56 | im = imresize(im, [NaN im_width]); 57 | 58 | idx = 1; 59 | for j = 1:length(ks) 60 | k = ks(j); % Segmentation threshold k 61 | minSize = k; % We set minSize = k 62 | for n = 1:length(colorTypes) 63 | colorType = colorTypes{n}; 64 | [boxesT{idx} blobIndIm blobBoxes hierarchy priorityT{idx}] = ... 65 | Image2HierarchicalGrouping(im, sigma, k, minSize, colorType, simFunctionHandles); 66 | idx = idx + 1; 67 | end 68 | end 69 | boxes = cat(1, boxesT{:}); % Concatenate boxes from all hierarchies 70 | priority = cat(1, priorityT{:}); % Concatenate priorities 71 | 72 | % Do pseudo random sorting as in paper 73 | priority = priority .* rand(size(priority)); 74 | [priority sortIds] = sort(priority, 'ascend'); 75 | boxes = boxes(sortIds,:); 76 | 77 | boxes = FilterBoxesWidth(boxes, minBoxWidth); 78 | boxes = BoxRemoveDuplicates(boxes); 79 | 80 | % Adjust boxes to cancel effect of canonical scaling. 81 | boxes = (boxes - 1) * scale + 1; 82 | 83 | boxes = FilterBoxesWidth(boxes, minBoxWidth); 84 | boxes = BoxRemoveDuplicates(boxes); 85 | all_boxes{i} = boxes; 86 | end 87 | 88 | if nargin > 1 89 | all_boxes 90 | save(output_filename, 'all_boxes', '-v7'); 91 | end 92 | --------------------------------------------------------------------------------