java - Java中的OpenCV ptr
问题描述
我想知道如何用 Java 制作这个 opencv c++ 代码
uchar *ptr = eye.ptr<uchar>(y);
我一直在环顾四周,我想我可以将 uchar 用作字节......但我不知道在 java 中获取 .ptr 的代码是什么
到目前为止,这是我的代码
private Rect getEyeball(Mat eye, MatOfRect circles) {
int[] sums = new int[circles.toArray().length];
for (int y = 0; y < eye.rows(); y++) {
// OpenCV method uchar *ptr = eye.ptr<uchar>(y); Goes here
}
int smallestSum = 9999999;
int smallestSumIndex = -1;
for (int i = 0; i < circles.toArray().length; i++) {
if (sums[i] < smallestSum) {
smallestSum = sums[i];
smallestSumIndex = i;
}
}
return circles.toArray()[smallestSumIndex];
}
完整的 C++ 代码是
cv::Vec3f getEyeball(cv::Mat &eye, std::vector<cv::Vec3f> &circles)
{
std::vector<int> sums(circles.size(), 0);
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
for (int i = 0; i < circles.size(); i++)
{
cv::Point center((int)std::round(circles[i][0]), (int)std::round(circles[i][1]));
int radius = (int)std::round(circles[i][2]);
if (std::pow(x - center.x, 2) + std::pow(y - center.y, 2) < std::pow(radius, 2))
{
sums[i] += value;
}
}
++ptr;
}
}
int smallestSum = 9999999;
int smallestSumIndex = -1;
for (int i = 0; i < circles.size(); i++)
{
if (sums[i] < smallestSum)
{
smallestSum = sums[i];
smallestSumIndex = i;
}
}
return circles[smallestSumIndex];
}
解决方案
提炼你的 C++:
for (int y = 0; y < eye.rows; y++)
{
uchar *ptr = eye.ptr<uchar>(y);
for (int x = 0; x < eye.cols; x++)
{
int value = static_cast<int>(*ptr);
// A loop not using ptr.
++ptr;
}
}
您只是从 .x 获取 (x,y) 处的像素值eye
。
因此,只需使用Mat.get
.
int[] values = new int[eye.channels()];
for (int y = 0; y < eye.rows(); y++) {
for (int x = 0; x < eye.cols(); x++) {
eye.get(x, y, values);
int value = values[0];
// A loop not using ptr.
}
}
请注意,使用get(int, int, int[])
而不是get(int, int)
在这里意味着您避免为每次迭代分配一个新数组,这将使事情变得快得多。
推荐阅读
- javascript - 如何在 html 下拉列表中将类名更改为我的选择?
- amazon-web-services - 在 Elasticsearch 上安装 repository-se 插件
- python - 多个midi节目同时播放?
- css - 如何拥有具有固定纵横比和最大高度的 DIV?(纯 CSS/无 vh 或 vw)
- phpspreadsheet - 如何在不修剪的情况下获取所有工作表名称?
- r - 行作为系列情节 R
- php - 如果项目在 laravel 中有 PDF,则显示链接
- python - 从 Selenium + Python 获取文本
- javascript - jquery 复制到剪贴板,然后在几秒钟后撤消
- laravel-5 - 我如何编写此查询。不想用 Eloquent