├── README.md ├── report ├── img │ ├── detect.jpg │ ├── relit.jpg │ └── removal.jpg └── report.pdf ├── result ├── DSC_0569 │ ├── DSC_0569.jpg │ ├── DSC_0570.jpg │ ├── detect.png │ └── removal.png ├── DSC_0736 │ ├── DSC_0736.jpg │ ├── DSC_0738.jpg │ ├── detect.png │ └── removal.png ├── SBU_1 │ ├── SBU_1.jpeg │ ├── detect.png │ ├── lssd9.png │ └── removal.png ├── SBU_2 │ ├── detect.png │ ├── lssd577.jpg │ ├── lssd577.png │ └── removal.png ├── SBU_3 │ ├── detect.png │ ├── lssd60.jpg │ ├── lssd60.png │ └── removal.png ├── p12_2 │ ├── detect.png │ ├── p12_2.jpg │ ├── p12_n.jpg │ └── removal.png ├── p14_2 │ ├── detect.png │ ├── p14_2.jpg │ ├── p14_n.jpg │ └── removal.png ├── p21_1 │ ├── detect.png │ ├── p21_1.jpg │ ├── p21_n.jpg │ └── removal.png └── p2_2 │ ├── detect.png │ ├── p2_2.jpg │ ├── p2_n.jpg │ └── removal.png ├── shadow_slides.pdf └── src ├── SBU_file.txt ├── UIUC_file.txt ├── annotate.m ├── detect.m ├── detect.m~ ├── etract_feature ├── assignTextons.m ├── bsd300_128.mat ├── calAvghsv.m ├── calGradient.m ├── calHsi.m ├── calHsv.m ├── calHsvHist.m ├── calYcbcr.m ├── calYcbcrHist.m ├── calcLabHist.m ├── calcShapeMean.m ├── calcTextonHist.m ├── distSqr.m ├── fbRun.m ├── fftconv2.m ├── lab2rgb.m ├── padReflect.m ├── rgb2hsi.m ├── rgb2hsi_refine.m └── rgb2lab.m ├── findshadowcut_cross.m ├── hist_match.m ├── main.m ├── meanshift └── readme.txt ├── removal.m └── utils ├── buildAdjacentMatrix.m └── dist_chi2.m /README.md: -------------------------------------------------------------------------------- 1 | # Image-Shadow-Detection-and-Removal 2 | 3 | ### Introduction 4 | * Using paired regions 5 | * No dataset or model training needed 6 | 7 | ### Shadow Detection 8 | - Mean shift segmentation 9 | - Features 10 | - YCbCr & HSI color space info 11 | - Gradient & Texture & Distant differences between every two regions 12 | 13 | ### Shadow Removal 14 | * Processed in HSV color space 15 | * Histogram matching technique 16 | 17 | ### Results 18 | * Detection Result 19 | ![](report/img/detect.jpg?raw=true) 20 |                 Input Image                           Detection Mask                    Ground Truth Mask                   Removal Result 21 | 22 | * Removal Result 23 | ![](report/img/removal.jpg?raw=true) 24 |                 Input Image                          Detection Mask                     Removal Result                    Ground Truth Image 25 | 26 | * Paired Regions 27 | ![](report/img/relit.jpg?raw=true) 28 |                                                                Paired Regions for Shadow Area Re-litting 29 | 30 | ### More 31 | * **./result** for more quantative results 32 | * **./report** for technical report 33 | -------------------------------------------------------------------------------- /report/img/detect.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/report/img/detect.jpg -------------------------------------------------------------------------------- /report/img/relit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/report/img/relit.jpg -------------------------------------------------------------------------------- /report/img/removal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/report/img/removal.jpg -------------------------------------------------------------------------------- /report/report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/report/report.pdf -------------------------------------------------------------------------------- /result/DSC_0569/DSC_0569.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/DSC_0569/DSC_0569.jpg -------------------------------------------------------------------------------- /result/DSC_0569/DSC_0570.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/DSC_0569/DSC_0570.jpg -------------------------------------------------------------------------------- /result/DSC_0569/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/DSC_0569/detect.png -------------------------------------------------------------------------------- /result/DSC_0569/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/DSC_0569/removal.png -------------------------------------------------------------------------------- /result/DSC_0736/DSC_0736.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/DSC_0736/DSC_0736.jpg -------------------------------------------------------------------------------- /result/DSC_0736/DSC_0738.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/DSC_0736/DSC_0738.jpg -------------------------------------------------------------------------------- /result/DSC_0736/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/DSC_0736/detect.png -------------------------------------------------------------------------------- /result/DSC_0736/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/DSC_0736/removal.png -------------------------------------------------------------------------------- /result/SBU_1/SBU_1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_1/SBU_1.jpeg -------------------------------------------------------------------------------- /result/SBU_1/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_1/detect.png -------------------------------------------------------------------------------- /result/SBU_1/lssd9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_1/lssd9.png -------------------------------------------------------------------------------- /result/SBU_1/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_1/removal.png -------------------------------------------------------------------------------- /result/SBU_2/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_2/detect.png -------------------------------------------------------------------------------- /result/SBU_2/lssd577.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_2/lssd577.jpg -------------------------------------------------------------------------------- /result/SBU_2/lssd577.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_2/lssd577.png -------------------------------------------------------------------------------- /result/SBU_2/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_2/removal.png -------------------------------------------------------------------------------- /result/SBU_3/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_3/detect.png -------------------------------------------------------------------------------- /result/SBU_3/lssd60.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_3/lssd60.jpg -------------------------------------------------------------------------------- /result/SBU_3/lssd60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_3/lssd60.png -------------------------------------------------------------------------------- /result/SBU_3/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/SBU_3/removal.png -------------------------------------------------------------------------------- /result/p12_2/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p12_2/detect.png -------------------------------------------------------------------------------- /result/p12_2/p12_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p12_2/p12_2.jpg -------------------------------------------------------------------------------- /result/p12_2/p12_n.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p12_2/p12_n.jpg -------------------------------------------------------------------------------- /result/p12_2/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p12_2/removal.png -------------------------------------------------------------------------------- /result/p14_2/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p14_2/detect.png -------------------------------------------------------------------------------- /result/p14_2/p14_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p14_2/p14_2.jpg -------------------------------------------------------------------------------- /result/p14_2/p14_n.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p14_2/p14_n.jpg -------------------------------------------------------------------------------- /result/p14_2/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p14_2/removal.png -------------------------------------------------------------------------------- /result/p21_1/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p21_1/detect.png -------------------------------------------------------------------------------- /result/p21_1/p21_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p21_1/p21_1.jpg -------------------------------------------------------------------------------- /result/p21_1/p21_n.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p21_1/p21_n.jpg -------------------------------------------------------------------------------- /result/p21_1/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p21_1/removal.png -------------------------------------------------------------------------------- /result/p2_2/detect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p2_2/detect.png -------------------------------------------------------------------------------- /result/p2_2/p2_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p2_2/p2_2.jpg -------------------------------------------------------------------------------- /result/p2_2/p2_n.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p2_2/p2_n.jpg -------------------------------------------------------------------------------- /result/p2_2/removal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/result/p2_2/removal.png -------------------------------------------------------------------------------- /shadow_slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/shadow_slides.pdf -------------------------------------------------------------------------------- /src/UIUC_file.txt: -------------------------------------------------------------------------------- 1 | data/UIUC/Our_test/gt/DSC_0394.png 2 | data/UIUC/Our_test/gt/DSC_0400.png 3 | data/UIUC/Our_test/gt/DSC_0445.png 4 | data/UIUC/Our_test/gt/DSC_0531.png 5 | data/UIUC/Our_test/gt/DSC_0552.png 6 | data/UIUC/Our_test/gt/DSC_0559.png 7 | data/UIUC/Our_test/gt/DSC_0569.png 8 | data/UIUC/Our_test/gt/DSC_0577.png 9 | data/UIUC/Our_test/gt/DSC_0587.png 10 | data/UIUC/Our_test/gt/DSC_0594.png 11 | data/UIUC/Our_test/gt/DSC_0599.png 12 | data/UIUC/Our_test/gt/DSC_0607.png 13 | data/UIUC/Our_test/gt/DSC_0625.png 14 | data/UIUC/Our_test/gt/DSC_0633.png 15 | data/UIUC/Our_test/gt/DSC_0637.png 16 | data/UIUC/Our_test/gt/DSC_0641.png 17 | data/UIUC/Our_test/gt/DSC_0647.png 18 | data/UIUC/Our_test/gt/DSC_0649.png 19 | data/UIUC/Our_test/gt/DSC_0655.png 20 | data/UIUC/Our_test/gt/DSC_0661.png 21 | data/UIUC/Our_test/gt/DSC_0665.png 22 | data/UIUC/Our_test/gt/DSC_0669.png 23 | data/UIUC/Our_test/gt/DSC_0673.png 24 | data/UIUC/Our_test/gt/DSC_0677.png 25 | data/UIUC/Our_test/gt/DSC_0681.png 26 | data/UIUC/Our_test/gt/DSC_0685.png 27 | data/UIUC/Our_test/gt/DSC_0689.png 28 | data/UIUC/Our_test/gt/DSC_0693.png 29 | data/UIUC/Our_test/gt/DSC_0697.png 30 | data/UIUC/Our_test/gt/DSC_0701.png 31 | data/UIUC/Our_test/gt/DSC_0705.png 32 | data/UIUC/Our_test/gt/DSC_0709.png 33 | data/UIUC/Our_test/gt/DSC_0713.png 34 | data/UIUC/Our_test/gt/DSC_0717.png 35 | data/UIUC/Our_test/gt/DSC_0721.png 36 | data/UIUC/Our_test/gt/DSC_0725.png 37 | data/UIUC/Our_test/gt/DSC_0729.png 38 | data/UIUC/Our_test/gt/DSC_0733.png 39 | data/UIUC/Our_test/gt/DSC_0736.png 40 | data/UIUC/Our_test/gt/DSC_0741.png 41 | data/UIUC/Our_test/gt/DSC_0745.png 42 | data/UIUC/Our_test/gt/DSC_0749.png 43 | data/UIUC/Our_test/gt/DSC_0753.png 44 | data/UIUC/Our_test/gt/DSC_0757.png 45 | data/UIUC/Our_test/gt/p10_1.png 46 | data/UIUC/Our_test/gt/p11_1.png 47 | data/UIUC/Our_test/gt/p12_1.png 48 | data/UIUC/Our_test/gt/p12_2.png 49 | data/UIUC/Our_test/gt/p13_1.png 50 | data/UIUC/Our_test/gt/p14_1.png 51 | data/UIUC/Our_test/gt/p14_2.png 52 | data/UIUC/Our_test/gt/p15_1.png 53 | data/UIUC/Our_test/gt/p16_1.png 54 | data/UIUC/Our_test/gt/p16_2.png 55 | data/UIUC/Our_test/gt/p17_1.png 56 | data/UIUC/Our_test/gt/p17_2.png 57 | data/UIUC/Our_test/gt/p18_1.png 58 | data/UIUC/Our_test/gt/p18_2.png 59 | data/UIUC/Our_test/gt/p19_1.png 60 | data/UIUC/Our_test/gt/p1_1.png 61 | data/UIUC/Our_test/gt/p1_2.png 62 | data/UIUC/Our_test/gt/p1_3.png 63 | data/UIUC/Our_test/gt/p20_1.png 64 | data/UIUC/Our_test/gt/p21_1.png 65 | data/UIUC/Our_test/gt/p21_2.png 66 | data/UIUC/Our_test/gt/p2_1.png 67 | data/UIUC/Our_test/gt/p2_2.png 68 | data/UIUC/Our_test/gt/p3_1.png 69 | data/UIUC/Our_test/gt/p4_1.png 70 | data/UIUC/Our_test/gt/p6_1.png 71 | data/UIUC/Our_test/gt/p6_2.png 72 | data/UIUC/Our_test/gt/p7_1.png 73 | data/UIUC/Our_test/gt/p7_2.png 74 | data/UIUC/Our_test/gt/p8_1.png 75 | data/UIUC/Our_test/gt/p9_1.png 76 | data/UIUC/Our_test/gt/p9_2.png 77 | data/UIUC/Our_test/img/DSC_0394.jpg 78 | data/UIUC/Our_test/img/DSC_0395.jpg 79 | data/UIUC/Our_test/img/DSC_0397.jpg 80 | data/UIUC/Our_test/img/DSC_0400.jpg 81 | data/UIUC/Our_test/img/DSC_0444.jpg 82 | data/UIUC/Our_test/img/DSC_0445.jpg 83 | data/UIUC/Our_test/img/DSC_0531.jpg 84 | data/UIUC/Our_test/img/DSC_0532.jpg 85 | data/UIUC/Our_test/img/DSC_0550.jpg 86 | data/UIUC/Our_test/img/DSC_0552.jpg 87 | data/UIUC/Our_test/img/DSC_0553.jpg 88 | data/UIUC/Our_test/img/DSC_0559.jpg 89 | data/UIUC/Our_test/img/DSC_0560.jpg 90 | data/UIUC/Our_test/img/DSC_0569.jpg 91 | data/UIUC/Our_test/img/DSC_0570.jpg 92 | data/UIUC/Our_test/img/DSC_0577.jpg 93 | data/UIUC/Our_test/img/DSC_0579.jpg 94 | data/UIUC/Our_test/img/DSC_0585.jpg 95 | data/UIUC/Our_test/img/DSC_0587.jpg 96 | data/UIUC/Our_test/img/DSC_0593.jpg 97 | data/UIUC/Our_test/img/DSC_0594.jpg 98 | data/UIUC/Our_test/img/DSC_0599.jpg 99 | data/UIUC/Our_test/img/DSC_0600.jpg 100 | data/UIUC/Our_test/img/DSC_0607.jpg 101 | data/UIUC/Our_test/img/DSC_0608.jpg 102 | data/UIUC/Our_test/img/DSC_0625.jpg 103 | data/UIUC/Our_test/img/DSC_0626.jpg 104 | data/UIUC/Our_test/img/DSC_0633.jpg 105 | data/UIUC/Our_test/img/DSC_0634.jpg 106 | data/UIUC/Our_test/img/DSC_0637.jpg 107 | data/UIUC/Our_test/img/DSC_0638.jpg 108 | data/UIUC/Our_test/img/DSC_0641.jpg 109 | data/UIUC/Our_test/img/DSC_0642.jpg 110 | data/UIUC/Our_test/img/DSC_0645.jpg 111 | data/UIUC/Our_test/img/DSC_0647.jpg 112 | data/UIUC/Our_test/img/DSC_0649.jpg 113 | data/UIUC/Our_test/img/DSC_0655.jpg 114 | data/UIUC/Our_test/img/DSC_0656.jpg 115 | data/UIUC/Our_test/img/DSC_0661.jpg 116 | data/UIUC/Our_test/img/DSC_0662.jpg 117 | data/UIUC/Our_test/img/DSC_0665.jpg 118 | data/UIUC/Our_test/img/DSC_0666.jpg 119 | data/UIUC/Our_test/img/DSC_0669.jpg 120 | data/UIUC/Our_test/img/DSC_0670.jpg 121 | data/UIUC/Our_test/img/DSC_0673.jpg 122 | data/UIUC/Our_test/img/DSC_0674.jpg 123 | data/UIUC/Our_test/img/DSC_0677.jpg 124 | data/UIUC/Our_test/img/DSC_0678.jpg 125 | data/UIUC/Our_test/img/DSC_0681.jpg 126 | data/UIUC/Our_test/img/DSC_0682.jpg 127 | data/UIUC/Our_test/img/DSC_0685.jpg 128 | data/UIUC/Our_test/img/DSC_0686.jpg 129 | data/UIUC/Our_test/img/DSC_0689.jpg 130 | data/UIUC/Our_test/img/DSC_0690.jpg 131 | data/UIUC/Our_test/img/DSC_0693.jpg 132 | data/UIUC/Our_test/img/DSC_0694.jpg 133 | data/UIUC/Our_test/img/DSC_0697.jpg 134 | data/UIUC/Our_test/img/DSC_0698.jpg 135 | data/UIUC/Our_test/img/DSC_0701.jpg 136 | data/UIUC/Our_test/img/DSC_0702.jpg 137 | data/UIUC/Our_test/img/DSC_0705.jpg 138 | data/UIUC/Our_test/img/DSC_0706.jpg 139 | data/UIUC/Our_test/img/DSC_0709.jpg 140 | data/UIUC/Our_test/img/DSC_0710.jpg 141 | data/UIUC/Our_test/img/DSC_0713.jpg 142 | data/UIUC/Our_test/img/DSC_0714.jpg 143 | data/UIUC/Our_test/img/DSC_0717.jpg 144 | data/UIUC/Our_test/img/DSC_0718.jpg 145 | data/UIUC/Our_test/img/DSC_0721.jpg 146 | data/UIUC/Our_test/img/DSC_0722.jpg 147 | data/UIUC/Our_test/img/DSC_0725.jpg 148 | data/UIUC/Our_test/img/DSC_0726.jpg 149 | data/UIUC/Our_test/img/DSC_0729.jpg 150 | data/UIUC/Our_test/img/DSC_0730.jpg 151 | data/UIUC/Our_test/img/DSC_0733.jpg 152 | data/UIUC/Our_test/img/DSC_0734.jpg 153 | data/UIUC/Our_test/img/DSC_0736.jpg 154 | data/UIUC/Our_test/img/DSC_0738.jpg 155 | data/UIUC/Our_test/img/DSC_0741.jpg 156 | data/UIUC/Our_test/img/DSC_0742.jpg 157 | data/UIUC/Our_test/img/DSC_0745.jpg 158 | data/UIUC/Our_test/img/DSC_0746.jpg 159 | data/UIUC/Our_test/img/DSC_0749.jpg 160 | data/UIUC/Our_test/img/DSC_0750.jpg 161 | data/UIUC/Our_test/img/DSC_0753.jpg 162 | data/UIUC/Our_test/img/DSC_0754.jpg 163 | data/UIUC/Our_test/img/DSC_0757.jpg 164 | data/UIUC/Our_test/img/DSC_0758.jpg 165 | data/UIUC/Our_test/img/p10_1.jpg 166 | data/UIUC/Our_test/img/p10_n.jpg 167 | data/UIUC/Our_test/img/p11_1.jpg 168 | data/UIUC/Our_test/img/p11_n.jpg 169 | data/UIUC/Our_test/img/p12_1.jpg 170 | data/UIUC/Our_test/img/p12_2.jpg 171 | data/UIUC/Our_test/img/p12_n.jpg 172 | data/UIUC/Our_test/img/p13_1.jpg 173 | data/UIUC/Our_test/img/p13_n.jpg 174 | data/UIUC/Our_test/img/p14_1.jpg 175 | data/UIUC/Our_test/img/p14_2.jpg 176 | data/UIUC/Our_test/img/p14_n.jpg 177 | data/UIUC/Our_test/img/p15_1.jpg 178 | data/UIUC/Our_test/img/p15_n.jpg 179 | data/UIUC/Our_test/img/p16_1.jpg 180 | data/UIUC/Our_test/img/p16_2.jpg 181 | data/UIUC/Our_test/img/p16_n.jpg 182 | data/UIUC/Our_test/img/p17_1.jpg 183 | data/UIUC/Our_test/img/p17_2.jpg 184 | data/UIUC/Our_test/img/p17_n.jpg 185 | data/UIUC/Our_test/img/p18_1.jpg 186 | data/UIUC/Our_test/img/p18_2.jpg 187 | data/UIUC/Our_test/img/p18_n.jpg 188 | data/UIUC/Our_test/img/p19_1.jpg 189 | data/UIUC/Our_test/img/p19_n.jpg 190 | data/UIUC/Our_test/img/p1_1.jpg 191 | data/UIUC/Our_test/img/p1_2.jpg 192 | data/UIUC/Our_test/img/p1_3.jpg 193 | data/UIUC/Our_test/img/p1_n.jpg 194 | data/UIUC/Our_test/img/p20_1.jpg 195 | data/UIUC/Our_test/img/p20_n.jpg 196 | data/UIUC/Our_test/img/p21_1.jpg 197 | data/UIUC/Our_test/img/p21_2.jpg 198 | data/UIUC/Our_test/img/p21_n.jpg 199 | data/UIUC/Our_test/img/p2_1.jpg 200 | data/UIUC/Our_test/img/p2_2.jpg 201 | data/UIUC/Our_test/img/p2_n.jpg 202 | data/UIUC/Our_test/img/p3_1.jpg 203 | data/UIUC/Our_test/img/p3_n.jpg 204 | data/UIUC/Our_test/img/p4_1.jpg 205 | data/UIUC/Our_test/img/p4_n.jpg 206 | data/UIUC/Our_test/img/p5_1.jpg 207 | data/UIUC/Our_test/img/p5_2.jpg 208 | data/UIUC/Our_test/img/p5_n.jpg 209 | data/UIUC/Our_test/img/p6_1.jpg 210 | data/UIUC/Our_test/img/p6_2.jpg 211 | data/UIUC/Our_test/img/p6_n.jpg 212 | data/UIUC/Our_test/img/p7_1.jpg 213 | data/UIUC/Our_test/img/p7_2.jpg 214 | data/UIUC/Our_test/img/p7_n.jpg 215 | data/UIUC/Our_test/img/p8_1.jpg 216 | data/UIUC/Our_test/img/p8_n.jpg 217 | data/UIUC/Our_test/img/p9_1.jpg 218 | data/UIUC/Our_test/img/p9_2.jpg 219 | data/UIUC/Our_test/img/p9_n.jpg 220 | data/UIUC/Our_test/original/DSC_0394.jpg 221 | data/UIUC/Our_test/original/DSC_0400.jpg 222 | data/UIUC/Our_test/original/DSC_0445.jpg 223 | data/UIUC/Our_test/original/DSC_0531.jpg 224 | data/UIUC/Our_test/original/DSC_0552.jpg 225 | data/UIUC/Our_test/original/DSC_0559.jpg 226 | data/UIUC/Our_test/original/DSC_0569.jpg 227 | data/UIUC/Our_test/original/DSC_0577.jpg 228 | data/UIUC/Our_test/original/DSC_0587.jpg 229 | data/UIUC/Our_test/original/DSC_0594.jpg 230 | data/UIUC/Our_test/original/DSC_0599.jpg 231 | data/UIUC/Our_test/original/DSC_0607.jpg 232 | data/UIUC/Our_test/original/DSC_0625.jpg 233 | data/UIUC/Our_test/original/DSC_0633.jpg 234 | data/UIUC/Our_test/original/DSC_0637.jpg 235 | data/UIUC/Our_test/original/DSC_0641.jpg 236 | data/UIUC/Our_test/original/DSC_0647.jpg 237 | data/UIUC/Our_test/original/DSC_0649.jpg 238 | data/UIUC/Our_test/original/DSC_0655.jpg 239 | data/UIUC/Our_test/original/DSC_0661.jpg 240 | data/UIUC/Our_test/original/DSC_0665.jpg 241 | data/UIUC/Our_test/original/DSC_0669.jpg 242 | data/UIUC/Our_test/original/DSC_0673.jpg 243 | data/UIUC/Our_test/original/DSC_0677.jpg 244 | data/UIUC/Our_test/original/DSC_0681.jpg 245 | data/UIUC/Our_test/original/DSC_0685.jpg 246 | data/UIUC/Our_test/original/DSC_0689.jpg 247 | data/UIUC/Our_test/original/DSC_0693.jpg 248 | data/UIUC/Our_test/original/DSC_0697.jpg 249 | data/UIUC/Our_test/original/DSC_0701.jpg 250 | data/UIUC/Our_test/original/DSC_0705.jpg 251 | data/UIUC/Our_test/original/DSC_0709.jpg 252 | data/UIUC/Our_test/original/DSC_0713.jpg 253 | data/UIUC/Our_test/original/DSC_0717.jpg 254 | data/UIUC/Our_test/original/DSC_0721.jpg 255 | data/UIUC/Our_test/original/DSC_0725.jpg 256 | data/UIUC/Our_test/original/DSC_0729.jpg 257 | data/UIUC/Our_test/original/DSC_0733.jpg 258 | data/UIUC/Our_test/original/DSC_0736.jpg 259 | data/UIUC/Our_test/original/DSC_0741.jpg 260 | data/UIUC/Our_test/original/DSC_0745.jpg 261 | data/UIUC/Our_test/original/DSC_0749.jpg 262 | data/UIUC/Our_test/original/DSC_0753.jpg 263 | data/UIUC/Our_test/original/DSC_0757.jpg 264 | data/UIUC/Our_test/original/p10_1.jpg 265 | data/UIUC/Our_test/original/p11_1.jpg 266 | data/UIUC/Our_test/original/p12_1.jpg 267 | data/UIUC/Our_test/original/p12_2.jpg 268 | data/UIUC/Our_test/original/p13_1.jpg 269 | data/UIUC/Our_test/original/p14_1.jpg 270 | data/UIUC/Our_test/original/p14_2.jpg 271 | data/UIUC/Our_test/original/p15_1.jpg 272 | data/UIUC/Our_test/original/p16_1.jpg 273 | data/UIUC/Our_test/original/p16_2.jpg 274 | data/UIUC/Our_test/original/p17_1.jpg 275 | data/UIUC/Our_test/original/p17_2.jpg 276 | data/UIUC/Our_test/original/p18_1.jpg 277 | data/UIUC/Our_test/original/p18_2.jpg 278 | data/UIUC/Our_test/original/p19_1.jpg 279 | data/UIUC/Our_test/original/p1_1.jpg 280 | data/UIUC/Our_test/original/p1_2.jpg 281 | data/UIUC/Our_test/original/p1_3.jpg 282 | data/UIUC/Our_test/original/p20_1.jpg 283 | data/UIUC/Our_test/original/p21_1.jpg 284 | data/UIUC/Our_test/original/p21_2.jpg 285 | data/UIUC/Our_test/original/p2_1.jpg 286 | data/UIUC/Our_test/original/p2_2.jpg 287 | data/UIUC/Our_test/original/p3_1.jpg 288 | data/UIUC/Our_test/original/p4_1.jpg 289 | data/UIUC/Our_test/original/p6_1.jpg 290 | data/UIUC/Our_test/original/p6_2.jpg 291 | data/UIUC/Our_test/original/p7_1.jpg 292 | data/UIUC/Our_test/original/p7_2.jpg 293 | data/UIUC/Our_test/original/p8_1.jpg 294 | data/UIUC/Our_test/original/p9_1.jpg 295 | data/UIUC/Our_test/original/p9_2.jpg 296 | data/UIUC/Our_train/1.png 297 | data/UIUC/Our_train/10.png 298 | data/UIUC/Our_train/11.png 299 | data/UIUC/Our_train/12.png 300 | data/UIUC/Our_train/13.png 301 | data/UIUC/Our_train/14.png 302 | data/UIUC/Our_train/15.png 303 | data/UIUC/Our_train/16.png 304 | data/UIUC/Our_train/17.png 305 | data/UIUC/Our_train/18.png 306 | data/UIUC/Our_train/19.png 307 | data/UIUC/Our_train/2.png 308 | data/UIUC/Our_train/20.png 309 | data/UIUC/Our_train/21.png 310 | data/UIUC/Our_train/22.png 311 | data/UIUC/Our_train/23.png 312 | data/UIUC/Our_train/24.png 313 | data/UIUC/Our_train/25.png 314 | data/UIUC/Our_train/26.png 315 | data/UIUC/Our_train/27.png 316 | data/UIUC/Our_train/28.png 317 | data/UIUC/Our_train/29.png 318 | data/UIUC/Our_train/3.png 319 | data/UIUC/Our_train/30.png 320 | data/UIUC/Our_train/31.png 321 | data/UIUC/Our_train/32.png 322 | data/UIUC/Our_train/4.png 323 | data/UIUC/Our_train/5.png 324 | data/UIUC/Our_train/6.png 325 | data/UIUC/Our_train/7.png 326 | data/UIUC/Our_train/8.png 327 | data/UIUC/Our_train/9.png -------------------------------------------------------------------------------- /src/annotate.m: -------------------------------------------------------------------------------- 1 | function annotate(aline) 2 | %% 3 | % s = regexp(aline, '/', 'split'); 4 | % if strcmp(s{4}, 'ShadowImages') 5 | % im = imread(aline); 6 | % file_name = [s{1},'/',s{2},'/',s{3},'/annotation/',s{5}]; 7 | % file_name = [file_name(1:length(file_name)-4), '.mat']; 8 | % disp 'Segmenting' 9 | % [dummy seg] = edison_wrapper(im, @RGB2Luv, ... 10 | % 'SpatialBandWidth', 9, 'RangeBandWidth', 15, ... 11 | % 'MinimumRegionArea', 200); 12 | % seg = seg + 1; 13 | % s{4} = 'ShadowMasks' 14 | % mask_name = [s{1},'/',s{2},'/',s{3},'/',s{4},'/',s{5}]; 15 | % mask_name = [mask_name(1:length(mask_name)-4), '.png']; 16 | % mask = imread(mask_name); 17 | % shadow = []; 18 | % lit = []; 19 | % segnum = max(max(seg)); 20 | % temp = zeros(2, segnum); 21 | % for i = 1:size(mask, 1) 22 | % for j = 1:size(mask, 2) 23 | % if mask(i,j) == 0 24 | % temp(1, seg(i,j)) = temp(1, seg(i,j)) + 1; 25 | % else 26 | % temp(2, seg(i,j)) = temp(2, seg(i,j)) + 1; 27 | % end 28 | % end 29 | % end 30 | % for i = 1:size(temp, 2) 31 | % if temp(1, i) <= temp(2, i) 32 | % shadow = [shadow, i]; 33 | % else 34 | % lit = [lit, i]; 35 | % end 36 | % end 37 | % save(file_name, 'seg', 'shadow', 'lit', 'segnum'); 38 | % end 39 | 40 | %% 41 | s = regexp(aline, '/', 'split'); 42 | if strcmp(s{3}, 'Our_test') && strcmp(s{4}, 'original') 43 | aline 44 | im = imread(aline); 45 | file_name = [s{1},'/',s{2},'/',s{3},'/annotation/',s{5}]; 46 | file_name = [file_name(1:length(file_name)-4), '.mat']; 47 | disp 'Segmenting' 48 | [dummy seg] = edison_wrapper(im, @RGB2Luv, ... 49 | 'SpatialBandWidth', 9, 'RangeBandWidth', 15, ... 50 | 'MinimumRegionArea', 200); 51 | seg = seg + 1; 52 | s{4} = 'gt'; 53 | mask_name = [s{1},'/',s{2},'/',s{3},'/',s{4},'/',s{5}]; 54 | mask_name = [mask_name(1:length(mask_name)-4), '.png']; 55 | mask = imread(mask_name); 56 | mask = imresize(mask, [size(im,1), size(im,2)]); 57 | shadow = []; 58 | lit = []; 59 | segnum = max(max(seg)); 60 | temp = zeros(2, segnum); 61 | for i = 1:size(mask, 1) 62 | for j = 1:size(mask, 2) 63 | if mask(i,j) == 0 64 | temp(1, seg(i,j)) = temp(1, seg(i,j)) + 1; 65 | else 66 | temp(2, seg(i,j)) = temp(2, seg(i,j)) + 1; 67 | end 68 | end 69 | end 70 | for i = 1:size(temp, 2) 71 | if temp(1, i) <= temp(2, i) 72 | shadow = [shadow, i]; 73 | else 74 | lit = [lit, i]; 75 | end 76 | end 77 | save(file_name, 'seg', 'shadow', 'lit', 'segnum'); 78 | end 79 | end -------------------------------------------------------------------------------- /src/detect.m: -------------------------------------------------------------------------------- 1 | function [seg, segnum, between, near, centroids, label, grad, texthist]=detect(url) 2 | 3 | %im = imread('/Users/mac/Desktop/Image Processing/project/data/SBU-shadow/SBU-Test/ShadowImages/983314s.jpg'); 4 | %im = imread('/Users/mac/Desktop/Image Processing/project/data/SBU-shadow/SBU-Test/ShadowImages/3392986913_d12c3b0aa2_z.jpg'); 5 | %im = imread('/Users/mac/Desktop/Image Processing/project/data/SBU-shadow/SBU-Test/ShadowImages/42eda42s-960.jpg'); 6 | 7 | im = imread(url); 8 | 9 | %load('data/SBU-shadow/SBU-Test/annotation/983314s.mat', 'seg', 'shadow', 'lit', 'segnum'); 10 | %load('data/SBU-shadow/SBU-Test/annotation/3392986913_d12c3b0aa2_z.mat', 'seg', 'shadow', 'lit', 'segnum'); 11 | %load('data/SBU-shadow/SBU-Test/annotation/42eda42s-960.mat', 'seg', 'shadow', 'lit', 'segnum'); 12 | %load('data/UIUC/Our_test/annotation/p21_1.mat', 'seg', 'shadow', 'lit', 'segnum'); 13 | % try 14 | % load('data/UIUC/Our_test/annotation/lssd355.mat', 'seg', 'shadow', 'lit', 'segnum'); 15 | % catch exp1 16 | disp 'Segmenting' 17 | [dummy seg] = edison_wrapper(im, @RGB2Luv, ... 18 | 'SpatialBandWidth', 9, 'RangeBandWidth', 15, ... 19 | 'MinimumRegionArea', 200); 20 | seg = seg + 1; 21 | segnum = max(max(seg)); 22 | % end 23 | disp 'Detect' 24 | hsi = calHsi(im, seg, segnum); 25 | hsv = calHsv(im, seg, segnum); 26 | ycbcr = calYcbcr(im, seg, segnum); 27 | %labhist = calcLabHist(im, seg, segnum); 28 | texthist = calcTextonHist(im, seg, segnum); 29 | grad = calGradient(im, seg, segnum); 30 | 31 | shapemean = calcShapeMean(seg, segnum); 32 | area = shapemean.area; 33 | centroids = shapemean.center; 34 | epsilon=0.01; 35 | 36 | % ycbcr(:,1) = (ycbcr(:,1)- min(ycbcr(:,1)) + epsilon) / (max(ycbcr(:,1)) - min(ycbcr(:,1))); 37 | % hsv(:,3) = (hsv(:,3) - min(hsv(:,3)) + epsilon) / (max(hsv(:,3)) - min(hsv(:,3))); 38 | ycbcr(:,1) = ycbcr(:,1) / max(ycbcr(:,1)); 39 | hsv(:,1) = hsv(:,1) / max(hsv(:,1)); 40 | 41 | between = zeros([segnum, segnum]); 42 | for i = 1:size(between, 1) 43 | for j = 1:size(between, 2) 44 | %between(i, j) = sum((grad(i,:)-grad(j,:)).^2./(grad(i,:)+grad(j,:)+epsilon)) + sum((texthist(i,:)-texthist(j,:)).^2./(texthist(i,:)+texthist(j,:)+epsilon)); 45 | distance = [centroids(i,1), centroids(i,2); centroids(j,1), centroids(j,2)]; 46 | distance = sqrt((distance(1,1)-distance(2,1))^2 + (distance(1,2)-distance(2,2))^2) / max(size(im, 1), size(im, 2)); 47 | between(i, j) = sum(abs(grad(i,:) - grad(j,:))) + sum(abs(texthist(i,:) - texthist(j,:))) + distance; 48 | if i == j 49 | between(i,j) = 100; 50 | end 51 | end 52 | end 53 | 54 | nim = im; 55 | [gx gy] = gradient(double(seg)); 56 | eim = (gx.^2+gy.^2)>1e-10; 57 | t = nim(:,:,1); t(eim)=0; nim(:,:,1)=t; 58 | t = nim(:,:,2); t(eim)=0; nim(:,:,2)=t; 59 | t = nim(:,:,3); t(eim)=0; nim(:,:,3)=t; 60 | % imshow(nim); 61 | % hold on; 62 | 63 | near = zeros([1, segnum]); 64 | for i = 1:segnum 65 | [value, near(1, i)] = min(between(i,:)); 66 | j = near(1, i); 67 | plot([centroids(i,1) centroids(j,1)], [centroids(i,2) centroids(j,2)], 'b'); 68 | %text(centroids(i,1),centroids(i,2),[ num2str(centroids(i,1)) , num2str(centroids(i,2))]) 69 | %between(i, j) = sum(abs(grad(i,:) - grad(j,:))) + sum(abs(texthist(i,:) - texthist(j,:))); 70 | end 71 | 72 | % ll = zeros([1, length(near)]) + 255; 73 | % for i = 1:length(shadow) 74 | % j = shadow(i); 75 | % ll(j) = 0; 76 | % end 77 | hh = zeros([3, length(near)]); 78 | for i = 1:length(near) 79 | j = near(i); 80 | max_hsv = max(hsv(i,3), hsv(j,3)); 81 | min_hsv = min(hsv(i,3), hsv(j,3)); 82 | max_ycbcr = max(ycbcr(i,1), ycbcr(j,1)); 83 | min_ycbcr = min(ycbcr(i,1), ycbcr(j,1)); 84 | max_hsi = max((hsi(i,1)+1/255)/(hsi(i,3)+1/255), (hsi(j,1)+1/255)/(hsi(j,3)+1/255)); 85 | min_hsi = min((hsi(i,1)+1/255)/(hsi(i,3)+1/255), (hsi(j,1)+1/255)/(hsi(j,3)+1/255)); 86 | %hh(1, i) = min_hsv/max_hsv; 87 | hh(2, i) = min_ycbcr/max_ycbcr; 88 | hh(1, i) = min_hsi / max_hsi; 89 | %hh(3, i) = ll(i) == ll(j); 90 | end 91 | 92 | 93 | x = reshape(hsi(:,1)./hsi(:,3), [size(hsi,1),1]); 94 | [idx,center] = kmeans(x,2); 95 | c_std = zeros([2,1]); 96 | temp = idx == 1; 97 | c_std(1,1) = std(x(temp)); 98 | temp = idx == 2; 99 | c_std(2,1) = std(x(temp)); 100 | if center(1,1) > center(2,1) 101 | center = sort(center); 102 | temp = c_std(1,1); 103 | c_std(1,1) = c_std(2,1); 104 | c_std(2,1) = temp; 105 | end 106 | 107 | 108 | %% 109 | 110 | label = zeros([1, segnum]) + 255; 111 | ycbcr_copy = ycbcr; 112 | n_nonshadow = segnum; 113 | avg_y = mean(ycbcr(:,1)); 114 | flag = 0; 115 | t_hsi = hsi(:,1) ./ hsi(:,3); 116 | level = graythresh(t_hsi); 117 | 118 | for i = 1:segnum 119 | 120 | if ycbcr(i,1) < avg_y * 0.6 121 | label(i) = 0; 122 | ycbcr_copy(i,:) = 0; 123 | n_nonshadow = n_nonshadow - 1; 124 | flag = flag + 1; 125 | end 126 | end 127 | 128 | %% 129 | refuse = zeros([1, segnum]); 130 | 131 | while 1 132 | update = 0; 133 | new = 0; 134 | max_v = 0; 135 | for i = 1:segnum 136 | val = hsi(i,1) / hsi(i, 3); 137 | temp1 = normcdf((val-center(2,1))/c_std(2,1)); 138 | temp2 = normcdf(-(val-center(1,1))/c_std(1,1)); 139 | if temp2 < temp1 && refuse(i) == 0 && label(i) == 255 140 | if temp1 > max_v 141 | 142 | new = i; 143 | max_v = temp1; 144 | update = 1; 145 | end 146 | end 147 | 148 | end 149 | if update == 0 || max_v < 0.0028 150 | break; 151 | end 152 | label(new) = 0; 153 | j = near(new); 154 | vali = hsi(i,1) / hsi(i, 3); 155 | valj = hsi(j,1) / hsi(j, 3); 156 | if ((vali-center(2,1))/c_std(2,1)) - ((valj-center(2,1))/c_std(2,1)) > 3 157 | refuse(j) = 1; 158 | label(j) = 255; 159 | 160 | end 161 | ycbcr_copy(i,:) = 0; 162 | n_nonshadow = n_nonshadow - 1; 163 | flag = flag + 1; 164 | end 165 | 166 | %% 167 | for i = 1:segnum 168 | if label(i) ~= 255 169 | continue 170 | end 171 | j = near(i); 172 | max_hsv = max(hsv(i,3), hsv(j,3)); 173 | min_hsv = min(hsv(i,3), hsv(j,3)); 174 | max_ycbcr = max(ycbcr(i,1), ycbcr(j,1)); 175 | min_ycbcr = min(ycbcr(i,1), ycbcr(j,1)); 176 | same = min_hsv / max_hsv + min_ycbcr / max_ycbcr + hh(1,i); 177 | 178 | if same > 2.5 && label(j) == 0 179 | hh(1,i) 180 | same 181 | label(i) = 0; 182 | end 183 | end 184 | %% 185 | imshow(label(seg)) 186 | %% 187 | % while flag < segnum 188 | % 189 | % new = 0; 190 | % new_value = 0; 191 | % new_2 = 0; 192 | % new_2_value = 0; 193 | % distance = 1e10; 194 | % miao = 0; 195 | % ff = 0; 196 | % for i = 1:segnum 197 | % if label(i) ~= 1 198 | % continue 199 | % end 200 | % j = near(i); 201 | % max_hsv = max(hsv(i,3), hsv(j,3)); 202 | % min_hsv = min(hsv(i,3), hsv(j,3)); 203 | % max_ycbcr = max(ycbcr(i,1), ycbcr(j,1)); 204 | % min_ycbcr = min(ycbcr(i,1), ycbcr(j,1)); 205 | % max_hsi = max(hsi(i,1)/hsi(i,3), hsi(j,1)/hsi(j,3)); 206 | % min_hsi = min(hsi(i,1)/hsi(i,3), hsi(j,1)/hsi(j,3)); 207 | % same = hh(1,i) + min_ycbcr / max_ycbcr; 208 | % 209 | % if label(j) ~= 1 210 | % if same > center(2, 1) - 2*c_std(2, 1) 211 | % faith = 1 / normcdf((same-center(2,1))/c_std(2,1)); 212 | % if between(i,j)*faith < distance 213 | % new = i; 214 | % new_value = label(j); 215 | % new_2 = 0; 216 | % new_2_value = 0; 217 | % distance = between(i,j)*faith; 218 | % ff = faith; 219 | % end 220 | % end 221 | % if same < center(1, 1) + 2*c_std(1, 1) 222 | % faith = 1 / normcdf(-(same-center(1,1))/c_std(1,1)); 223 | % if between(i,j)*faith < distance 224 | % new = i; 225 | % new_value = abs(255-label(j)); 226 | % new_2 = 0; 227 | % new_2_value = 0; 228 | % distance = between(i,j)*faith; 229 | % ff = faith; 230 | % end 231 | % end 232 | % else 233 | % if same > center(1, 1) + 2*c_std(1, 1) 234 | % ; 235 | % else 236 | % if hsi(i,1)/hsi(i,3) > hsi(j,1)/hsi(j,3) %hsv(i,3) < hsv(j,3) && ycbcr(i,1) < ycbcr(j,1) 237 | % faith = 1 / normcdf(-(same-center(1,1))/c_std(1,1)); 238 | % if between(i,j)*faith < distance 239 | % new = i; 240 | % new_value = 0; 241 | % new_2 = j; 242 | % new_2_value = 255; 243 | % distance = between(i,j)*faith; 244 | % ff = faith; 245 | % end 246 | % 247 | % else% hsv(i,3) > hsv(j,3) && ycbcr(i,1) > ycbcr(j,1) 248 | % faith = 1 / normcdf(-(same-center(1,1))/c_std(1,1)); 249 | % if between(i,j)*faith < distance 250 | % new = i; 251 | % new_value = 255; 252 | % new_2 = j; 253 | % new_2_value = 0; 254 | % distance = between(i,j)*faith; 255 | % ff = faith; 256 | % end 257 | % end 258 | % end 259 | % end 260 | % end 261 | % 262 | % if distance ~= 1e10 263 | % label(new) = new_value; 264 | % 265 | % 266 | % ycbcr_copy(new,:) = 0; 267 | % n_nonshadow = n_nonshadow - 1; 268 | % 269 | % if new_2 ~= 0 270 | % label(new_2) = new_2_value; 271 | % 272 | % ycbcr_copy(new_2,:) = 0; 273 | % n_nonshadow = n_nonshadow - 1; 274 | % flag = flag + 1; 275 | % end 276 | % 277 | % if label(near(1, new)) == label(new) 278 | % plot([centroids(new,1) centroids(near(1, new),1)], [centroids(new,2) centroids(near(1, new),2)], 'b'); 279 | % else 280 | % 281 | % 282 | % plot([centroids(new,1) centroids(near(1, new),1)], [centroids(new,2) centroids(near(1, new),2)], 'r'); 283 | % end 284 | % flag = flag + 1; 285 | % miao = 1; 286 | % end 287 | % 288 | % 289 | % avg_y = sum(ycbcr_copy(:,1)) / double(n_nonshadow); 290 | % 291 | % for i = 1:segnum 292 | % if label(i) ~= 1 293 | % continue 294 | % end 295 | % if ycbcr(i,1) < avg_y * 0.6 296 | % label(i) = 0; 297 | % ycbcr_copy(i,:) = 0; 298 | % n_nonshadow = n_nonshadow - 1; 299 | % flag = flag + 1; 300 | % miao = 1; 301 | % 302 | % end 303 | % end 304 | % 305 | % if miao == 0 306 | % avg_y = sum(ycbcr(:,1)) / double(segnum); 307 | % max_temp = 0; 308 | % min_temp = 0; 309 | % min_y = 10; 310 | % max_y = 10; 311 | % for i = 1:segnum 312 | % if label(i) ~= 1 313 | % continue 314 | % end 315 | % if abs((hsi(i,1) / hsi(i,3) - center(1,1))/c_std(1,1)) < min_y 316 | % min_temp = i; 317 | % min_y = abs(hsi(i,1) / hsi(i,3) - center(1,1)); 318 | % end 319 | % if abs((hsi(i,1) / hsi(i,3) - center(2,1))/c_std(2,1)) < max_y 320 | % max_temp = i; 321 | % max_y = abs(hsi(i,1) / hsi(i,3) - center(2,1)); 322 | % end 323 | % end 324 | % 325 | % if max_y < min_y && max_temp~=0 326 | % 327 | % label(max_temp) = 0; 328 | % ycbcr_copy(max_temp,:) = 0; 329 | % n_nonshadow = n_nonshadow + 1; 330 | % flag = flag + 1; 331 | % else 332 | % label(min_temp) = 255; 333 | % ycbcr_copy(min_temp,:) = 0; 334 | % n_nonshadow = n_nonshadow + 1; 335 | % flag = flag + 1; 336 | % end 337 | % end 338 | % end 339 | % 340 | % 341 | end -------------------------------------------------------------------------------- /src/detect.m~: -------------------------------------------------------------------------------- 1 | %function [seg, segnum, between, near, centroids, label, grad, texthist]=detect(url) 2 | 3 | %im = imread('/Users/mac/Desktop/Image Processing/project/data/SBU-shadow/SBU-Test/ShadowImages/983314s.jpg'); 4 | %im = imread('/Users/mac/Desktop/Image Processing/project/data/SBU-shadow/SBU-Test/ShadowImages/3392986913_d12c3b0aa2_z.jpg'); 5 | %im = imread('/Users/mac/Desktop/Image Processing/project/data/SBU-shadow/SBU-Test/ShadowImages/42eda42s-960.jpg'); 6 | url = 'data/UIUC/Our_test/original/p21_1.jpg'; 7 | im = imre 8 | 9 | %load('data/SBU-shadow/SBU-Test/annotation/983314s.mat', 'seg', 'shadow', 'lit', 'segnum'); 10 | %load('data/SBU-shadow/SBU-Test/annotation/3392986913_d12c3b0aa2_z.mat', 'seg', 'shadow', 'lit', 'segnum'); 11 | %load('data/SBU-shadow/SBU-Test/annotation/42eda42s-960.mat', 'seg', 'shadow', 'lit', 'segnum'); 12 | load('data/UIUC/Our_test/annotation/p21_1.mat', 'seg', 'shadow', 'lit', 'segnum'); 13 | % try 14 | % load('data/UIUC/Our_test/annotation/lssd355.mat', 'seg', 'shadow', 'lit', 'segnum'); 15 | % catch exp1 16 | % disp 'Segmenting' 17 | % [dummy seg] = edison_wrapper(im, @RGB2Luv, ... 18 | % 'SpatialBandWidth', 9, 'RangeBandWidth', 15, ... 19 | % 'MinimumRegionArea', 200); 20 | % seg = seg + 1; 21 | % segnum = max(max(seg)); 22 | % end 23 | hsi = calHsi(im, seg, segnum); 24 | hsv = calHsv(im, seg, segnum); 25 | ycbcr = calYcbcr(im, seg, segnum); 26 | %labhist = calcLabHist(im, seg, segnum); 27 | texthist = calcTextonHist(im, seg, segnum); 28 | grad = calGradient(im, seg, segnum); 29 | 30 | shapemean = calcShapeMean(seg, segnum); 31 | area = shapemean.area; 32 | centroids = shapemean.center; 33 | epsilon=0.01; 34 | 35 | % ycbcr(:,1) = (ycbcr(:,1)- min(ycbcr(:,1)) + epsilon) / (max(ycbcr(:,1)) - min(ycbcr(:,1))); 36 | % hsv(:,3) = (hsv(:,3) - min(hsv(:,3)) + epsilon) / (max(hsv(:,3)) - min(hsv(:,3))); 37 | ycbcr(:,1) = ycbcr(:,1) / max(ycbcr(:,1)); 38 | hsv(:,1) = hsv(:,1) / max(hsv(:,1)); 39 | 40 | between = zeros([segnum, segnum]); 41 | for i = 1:size(between, 1) 42 | for j = 1:size(between, 2) 43 | %between(i, j) = sum((grad(i,:)-grad(j,:)).^2./(grad(i,:)+grad(j,:)+epsilon)) + sum((texthist(i,:)-texthist(j,:)).^2./(texthist(i,:)+texthist(j,:)+epsilon)); 44 | distance = [centroids(i,1), centroids(i,2); centroids(j,1), centroids(j,2)]; 45 | distance = sqrt((distance(1,1)-distance(2,1))^2 + (distance(1,2)-distance(2,2))^2) / max(size(im, 1), size(im, 2)); 46 | between(i, j) = sum(abs(grad(i,:) - grad(j,:))) + sum(abs(texthist(i,:) - texthist(j,:))) + distance; 47 | if i == j 48 | between(i,j) = 100; 49 | end 50 | end 51 | end 52 | 53 | nim = im; 54 | [gx gy] = gradient(double(seg)); 55 | eim = (gx.^2+gy.^2)>1e-10; 56 | t = nim(:,:,1); t(eim)=0; nim(:,:,1)=t; 57 | t = nim(:,:,2); t(eim)=0; nim(:,:,2)=t; 58 | t = nim(:,:,3); t(eim)=0; nim(:,:,3)=t; 59 | imshow(nim); 60 | hold on; 61 | 62 | near = zeros([1, segnum]); 63 | for i = 1:segnum 64 | [value, near(1, i)] = min(between(i,:)); 65 | j = near(1, i); 66 | plot([centroids(i,1) centroids(j,1)], [centroids(i,2) centroids(j,2)], 'b'); 67 | text(centroids(i,1),centroids(i,2),[ num2str(centroids(i,1)) , num2str(centroids(i,2))]) 68 | %between(i, j) = sum(abs(grad(i,:) - grad(j,:))) + sum(abs(texthist(i,:) - texthist(j,:))); 69 | end 70 | 71 | % ll = zeros([1, length(near)]) + 255; 72 | % for i = 1:length(shadow) 73 | % j = shadow(i); 74 | % ll(j) = 0; 75 | % end 76 | hh = zeros([3, length(near)]); 77 | for i = 1:length(near) 78 | j = near(i); 79 | max_hsv = max(hsv(i,3), hsv(j,3)); 80 | min_hsv = min(hsv(i,3), hsv(j,3)); 81 | max_ycbcr = max(ycbcr(i,1), ycbcr(j,1)); 82 | min_ycbcr = min(ycbcr(i,1), ycbcr(j,1)); 83 | max_hsi = max((hsi(i,1)+1/255)/(hsi(i,3)+1/255), (hsi(j,1)+1/255)/(hsi(j,3)+1/255)); 84 | min_hsi = min((hsi(i,1)+1/255)/(hsi(i,3)+1/255), (hsi(j,1)+1/255)/(hsi(j,3)+1/255)); 85 | %hh(1, i) = min_hsv/max_hsv; 86 | hh(2, i) = min_ycbcr/max_ycbcr; 87 | hh(1, i) = min_hsi / max_hsi; 88 | %hh(3, i) = ll(i) == ll(j); 89 | end 90 | 91 | 92 | x = reshape(hsi(:,1)./hsi(:,3), [size(hsi,1),1]); 93 | [idx,center] = kmeans(x,2); 94 | c_std = zeros([2,1]); 95 | temp = idx == 1; 96 | c_std(1,1) = std(x(temp)); 97 | temp = idx == 2; 98 | c_std(2,1) = std(x(temp)); 99 | if center(1,1) > center(2,1) 100 | center = sort(center); 101 | temp = c_std(1,1); 102 | c_std(1,1) = c_std(2,1); 103 | c_std(2,1) = temp; 104 | end 105 | 106 | 107 | %% 108 | 109 | label = zeros([1, segnum]) + 255; 110 | ycbcr_copy = ycbcr; 111 | n_nonshadow = segnum; 112 | avg_y = mean(ycbcr(:,1)); 113 | flag = 0; 114 | t_hsi = hsi(:,1) ./ hsi(:,3); 115 | level = graythresh(t_hsi); 116 | 117 | for i = 1:segnum 118 | 119 | if ycbcr(i,1) < avg_y * 0.6 120 | label(i) = 0; 121 | ycbcr_copy(i,:) = 0; 122 | n_nonshadow = n_nonshadow - 1; 123 | flag = flag + 1; 124 | end 125 | end 126 | 127 | %% 128 | refuse = zeros([1, segnum]); 129 | 130 | while 1 131 | update = 0; 132 | new = 0; 133 | max_v = 0; 134 | for i = 1:segnum 135 | val = hsi(i,1) / hsi(i, 3); 136 | temp1 = normcdf((val-center(2,1))/c_std(2,1)); 137 | temp2 = normcdf(-(val-center(1,1))/c_std(1,1)); 138 | if temp2 < temp1 && refuse(i) == 0 && label(i) == 255 139 | if temp1 > max_v 140 | 141 | new = i; 142 | max_v = temp1; 143 | update = 1; 144 | end 145 | end 146 | 147 | end 148 | if update == 0 || max_v < 0.0028 149 | break; 150 | end 151 | label(new) = 0; 152 | j = near(new); 153 | vali = hsi(i,1) / hsi(i, 3); 154 | valj = hsi(j,1) / hsi(j, 3); 155 | if ((vali-center(2,1))/c_std(2,1)) - ((valj-center(2,1))/c_std(2,1)) > 3 156 | refuse(j) = 1; 157 | label(j) = 255; 158 | 159 | end 160 | ycbcr_copy(i,:) = 0; 161 | n_nonshadow = n_nonshadow - 1; 162 | flag = flag + 1; 163 | end 164 | 165 | %% 166 | for i = 1:segnum 167 | if label(i) ~= 255 168 | continue 169 | end 170 | j = near(i); 171 | max_hsv = max(hsv(i,3), hsv(j,3)); 172 | min_hsv = min(hsv(i,3), hsv(j,3)); 173 | max_ycbcr = max(ycbcr(i,1), ycbcr(j,1)); 174 | min_ycbcr = min(ycbcr(i,1), ycbcr(j,1)); 175 | same = min_hsv / max_hsv + min_ycbcr / max_ycbcr + hh(1,i); 176 | 177 | if same > 2.5 && label(j) == 0 178 | hh(1,i) 179 | same 180 | label(i) = 0; 181 | end 182 | end 183 | 184 | %% 185 | % while flag < segnum 186 | % 187 | % new = 0; 188 | % new_value = 0; 189 | % new_2 = 0; 190 | % new_2_value = 0; 191 | % distance = 1e10; 192 | % miao = 0; 193 | % ff = 0; 194 | % for i = 1:segnum 195 | % if label(i) ~= 1 196 | % continue 197 | % end 198 | % j = near(i); 199 | % max_hsv = max(hsv(i,3), hsv(j,3)); 200 | % min_hsv = min(hsv(i,3), hsv(j,3)); 201 | % max_ycbcr = max(ycbcr(i,1), ycbcr(j,1)); 202 | % min_ycbcr = min(ycbcr(i,1), ycbcr(j,1)); 203 | % max_hsi = max(hsi(i,1)/hsi(i,3), hsi(j,1)/hsi(j,3)); 204 | % min_hsi = min(hsi(i,1)/hsi(i,3), hsi(j,1)/hsi(j,3)); 205 | % same = hh(1,i) + min_ycbcr / max_ycbcr; 206 | % 207 | % if label(j) ~= 1 208 | % if same > center(2, 1) - 2*c_std(2, 1) 209 | % faith = 1 / normcdf((same-center(2,1))/c_std(2,1)); 210 | % if between(i,j)*faith < distance 211 | % new = i; 212 | % new_value = label(j); 213 | % new_2 = 0; 214 | % new_2_value = 0; 215 | % distance = between(i,j)*faith; 216 | % ff = faith; 217 | % end 218 | % end 219 | % if same < center(1, 1) + 2*c_std(1, 1) 220 | % faith = 1 / normcdf(-(same-center(1,1))/c_std(1,1)); 221 | % if between(i,j)*faith < distance 222 | % new = i; 223 | % new_value = abs(255-label(j)); 224 | % new_2 = 0; 225 | % new_2_value = 0; 226 | % distance = between(i,j)*faith; 227 | % ff = faith; 228 | % end 229 | % end 230 | % else 231 | % if same > center(1, 1) + 2*c_std(1, 1) 232 | % ; 233 | % else 234 | % if hsi(i,1)/hsi(i,3) > hsi(j,1)/hsi(j,3) %hsv(i,3) < hsv(j,3) && ycbcr(i,1) < ycbcr(j,1) 235 | % faith = 1 / normcdf(-(same-center(1,1))/c_std(1,1)); 236 | % if between(i,j)*faith < distance 237 | % new = i; 238 | % new_value = 0; 239 | % new_2 = j; 240 | % new_2_value = 255; 241 | % distance = between(i,j)*faith; 242 | % ff = faith; 243 | % end 244 | % 245 | % else% hsv(i,3) > hsv(j,3) && ycbcr(i,1) > ycbcr(j,1) 246 | % faith = 1 / normcdf(-(same-center(1,1))/c_std(1,1)); 247 | % if between(i,j)*faith < distance 248 | % new = i; 249 | % new_value = 255; 250 | % new_2 = j; 251 | % new_2_value = 0; 252 | % distance = between(i,j)*faith; 253 | % ff = faith; 254 | % end 255 | % end 256 | % end 257 | % end 258 | % end 259 | % 260 | % if distance ~= 1e10 261 | % label(new) = new_value; 262 | % 263 | % 264 | % ycbcr_copy(new,:) = 0; 265 | % n_nonshadow = n_nonshadow - 1; 266 | % 267 | % if new_2 ~= 0 268 | % label(new_2) = new_2_value; 269 | % 270 | % ycbcr_copy(new_2,:) = 0; 271 | % n_nonshadow = n_nonshadow - 1; 272 | % flag = flag + 1; 273 | % end 274 | % 275 | % if label(near(1, new)) == label(new) 276 | % plot([centroids(new,1) centroids(near(1, new),1)], [centroids(new,2) centroids(near(1, new),2)], 'b'); 277 | % else 278 | % 279 | % 280 | % plot([centroids(new,1) centroids(near(1, new),1)], [centroids(new,2) centroids(near(1, new),2)], 'r'); 281 | % end 282 | % flag = flag + 1; 283 | % miao = 1; 284 | % end 285 | % 286 | % 287 | % avg_y = sum(ycbcr_copy(:,1)) / double(n_nonshadow); 288 | % 289 | % for i = 1:segnum 290 | % if label(i) ~= 1 291 | % continue 292 | % end 293 | % if ycbcr(i,1) < avg_y * 0.6 294 | % label(i) = 0; 295 | % ycbcr_copy(i,:) = 0; 296 | % n_nonshadow = n_nonshadow - 1; 297 | % flag = flag + 1; 298 | % miao = 1; 299 | % 300 | % end 301 | % end 302 | % 303 | % if miao == 0 304 | % avg_y = sum(ycbcr(:,1)) / double(segnum); 305 | % max_temp = 0; 306 | % min_temp = 0; 307 | % min_y = 10; 308 | % max_y = 10; 309 | % for i = 1:segnum 310 | % if label(i) ~= 1 311 | % continue 312 | % end 313 | % if abs((hsi(i,1) / hsi(i,3) - center(1,1))/c_std(1,1)) < min_y 314 | % min_temp = i; 315 | % min_y = abs(hsi(i,1) / hsi(i,3) - center(1,1)); 316 | % end 317 | % if abs((hsi(i,1) / hsi(i,3) - center(2,1))/c_std(2,1)) < max_y 318 | % max_temp = i; 319 | % max_y = abs(hsi(i,1) / hsi(i,3) - center(2,1)); 320 | % end 321 | % end 322 | % 323 | % if max_y < min_y && max_temp~=0 324 | % 325 | % label(max_temp) = 0; 326 | % ycbcr_copy(max_temp,:) = 0; 327 | % n_nonshadow = n_nonshadow + 1; 328 | % flag = flag + 1; 329 | % else 330 | % label(min_temp) = 255; 331 | % ycbcr_copy(min_temp,:) = 0; 332 | % n_nonshadow = n_nonshadow + 1; 333 | % flag = flag + 1; 334 | % end 335 | % end 336 | % end 337 | % 338 | % 339 | %end -------------------------------------------------------------------------------- /src/etract_feature/assignTextons.m: -------------------------------------------------------------------------------- 1 | function [map] = assignTextons(fim,textons) 2 | % function [map] = assignTextons(fim,textons) 3 | 4 | d = numel(fim); 5 | n = numel(fim{1}); 6 | data = zeros(d,n); 7 | for i = 1:d, 8 | data(i,:) = fim{i}(:)'; 9 | end 10 | 11 | d2 = distSqr(data,textons); 12 | [y,map] = min(d2,[],2); 13 | [w,h] = size(fim{1}); 14 | map = reshape(map,w,h); 15 | -------------------------------------------------------------------------------- /src/etract_feature/bsd300_128.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittenish/Image-Shadow-Detection-and-Removal/03de533b7ba1104a2551b0b670e7210d9c114b1e/src/etract_feature/bsd300_128.mat -------------------------------------------------------------------------------- /src/etract_feature/calAvghsv.m: -------------------------------------------------------------------------------- 1 | function desc = calAvghsv(im, seg, segnum) 2 | end -------------------------------------------------------------------------------- /src/etract_feature/calGradient.m: -------------------------------------------------------------------------------- 1 | function desc = calGradient(rgb_im, seg, numRegion) 2 | 3 | G = fspecial('gaussian',[4 4],2); 4 | Ig = imfilter(rgb_im,G,'same'); 5 | [Gmag,Gdir] = imgradient(rgb2gray(Ig)); 6 | 7 | binNum = 20; 8 | inter = max(max(Gmag)) / binNum; 9 | Gmag = int32(Gmag / inter); 10 | 11 | binVal = 1:binNum; 12 | desc = zeros([numRegion binNum]); 13 | 14 | cnt = 0; 15 | ind={}; 16 | for iReg=1:numRegion 17 | ind{iReg} = seg(:)==iReg; 18 | end 19 | 20 | for bin=1:binNum 21 | cnt = cnt + 1; 22 | I = (Gmag(:)==binVal(bin)) ; 23 | for iReg=1:numRegion 24 | desc(iReg,cnt) = sum(I(ind{iReg})); 25 | end 26 | end 27 | 28 | tmp = sum(desc, 2); 29 | desc = desc ./ repmat(tmp(:,:), [1 size(desc,2)]); 30 | 31 | end -------------------------------------------------------------------------------- /src/etract_feature/calHsi.m: -------------------------------------------------------------------------------- 1 | function desc = calHsi(rgb_im, seg, numRegion) 2 | 3 | 4 | hsi = rgb2hsi_refine(rgb_im); 5 | 6 | ind={}; 7 | for iReg=1:numRegion 8 | ind{iReg} = seg(:)==iReg; 9 | end 10 | 11 | desc = zeros([numRegion 3]); 12 | 13 | for ch=1:3 14 | for iReg = 1:numRegion 15 | feature = hsi(:,:,ch); 16 | feature = feature(ind{iReg}); 17 | desc(iReg, ch) = sum(feature) / sum(ind{iReg}); 18 | end 19 | end 20 | 21 | end 22 | -------------------------------------------------------------------------------- /src/etract_feature/calHsv.m: -------------------------------------------------------------------------------- 1 | function desc = calHsv(rgb_im, seg, numRegion) 2 | im = rgb2hsv(rgb_im); 3 | 4 | ind={}; 5 | for iReg=1:numRegion 6 | ind{iReg} = seg(:)==iReg; 7 | end 8 | 9 | desc = zeros([numRegion 3]); 10 | 11 | for ch=1:3 12 | for iReg = 1:numRegion 13 | feature = im(:,:,ch); 14 | feature = feature(ind{iReg}); 15 | desc(iReg, ch) = sum(feature) / sum(ind{iReg}); 16 | end 17 | end 18 | 19 | end -------------------------------------------------------------------------------- /src/etract_feature/calHsvHist.m: -------------------------------------------------------------------------------- 1 | function desc = calHsvHist(im, seg, numRegion) 2 | 3 | binNum = 50; 4 | binVal = 0:1/(binNum):1; 5 | desc = zeros([numRegion binNum*3]); 6 | 7 | cnt = 0; 8 | ind={}; 9 | for iReg=1:numRegion 10 | ind{iReg} = seg(:)==iReg; 11 | end 12 | 13 | for ch=1:3 14 | for bin=1:binNum 15 | cnt = cnt + 1; 16 | I = im(:,:,ch); 17 | I = ( (I>=binVal(bin)) & (I=binVal(bin)) & (I=binVal(bin)) & (In. 15 | % 16 | % David Martin 17 | % March 2003 18 | 19 | % Based on dist2.m code, 20 | % Copyright (c) Christopher M Bishop, Ian T Nabney (1996, 1997) 21 | 22 | if size(x,1)~=size(y,1), 23 | error('size(x,1)~=size(y,1)'); 24 | end 25 | 26 | [d,n] = size(x); 27 | [d,m] = size(y); 28 | 29 | % z = repmat(sum(x.^2)',1,m) ... 30 | % + repmat(sum(y.^2),n,1) ... 31 | % - 2*x'*y; 32 | 33 | z = x'*y; 34 | x2 = sum(x.^2)'; 35 | y2 = sum(y.^2); 36 | for i = 1:m, 37 | z(:,i) = x2 + y2(i) - 2*z(:,i); 38 | end 39 | 40 | -------------------------------------------------------------------------------- /src/etract_feature/fbRun.m: -------------------------------------------------------------------------------- 1 | function [fim] = fbRun(fb,im) 2 | % function [fim] = fbRun(fb,im) 3 | % 4 | % Run a filterbank on an image with reflected boundary conditions. 5 | % 6 | % See also fbCreate,padReflect. 7 | % 8 | % David R. Martin 9 | % March 2003 10 | 11 | % find the max filter size 12 | maxsz = max(size(fb{1})); 13 | for i = 1:numel(fb), 14 | maxsz = max(maxsz,max(size(fb{i}))); 15 | end 16 | 17 | % pad the image 18 | r = floor(maxsz/2); 19 | impad = padReflect(im,r); 20 | 21 | % run the filterbank on the padded image, and crop the result back 22 | % to the original image size 23 | fim = cell(size(fb)); 24 | for i = 1:numel(fb), 25 | if size(fb{i},1)<50, 26 | fim{i} = conv2(impad,fb{i},'same'); 27 | else 28 | fim{i} = fftconv2(impad,fb{i}); 29 | end 30 | fim{i} = fim{i}(r+1:end-r,r+1:end-r); 31 | end 32 | -------------------------------------------------------------------------------- /src/etract_feature/fftconv2.m: -------------------------------------------------------------------------------- 1 | function fim = fftconv2(im,f) 2 | % function fim = fftconv2(im,f) 3 | % 4 | % Convolution using FFT. 5 | % 6 | % David R. Martin 7 | % March 2003 8 | 9 | % wrap the filter around the origin and pad with zeros 10 | padf = zeros(size(im)); 11 | r = floor(size(f,1)/2); 12 | padf(1:r+1,1:r+1) = f(r+1:end,r+1:end); 13 | padf(1:r,end-r+1:end) = f(r+2:end,1:r); 14 | padf(end-r+1:end,1:r) = f(1:r,r+2:end); 15 | padf(end-r+1:end,end-r+1:end) = f(1:r,1:r); 16 | 17 | % magic 18 | fftim = fft2(im); 19 | fftf = fft2(padf); 20 | fim = real(ifft2(fftim.*fftf)); 21 | 22 | -------------------------------------------------------------------------------- /src/etract_feature/lab2rgb.m: -------------------------------------------------------------------------------- 1 | function [R, G, B] = lab2rgb(L, a, b) 2 | %LAB2RGB Convert an image from CIELAB to RGB 3 | % 4 | % function [R, G, B] = Lab2RGB(L, a, b) 5 | % function [R, G, B] = Lab2RGB(I) 6 | % function I = Lab2RGB(...) 7 | % 8 | % Lab2RGB takes L, a, and b double matrices, or an M x N x 3 double 9 | % image, and returns an image in the RGB color space. Values for L are in 10 | % the range [0,100] while a* and b* are roughly in the range [-110,110]. 11 | % If 3 outputs are specified, the values will be returned as doubles in the 12 | % range [0,1], otherwise the values will be uint8s in the range [0,255]. 13 | % 14 | % This transform is based on ITU-R Recommendation BT.709 using the D65 15 | % white point reference. The error in transforming RGB -> Lab -> RGB is 16 | % approximately 10^-5. 17 | % 18 | % See also RGB2LAB. 19 | 20 | % By Mark Ruzon from C code by Yossi Rubner, 23 September 1997. 21 | % Updated for MATLAB 5 28 January 1998. 22 | % Fixed a bug in conversion back to uint8 9 September 1999. 23 | % Updated for MATLAB 7 30 March 2009. 24 | 25 | if nargin == 1 26 | b = L(:,:,3); 27 | a = L(:,:,2); 28 | L = L(:,:,1); 29 | end 30 | 31 | % Thresholds 32 | T1 = 0.008856; 33 | T2 = 0.206893; 34 | 35 | [M, N] = size(L); 36 | s = M * N; 37 | L = reshape(L, 1, s); 38 | a = reshape(a, 1, s); 39 | b = reshape(b, 1, s); 40 | 41 | % Compute Y 42 | fY = ((L + 16) / 116) .^ 3; 43 | YT = uint8(fY > T1); 44 | fY = uint8(~YT) .* (L / 903.3) + YT .* fY; 45 | Y = fY; 46 | 47 | % Alter fY slightly for further calculations 48 | fY = uint8(double(YT) .* (double(fY) .^ (1/3)) + double(~YT) .* (7.787 .* double(fY) + 16/116)); 49 | 50 | % Compute X 51 | fX = a / 500 + fY; 52 | XT = fX > T2; 53 | X = uint8((double(XT) .* (double(fX) .^ 3) + double(~XT) .* ((double(fX) - 16/116) / 7.787))); 54 | 55 | % Compute Z 56 | fZ = fY - b / 200; 57 | ZT = fZ > T2; 58 | Z = uint8((double(ZT) .* (double(fZ) .^ 3) + double(~ZT) .* ((double(fZ) - 16/116) / 7.787))); 59 | 60 | % Normalize for D65 white point 61 | X = X * 0.950456; 62 | Z = Z * 1.088754; 63 | 64 | % XYZ to RGB 65 | MAT = [ 3.240479 -1.537150 -0.498535; 66 | -0.969256 1.875992 0.041556; 67 | 0.055648 -0.204043 1.057311]; 68 | 69 | RGB = max(min(uint8(MAT * double([X; Y; Z])), 1), 0); 70 | 71 | R = reshape(RGB(1,:), M, N); 72 | G = reshape(RGB(2,:), M, N); 73 | B = reshape(RGB(3,:), M, N); 74 | 75 | if nargout < 2 76 | R = uint8(round(cat(3,R,G,B) * 255)); 77 | end 78 | -------------------------------------------------------------------------------- /src/etract_feature/padReflect.m: -------------------------------------------------------------------------------- 1 | function [impad] = padReflect(im,r) 2 | % function [impad] = padReflect(im,r) 3 | % 4 | % Pad an image with a border of size r, and reflect the image into 5 | % the border. 6 | % 7 | % David R. Martin 8 | % March 2003 9 | 10 | impad = zeros(size(im)+2*r); 11 | impad(r+1:end-r,r+1:end-r) = im; % middle 12 | impad(1:r,r+1:end-r) = flipud(im(1:r,:)); % top 13 | impad(end-r+1:end,r+1:end-r) = flipud(im(end-r+1:end,:)); % bottom 14 | impad(r+1:end-r,1:r) = fliplr(im(:,1:r)); % left 15 | impad(r+1:end-r,end-r+1:end) = fliplr(im(:,end-r+1:end)); % right 16 | impad(1:r,1:r) = flipud(fliplr(im(1:r,1:r))); % top-left 17 | impad(1:r,end-r+1:end) = flipud(fliplr(im(1:r,end-r+1:end))); % top-right 18 | impad(end-r+1:end,1:r) = flipud(fliplr(im(end-r+1:end,1:r))); % bottom-left 19 | impad(end-r+1:end,end-r+1:end) = flipud(fliplr(im(end-r+1:end,end-r+1:end))); % bottom-right 20 | -------------------------------------------------------------------------------- /src/etract_feature/rgb2hsi.m: -------------------------------------------------------------------------------- 1 | function hsi = rgb2hsi(x) 2 | 3 | F=im2double(x); 4 | r=F(:,:,1); 5 | g=F(:,:,2); 6 | b=F(:,:,3); 7 | th=acos((0.5*((r-g)+(r-b)))./((sqrt((r-g).^2+(r-b).*(g-b)))+eps)); 8 | H=th; 9 | H(b>g)=2*pi-H(b>g); 10 | H=H/(2*pi); 11 | S=1-3.*(min(min(r,g),b))./(r+g+b+eps); 12 | I=(r+g+b)/3; 13 | hsi=cat(3,H,S,I); 14 | 15 | end 16 | 17 | -------------------------------------------------------------------------------- /src/etract_feature/rgb2hsi_refine.m: -------------------------------------------------------------------------------- 1 | function hsi = rgb2hsi_refine(x) 2 | 3 | F=im2double(x); 4 | r=F(:,:,1); 5 | g=F(:,:,2); 6 | b=F(:,:,3); 7 | 8 | I=(r+g+b)/3; 9 | V1 = - sqrt(1/6)*r - sqrt(1/6)*g + sqrt(2/3)*b; 10 | V2 = sqrt(1/6)*r - sqrt(2/3)*g; 11 | S = sqrt(V1.*V1 + V2.*V2); 12 | H = (tan(V2./V1)).^(-1); 13 | 14 | I = (I - min(min(I))) ./ (max(max(I)) - min(min(I))); 15 | H(isinf(H)) = max(H(~isinf(H))); 16 | H(isnan(H)) = max(H(~isnan(H))); 17 | H = (H - min(min(H))) ./ (max(max(H)) - min(min(H))); 18 | 19 | hsi=cat(3,H,S,I); 20 | 21 | end 22 | 23 | -------------------------------------------------------------------------------- /src/etract_feature/rgb2lab.m: -------------------------------------------------------------------------------- 1 | function lab = rgb2lab(rgb_im) 2 | 3 | if ~isa(rgb_im,'uint8'), 4 | rgb_im = im2uint8(rgb_im); 5 | end 6 | 7 | cform = makecform('srgb2lab'); 8 | lab = applycform(rgb_im,cform); 9 | 10 | end -------------------------------------------------------------------------------- /src/findshadowcut_cross.m: -------------------------------------------------------------------------------- 1 | function [hardmap] = findshadow(opt) 2 | if ~isfield(opt, 'save') 3 | opt.save = 0; 4 | end 5 | if ~isfield(opt, 'adjecent') 6 | opt.adjecent = 0; 7 | end 8 | if ~isfield(opt, 'pairwiseonly') 9 | opt.pairwiseonly = 0; 10 | end 11 | basename = opt.fn(1:end-4); 12 | im=imread([opt.dir 'original/' opt.fn]); 13 | 14 | %im = (double(im)/255).^(.45).*255; 15 | 16 | oim = im; 17 | org_siz = [size(im, 1) size(im, 2)]; 18 | 19 | if opt.resize == 0 20 | resiz_ratio = 1; 21 | else 22 | resiz_ratio = 640/org_siz(2); 23 | end 24 | im = imresize(oim, resiz_ratio); 25 | if opt.linearize 26 | gamma_im = (double(im)/255).^(1/.45); 27 | im = gamma_im; 28 | end 29 | %im = (double(im)/255); 30 | %im = (double(im)/255).^(1/.45); 31 | 32 | try 33 | load([opt.dir 'cache/' basename '_seg.mat']); 34 | catch exp1 35 | disp 'Segmenting' 36 | [dummy seg] = edison_wrapper(im, @RGB2Luv, ... 37 | 'SpatialBandWidth', 9, 'RangeBandWidth', 15, ... 38 | 'MinimumRegionArea', 200); 39 | seg = seg + 1; 40 | save([opt.dir 'cache/' basename '_seg.mat'], 'seg'); 41 | end 42 | 43 | numlabel = length(unique(seg(:))); 44 | 45 | try 46 | load([opt.dir 'cache/' basename '_single.mat']); 47 | catch exp1 48 | disp 'Single region feature' 49 | 50 | %load('cache/model_our.mat', 'model'); 51 | labhist = calcLabHist(im, seg, numlabel); 52 | texthist = calcTextonHistNoInv(im, seg, numlabel); 53 | hsv = calHsv(im, seg, numlabel, 10); 54 | testdata=[labhist, texthist, hsv]; 55 | testlabel = ones(numlabel, 1); 56 | save([opt.dir 'cache/' basename '_single.mat'], 'testdata', 'testlabel'); 57 | end 58 | %load(opt.unaryClassifier, 'model'); 59 | load(opt.unaryClassifier, 'model'); 60 | disp 'Single region classification' 61 | %[err, s]=svmclassify(testdata, testlabel, model); 62 | %s 63 | [s]=svmpredict(testlabel, testdata, model); 64 | ss = double(sign(s)); 65 | ssmap=(1-ss(seg))/2; 66 | min(min(ssmap)) 67 | try 68 | load([opt.dir 'cache/' basename '_pair.mat']); 69 | catch exp1 70 | 71 | disp 'Pair region feature' 72 | 73 | shapemean = calcShapeMean(seg, numlabel); 74 | area = shapemean.area; 75 | centroids = shapemean.center; 76 | rgbmean = calcRGBMean(im, seg, numlabel); 77 | texthist = calcTextonHist(im, seg, numlabel); 78 | labhist = calcLabHist(im, seg, numlabel); 79 | d_text=dist_chi2(texthist', texthist'); 80 | d_lab =dist_chi2(labhist', labhist'); 81 | 82 | ind = []; 83 | finalvector = zeros(numlabel,numlabel, 8); 84 | 85 | for i=1:numlabel 86 | for j=1:numlabel 87 | if i==j , continue; end 88 | r = rgbmean(i,:)./rgbmean(j,:); 89 | dist = norm(centroids(i,:) - centroids(j,:))/sqrt(sqrt(area(i)*area(j))); 90 | finalfeature = [d_text(i,j), d_lab(i,j), ... 91 | r, dist, abs(r(1)./r(2)-1)*10, abs(r(2)./r(3)-1)*10]; 92 | finalvector(j,i,:)=finalfeature; 93 | end 94 | end 95 | finalvector = reshape(finalvector, [numlabel*numlabel 8]); 96 | g_diff = zeros(numlabel, numlabel); 97 | g_same = zeros(numlabel, numlabel); 98 | save([opt.dir 'cache/' basename '_pair.mat'], 'finalvector', 'shapemean'); 99 | end 100 | load(opt.binaryClassifier, 'diffmodel', 'samemodel'); 101 | disp 'Pair region classification' 102 | [err, s1]=svmclassify(finalvector, zeros(numlabel*numlabel, 1) , diffmodel); 103 | [err, s2]=svmclassify(finalvector, zeros(numlabel*numlabel, 1), samemodel); 104 | s1 = reshape(s1, [numlabel numlabel]); 105 | s2 = reshape(s2, [numlabel numlabel]); 106 | k1=100; k2=200; 107 | t1 = sort(s1(:));t1(isnan(t1))=[]; 108 | t2 = sort(s2(:));t2(isnan(t2))=[]; 109 | thresh1 = t1(max(1, length(t1(:))-k1)); thresh1 = max(.6, thresh1); 110 | thresh2 = t2(max(1, length(t2(:))-k2)); thresh2 = max(.6, thresh2); 111 | % thresh1 = t1(max(1, length(t1(:))-k1)); thresh1 = max(0, thresh1); 112 | % thresh2 = t2(max(1, length(t2(:))-k2)); thresh2 = max(0, thresh2); 113 | % thresh1 = 0; 114 | % thresh2 = 0; 115 | %FIXME!!!!!!!!!!!! 116 | if strcmp('models/model_pair_our.mat', opt.binaryClassifier) 117 | for i=1:numlabel 118 | for j=1:numlabel 119 | if i==j , continue; end 120 | w = sqrt(shapemean.area(i)*shapemean.area(j)); 121 | g_diff(i,j) = w*s1(j,i); 122 | g_same(i,j) = w*s2(j,i); 123 | end 124 | end 125 | else 126 | for i=1:numlabel 127 | for j=1:numlabel 128 | if i==j , continue; end 129 | w = sqrt(shapemean.area(i)*shapemean.area(j)); 130 | g_diff(i,j) = w*s1(i,j); 131 | g_same(i,j) = w*s2(i,j); 132 | end 133 | end 134 | end 135 | 136 | nNodes = numlabel; 137 | nStates = 2; 138 | adj1 = logical(sparse(nNodes,nNodes)); 139 | adj2 = logical(sparse(nNodes,nNodes)); 140 | 141 | m=buildAdjacentMatrix(seg, numlabel); 142 | 143 | for i=1:numlabel 144 | for j=1:numlabel 145 | if opt.adjecent, 146 | if ~m(i,j) continue; end; 147 | end; 148 | if s1(i,j)>thresh1 149 | adj1(i,j)=1; 150 | end 151 | if s2(i,j)>thresh2 152 | adj2(i,j)=1; 153 | end 154 | end 155 | end 156 | 157 | nodePot = zeros(nNodes,nStates); 158 | w1 = 1; 159 | if ~opt.pairwiseonly 160 | for i=1:numlabel 161 | wi = w1 * shapemean.area(i); 162 | nodePot(i,1) = -s(i)*wi; 163 | nodePot(i,2) = s(i)*wi; 164 | end 165 | end 166 | 167 | if 1 168 | sc = shapemean.center; 169 | nim = im; 170 | [gx gy] = gradient(double(seg)); 171 | eim = (gx.^2+gy.^2)>1e-10; 172 | 173 | t = nim(:,:,1); t(eim)=0; nim(:,:,1)=t; 174 | t = nim(:,:,2); t(eim)=0; nim(:,:,2)=t; 175 | t = nim(:,:,3); t(eim)=0; nim(:,:,3)=t; 176 | f3 = figure(3); 177 | clf; 178 | %imshow(nim); 179 | %hold on 180 | for i=1:numlabel 181 | for j=1:numlabel 182 | if ~adj1(i,j) && ~adj2(i,j), continue; end 183 | if s1(i,j)>thresh1 184 | plot([sc(i,1) sc(j,1)], [sc(i,2) sc(j,2)], 'b'); 185 | plot(sc(i,1), sc(i,2), 'bo') 186 | elseif s2(i,j)>thresh2 187 | plot([sc(i,1) sc(j,1)], [sc(i,2) sc(j,2)], 'r'); 188 | end 189 | end 190 | end 191 | %print(f3, '-dpsc', [opt.dir 'cache/' basename '_graph.eps']); 192 | if ~opt.save 193 | figure(3) 194 | imshow(nim); 195 | hold on 196 | for i=1:numlabel 197 | for j=1:numlabel 198 | if ~adj1(i,j) && ~adj2(i,j), continue; end 199 | if s1(i,j)>thresh1 200 | plot([sc(i,1) sc(j,1)], [sc(i,2) sc(j,2)], 'b'); 201 | plot(sc(i,1), sc(i,2), 'bo') 202 | elseif s2(i,j)>thresh2 203 | plot([sc(i,1) sc(j,1)], [sc(i,2) sc(j,2)], 'r'); 204 | end 205 | end 206 | end 207 | figure(6), imagesc(s(seg)) 208 | figure(4), imagesc(seg) 209 | else 210 | imwrite(nim,[opt.dir 'segment/' basename '_segment.png']); 211 | end 212 | end 213 | 214 | edgeStruct1 = UGM_makeEdgeStruct_directed(adj1,nStates); 215 | edgeStruct2 = UGM_makeEdgeStruct_directed(adj2,nStates); 216 | edgePot = []; 217 | w3=1; 218 | w2=2; 219 | for e = 1:edgeStruct1.nEdges 220 | n1 = edgeStruct1.edgeEnds(e,1); 221 | n2 = edgeStruct1.edgeEnds(e,2); 222 | %nodePot(n1,1) = nodePot(n1,1)+ w2*(g_diff(n1, n2)-g_diff(n2, n1)); 223 | %nodePot(n2,2) = nodePot(n2,2)+ w2*(g_diff(n1, n2)-g_diff(n2, n1)); 224 | nodePot(n1,1) = nodePot(n1,1)+ w2*g_diff(n1, n2); 225 | nodePot(n1,2) = nodePot(n1,2)- w2*g_diff(n1, n2); 226 | nodePot(n2,1) = nodePot(n2,1)- w2*g_diff(n1, n2); 227 | nodePot(n2,2) = nodePot(n2,2)+ w2*g_diff(n1, n2); 228 | end 229 | 230 | for e = 1:edgeStruct2.nEdges 231 | n1 = edgeStruct2.edgeEnds(e,1); 232 | n2 = edgeStruct2.edgeEnds(e,2); 233 | 234 | edgePot(:,:,e) = [g_same(n1, n2) 0;... 235 | 0, g_same(n1, n2) ].*[w3 1; 1 w3]; 236 | end 237 | 238 | if ~isempty(edgePot) 239 | Decoding = UGM_Decode_ModifiedCut(nodePot,edgePot,edgeStruct2); 240 | else 241 | Decoding = double(sign(s))+1; 242 | end 243 | 244 | hardmap = Decoding(seg)-1; 245 | 246 | if ~opt.save 247 | figure(4) 248 | ss = double(sign(s)); 249 | ssmap=(1-ss(seg))/2; 250 | imshow(double(ssmap)) 251 | 252 | figure(5) 253 | imshow(1-double(hardmap)) 254 | 255 | figure(1); 256 | imshow(im); 257 | else 258 | imwrite(hardmap,[opt.dir 'binary/' basename '_binary.png']); 259 | ss = double(sign(s)); 260 | ssmap=(1-ss(seg))/2; 261 | imwrite(1-ssmap,[opt.dir 'unary/' basename '_unary.png']); 262 | save([opt.dir 'cache/' basename '_detect.mat'],'hardmap','ssmap'); 263 | 264 | shadowim = hardmap; 265 | 266 | tmp = ones(size(im)); 267 | tmp2 = cat(3, zeros([size(shadowim) 2]), 0.5*shadowim); 268 | 269 | mask = logical(repmat(shadowim, [1 1 3])); 270 | tmp(mask) = tmp2(mask); 271 | %tmp = shadowim.*ones(size(shadowim)); 272 | %tmp = cat(3, zeros([size(shadowim) 2]), shadowim); 273 | 274 | grayim = im2double(repmat(rgb2gray(im), [1 1 3])); 275 | im2 = (grayim+tmp)/2; 276 | imwrite(im2, [opt.dir 'mask/' basename '_mask.png']); 277 | end 278 | 279 | RegionScore = s; 280 | DiffScore = s1; 281 | SameScore = s2; 282 | DiffAdj = adj1; 283 | SameAdj = adj2; 284 | save([opt.dir 'cache/' basename '_everything.mat'], 'RegionScore', 'DiffScore', 'SameScore', 'DiffAdj', 'SameAdj', 'seg', 'im', 'hardmap', 'ssmap'); 285 | 286 | % get the pairing information consistent with the final detection. 287 | [shadow, non_shadow] = find(adj1 == 1); 288 | 289 | pair = []; 290 | 291 | num_pair = numel(non_shadow); 292 | for i = 1:num_pair 293 | if Decoding(non_shadow(i))==1 && Decoding(shadow(i)) == 2 294 | pair = [pair; non_shadow(i) shadow(i)]; 295 | end 296 | end 297 | 298 | save([opt.dir 'cache/' basename '_finalpair.mat'], 'pair'); 299 | end 300 | -------------------------------------------------------------------------------- /src/hist_match.m: -------------------------------------------------------------------------------- 1 | function desc = hist_match(original, match) 2 | 3 | original = double(original); 4 | binNum = 50; 5 | binVal = 0:1/(binNum):1; 6 | desc = zeros([size(original, 1), 1]); 7 | o_hist = zeros(50, 1); 8 | 9 | for bin = 1:size(o_hist, 1) 10 | oo = (original >= binVal(bin)) & (original < binVal(bin+1)); 11 | o_hist(bin, 1) = sum(oo); 12 | end 13 | 14 | o_hist = o_hist ./ size(original,1); 15 | 16 | G=[]; 17 | for i = 1:binNum 18 | G=[G sum(match(1:i))]; 19 | end 20 | 21 | S=[]; 22 | for i = 1:binNum 23 | S=[S sum(o_hist(1:i))]; 24 | end 25 | 26 | index = zeros([binNum, 1]); 27 | for i = 1:binNum 28 | tmp{i} = G - S(i); 29 | tmp{i} = abs(tmp{i}); 30 | [a index(i)] = min(tmp{i}); 31 | end 32 | 33 | for i = 1:size(desc, 1) 34 | o_bin = ceil(original(i, 1) / double(1.0/50)); 35 | if o_bin == 0 36 | o_bin = 1; 37 | end 38 | off = o_bin * double(1.0/50) - original(i, 1); 39 | desc(i,1) = index(o_bin) * double(1.0/50) - off; 40 | end 41 | 42 | end -------------------------------------------------------------------------------- /src/main.m: -------------------------------------------------------------------------------- 1 | addpath('meanshift'); 2 | addpath('libsvm/') 3 | addpath('./libsvm/matlab/'); 4 | addpath('./etract_feature/'); 5 | addpath('./utils/'); 6 | 7 | % fid=fopen('UIUC_file.txt'); 8 | % i=0; 9 | % while ~feof(fid) 10 | % aline=fgetl(fid); 11 | % 12 | % annotate(aline); 13 | % i = i + 1 14 | % end 15 | 16 | % if i < 2598 17 | % i = i + 1; 18 | % continue; 19 | % end 20 | 21 | url = 'data/UIUC/Our_test/original/p2_2.jpg'; 22 | [seg, segnum, between, near, centroids, label, grad, texthist] = detect(url); 23 | removal(seg, segnum, between, label, near, centroids, url, grad, texthist); -------------------------------------------------------------------------------- /src/meanshift/readme.txt: -------------------------------------------------------------------------------- 1 | MATLAB Deployment Checklist 2 | =========================== 3 | 4 | Before you deploy your application, component, or library, 5 | use this checklist to ensure you create a complete deployment package. 6 | You can distribute a MATLAB Compiler-generated standalone 7 | application, component, or library to any target machine with 8 | the same operating system as the machine on which 9 | the application was compiled (the "source" development machine). 10 | 11 | For more information, see the documentation for the MATLAB Compiler at: 12 | http://www.mathworks.com/access/helpdesk/help/toolbox/compiler/ 13 | 14 | NOTE: Items enclosed in angle brackets ( < > ) are variables. 15 | 16 | 1. Ensure that the MATLAB Component 17 | Runtime (MCR) is installed on target machines, and ensure you 18 | have installed the correct version. To verify this, do the following: 19 | 20 | A. Attempt to verify the MCR exists on your system. 21 | The MCR usually resides in these locations on these platforms: 22 | 23 | Operating System Path to MCR 24 | ================ =========== 25 | Windows C:\Program Files\MATLAB\MATLAB Component Runtime\v76 26 | Mac/Intel Mac Applications/MATLAB/MATLAB_Component_Runtime/ 27 | Linux *** No predetermined installation location *** 28 | Contact your system administrator. 29 | 30 | *IMPORTANT: Always attempt to locate an existing MCR on your 31 | system before using the BUILDMCR command to create 32 | an MCR. If you must create an MCR, see the Compiler 33 | documentation for more details 34 | 35 | B. MATLAB 2007a uses MCR version 7.6. To verify 36 | the version number of the installed MCR, type the following command: 37 | [mcrmajor,mcrminor]=mcrversion 38 | at the MATLAB command prompt. 39 | 40 | 41 | 2. Add the MCR directory to the path specified by the target system's 42 | environment variable. 43 | 44 | A. Locate the name of the environment variable to set, using the 45 | table below: 46 | 47 | Operating System Environment Variable 48 | ================ ==================== 49 | Windows PATH 50 | Linux LD_LIBRARY_PATH 51 | Solaris LD_LIBRARY_PATH 52 | Mac DYLD_LIBRARY_PATH 53 | 54 | B. Set the path by doing one of the following: 55 | 56 | On Windows systems: 57 | 58 | * Add the MCR directory to the environment variable by opening a 59 | command prompt and issuing the DOS command: 60 | 61 | set PATH=C:\Program Files\MATLAB\MATLAB Component 62 | Runtime\v76;%PATH% 63 | 64 | or, add: 65 | 66 | C:\Program Files\MATLAB\MATLAB Component Runtime\v76 67 | 68 | to the PATH environment variable by doing the following: 69 | 70 | 1. Select the My Computer icon on your desktop. 71 | 2. Right-click the icon and select Properties from the menu. 72 | 3. Select the Advanced tab. 73 | 4. Click Environment Variables. 74 | Your environment variables are listed. 75 | 76 | On UNIX systems: 77 | 78 | * Add the MCR directory to the environment variable by issuing 79 | this command: 80 | 81 | setenv PATH //v76:"$PATH" 82 | 83 | To make this change persistent after logout on UNIX machines, 84 | modify the .cshrc file to include this setenv command. 85 | 86 | NOTE: On Windows, the Environment Variable syntax utilizes 87 | backslashes (\), delimited by semi-colons (;). On UNIX, 88 | the EV syntax utilizes forward 89 | slashes (/), delimited by colons (:). 90 | NOTE: When deploying C and C++ standalone applications, it is possible 91 | to run the shell script file run_edison_wrapper.sh on UNIX and Mac 92 | instead of setting environment variables. See "Requirements 93 | for Standalone Applications," below. 94 | 95 | 96 | 3. Create packages that contain the following files, based on what 97 | you are deploying: 98 | 99 | Requirements for Standalone Applications: 100 | ========================================= 101 | 102 | All Platforms: 103 | * edison_wrapper.ctf (Component Technology File archive, 104 | platform-dependent on target machine) 105 | 106 | Windows: 107 | * MCRInstaller.exe (self-extracting MATLAB Component Runtime 108 | library archive) 109 | * edison_wrapper.exe (application created by MATLAB Compiler) 110 | UNIX: 111 | * MCRInstaller.zip (MATLAB Component Runtime library 112 | archive - manual unzip required) 113 | * edison_wrapper (application created by MATLAB Compiler) 114 | * run_edison_wrapper.sh (shell script that can be run 115 | to temporarily set environment paths and execute the application) 116 | 117 | 118 | Requirements for C and C++ shared libraries: 119 | ============================================================ 120 | 121 | All Platforms: 122 | * edison_wrapper.so (shared library, file extension 123 | varies by platform) 124 | * edison_wrapper.h (library header file) 125 | * edison_wrapper.ctf (Component Technology File archive, 126 | platform-dependent on target machine) 127 | 128 | Windows: 129 | * MCRInstaller.exe (self-extracting MATLAB Component Runtime 130 | library archive) 131 | 132 | UNIX: 133 | * MCRInstaller.zip (MATLAB Component Runtime library 134 | archive - manual unzip required) 135 | * unzip (optional utility to unzip MCRInstaller.zip) 136 | 137 | 138 | 139 | Requirements for .NET Components (Windows only): 140 | ================================================ 141 | 142 | * edison_wrapper.ctf (Component Technology File archive) 143 | * edison_wrapper.xml (documentation files) 144 | * edison_wrapper.pdb (program database file - if 145 | DEBUG option selected) 146 | * edison_wrapper.dll (component assembly file) 147 | * MCRInstaller.exe (self-extracting MATLAB Component Runtime 148 | library archive) 149 | 150 | 151 | Requirements for COM Components (Windows only): 152 | =============================================== 153 | 154 | * edison_wrapper.ctf (Component Technology File archive) 155 | * edison_wrapper_1_0.dll (component containing 156 | compiled M-code) 157 | * _install.bat (script run by self-extracting executable) 158 | * MCRInstaller.exe (self-extracting MATLAB Component Runtime 159 | library archive) 160 | 161 | 162 | Requirements for Java Components (All Platforms): 163 | ================================================= 164 | 165 | * edison_wrapper.jar (Java archive containing Java 166 | interface to M-code in edison_wrapper.ctf) 167 | * edison_wrapper.ctf (Component Technology File archive, 168 | platform-dependent on target machine) 169 | 170 | 171 | Requirements for COM Components for Use with Microsoft Excel (Windows only): 172 | ============================================================================ 173 | 174 | * edison_wrapper_1_0.dll (component containing 175 | compiled M-code) 176 | * edison_wrapper.ctf (Component Technology File archive) 177 | * _install.bat (script run by self-extracting executable) 178 | * .xla (Excel add-ins found in: 179 | \distrib directory) 180 | * MCRInstaller.exe (self-extracting MATLAB Component Runtime 181 | library archive) 182 | 183 | 184 | -------------------------------------------------------------------------------- /src/removal.m: -------------------------------------------------------------------------------- 1 | function removal(seg, segnum, between, label, near, centroids, url, grad, texthist) 2 | 3 | disp 'Removal' 4 | im = imread(url); 5 | 6 | epsilon=0.01; 7 | l_label = label; 8 | 9 | between = zeros([segnum, segnum]); 10 | for i = 1:size(between, 1) 11 | for j = 1:size(between, 2) 12 | %between(i, j) = sum((grad(i,:)-grad(j,:)).^2./(grad(i,:)+grad(j,:)+epsilon)) + sum((texthist(i,:)-texthist(j,:)).^2./(texthist(i,:)+texthist(j,:)+epsilon)); 13 | distance = [centroids(i,1), centroids(i,2); centroids(j,1), centroids(j,2)]; 14 | distance = sqrt((distance(1,1)-distance(2,1))^2 + (distance(1,2)-distance(2,2))^2) / max(size(im, 1), size(im, 2)); 15 | between(i, j) = sum(abs(grad(i,:) - grad(j,:))) + sum(abs(texthist(i,:) - texthist(j,:))) + distance; 16 | 17 | if i == j 18 | between(i,j) = 100; 19 | end 20 | end 21 | end 22 | 23 | nim = im; 24 | [gx gy] = gradient(double(seg)); 25 | eim = (gx.^2+gy.^2)>1e-10; 26 | t = nim(:,:,1); t(eim)=0; nim(:,:,1)=t; 27 | t = nim(:,:,2); t(eim)=0; nim(:,:,2)=t; 28 | t = nim(:,:,3); t(eim)=0; nim(:,:,3)=t; 29 | % imshow(nim); 30 | % hold on; 31 | 32 | c_im = double(rgb2hsv(im)); 33 | c1_im = c_im(:,:,1); 34 | c2_im = c_im(:,:,2); 35 | c3_im = c_im(:,:,3); 36 | lab = calHsvHist(c_im, seg, segnum); 37 | 38 | for i = 1:size(label,2) 39 | if label(i) == 0 40 | j = near(i); 41 | num = 0; 42 | while label(j) ~= 255 43 | [value, j] = min(between(i,:)); 44 | between(i,j) = 100; 45 | num = num + 1; 46 | 47 | end 48 | near(i) = j; 49 | if num ~= 21 50 | plot([centroids(i,1) centroids(j,1)], [centroids(i,2) centroids(j,2)], 'r'); 51 | %text(centroids(i,1),centroids(i,2),[ num2str(centroids(i,1)) , num2str(centroids(i,2))]) 52 | end 53 | end 54 | end 55 | 56 | for i = 1:size(label, 2) 57 | if label(i) == 0 && label(near(i)) == 255 58 | j = near(i); 59 | temp3 = reshape(lab(j,101:150),[50,1]); 60 | c3_im(seg==i) = hist_match(c3_im(seg==i),temp3); 61 | temp2 = reshape(lab(j,51:100),[50,1]); 62 | c2_im(seg==i) = hist_match(c2_im(seg==i),temp2); 63 | temp1 = reshape(lab(j,1:50),[50,1]); 64 | c1_im(seg==i) = hist_match(c1_im(seg==i),temp1); 65 | %c2_im(seg==i) = c2_im(seg==i) + (median(c2_im(seg==j)) - median(c2_im(seg==i))); 66 | %c1_im(seg==i) = c1_im(seg==i) + (median(c1_im(seg==j)) - median(c1_im(seg==i))); 67 | end 68 | end 69 | c_im(:,:,1) = c1_im; 70 | c_im(:,:,2) = c2_im; 71 | c_im(:,:,3) = c3_im; 72 | %imshow(hsv2rgb(c_im)); 73 | 74 | %end 75 | 76 | %% 77 | test_im = hsv2rgb(c_im); 78 | circle = zeros(size(c_im, 1), size(c_im, 2)); 79 | for i = 8:size(c_im, 1)-8 80 | for j = 8:size(c_im, 2)-8 81 | if label(seg(i,j)) ~= label(seg(i-1,j)) || label(seg(i,j)) ~= label(seg(i+1,j)) || label(seg(i,j)) ~= label(seg(i,j-1)) || label(seg(i,j)) ~= label(seg(i,j+1)) 82 | circle(i-1:i+1,j-1:j+1) = 1; 83 | 84 | end 85 | end 86 | end 87 | h = fspecial('gaussian', 15, 15); 88 | pattern = imfilter(test_im, h); 89 | 90 | label = l_label; 91 | 92 | for i = 1:segnum 93 | if label(i) == 0 94 | 95 | for ch = 1:3 96 | fig = test_im(:,:,ch); 97 | 98 | for x = 20:size(fig, 1) - 20 99 | for y = 20:size(fig,2) - 20 100 | 101 | if seg(x,y) == i && seg(x-4, y) ~= i && circle(x,y) == 1 102 | %if abs(test_im(x,y,3) - avg(i,3)) > 0.05 103 | fig(x,y) = pattern(x,y,ch); 104 | fig(x-4,y) = pattern(x,y,ch); 105 | %end 106 | % 107 | elseif seg(x,y) == i && seg(x+4, y) ~= i && circle(x,y) == 1 108 | %if abs(test_im(x,y,3) - avg(i,3)) > 0.05 109 | fig(x,y) = pattern(x,y,ch); 110 | fig(x+4,y) = pattern(x,y,ch); 111 | %end 112 | % 113 | elseif seg(x,y) == i && seg(x, y+4) ~= i && circle(x,y) == 1 114 | %if abs(test_im(x,y,3) - avg(i,3)) > 0.05 115 | fig(x,y) = pattern(x,y,ch); 116 | fig(x,y+4) = pattern(x,y,ch); 117 | %end 118 | % 119 | elseif seg(x,y) == i && seg(x, y-4) ~= i && circle(x,y) == 1 120 | %if abs(test_im(x,y,3) - avg(i,3)) > 0.05 121 | fig(x,y) = pattern(x,y,ch); 122 | fig(x,y-4) = pattern(x,y,ch); 123 | %end 124 | % 125 | end 126 | end 127 | end 128 | test_im(:,:,ch) = fig; 129 | end 130 | end 131 | 132 | end 133 | imshow(test_im); 134 | end 135 | 136 | 137 | -------------------------------------------------------------------------------- /src/utils/buildAdjacentMatrix.m: -------------------------------------------------------------------------------- 1 | function m = buildAdjacentMatrix(seg, numlabel) 2 | m = zeros(numlabel, numlabel); 3 | [dx, dy] = gradient(double(seg)); 4 | I = abs(dx)+abs(dy); 5 | w = size(I, 2); 6 | h = size(I, 1); 7 | for i=1:h 8 | for j=1:w 9 | if I(i,j)>0 10 | r1=max(i-1, 1):min(i+1, h); 11 | r2=max(j-1, 1):min(j+1, w); 12 | alladj = unique(seg(r1,r2))'; 13 | for k1=alladj 14 | for k2=alladj 15 | if k1~=k2 16 | m(k1,k2)=1; 17 | m(k2,k1)=1; 18 | end 19 | end 20 | end 21 | end 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /src/utils/dist_chi2.m: -------------------------------------------------------------------------------- 1 | % Chi^2 histogram distance. A,B are matrices of example data 2 | % vectors, one per column. The distance is sum_i 3 | % (u_i-v_i)^2/(u_i+v_i+epsilon). The output distance matrix is 4 | % (#examples in A)x(#examples in B) 5 | 6 | function D = dist_chi2(A,B,epsilon) 7 | 8 | if nargin<3, epsilon=1e-100; end 9 | [d,m]=size(A); 10 | [d1,n]=size(B); 11 | if (d ~= d1) 12 | error('column length of A (%d) != column length of B (%d)\n',d,d1); 13 | end 14 | 15 | % With the MATLAB JIT compiler the trivial implementation turns out 16 | % to be the fastest, especially for large matrices. 17 | D = zeros(m,n); 18 | for i=1:m % m is number of samples of A 19 | for j=1:n % n is number of samples of B 20 | D(i,j) = sum((A(:,i)-B(:,j)).^2./(A(:,i)+B(:,j)+epsilon)); 21 | end 22 | end 23 | %end 24 | --------------------------------------------------------------------------------