Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / java / generator / src / java / core+MatOfDMatch.java
1 package org.opencv.core;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import org.opencv.features2d.DMatch;
7
8 public class MatOfDMatch extends Mat {
9     // 32FC4
10     private static final int _depth = CvType.CV_32F;
11     private static final int _channels = 4;
12
13     public MatOfDMatch() {
14         super();
15     }
16
17     protected MatOfDMatch(long addr) {
18         super(addr);
19         if( !empty() && checkVector(_channels, _depth) < 0 )
20             throw new IllegalArgumentException("Incompatible Mat: " + toString());
21         //FIXME: do we need release() here?
22     }
23
24     public static MatOfDMatch fromNativeAddr(long addr) {
25         return new MatOfDMatch(addr);
26     }
27
28     public MatOfDMatch(Mat m) {
29         super(m, Range.all());
30         if( !empty() && checkVector(_channels, _depth) < 0 )
31             throw new IllegalArgumentException("Incompatible Mat: " + toString());
32         //FIXME: do we need release() here?
33     }
34
35     public MatOfDMatch(DMatch...ap) {
36         super();
37         fromArray(ap);
38     }
39
40     public void alloc(int elemNumber) {
41         if(elemNumber>0)
42             super.create(elemNumber, 1, CvType.makeType(_depth, _channels));
43     }
44
45
46     public void fromArray(DMatch...a) {
47         if(a==null || a.length==0)
48             return;
49         int num = a.length;
50         alloc(num);
51         float buff[] = new float[num * _channels];
52         for(int i=0; i<num; i++) {
53             DMatch m = a[i];
54             buff[_channels*i+0] = m.queryIdx;
55             buff[_channels*i+1] = m.trainIdx;
56             buff[_channels*i+2] = m.imgIdx;
57             buff[_channels*i+3] = m.distance;
58         }
59         put(0, 0, buff); //TODO: check ret val!
60     }
61
62     public DMatch[] toArray() {
63         int num = (int) total();
64         DMatch[] a = new DMatch[num];
65         if(num == 0)
66             return a;
67         float buff[] = new float[num * _channels];
68         get(0, 0, buff); //TODO: check ret val!
69         for(int i=0; i<num; i++)
70             a[i] = new DMatch((int) buff[_channels*i+0], (int) buff[_channels*i+1], (int) buff[_channels*i+2], buff[_channels*i+3]);
71         return a;
72     }
73
74     public void fromList(List<DMatch> ldm) {
75         DMatch adm[] = ldm.toArray(new DMatch[0]);
76         fromArray(adm);
77     }
78
79     public List<DMatch> toList() {
80         DMatch[] adm = toArray();
81         return Arrays.asList(adm);
82     }
83 }