use of org.bytedeco.opencv.opencv_core.Point in project karate by karatelabs.
the class OpenCvUtils method getPointsBelowThreshold.
private static List<int[]> getPointsBelowThreshold(Mat src, double threshold) {
Mat dst = new Mat();
threshold(src, dst, threshold, 1, CV_THRESH_BINARY_INV);
Mat non = new Mat();
findNonZero(dst, non);
int len = (int) non.total();
int xPrev = -BLOCK_SIZE;
int yPrev = -BLOCK_SIZE;
int countPrev = 0;
int xSum = 0;
int ySum = 0;
List<int[]> points = new ArrayList(len);
for (int i = 0; i < len; i++) {
Pointer ptr = non.ptr(i);
Point p = new Point(ptr);
int x = p.x();
int y = p.y();
int xDelta = Math.abs(x - xPrev);
int yDelta = Math.abs(y - yPrev);
if (xDelta < BLOCK_SIZE && yDelta < BLOCK_SIZE) {
countPrev++;
xSum += x;
ySum += y;
} else {
if (countPrev > 0) {
int xFinal = Math.floorDiv(xSum, countPrev);
int yFinal = Math.floorDiv(ySum, countPrev);
// logger.debug("end: {}:{}", xFinal, yFinal);
points.add(new int[] { xFinal, yFinal });
}
xSum = x;
ySum = y;
countPrev = 1;
}
xPrev = x;
yPrev = y;
}
if (countPrev > 0) {
int xFinal = Math.floorDiv(xSum, countPrev);
int yFinal = Math.floorDiv(ySum, countPrev);
points.add(new int[] { xFinal, yFinal });
}
return points;
}
use of org.bytedeco.opencv.opencv_core.Point in project karate by karatelabs.
the class OpenCvUtils method drawOnImage.
public static Mat drawOnImage(Mat image, Point2fVector points) {
Mat dest = image.clone();
int radius = 5;
Scalar red = new Scalar(0, 0, 255, 0);
for (int i = 0; i < points.size(); i++) {
Point2f p = points.get(i);
circle(dest, new Point(Math.round(p.x()), Math.round(p.y())), radius, red);
}
return dest;
}
use of org.bytedeco.opencv.opencv_core.Point in project karate by karatelabs.
the class OpenCvUtils method templateAndMin.
private static int[] templateAndMin(int strictness, double scale, Mat source, Mat target, Mat result) {
Mat resized = scale == 1 ? source : rescale(source, scale);
matchTemplate(resized, target, result, CV_TM_SQDIFF);
DoublePointer minValPtr = new DoublePointer(1);
DoublePointer maxValPtr = new DoublePointer(1);
Point minPt = new Point();
Point maxPt = new Point();
minMaxLoc(result, minValPtr, maxValPtr, minPt, maxPt, null);
int minVal = (int) minValPtr.get();
int x = minPt.x();
int y = minPt.y();
return new int[] { x, y, minVal };
}
use of org.bytedeco.opencv.opencv_core.Point in project qupath by qupath.
the class OpenCVTools method filterSingleZ.
// Alternative weighted sum code that converts to 32-bit
// static void weightedSum(List<Mat> mats, double[] weights, Mat dest) {
// boolean isFirst = true;
// for (int i = 0; i < weights.length; i++) {
// double w = weights[i];
// if (w == 0)
// continue;
// var temp = mats.get(i);
// int type = temp.depth();
// if (type != opencv_core.CV_32F && type != opencv_core.CV_64F) {
// var temp2 = new Mat();
// temp.convertTo(temp2, opencv_core.CV_32F);
// temp = temp2;
// }
// if (isFirst) {
// dest.put(opencv_core.multiply(temp, w));
// isFirst = false;
// } else
// opencv_core.scaleAdd(temp, w, dest, dest);
// if (mats.get(i) != temp)
// temp.release();
// }
// // TODO: Check this does something sensible!
// if (isFirst) {
// dest.create(mats.get(0).size(), mats.get(0).type());
// dest.put(Scalar.ZERO);
// }
// }
/**
* Apply a filter along the 'list' dimension for a list of Mats, computing the value
* for a single entry. This is effectively computing a weighted sum of images in the list.
* <p>
* Note: this method does not change the depth of the input images.
* If a floating point output is needed, the Mats should be converted before input.
*
* @param mats
* @param kernel
* @param ind3D
* @param border
* @return
*/
public static Mat filterSingleZ(List<Mat> mats, double[] kernel, int ind3D, int border) {
// Calculate weights for each image
int n = mats.size();
int halfSize = kernel.length / 2;
int startInd = ind3D - halfSize;
int endInd = startInd + kernel.length;
double[] weights = new double[mats.size()];
int k = 0;
for (int i = startInd; i < endInd; i++) {
int ind = ensureInRange(i, n, border);
weights[ind] += kernel[k];
k++;
}
Mat result = new Mat();
weightedSum(mats, weights, result);
return result;
}
use of org.bytedeco.opencv.opencv_core.Point in project qupath by qupath.
the class OpenCVTools method getCircularStructuringElement.
/**
* Create a Mat depicting a circle of the specified radius.
* <p>
* Pixels within the circle have the value 1, pixels outside are 0.
*
* @param radius
* @return
* @deprecated {@link #createDisk(int, boolean)} gives more reliable shapes.
*/
@Deprecated
public static Mat getCircularStructuringElement(int radius) {
// TODO: Find out why this doesn't just call a standard request for a strel...
Mat strel = new Mat(radius * 2 + 1, radius * 2 + 1, opencv_core.CV_8UC1, Scalar.ZERO);
opencv_imgproc.circle(strel, new Point(radius, radius), radius, Scalar.ONE, -1, opencv_imgproc.LINE_8, 0);
return strel;
}
Aggregations