Skip to content

Instantly share code, notes, and snippets.

@gbiz123
Created November 16, 2024 14:34
Show Gist options
  • Save gbiz123/7ff77c16ada045cd50cf7f7a832206b7 to your computer and use it in GitHub Desktop.
Save gbiz123/7ff77c16ada045cd50cf7f7a832206b7 to your computer and use it in GitHub Desktop.
Clean Java OpenCV Code With Chained Calls!
package processor.computer_vision;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Execute chain of commands on image that modify the Mat object in-place
*/
public class ImageProcessChain implements Consumer<Mat> {
private static final Logger logger = LoggerFactory.getLogger(ImageProcessChain.class);
private final List<Consumer<Mat>> CHAIN = new ArrayList<>();
@Override
/**
* Execute the chain of processinng functions on target Image
*/
public void accept(Mat image) {
CHAIN.forEach(function -> {
function.accept(image);
});
}
public ImageProcessChain sharpen() {
CHAIN.add(mat -> {
Mat kernel = Mat.ones(3, 3, CvType.CV_32F);
Core.multiply(kernel, new Scalar(-1), kernel);
kernel.put(1, 1, new float[]{9.0f});
Imgproc.filter2D(mat, mat, -1, kernel);
logger.debug("executed sharpen on mat " + System.identityHashCode(mat));
kernel.release();
});
logger.debug("added sharpen to chain");
return this;
}
public ImageProcessChain addBlackBoxPadding(int pixels) {
CHAIN.add(mat -> {
Core.copyMakeBorder(mat, mat, pixels, pixels, pixels, pixels, Core.BORDER_CONSTANT);
logger.debug("executed add black box on mat " + System.identityHashCode(mat));
});
logger.debug("added pad to chain");
return this;
}
public ImageProcessChain blur(int blurSize) {
CHAIN.add(mat -> {
Imgproc.GaussianBlur(mat, mat, new Size(blurSize, blurSize), 0);
logger.debug("executed blur on mat " + System.identityHashCode(mat));
});
logger.debug("added blur to chain");
return this;
}
public ImageProcessChain bgrToHsv() {
CHAIN.add(mat -> {
Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2HSV);
logger.debug("executed bgr to hsv on mat " + System.identityHashCode(mat));
});
logger.debug("added bgr to hsv to chain");
return this;
}
public ImageProcessChain bgrToGray() {
CHAIN.add(mat -> {
Imgproc.cvtColor(mat, mat, Imgproc.COLOR_BGR2GRAY);
logger.debug("executed bgr to gray on mat " + System.identityHashCode(mat));
});
logger.debug("added bgr to gray to chain");
return this;
}
/**
* @param threshold an int between 0 and 255
*/
public ImageProcessChain binaryThreshold(int threshold) {
CHAIN.add(mat -> {
Imgproc.threshold(mat, mat, threshold, 255, Imgproc.THRESH_BINARY);
logger.debug("executed binary threshold on mat " + System.identityHashCode(mat));
});
logger.debug("added binary threshold to chain");
return this;
}
public ImageProcessChain scharrEdgeDetect(double scale) {
CHAIN.add(mat -> {
int delta = 0;
int ddepth = CvType.CV_16S;
Mat gradX = new Mat();
Mat gradY = new Mat();
Imgproc.Scharr(mat, gradX, ddepth, 1, 0, scale, delta, Core.BORDER_DEFAULT);
Imgproc.Scharr(mat, gradY, ddepth, 0, 1, scale, delta, Core.BORDER_DEFAULT);
Core.convertScaleAbs(gradX, gradX);
Core.convertScaleAbs(gradY, gradY);
Core.addWeighted(gradX, 0.5, gradY, 0.5, 0, mat);
gradX.release();
gradY.release();
logger.debug("executed scharr edge detect on mat " + System.identityHashCode(mat));
});
logger.debug("added scharr edge detectto chain");
return this;
}
/**
* compute gradients (Scharr edge detect) with default scale = 1
*/
public ImageProcessChain scharrEdgeDetect() {
CHAIN.add(mat -> {
double scale = 1.0;
int delta = 0;
int ddepth = CvType.CV_16S;
Mat gradX = new Mat();
Mat gradY = new Mat();
Imgproc.Scharr(mat, gradX, ddepth, 1, 0, scale, delta, Core.BORDER_DEFAULT);
Imgproc.Scharr(mat, gradY, ddepth, 0, 1, scale, delta, Core.BORDER_DEFAULT);
Core.convertScaleAbs(gradX, gradX);
Core.convertScaleAbs(gradY, gradY);
Core.addWeighted(gradX, 0.5, gradY, 0.5, 0, mat);
gradX.release();
gradY.release();
logger.debug("executed scharr edge detect on mat " + System.identityHashCode(mat));
});
logger.debug("added scharr edge detect to chain");
return this;
}
public ImageProcessChain canny(int lowerThresh, int upperThresh) {
CHAIN.add(mat -> {
Imgproc.Canny(mat, mat, lowerThresh, upperThresh);
logger.debug("executed canny on mat " + System.identityHashCode(mat));
});
logger.debug("added canny edge detect to chain");
return this;
}
public ImageProcessChain erode(int pixels) {
CHAIN.add(mat -> {
Imgproc.erode(mat, mat, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(pixels, pixels)));
logger.debug("executed erode on mat " + System.identityHashCode(mat));
});
logger.debug("added erode to chain");
return this;
}
public ImageProcessChain dilate(int pixels) {
CHAIN.add(mat -> {
Imgproc.dilate(mat, mat, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(pixels, pixels)));
logger.debug("executed dilate on mat " + System.identityHashCode(mat));
});
logger.debug("added dilate to chain");
return this;
}
public ImageProcessChain inRange(Scalar lowerThresh, Scalar upperThresh) {
CHAIN.add(mat -> {
Core.inRange(mat, lowerThresh, upperThresh, mat);
logger.debug("executed in range on mat " + System.identityHashCode(mat));
});
logger.debug("added in range to chain");
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment