- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在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]
关于java - 如何在Java包装器中使用Opencv的分区功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55636717/
是一种在 Neo4j 分区之间进行物理分离的方法吗? 这意味着以下查询将转到 node1: Match (a:User:Facebook) 虽然此查询将转到另一个节点(可能托管在 docker 上)
我尝试在我的 SQL 服务器上使用分区函数对我的一个大表进行分区,但我收到一条错误消息 “只能在SQL Server企业版中创建分区功能。只有SQL Server企业版支持分区。” 所以我想知道没有企
在hadoop文件系统中,我有两个文件,分别是X和Y。通常,hadoop制作的文件X和Y的大小为64 MB。是否可以强制hadoop划分两个文件,以便从X的32 MB和Y的32 MB中创建一个64 M
据我了解,如果我们有一个主键,则使用该键对数据进行分区并将其存储在节点中(例如使用随机分区器)。 现在我不确定的是,如果我有多个键(又名复合键),是用于分区数据的键的组合还是它将是第一个主键? 例如,
我正在向我的 SSAS 多维数据集添加分区,我想知道是否有多个分区可以保留在下面?多少太多了,最佳实践限制是 20 还是 200?有没有人可以分享任何真实世界的知识? 最佳答案 这是 another
我有一个包含大约 200 万条记录的大表,我想对其进行分区。 我将 id 列设置为 PRIMARY AUTO_INCRMENT int (并且它必须始终是唯一的)。我有一列“theyear”int(4
我正在做 mysql 列表分区。我的表数据如下 ---------------------------------------- id | unique_token | city | student_
我有一个表,我们每天在其中插入大约 2000 万个条目(没有任何限制的盲插入)。我们有两个外键,其中一个是对包含大约 1000 万个条目的表的引用 ID。 我打算删除此表中超过一个月的所有数据,因为不
我想在一款足球奇幻游戏中尝试使用 MySQL Partitioning,该游戏的用户分布在联赛中,每个联赛都有一个用户可以买卖球员的市场。当很多用户同时玩时,我在这张表中遇到了一些僵局(在撰写本文时大
我是 jQuery 的新手,想知道是否可以获取一些变量并将它们的除法作为 CSS 宽度。到目前为止我在这里: var x = $(".some-container").length; var y =
所以我正在做家庭作业,我需要为分区、斯特林数(第一类和第二类)和第一类的切比雪夫多项式创建递归函数。我的程序应该能够让用户输入一个正整数 n,然后创建名为 Partitions.txt、Stirlin
我在数据框中有一列,其中包含大约 1,4M 行聊天对话,其中每个单元格中的一般格式为 (1): “名称代理 : 对话” 但是,并非列中的所有单元格都采用这种格式。有些单元格只是 (2): “对话” 我
我在尝试隐藏 a 时遇到了一些问题,直到用户单击某个元素为止。 HTML 看起来像: BRAND item 1 item 2 item 3
一.为什么kafka要做分区? 因为当一台机器有可能扛不住(类比:就像redis集群中的redis-cluster一样,一个master抗不住写,那么就多个master去抗写)
我有一些销售数据,我需要发送存储在单独表中的可用槽中的数量。 销售数据示例: id数量112131415369 create table sales (id serial primary key, q
我计划设置多个节点以使用 glusterfs 创建分布式复制卷 我使用主(也是唯一)分区上的目录在两个节点上创建了一个 gluster 复制卷。 gluster volume create vol_d
我正在尝试使用 sum() over (partition by) 但在总和中过滤。我的用例是将每个产品的 12 个月累计到一个月的条目,因此: ITEM MONTH SALES Item
是否可以创建多个 Enumerators出单Enumerator ? 我正在寻找的相当于 List.partition返回 (List[A], List[A]) ,比如 List().partitio
我正在创建一个基于 x86 的非常简单的 Yocto 图像。 我希望/文件系统是只读的,所以我设置了 IMAGE_FEATURES_append = " read-only-rootfs " 在原件的
是否可以使用一次 collect 调用来创建 2 个新列表?如果没有,我该如何使用分区来做到这一点? 最佳答案 collect(在TraversableLike上定义并在所有子类中可用)与集合和Par
我是一名优秀的程序员,十分优秀!