001package org.openimaj.demos.sandbox.tldcpp.videotld;
002
003import java.util.List;
004
005import org.openimaj.demos.sandbox.tldcpp.detector.NormalizedPatch;
006import org.openimaj.demos.sandbox.tldcpp.detector.ScaleIndexRectangle;
007import org.openimaj.image.FImage;
008import org.openimaj.image.processing.algorithm.MeanCenter;
009import org.openimaj.image.processing.resize.ResizeProcessor;
010import org.openimaj.math.geometry.shape.Rectangle;
011
012/**
013 * Variaus TLD utility functions
014 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
015 *
016 */
017public class TLDUtil {
018        
019        /**
020         * Resize image to fit in a {@link NormalizedPatch#TLD_PATCH_SIZE} squared image and mean center 
021         * @param img
022         * @return new (image.process is called) image
023         */
024        public static FImage tldNormalizeImg(FImage img) {
025                int size = NormalizedPatch.TLD_PATCH_SIZE;
026                
027//              resize(img, result, cvSize(size,size)); //Default is bilinear
028                ResizeProcessor resizeProc = new ResizeProcessor(size, size, false);
029                FImage result = img.process(resizeProc);
030                result.processInplace(new MeanCenter());
031                return result;
032        }
033
034        
035
036        /**
037         * {@link #tldOverlapNorm(Rectangle, Rectangle)} called on every window and boundary.
038         * @param windows
039         * @param numWindows
040         * @param boundary
041         * @param overlap
042         */
043        public static void tldOverlap(Rectangle[] windows, int numWindows, Rectangle boundary,float[] overlap) {
044                for(int i = 0; i < numWindows; i++) {
045                        overlap[i] = (float) tldOverlapNorm(windows[i],boundary);
046                }
047        }
048
049        /**
050         * {@link Rectangle#overlapping(Rectangle)} called and normalised by:
051         * 
052         * @param A
053         * @param B
054         * @return intersect / (areaA + areaB - intersect)
055         */
056        public static float tldOverlapNorm(Rectangle A, Rectangle B) {
057                Rectangle overlap = A.overlapping(B);
058                double intersect = overlap == null ? 0 : overlap.calculateArea();
059                double areaA = A.calculateArea();
060                double areaB = B.calculateArea();
061                
062                return (float) (intersect / (areaA + areaB - intersect));
063        }
064
065        /**
066         * The overlap of the window at the firstIndex to all windows in the confidentIndices
067         * @param windows
068         * @param firstIndex
069         * @param confidentIndices
070         * @param distances
071         * @param distIDX
072         */
073        public static void tldOverlapOne(ScaleIndexRectangle[] windows, int firstIndex, List<Integer> confidentIndices, float[] distances, int distIDX) {
074                Rectangle comp = windows[firstIndex];
075                int i = 0;
076                for (int idx : confidentIndices) {
077                        ScaleIndexRectangle other = windows[idx];
078                        distances[distIDX + i] = tldOverlapNorm(comp,other);
079                        i++;
080                }
081        }
082}