001package org.opencv.core;
002
003import java.util.Arrays;
004import java.util.List;
005
006import org.opencv.core.KeyPoint;
007
008public class MatOfKeyPoint extends Mat {
009    // 32FC7
010    private static final int _depth = CvType.CV_32F;
011    private static final int _channels = 7;
012
013    public MatOfKeyPoint() {
014        super();
015    }
016
017    protected MatOfKeyPoint(long addr) {
018        super(addr);
019        if( !empty() && checkVector(_channels, _depth) < 0 )
020            throw new IllegalArgumentException("Incompatible Mat");
021        //FIXME: do we need release() here?
022    }
023
024    public static MatOfKeyPoint fromNativeAddr(long addr) {
025        return new MatOfKeyPoint(addr);
026    }
027
028    public MatOfKeyPoint(Mat m) {
029        super(m, Range.all());
030        if( !empty() && checkVector(_channels, _depth) < 0 )
031            throw new IllegalArgumentException("Incompatible Mat");
032        //FIXME: do we need release() here?
033    }
034
035    public MatOfKeyPoint(KeyPoint...a) {
036        super();
037        fromArray(a);
038    }
039
040    public void alloc(int elemNumber) {
041        if(elemNumber>0)
042            super.create(elemNumber, 1, CvType.makeType(_depth, _channels));
043    }
044
045    public void fromArray(KeyPoint...a) {
046        if(a==null || a.length==0)
047            return;
048        int num = a.length;
049        alloc(num);
050        float buff[] = new float[num * _channels];
051        for(int i=0; i<num; i++) {
052            KeyPoint kp = a[i];
053            buff[_channels*i+0] = (float) kp.pt.x;
054            buff[_channels*i+1] = (float) kp.pt.y;
055            buff[_channels*i+2] = kp.size;
056            buff[_channels*i+3] = kp.angle;
057            buff[_channels*i+4] = kp.response;
058            buff[_channels*i+5] = kp.octave;
059            buff[_channels*i+6] = kp.class_id;
060        }
061        put(0, 0, buff); //TODO: check ret val!
062    }
063
064    public KeyPoint[] toArray() {
065        int num = (int) total();
066        KeyPoint[] a = new KeyPoint[num];
067        if(num == 0)
068            return a;
069        float buff[] = new float[num * _channels];
070        get(0, 0, buff); //TODO: check ret val!
071        for(int i=0; i<num; i++)
072            a[i] = new KeyPoint( buff[_channels*i+0], buff[_channels*i+1], buff[_channels*i+2], buff[_channels*i+3],
073                                 buff[_channels*i+4], (int) buff[_channels*i+5], (int) buff[_channels*i+6] );
074        return a;
075    }
076
077    public void fromList(List<KeyPoint> lkp) {
078        KeyPoint akp[] = lkp.toArray(new KeyPoint[0]);
079        fromArray(akp);
080    }
081
082    public List<KeyPoint> toList() {
083        KeyPoint[] akp = toArray();
084        return Arrays.asList(akp);
085    }
086}