本文整理了Java中org.opencv.imgproc.Imgproc.threshold()
方法的一些代码示例,展示了Imgproc.threshold()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Imgproc.threshold()
方法的具体详情如下:
包路径:org.opencv.imgproc.Imgproc
类名称:Imgproc
方法名:threshold
[英]Applies a fixed-level threshold to each array element.
The function applies fixed-level thresholding to a single-channel array. The function is typically used to get a bi-level (binary) image out of a grayscale image ("compare" could be also used for this purpose) or for removing a noise, that is, filtering out pixels with too small or too large values. There are several types of thresholding supported by the function. They are determined by type
:
dst(x,y) = maxval if src(x,y) > thresh; 0 otherwise
dst(x,y) = 0 if src(x,y) > thresh; maxval otherwise
dst(x,y) = threshold if src(x,y) > thresh; src(x,y) otherwise
dst(x,y) = src(x,y) if src(x,y) > thresh; 0 otherwise
dst(x,y) = 0 if src(x,y) > thresh; src(x,y) otherwise
Also, the special value THRESH_OTSU
may be combined with one of the above values. In this case, the function determines the optimal threshold value using the Otsu's algorithm and uses it instead of the specified thresh
. The function returns the computed threshold value. Currently, the Otsu's method is implemented only for 8-bit images.
[中]将固定级别阈值应用于每个数组元素。
该函数将固定级别阈值应用于单通道阵列。该函数通常用于从灰度图像中获取二级(二进制)图像(“比较”也可用于此目的)或用于去除噪声,即过滤掉值过小或过大的像素。该函数支持多种类型的阈值设置。它们由type
确定:
*THRESH_二进制
如果src(x,y)>thresh,则dst(x,y)=maxval;否则为0
*阈值二元变量
如果src(x,y)>thresh,则dst(x,y)=0;maxval否则
*脱粒
dst(x,y)=如果src(x,y)>阈值,则为阈值;src(x,y)否则
*脱粒至零
如果src(x,y)>thresh,则dst(x,y)=src(x,y);否则为0
*脱粒至零库存
如果src(x,y)>thresh,则dst(x,y)=0;src(x,y)否则
此外,特殊值THRESH_OTSU
可与上述值之一组合。在这种情况下,函数使用大津算法确定最佳阈值,并使用它代替指定的thresh
。该函数返回计算出的阈值。目前,大津的方法仅用于8位图像。
代码示例来源:origin: openpnp/openpnp
public static Mat thresholdOtsu(Mat mat, boolean invert) {
Imgproc.threshold(mat, mat, 0, 255,
(invert ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY) | Imgproc.THRESH_OTSU);
return mat;
}
代码示例来源:origin: RaiMan/SikuliX2
public static List<Element> detectChanges(Mat base, Mat mChanged) {
int PIXEL_DIFF_THRESHOLD = 3;
int IMAGE_DIFF_THRESHOLD = 5;
Mat mBaseGray = Element.getNewMat();
Mat mChangedGray = Element.getNewMat();
Mat mDiffAbs = Element.getNewMat();
Mat mDiffTresh = Element.getNewMat();
Mat mChanges = Element.getNewMat();
List<Element> rectangles = new ArrayList<>();
Imgproc.cvtColor(base, mBaseGray, toGray);
Imgproc.cvtColor(mChanged, mChangedGray, toGray);
Core.absdiff(mBaseGray, mChangedGray, mDiffAbs);
Imgproc.threshold(mDiffAbs, mDiffTresh, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
if (Core.countNonZero(mDiffTresh) > IMAGE_DIFF_THRESHOLD) {
Imgproc.threshold(mDiffAbs, mDiffAbs, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
Imgproc.dilate(mDiffAbs, mDiffAbs, Element.getNewMat());
Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
Imgproc.morphologyEx(mDiffAbs, mDiffAbs, Imgproc.MORPH_CLOSE, se);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat mHierarchy = Element.getNewMat();
Imgproc.findContours(mDiffAbs, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
rectangles = contoursToRectangle(contours);
Core.subtract(mDiffAbs, mDiffAbs, mChanges);
Imgproc.drawContours(mChanges, contours, -1, new Scalar(255));
//logShow(mDiffAbs);
}
return rectangles;
}
代码示例来源:origin: openpnp/openpnp
@Override
public Result process(CvPipeline pipeline) throws Exception {
Mat mat = pipeline.getWorkingImage();
int type = invert ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY;
type |= auto ? Imgproc.THRESH_OTSU : 0;
Imgproc.threshold(mat, mat, threshold, 255, type);
return null;
}
}
代码示例来源:origin: openpnp/openpnp
public FluentCv threshold(double threshold, boolean invert, String... tag) {
int type = invert ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY;
if (threshold == 0) {
type |= Imgproc.THRESH_OTSU;
}
Imgproc.threshold(mat, mat, threshold, 255, type);
return store(mat, tag);
}
代码示例来源:origin: JavaOpenCVBook/code
protected void processOperation() {
if(adaptiveMeanString.equals(thresholdMode)){
Imgproc.adaptiveThreshold(originalImage, image, maxval, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, blockSize, constantC);
}
else if(noneString.equals(thresholdMode)){
image = originalImage.clone();
}
else{
Imgproc.threshold(originalImage, image, level, maxval, modeMap.get(thresholdMode));
}
updateView(image);
}
代码示例来源:origin: ytai/IOIOPlotter
Imgproc.threshold(edgesImage_, edgesImage_, 0, 1, Imgproc.THRESH_BINARY);
Log.v(TAG, "Canny took: " + (System.currentTimeMillis() - start));
代码示例来源:origin: JavaOpenCVBook/code
private void resetImage() {
image = originalImage.clone();
Mat temp = new Mat();
Imgproc.cvtColor(image, temp,Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(temp, binary, imageThreshold, 255.0, Imgproc.THRESH_BINARY_INV);
updateView();
}
代码示例来源:origin: JavaOpenCVBook/code
protected void processOperation() {
Imgproc.Canny(originalImage, image, 220, 255, 3, false);
Imgproc.threshold(image, image, 100, 255, Imgproc.THRESH_BINARY_INV);
Imgproc.distanceTransform(image, image, Imgproc.CV_DIST_L2, 3);
image.convertTo(image, CvType.CV_8UC1);
Core.multiply(image, new Scalar(20), image);
updateView();
}
代码示例来源:origin: com.sikulix/sikulixapi
public boolean hasChanges(Mat current) {
int PIXEL_DIFF_THRESHOLD = 5;
int IMAGE_DIFF_THRESHOLD = 5;
Mat bg = new Mat();
Mat cg = new Mat();
Mat diff = new Mat();
Mat tdiff = new Mat();
Imgproc.cvtColor(base, bg, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(current, cg, Imgproc.COLOR_BGR2GRAY);
Core.absdiff(bg, cg, diff);
Imgproc.threshold(diff, tdiff, PIXEL_DIFF_THRESHOLD, 0.0, Imgproc.THRESH_TOZERO);
if (Core.countNonZero(tdiff) <= IMAGE_DIFF_THRESHOLD) {
return false;
}
Imgproc.threshold(diff, diff, PIXEL_DIFF_THRESHOLD, 255, Imgproc.THRESH_BINARY);
Imgproc.dilate(diff, diff, new Mat());
Mat se = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5,5));
Imgproc.morphologyEx(diff, diff, Imgproc.MORPH_CLOSE, se);
List<MatOfPoint> points = new ArrayList<MatOfPoint>();
Mat contours = new Mat();
Imgproc.findContours(diff, points, contours, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
int n = 0;
for (Mat pm: points) {
log(lvl, "(%d) %s", n++, pm);
printMatI(pm);
}
log(lvl, "contours: %s", contours);
printMatI(contours);
return true;
}
代码示例来源:origin: JavaOpenCVBook/code
public Mat process(Mat inputImage) {
Mat foregroundThresh = new Mat();
// Firstly, convert to gray-level image, yields good results with performance
Imgproc.cvtColor(inputImage, inputGray, Imgproc.COLOR_BGR2GRAY);
// initialize background to 1st frame, convert to floating type
if (accumulatedBackground.empty())
inputGray.convertTo(accumulatedBackground, CvType.CV_32F);
// convert background to 8U, for differencing with input image
accumulatedBackground.convertTo(backImage,CvType.CV_8U);
// compute difference between image and background
Core.absdiff(backImage,inputGray,foreground);
// apply threshold to foreground image
Imgproc.threshold(foreground,foregroundThresh, threshold,255, Imgproc.THRESH_BINARY_INV);
// accumulate background
Mat inputFloating = new Mat();
inputGray.convertTo(inputFloating, CvType.CV_32F);
Imgproc.accumulateWeighted(inputFloating, accumulatedBackground,learningRate, foregroundThresh);
return negative(foregroundThresh);
}
代码示例来源:origin: nroduit/Weasis
public List<MatOfPoint> getIsoDoseContourPoints(KeyDouble slicePosition, double isoDoseThreshold) {
List<MatOfPoint> contours = new ArrayList<>();
// Convert from threshold in cCy to raw pixel value threshold
double rawThreshold = (isoDoseThreshold / 100) / this.doseGridScaling;
DicomImageElement dosePlane = (DicomImageElement) this.getDosePlaneBySlice(slicePosition.getValue());
int rows = dosePlane.getImage().toMat().rows();
int cols = dosePlane.getImage().toMat().cols();
Mat src = new Mat(rows, cols, CvType.CV_32FC1);
Mat thr = new Mat(rows, cols, CvType.CV_32FC1);
dosePlane.getImage().toMat().convertTo(src, CvType.CV_32FC1);
Mat hierarchy = new Mat();
Imgproc.threshold(src, thr, rawThreshold, 255, Imgproc.THRESH_BINARY);
Mat thrSrc = new Mat(rows, cols, CvType.CV_8U);
thr.convertTo(thrSrc, CvType.CV_8U);
Imgproc.findContours(thrSrc, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
return contours;
}
代码示例来源:origin: JavaOpenCVBook/code
Imgproc.threshold(disparityImage, disparityThreshold, gui.getLevel(), 255.0f, Imgproc.THRESH_BINARY);
maskedImage.setTo(new Scalar(0,0,0));
colorImage.copyTo(maskedImage,disparityThreshold);
内容来源于网络,如有侵权,请联系作者删除!