gpt4 book ai didi

java - 如何在Java包装器中使用Opencv的分区功能

转载 作者:行者123 更新时间:2023-12-02 16:29:19 25 4
gpt4 key购买 nike

我想在partition包装器中使用 Opencv function of Java 。但是我找不到它的包装。

编辑
这是opencv上的open issue。还有其他选择吗?

最佳答案

我知道这个问题已经存在了半年,但是我最近在搜索opencv分区时遇到了这个问题。该方法仍未在Java中实现,因此我复制了C++代码并将其更改为可在Java中进行编译。这里是:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiPredicate;

public class Partition {

public static void main(String[] args) {
List<String> list = new LinkedList<>(Arrays.asList("aa", "ab", "aa", "bb", "bb", "a", "b", "a", "b", "b"));
List<Integer> list2 = new ArrayList<>(Arrays.asList(1, 2, 10, 11, 10, 42, 3, 2, 3));

List<Integer> labels = new ArrayList<>();
int classes = partition(list, labels);
System.out.println("There are " + classes + " classes of equal strings:");
System.out.println(labels);

classes = partition(list2, labels);
System.out.println("There are " + classes + " classes of equal integers:");
System.out.println(labels);

classes = partition(list, labels, (str1, str2) -> str1.length() == str2.length());
System.out.println("There are " + classes + " classes of strings with same length:");
System.out.println(labels);

classes = partition(list2, labels, (i1, i2) -> Math.abs(i1-i2) <= 1);
System.out.println("There are " + classes + " classes of integers within 1 range:");
System.out.println(labels);
}

/**
* Overloaded method with default equality predicate
*
* @param _vec vector of elements to be partitioned
* @param labels output list of labels
* @return number of classes
*/
public static <E> int partition(final List<E> _vec, List<Integer> labels) {
return partition(_vec, labels, E::equals);
}

/**
* Port of C++ partition function
*
* @param _vec list of elements to be partitioned
* @param labels output list of labels
* @param predicate predicate to test whether two elements belong to the same class
* @return number of classes
* @see https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/operations.hpp
* @see https://docs.opencv.org/2.4/modules/core/doc/clustering.html
*/
//template<typename _Tp, class _EqPredicate> int
//partition( const std::vector<_Tp>& _vec, std::vector<int>& labels,
// _EqPredicate predicate=_EqPredicate())
public static <E> int partition(final List<E> _vec, List<Integer> labels, BiPredicate<E, E> predicate)
{

int i, j, N = (int)_vec.size();

//const _Tp* vec = &_vec[0];
final ArrayList<E> vec = new ArrayList<>(_vec);

final int PARENT=0;
final int RANK=1;

//std::vector<int> _nodes(N*2);
//int (*nodes)[2] = (int(*)[2])&_nodes[0];
int[][] nodes = new int[N*2][2];

// The first O(N) pass: create N single-vertex trees
for(i = 0; i < N; i++)
{
nodes[i][PARENT]=-1;
nodes[i][RANK] = 0;
}

// The main O(N^2) pass: merge connected components
for( i = 0; i < N; i++ )
{
int root = i;

// find root
while( nodes[root][PARENT] >= 0 )
root = nodes[root][PARENT];

for( j = 0; j < N; j++ )
{
//if( i == j || !predicate(vec[i], vec[j]))
if(i == j || !predicate.test(vec.get(i), vec.get(j)))
continue;
int root2 = j;

while( nodes[root2][PARENT] >= 0 )
root2 = nodes[root2][PARENT];

if( root2 != root )
{
// unite both trees
int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
if( rank > rank2 )
nodes[root2][PARENT] = root;
else
{
nodes[root][PARENT] = root2;
nodes[root2][RANK] += (rank == rank2 ? 1 : 0);
root = root2;
}
//CV_Assert( nodes[root][PARENT] < 0 );
assert(nodes[root][PARENT] < 0);

int k = j, parent;

// compress the path from node2 to root
while( (parent = nodes[k][PARENT]) >= 0 )
{
nodes[k][PARENT] = root;
k = parent;
}

// compress the path from node to root
k = i;
while( (parent = nodes[k][PARENT]) >= 0 )
{
nodes[k][PARENT] = root;
k = parent;
}
}
}
}

// Final O(N) pass: enumerate classes
//labels.resize(N);
Integer[] _labels = new Integer[N];
int nclasses = 0;

for( i = 0; i < N; i++ )
{
int root = i;
while( nodes[root][PARENT] >= 0 )
root = nodes[root][PARENT];
// re-use the rank as the class label
if( nodes[root][RANK] >= 0 )
nodes[root][RANK] = ~nclasses++;
_labels[i] = ~nodes[root][RANK];
}
labels.clear();
labels.addAll(Arrays.asList(_labels));
return nclasses;
}
}

输出如下:
There are 5 classes of equal strings:
[0, 1, 0, 2, 2, 3, 4, 3, 4, 4]
There are 6 classes of equal integers:
[0, 1, 2, 3, 2, 4, 5, 1, 5]
There are 2 classes of strings with same length:
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
There are 3 classes of integers within 1 range:
[0, 0, 1, 1, 1, 2, 0, 0, 0]

我试图保留尽可能多的原始代码,并注释掉必须更改的行。要查看原始代码(在C++中),您可能需要查看 here。该文档可以在 here中找到。

关于java - 如何在Java包装器中使用Opencv的分区功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55636717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com