【发布时间】:2018-10-23 19:13:55
【问题描述】:
我想知道如何用 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];
}
【问题讨论】:
-
收到
ptr后,您实际上想做什么?沿行求和? -
(请注意,如果是这种情况,您实际上并不需要
sums数组) -
(而
Integer.MAX_VALUE是smallestSum的更稳健的初始值;尽管最好再次使用i == 0的总和,以防所有行总和为Integer.MAX_VALUE) -
是的,我已经添加了 C++ 代码
-
(格式化很麻烦)