Java Code Examples for org.opencv.core.Core#convertScaleAbs()

The following examples show how to use org.opencv.core.Core#convertScaleAbs() . 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: WeightCalculate.java    From ImageEnhanceViaFusion with MIT License 7 votes vote down vote up
public static Mat LaplacianContrast(Mat img) {
	Mat laplacian = new Mat();
	Imgproc.Laplacian(img, laplacian, img.depth());
	//Imgproc.Laplacian(img, laplacian, img.depth(), 3, 1, 0);
	Core.convertScaleAbs(laplacian, laplacian);
	return laplacian;
}
 
Example 2
Source File: ProcessHelper.java    From OpenCV-android with Apache License 2.0 6 votes vote down vote up
/**
 * 角点检测
 *
 * @param origin   原始bitmap
 * @param callback 回调
 */
public void harris(Bitmap origin, ProcessCallback callback) {
    if (origin == null) {
        return;
    }
    try {
        Bitmap bitmap = Bitmap.createBitmap(origin.getWidth(), origin.getHeight(), Bitmap.Config.RGB_565);
        Utils.bitmapToMat(origin, rgbMat);
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);
        Mat corners = new Mat();
        Mat tempDst = new Mat();
        Mat tempDstNorm = new Mat();
        // 找出角点
        Imgproc.cornerHarris(grayMat, tempDst, 2, 3, 0.04);
        // 归一化Harris角点的输出
        Core.normalize(tempDst, tempDstNorm, 0, 255, Core.NORM_MINMAX);
        Core.convertScaleAbs(tempDstNorm, corners);
        // 绘制角点
        Random random = new Random();
        for (int i = 0; i < tempDstNorm.cols(); i++) {
            for (int j = 0; j < tempDstNorm.rows(); j++) {
                double[] value = tempDstNorm.get(j, i);
                if (value[0] > 250) {
                    // 决定了画出哪些角点,值越大选择画出的点就越少
                    Imgproc.circle(corners, new Point(i, j), 5, new Scalar(random.nextInt(255)), 2);
                }
            }
        }
        Utils.matToBitmap(corners, bitmap);
        callback.onSuccess(bitmap);
    } catch (Exception e) {
        callback.onFailed(e.getMessage());
    }
}
 
Example 3
Source File: MainActivity.java    From MOAAP with MIT License 6 votes vote down vote up
void Sobel() {
    Mat grayMat = new Mat();
    Mat sobel = new Mat(); //Mat to store the final result

    //Matrices to store gradient and absolute gradient respectively
    Mat grad_x = new Mat();
    Mat abs_grad_x = new Mat();

    Mat grad_y = new Mat();
    Mat abs_grad_y = new Mat();

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    //Calculating gradient in horizontal direction
    Imgproc.Sobel(grayMat, grad_x, CvType.CV_16S, 1, 0, 3, 1, 0);

    //Calculating gradient in vertical direction
    Imgproc.Sobel(grayMat, grad_y, CvType.CV_16S, 0, 1, 3, 1, 0);

    //Calculating absolute value of gradients in both the direction
    Core.convertScaleAbs(grad_x, abs_grad_x);
    Core.convertScaleAbs(grad_y, abs_grad_y);

    //Calculating the resultant gradient
    Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 1, sobel);

    //Converting Mat back to Bitmap
    Utils.matToBitmap(sobel, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 4
Source File: FeatureWeight.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
public static Mat LaplacianContrast(Mat img) {
	Mat laplacian = new Mat();
	Imgproc.Laplacian(img, laplacian, img.depth());
	//Imgproc.Laplacian(img, laplacian, img.depth(), 3, 1, 0);
	Core.convertScaleAbs(laplacian, laplacian);
	return laplacian;
}
 
Example 5
Source File: MainActivity.java    From MOAAP with MIT License 5 votes vote down vote up
void HarrisCorner() {
    Mat grayMat = new Mat();
    Mat corners = new Mat();

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    Mat tempDst = new Mat();
    //finding contours
    Imgproc.cornerHarris(grayMat, tempDst, 2, 3, 0.04);

    //Normalizing harris corner's output
    Mat tempDstNorm = new Mat();
    Core.normalize(tempDst, tempDstNorm, 0, 255, Core.NORM_MINMAX);
    Core.convertScaleAbs(tempDstNorm, corners);

    //Drawing corners on a new image
    Random r = new Random();
    for (int i = 0; i < tempDstNorm.cols(); i++) {
        for (int j = 0; j < tempDstNorm.rows(); j++) {
            double[] value = tempDstNorm.get(j, i);
            if (value[0] > 150)
                Imgproc.circle(corners, new Point(i, j), 5, new Scalar(r.nextInt(255)), 2);
        }
    }

    //Converting Mat back to Bitmap
    Utils.matToBitmap(corners, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 6
Source File: CVProcessor.java    From CVScanner with GNU General Public License v3.0 5 votes vote down vote up
public static List<MatOfPoint> findContoursForMRZ(Mat src){
    Mat img = src.clone();
    src.release();
    double ratio = getScaleRatio(img.size());
    int width = (int) (img.size().width / ratio);
    int height = (int) (img.size().height / ratio);
    Size newSize = new Size(width, height);
    Mat resizedImg = new Mat(newSize, CvType.CV_8UC4);
    Imgproc.resize(img, resizedImg, newSize);

    Mat gray = new Mat();
    Imgproc.cvtColor(resizedImg, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.medianBlur(gray, gray, 3);
    //Imgproc.blur(gray, gray, new Size(3, 3));

    Mat morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(13, 5));
    Mat dilatedImg = new Mat();
    Imgproc.morphologyEx(gray, dilatedImg, Imgproc.MORPH_BLACKHAT, morph);
    gray.release();

    Mat gradX = new Mat();
    Imgproc.Sobel(dilatedImg, gradX, CvType.CV_32F, 1, 0);
    dilatedImg.release();
    Core.convertScaleAbs(gradX, gradX, 1, 0);
    Core.MinMaxLocResult minMax = Core.minMaxLoc(gradX);
    Core.convertScaleAbs(gradX, gradX, (255/(minMax.maxVal - minMax.minVal)),
            - ((minMax.minVal * 255) / (minMax.maxVal - minMax.minVal)));
    Imgproc.morphologyEx(gradX, gradX, Imgproc.MORPH_CLOSE, morph);

    Mat thresh = new Mat();
    Imgproc.threshold(gradX, thresh, 0, 255, Imgproc.THRESH_OTSU);
    gradX.release();
    morph.release();

    morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 21));
    Imgproc.morphologyEx(thresh, thresh, Imgproc.MORPH_CLOSE, morph);
    Imgproc.erode(thresh, thresh, new Mat(), new Point(-1, -1), 4);
    morph.release();

    int col = (int) resizedImg.size().width;
    int p = (int) (resizedImg.size().width * 0.05);
    int row = (int) resizedImg.size().height;
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < p; j++){
            thresh.put(i, j, 0);
            thresh.put(i, col-j, 0);
        }
    }

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(thresh, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    hierarchy.release();

    Log.d(TAG, "contours found: " + contours.size());

    Collections.sort(contours, new Comparator<MatOfPoint>() {
        @Override
        public int compare(MatOfPoint o1, MatOfPoint o2) {
            return Double.valueOf(Imgproc.contourArea(o2)).compareTo(Imgproc.contourArea(o1));
        }
    });

    return contours;
}