Example usage for org.opencv.imgproc Imgproc dilate

List of usage examples for org.opencv.imgproc Imgproc dilate

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc dilate.

Prototype

public static void dilate(Mat src, Mat dst, Mat kernel) 

Source Link

Usage

From source file:Questao2.java

void maximo() {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    String url = escolherUrl();/*from  ww  w . ja  v  a 2 s  .  c  o  m*/
    /**
     * Transforma imagem em matriz para facilitar manipulacao
     */
    Mat img = Imgcodecs.imread(url);
    /**
     * Cria matriz destinoo
     */
    Mat dst = new Mat();
    /**
     * Cria imagem com o valor 1 em todos os pixels de
     * tamnho escolhido pelo usuario
     */
    System.out.println("Digite as dimenses da mscara: ");
    System.out.print("X: ");
    int mx = in.nextInt();
    System.out.print("Y: ");
    int my = in.nextInt();
    Mat one = Mat.ones(mx, my, CvType.CV_32F);
    /**
     * Aplica o filtro maximo utilizando a matriz one como mascara
     * dilate(imagem original, imagem destino, mascara)
     */
    Imgproc.dilate(img, dst, one);
    /**
     * Salva o resultado na matriz maximo.jpg
     */
    Imgcodecs.imwrite("maximo.jpg", dst);
    showResult("maximo.jpg");
}

From source file:MainDilation.java

public static void main(String[] args) {

    try {// w  w w .j  a  v  a2s .c  o m

        //int erosion_size = 5;
        int dilation_size = 5;

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat source = Highgui.imread("D://teste.png", Highgui.CV_LOAD_IMAGE_COLOR);

        Mat destination = new Mat(source.rows(), source.cols(), source.type());

        destination = source;

        Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT,
                new Size(2 * dilation_size + 1, 2 * dilation_size + 1));

        Imgproc.dilate(source, destination, element);

        Highgui.imwrite("D://Dilation.jpg", destination);

    } catch (Exception e) {
        System.out.println("Exception: " + e.getMessage());
    }

}

From source file:br.cefetmg.lsi.opencv.multipleObjectTracking.processing.MultipleObjectTracking.java

License:Open Source License

private void morphOps(Mat thresh) {
    //create structuring element that will be used to "dilate" and "erode" image.
    //the element chosen here is a 3px by 3px rectangle

    Mat erodeElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
    //dilate with larger element so make sure object is nicely visible
    Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));

    Imgproc.erode(thresh, thresh, erodeElement);
    Imgproc.erode(thresh, thresh, erodeElement);

    Imgproc.dilate(thresh, thresh, dilateElement);
    Imgproc.dilate(thresh, thresh, dilateElement);
}

From source file:by.zuyeu.deyestracker.core.detection.task.DetectPupilsTask.java

@Override
public Point call() throws Exception {
    long startTime = System.nanoTime();

    final Mat imageHSV = new Mat(frame.size(), Core.DEPTH_MASK_8U);
    Imgproc.cvtColor(frame, imageHSV, Imgproc.COLOR_BGR2GRAY);

    Imgproc.erode(imageHSV, imageHSV, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, STRUCT_ELEMENT_SIZE));
    Imgproc.dilate(imageHSV, imageHSV, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, STRUCT_ELEMENT_SIZE));
    Imgproc.GaussianBlur(imageHSV, imageHSV, STRUCT_ELEMENT_SIZE, GAUS_BLUR_DELTA);

    Core.MinMaxLocResult mmG = Core.minMaxLoc(imageHSV);

    long endTime = System.nanoTime();
    LOG.debug("pupil detected = {}", mmG.minLoc);
    LOG.debug("detection time: {} ms", (float) (endTime - startTime) / 1000000);
    return mmG.minLoc;
}

From source file:Clases.Segmentador.java

public Mat dilatarImg(Mat umbralizada) {
    Mat dstd = umbralizada.clone();//w w w.j  a va2 s .  c o m
    int dilatacion_size = 5;
    Size sd = new Size(2 * dilatacion_size + 1, 2 * dilatacion_size + 1);
    Point pd = new Point(dilatacion_size, dilatacion_size);
    Mat elementd = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, sd, pd);
    Imgproc.dilate(umbralizada, dstd, elementd);
    return dstd;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

/**
 * Applies morphological dilation.// w  ww.  jav  a 2 s.c  o m
 *
 * @param src - source image.
 * @param radius - radius of structure element.
 * @return dilated image.
 */
public static Mat dilate(Mat src, int radius) {
    Mat dest = new Mat();
    int kernelSize = radius * 2 + 1;
    Mat kernel = getStructuringElement(Imgproc.CV_SHAPE_ELLIPSE, new Size(kernelSize, kernelSize),
            new Point(radius, radius));
    Imgproc.dilate(src, dest, kernel);
    return dest;
}

From source file:com.compta.firstak.notedefrais.MainActivity.java

public void Opencv(String imageName) {
        bitmap = BitmapFactory.decodeFile(imageName);
        Mat imageMat = new Mat();
        org.opencv.android.Utils.bitmapToMat(bitmap, imageMat);

        Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_BGR2GRAY);
        // 1) Apply gaussian blur to remove noise
        Imgproc.GaussianBlur(imageMat, imageMat, new Size(9, 9), 0);
        // 2) AdaptiveThreshold -> classify as either black or white
        Imgproc.adaptiveThreshold(imageMat, imageMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 5,
                2);/*from   w  w w .jav a 2  s . c  o  m*/

        // 3) Invert the image -> so most of the image is black
        Core.bitwise_not(imageMat, imageMat);

        // 4) Dilate -> fill the image using the MORPH_DILATE
        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE, new Size(3, 3), new Point(1, 1));
        Imgproc.dilate(imageMat, imageMat, kernel);

        org.opencv.android.Utils.matToBitmap(imageMat, bitmap);
        mImageViewer.setImageBitmap(bitmap);
        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1);
        byteArray = stream1.toByteArray();
    }

From source file:com.davidmiguel.gobees.monitoring.algorithm.processors.Morphology.java

License:Open Source License

@Override
public Mat process(@NonNull Mat frame) {
    if (frame.empty()) {
        Log.e("Invalid input frame.");
        return null;
    }/*from  w  w w  . j  av a 2s. co  m*/
    Mat tmp = frame.clone();
    // Step 1: erode to remove legs
    Imgproc.erode(tmp, tmp, KERNEL3);
    // Step 2: dilate to join bodies and heads
    Imgproc.dilate(tmp, tmp, KERNEL2);
    for (int i = 0; i < REPETITIONS_DILATE; i++) {
        Imgproc.dilate(tmp, tmp, kernelDilate);
    }
    // Step 3: erode to recover original size
    Imgproc.erode(tmp, tmp, KERNEL1);
    for (int i = 0; i < REPETITIONS_ERODE; i++) {
        Imgproc.erode(tmp, tmp, kernelErode);
    }
    return tmp;
}

From source file:com.example.root.dipproj.MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            File f = new File(Environment.getExternalStorageDirectory().toString());
            for (File temp : f.listFiles()) {
                if (temp.getName().equals("temp.jpg")) {
                    f = temp;//from w w  w .j  a  v a  2  s. c om
                    break;
                }
            }
            try {
                Bitmap bitmap;
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
                viewImage.setImageBitmap(bitmap);
                String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix"
                        + File.separator + "default";
                f.delete();
                OutputStream outFile = null;
                File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                try {
                    outFile = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                    outFile.flush();
                    outFile.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestCode == 2) {
            Uri selectedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            c.close();
            Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
            Log.w("path of image", picturePath + "");
            Mat imgMat = new Mat();
            Mat imgMat2 = new Mat();
            Mat imgMat3 = new Mat();
            Utils.bitmapToMat(thumbnail, imgMat);
            Imgproc.cvtColor(imgMat, imgMat, Imgproc.COLOR_RGB2GRAY);
            org.opencv.core.Size s = new Size(3, 3);
            Imgproc.createCLAHE();
            Imgproc.GaussianBlur(imgMat, imgMat, s, 2);
            Imgproc.erode(imgMat, imgMat2, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
            Imgproc.dilate(imgMat2, imgMat3, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
            Imgproc.Sobel(imgMat, imgMat, CvType.CV_8UC1, 1, 0);
            Core.absdiff(imgMat, imgMat3, imgMat);
            Imgproc.threshold(imgMat, imgMat, 123, 255, Imgproc.THRESH_OTSU);
            Utils.matToBitmap(imgMat, thumbnail);
            viewImage.setImageBitmap(thumbnail);
            saveBitmaptoSDCard(thumbnail);
        }
    }
}

From source file:com.example.sarthuak.opencv.MainActivity.java

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    // TODO Auto-generated method stub
    final int viewMode = mViewMode;
    switch (viewMode) {

    case VIEW_MODE_RGBA:
        // input frame has RBGA format
        mRgba = inputFrame.rgba();//w ww . j a va2s .c om
        break;
    case VIEW_MODE_CANNY:
        // input frame has gray scale format
        mRgba = inputFrame.rgba();
        Imgproc.Canny(inputFrame.gray(), mRgbaF, 80, 100);
        Imgproc.cvtColor(mRgbaF, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
        break;

    case VIEW_MODE_ocr:
        startActivity(new Intent(this, ScanLicensePlateActivity.class));
        break;

    case VIEW_MODE_new:
        Mat mRgba;

        mRgba = inputFrame.rgba();
        drawing = mRgba.clone();

        mRgbaT = drawing;

        Imgproc.cvtColor(drawing, mRgbaT, Imgproc.COLOR_BGR2GRAY);

        org.opencv.core.Size s = new Size(1, 1);
        Imgproc.GaussianBlur(mRgbaT, mRgbaT, s, 0, 0);

        Imgproc.Canny(mRgbaT, mRgbaT, 100, 255);
        Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5));
        Imgproc.dilate(mRgbaT, mRgbaT, element);
        List<MatOfPoint> contours = new ArrayList<>();

        Imgproc.findContours(drawing, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE,
                new Point(0, 0));
        double maxArea = -1;
        int maxAreaIdx = -1;

        for (int idx = 0; idx < contours.size(); idx++) {
            Mat contour = contours.get(idx);

            double contourarea = Imgproc.contourArea(contour);
            if (contourarea > maxArea) {

                maxArea = contourarea;
                maxAreaIdx = idx;
            }
        }

        Imgproc.drawContours(mRgba, contours, maxAreaIdx, new Scalar(255, 0, 0), 5);

    }
    return mRgba; // This function must return

}