本文整理汇总了Java中org.opencv.imgproc.Imgproc.threshold方法的典型用法代码示例。如果您正苦于以下问题:Java Imgproc.threshold方法的具体用法?Java Imgproc.threshold怎么用?Java Imgproc.threshold使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.opencv.imgproc.Imgproc
的用法示例。
在下文中一共展示了Imgproc.threshold方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: adaptativeProcess
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static Mat adaptativeProcess(Mat img){
Mat im = new Mat();
Imgproc.threshold(img,im,120,255,Imgproc.THRESH_TRUNC);
im = Thresholding.adaptativeThresholding(im);
Imgproc.medianBlur(im,im,7);
Mat threshImg = Thresholding.InvertImageColor(im);
Thresholding.gridDetection(threshImg);
Mat mat = Mat.zeros(4,2,CvType.CV_32F);
mat.put(0,0,0); mat.put(0,1,512);
mat.put(1,0,0); mat.put(1,1,0);
mat.put(2,0,512); mat.put(2,1,0);
mat.put(3,0,512); mat.put(3,1,512);
mat = Imgproc.getPerspectiveTransform(Thresholding.grid,mat);
Mat M = new Mat();
Imgproc.warpPerspective(threshImg,M,mat, new Size(512,512));
Imgproc.medianBlur(M,M,3);
Imgproc.threshold(M,M,254,255,Imgproc.THRESH_BINARY);
return Thresholding.InvertImageColor(M);
}
示例2: doBackgroundRemovalAbsDiff
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
private Mat doBackgroundRemovalAbsDiff(Mat currFrame) {
Mat greyImage = new Mat();
Mat foregroundImage = new Mat();
if (oldFrame == null)
oldFrame = currFrame;
Core.absdiff(currFrame, oldFrame, foregroundImage);
Imgproc.cvtColor(foregroundImage, greyImage, Imgproc.COLOR_BGR2GRAY);
int thresh_type = Imgproc.THRESH_BINARY_INV;
if (inverse.isSelected())
thresh_type = Imgproc.THRESH_BINARY;
Imgproc.threshold(greyImage, greyImage, 10, 255, thresh_type);
currFrame.copyTo(foregroundImage, greyImage);
oldFrame = currFrame;
return foregroundImage;
}
示例3: DifferenceOfGaussian
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public void DifferenceOfGaussian() {
Mat grayMat = new Mat();
Mat blur1 = new Mat();
Mat blur2 = new Mat();
//Converting the image to grayscale
Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(grayMat, blur1, new Size(15, 15), 5);
Imgproc.GaussianBlur(grayMat, blur2, new Size(21, 21), 5);
//Subtracting the two blurred images
Mat DoG = new Mat();
Core.absdiff(blur1, blur2, DoG);
//Inverse Binary Thresholding
Core.multiply(DoG, new Scalar(100), DoG);
Imgproc.threshold(DoG, DoG, 50, 255, Imgproc.THRESH_BINARY_INV);
//Converting Mat back to Bitmap
Utils.matToBitmap(DoG, currentBitmap);
imageView.setImageBitmap(currentBitmap);
}
示例4: enhancement
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Enhance the image after ridge filter.
* Apply mask, binary threshold, thinning, ..., etc.
*/
private void enhancement(Mat source, Mat result, int blockSize, int rows, int cols, int padding) {
Mat MatSnapShotMask = snapShotMask(rows, cols, padding);
Mat paddedMask = imagePadding(MatSnapShotMask, blockSize);
if (BuildConfig.DEBUG && !paddedMask.size().equals(source.size())) {
throw new RuntimeException("Incompatible sizes of image and mask");
}
// apply the original mask to get rid of extras
Core.multiply(source, paddedMask, result, 1.0, CvType.CV_8UC1);
// apply binary threshold
Imgproc.threshold(result, result, 0, 255, Imgproc.THRESH_BINARY);
}
示例5: leviRedFilter
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public void leviRedFilter (Mat input, Mat mask, double threshold){
Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2Lab);
Imgproc.GaussianBlur(input,input,new Size(3,3),0);
Core.split(input, channels);
Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
for(int i=0;i<channels.size();i++){
channels.get(i).release();
}
}
示例6: leviBlueFilter
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public void leviBlueFilter (Mat input, Mat mask){
List<Mat> channels = new ArrayList<>();
Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2Lab);
Imgproc.GaussianBlur(input,input,new Size(3,3),0);
Core.split(input, channels);
Imgproc.threshold(channels.get(1), mask, 145, 255, Imgproc.THRESH_BINARY);
for(int i=0;i<channels.size();i++){
channels.get(i).release();
}
}
示例7: bin_second
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static Mat bin_second(Mat input) {
final Mat gray = new Mat();
Imgproc.cvtColor(input, gray, Imgproc.COLOR_RGB2GRAY);
final Mat binImg = new Mat();
Imgproc.threshold(gray, binImg, 0, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY_INV);
// Imgproc.adaptiveThreshold(gray, binImg, 255,
// Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 11, 0);
return binImg;
}
示例8: binarize
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Method for image binarization
* @parm img image to process
* @parm flag precize which method to
* @return imB binarized image
* @see org.opencv.imgproc.Imgproc #threshold(Mat,int)
*/
public static Mat binarize(Mat img, int flag){
Mat imB = new Mat();
if(flag == Imgproc.THRESH_BINARY)
Imgproc.threshold(img,imB,0,255,Imgproc.THRESH_BINARY);
if(flag == Imgproc.THRESH_OTSU)
Imgproc.threshold(img,imB,0,255,Imgproc.THRESH_BINARY+Imgproc.THRESH_OTSU);
if(flag == Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C)
Imgproc.adaptiveThreshold(img,imB,255,Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY,7,2);
if(flag == Imgproc.ADAPTIVE_THRESH_MEAN_C)
Imgproc.adaptiveThreshold(img,imB,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY,7,2);
return imB;
}
示例9: getCanny
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
protected Mat getCanny(Mat gray) {
Mat threshold = new Mat();
Mat canny = new Mat();
// last paramter 8 is using OTSU algorithm
double high_threshold = Imgproc.threshold(gray, threshold, 0, 255, 8);
double low_threshold = high_threshold * 0.5;
Imgproc.Canny(gray, canny, low_threshold, high_threshold);
return canny;
}
示例10: doBackgroundRemoval
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
/**
* Perform the operations needed for removing a uniform background
*
* @param frame the current frame
* @return an image with only foreground objects
*/
private Mat doBackgroundRemoval(Mat frame) {
// init
Mat hsvImg = new Mat();
List<Mat> hsvPlanes = new ArrayList<>();
Mat thresholdImg = new Mat();
int thresh_type = Imgproc.THRESH_BINARY_INV;
if (inverse.isSelected())
thresh_type = Imgproc.THRESH_BINARY;
// threshold the image with the average hue value
hsvImg.create(frame.size(), CvType.CV_8U);
Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV);
Core.split(hsvImg, hsvPlanes);
// get the average hue value of the image
double threshValue = getHistAverage(hsvImg, hsvPlanes.get(0));
Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0, thresh_type);
Imgproc.blur(thresholdImg, thresholdImg, new Size(5, 5));
// dilate to fill gaps, erode to smooth edges
Imgproc.dilate(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1), 1);
Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1), 3);
Imgproc.threshold(thresholdImg, thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY);
// create the new image
Mat foreground = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
frame.copyTo(foreground, thresholdImg);
return foreground;
}
示例11: main
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static void main(String args[]) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Import openCV
Webcam panel = new Webcam(); // Initialize itself
// Initialize JPanel
JFrame frame = new JFrame("Webcam");
frame.setSize(200, 200);
frame.setContentPane(panel);
frame.setVisible(true);
VideoCapture camera = new VideoCapture(0); // The camera
// Attempt to set the frame size, but it doesn't really work. Only just makes it bigger
camera.set(Videoio.CAP_PROP_FRAME_WIDTH, 1900);
camera.set(Videoio.CAP_PROP_FRAME_HEIGHT, 1000);
// Special window listener because there are threads that need to be shutdown on a close
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
camera.release();
System.exit(0);
}
});
if (camera.isOpened()) {
// Create SwingWorker to encapsulate the process in a thread
SwingWorker<Void, Mat> worker = new SwingWorker<Void, Mat>() {
@Override
protected Void doInBackground() throws Exception {
// Put something into thisFrame so it doesn't glitch at the beginning
Mat thisFrame = new Mat();
camera.read(thisFrame);
Imgproc.cvtColor(thisFrame, thisFrame, Imgproc.COLOR_BGR2GRAY); // Convert the diff to gray
// Set up new Mats for diffing them later, manually set the amount of channels to avoid an openCV error
Mat pastFrame = new Mat();
Mat diff = new Mat();
// isCancelled is set by the SwingWorker
while (!isCancelled()) {
thisFrame.copyTo(pastFrame); // What was previously the frame is now the pastFrame
camera.read(thisFrame); // Get camera image, and set it to currentImage
Imgproc.cvtColor(thisFrame, thisFrame, Imgproc.COLOR_BGR2GRAY); // Convert the diff to gray
if (!thisFrame.empty()) {
// Set the frame size to have a nice border around the image
frame.setSize(thisFrame.width() + 40, thisFrame.height() + 60);
Core.absdiff(thisFrame, pastFrame, diff); // Diff the frames
Imgproc.GaussianBlur(diff, diff, new Size(7, 7), 7); // Despeckle
Imgproc.threshold(diff, diff, 5, 255, 1); // Threshhold the gray
image = matrixToBuffer(getFace(thisFrame, diff)); // Update the display image
panel.repaint(); // Refresh the panel
} else {
System.err.println("Error: no frame captured");
}
//Thread.sleep(70); // Set refresh rate, as well as prevent the code from tripping over itself
}
return null;
}
};
worker.execute();
}
return;
}
示例12: process
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
@Override
public void process(Mat input, Mat mask) {
channels = new ArrayList<>();
switch(color){
case RED:
if(threshold == -1){
threshold = 164;
}
Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2Lab);
Imgproc.GaussianBlur(input,input,new Size(3,3),0);
Core.split(input, channels);
Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
break;
case BLUE:
if(threshold == -1){
threshold = 145;
}
Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2YUV);
Imgproc.GaussianBlur(input,input,new Size(3,3),0);
Core.split(input, channels);
Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
break;
case YELLOW:
if(threshold == -1){
threshold = 95;
}
Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2YUV);
Imgproc.GaussianBlur(input,input,new Size(3,3),0);
Core.split(input, channels);
Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY_INV);
break;
}
for(int i=0;i<channels.size();i++){
channels.get(i).release();
}
input.release();
}
示例13: computeModel
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public void computeModel(ArrayList<MetaData> photos)
{
numPhotos = photos.size();
model.setNumPhotos(numPhotos);
MatOfKeyPoint[] keypoints = new MatOfKeyPoint[numPhotos];
Mat[] descriptors = new Mat[numPhotos];
Mat allDescriptors = new Mat();
ArrayList<Integer> descriptorLabels = new ArrayList<Integer>();
// compute keypoints and descriptors
Mat currentImg = null;
for (int a = 0; a < numPhotos; a++)
{
// System.out.println("now:" + animalFiles.get(a));
currentImg = Highgui.imread(photos.get(a).getZooName().toString(), 0);
Imgproc.resize(currentImg, currentImg, new Size(150, 250));
Imgproc.equalizeHist(currentImg, currentImg);
Imgproc.threshold(currentImg, currentImg, 127, 255, Imgproc.THRESH_BINARY);
featureDetector.detect(currentImg, keypoints[a]);
descriptorExtractor.compute(currentImg, keypoints[a], descriptors[a]);
allDescriptors.push_back(descriptors[a]);
for (int i = 0; i < descriptors[a].rows(); i++)
descriptorLabels.add(a);
}
System.out.println("label size:" + descriptorLabels.size());
Mat clusterLabels = new Mat();
Mat centers = new Mat();
// set up all desriptors, init criteria
allDescriptors.convertTo(allDescriptors, CvType.CV_32F);
TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER, 100, 0.1);
long before = System.currentTimeMillis();
// compute clusters
System.out.print("creating kmeans clusters...");
Core.kmeans(allDescriptors, k, clusterLabels, criteria, 10, Core.KMEANS_PP_CENTERS, centers);
System.out.println("done.");
// map k-means centroid labels to descriptors of all images
ArrayList<ArrayList<Integer>> clusterImageMap = new ArrayList<ArrayList<Integer>>();
for (int nk = 0; nk < k + 1; nk++)
clusterImageMap.add(new ArrayList<Integer>());
for (int r = 0; r < clusterLabels.rows(); r++)
clusterImageMap.get((int) clusterLabels.get(r, 0)[0]).add(descriptorLabels.get(r));
model.setCentroids(centers);
model.setLabels(clusterLabels);
model.setClusterImageMap(clusterImageMap);
model.setKeypoints(keypoints);
model.setDescriptors(descriptors);
}
示例14: normalThresholding
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
public static Mat normalThresholding(Mat img){
Mat im = new Mat();
Imgproc.threshold(img,im,120,255,Imgproc.THRESH_TRUNC);
Imgproc.threshold(im,img,0,255,Imgproc.THRESH_OTSU+Imgproc.THRESH_BINARY);
return img;
}
示例15: onActivityResult
import org.opencv.imgproc.Imgproc; //导入方法依赖的package包/类
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
//Put it there, just in case:)
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK && read_external_storage_granted){
try {
final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
src = new Mat(selectedImage.getHeight(), selectedImage.getWidth(), CvType.CV_8UC4);
Utils.bitmapToMat(selectedImage, src);
src_gray = new Mat(selectedImage.getHeight(), selectedImage.getWidth(), CvType.CV_8UC1);
switch (ACTION_MODE) {
case HomeActivity.GAUSSIAN_BLUR:
Imgproc.GaussianBlur(src, src, new Size(9, 9), 0);
break;
case HomeActivity.MEAN_BLUR:
Imgproc.blur(src, src, new Size(9, 9));
break;
case HomeActivity.MEDIAN_BLUR:
Imgproc.medianBlur(src, src, 9);
break;
case HomeActivity.SHARPEN:
Mat kernel = new Mat(3, 3, CvType.CV_16SC1);
//int[] values = {0, -1, 0, -1, 5, -1, 0, -1, 0};
Log.d("imageType", CvType.typeToString(src.type()) + "");
kernel.put(0, 0, 0, -1, 0, -1, 5, -1, 0, -1, 0);
Imgproc.filter2D(src, src, src_gray.depth(), kernel);
break;
case HomeActivity.DILATE:
Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(src_gray, src_gray, 100, 255, Imgproc.THRESH_BINARY);
Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
Imgproc.dilate(src_gray, src_gray, kernelDilate);
Imgproc.cvtColor(src_gray, src, Imgproc.COLOR_GRAY2RGBA, 4);
break;
case HomeActivity.ERODE:
Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(src_gray, src_gray, 100, 255, Imgproc.THRESH_BINARY);
Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
Imgproc.erode(src_gray, src_gray, kernelErode);
Imgproc.cvtColor(src_gray, src, Imgproc.COLOR_GRAY2RGBA, 4);
break;
case HomeActivity.THRESHOLD:
Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(src_gray, src_gray, 100, 255, Imgproc.THRESH_BINARY);
Imgproc.cvtColor(src_gray, src, Imgproc.COLOR_GRAY2RGBA, 4);
break;
case HomeActivity.ADAPTIVE_THRESHOLD:
Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.adaptiveThreshold(src_gray, src_gray, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 3, 0);
Imgproc.cvtColor(src_gray, src, Imgproc.COLOR_GRAY2RGBA, 4);
break;
}
Bitmap processedImage = Bitmap.createBitmap(src.cols(), src.rows(), Bitmap.Config.ARGB_8888);
Log.i("imageType", CvType.typeToString(src.type()) + "");
Utils.matToBitmap(src, processedImage);
ivImage.setImageBitmap(selectedImage);
ivImageProcessed.setImageBitmap(processedImage);
Log.i("process", "process done");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
}
}