Java Code Examples for org.opencv.imgproc.Imgproc#adaptiveThreshold()

The following examples show how to use org.opencv.imgproc.Imgproc#adaptiveThreshold() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OCRProcessor.java    From Camdroid with Apache License 2.0 8 votes vote down vote up
protected void execute() {
    out = gray();

    Imgproc.equalizeHist(out, out);
    Core.normalize(out, out, min, max, Core.NORM_MINMAX);

    Imgproc.adaptiveThreshold(out, out, 255, Imgproc.THRESH_BINARY,
            Imgproc.ADAPTIVE_THRESH_MEAN_C, blocksize, reduction);

    byte[] data = new byte[(int) out.total()];
    out.get(0, 0, data);

    this.tessBaseAPI.setImage(data, out.width(), out.height(),
            out.channels(), (int) out.step1());

    String utf8Text = this.tessBaseAPI.getUTF8Text();
    int score = this.tessBaseAPI.meanConfidence();
    this.tessBaseAPI.clear();


    if (score >= SIMPLETEXT_MIN_SCORE && utf8Text.length() > 0) {
        simpleText = utf8Text;
    } else {
        simpleText = new String();
    }
}
 
Example 2
Source File: ImageProcessor.java    From Document-Scanner with GNU General Public License v3.0 7 votes vote down vote up
private void enhanceDocument( Mat src ) {
    if (colorMode && filterMode) {
        src.convertTo(src,-1, colorGain , colorBias);
        Mat mask = new Mat(src.size(), CvType.CV_8UC1);
        Imgproc.cvtColor(src,mask,Imgproc.COLOR_RGBA2GRAY);

        Mat copy = new Mat(src.size(), CvType.CV_8UC3);
        src.copyTo(copy);

        Imgproc.adaptiveThreshold(mask,mask,255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY_INV,15,15);

        src.setTo(new Scalar(255,255,255));
        copy.copyTo(src,mask);

        copy.release();
        mask.release();

        // special color threshold algorithm
        colorThresh(src,colorThresh);
    } else if (!colorMode) {
        Imgproc.cvtColor(src,src,Imgproc.COLOR_RGBA2GRAY);
        if (filterMode) {
            Imgproc.adaptiveThreshold(src, src, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 15);
        }
    }
}
 
Example 3
Source File: MainActivity.java    From MOAAP with MIT License 6 votes vote down vote up
@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;
    }
}
 
Example 4
Source File: MainActivity.java    From SimpleDocumentScanner-Android with MIT License 6 votes vote down vote up
/**
     * Apply a threshold to give the "scanned" look
     *
     * NOTE:
     * See the following link for more info http://docs.opencv.org/3.1.0/d7/d4d/tutorial_py_thresholding.html#gsc.tab=0
     * @param src A valid Mat
     * @return The processed Bitmap
     */
    private Bitmap applyThreshold(Mat src) {
        Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2GRAY);

        // Some other approaches
//        Imgproc.adaptiveThreshold(src, src, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 15);
//        Imgproc.threshold(src, src, 0, 255, Imgproc.THRESH_BINARY + Imgproc.THRESH_OTSU);

        Imgproc.GaussianBlur(src, src, new Size(5, 5), 0);
        Imgproc.adaptiveThreshold(src, src, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 11, 2);

        Bitmap bm = Bitmap.createBitmap(src.width(), src.height(), Bitmap.Config.ARGB_8888);
        org.opencv.android.Utils.matToBitmap(src, bm);

        return bm;
    }
 
Example 5
Source File: HighGuiUtil.java    From javautils with Apache License 2.0 5 votes vote down vote up
/**
 * 二值化
 *
 * @param oriImg
 * @param outputImg
 */
public static void binarization(String oriImg, String outputImg) {
    Mat img = Highgui.imread(oriImg);
    Imgproc.cvtColor(img, img, Imgproc.COLOR_RGB2GRAY);
    //
    Imgproc.adaptiveThreshold(img, img, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 25, 10);
    Highgui.imwrite(outputImg, img);
}
 
Example 6
Source File: DigitRecognizer.java    From MOAAP with MIT License 5 votes vote down vote up
int FindMatch(Mat test_image)
{

    //Dilate the image
    Imgproc.dilate(test_image, test_image, Imgproc.getStructuringElement(Imgproc.CV_SHAPE_CROSS, new Size(3,3)));
    //Resize the image to match it with the sample image size
    Imgproc.resize(test_image, test_image, new Size(width, height));
    //Convert the image to grayscale
    Imgproc.cvtColor(test_image, test_image, Imgproc.COLOR_RGB2GRAY);
    //Adaptive Threshold
    Imgproc.adaptiveThreshold(test_image,test_image,255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV,15, 2);

    Mat test = new Mat(1, test_image.rows() * test_image.cols(), CvType.CV_32FC1);
    int count = 0;
    for(int i = 0 ; i < test_image.rows(); i++)
    {
        for(int j = 0 ; j < test_image.cols(); j++) {
            test.put(0, count, test_image.get(i, j)[0]);
            count++;
        }
    }

    Mat results = new Mat(1, 1, CvType.CV_8U);

    //K-NN Prediction
    //return (int)knn.findNearest(test, 10, results);

    //SVM Prediction
    return (int)svm.predict(test);
}
 
Example 7
Source File: AdaptiveThresholdProcessor.java    From Camdroid with Apache License 2.0 5 votes vote down vote up
protected void execute() {
    Mat gray = gray();

    Imgproc.adaptiveThreshold(gray, out, 255,
            Imgproc.THRESH_BINARY_INV, Imgproc.ADAPTIVE_THRESH_MEAN_C,
            blocksize, reduction);
}
 
Example 8
Source File: BinaryUtils.java    From super-cloudops with Apache License 2.0 2 votes vote down vote up
/**
 * opencv自带的二值化
 * 
 * @param src
 * @return
 */
public static Mat binaryNative(Mat src) {
	Mat dst = src.clone();
	Imgproc.adaptiveThreshold(src, dst, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 25, 10);
	return dst;
}