use of org.bytedeco.opencv.opencv_core.MatVector in project qupath by qupath.
the class OpenCVTools method mergeChannels.
/**
* Merge channels from a multichannel {@link Mat}.
* May be more convenient than OpenCV's built-in approach.
*
* @param channels separate channels
* @param dest optional destination (may be null)
* @return merged {@link Mat}, which will be the same as dest if provided
*/
@SuppressWarnings("unchecked")
public static Mat mergeChannels(Collection<? extends Mat> channels, Mat dest) {
if (dest == null)
dest = new Mat();
boolean firstMat = true;
boolean allSingleChannel = true;
int rows = -1;
int cols = -1;
int depth = 0;
int nChannels = 0;
for (var m : channels) {
if (firstMat) {
rows = m.rows();
cols = m.cols();
depth = m.depth();
if (rows < 0 || cols < 0)
throw new IllegalArgumentException("Rows and columns must be >= 0");
firstMat = false;
}
int nc = m.channels();
allSingleChannel = allSingleChannel && nc == 1;
nChannels += nc;
if (depth != m.depth() || rows != m.rows() || cols != m.cols())
throw new IllegalArgumentException("mergeChannels() requires all Mats to have the same dimensions and depth!");
}
// We can only use OpenCV's merge if all Mats are single-channel
boolean mixChannels = !allSingleChannel;
try (var scope = new PointerScope()) {
if (mixChannels) {
dest.create(rows, cols, opencv_core.CV_MAKETYPE(depth, nChannels));
var vecSource = new MatVector(channels.toArray(Mat[]::new));
var vecDest = new MatVector(dest);
int[] inds = new int[nChannels * 2];
for (int i = 0; i < nChannels; i++) {
inds[i * 2] = i;
inds[i * 2 + 1] = i;
}
opencv_core.mixChannels(vecSource, vecDest, new IntPointer(inds));
} else {
opencv_core.merge(new MatVector(channels.toArray(Mat[]::new)), dest);
}
// scope.deallocate();
}
return dest;
}
use of org.bytedeco.opencv.opencv_core.MatVector in project qupath by qupath.
the class ImageOps method doPrediction.
private static <T> Mat doPrediction(DnnModel<T> model, Mat mat, String inputName, String... outputNames) {
var matResult = new Mat();
try (@SuppressWarnings("unchecked") var scope = new PointerScope()) {
var output = model.convertAndPredict(Map.of(inputName, mat));
if (!output.isEmpty()) {
if (outputNames.length == 0 || (outputNames.length == 1 && output.containsKey(outputNames[0])))
matResult.put(output.values().iterator().next());
else {
var tempArray = new Mat[outputNames.length];
for (int i = 0; i < outputNames.length; i++) {
var name = outputNames[i];
if (output.containsKey(name)) {
tempArray[i] = output.get(name);
} else
throw new RuntimeException(String.format("Unable to find output '%s' in %s", name, model));
}
opencv_core.merge(new MatVector(tempArray), matResult);
}
}
scope.deallocate();
}
return matResult;
}
Aggregations