Example usage for org.opencv.core Mat cols

List of usage examples for org.opencv.core Mat cols

Introduction

In this page you can find the example usage for org.opencv.core Mat cols.

Prototype

public int cols() 

Source Link

Usage

From source file:finalpro.FinalPro.java

public static String threshholding() {
    Mat destination = null;//www .ja  va 2 s  . c o  m
    Mat source = null;
    String str = "";
    try {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        source = Imgcodecs.imread("C:/QuadPotroler/FinalPro/src/images/20151207_153915.jpg",
                Imgcodecs.CV_LOAD_IMAGE_COLOR);
        destination = new Mat(source.rows(), source.cols(), source.type());
        destination = source;
        Imgproc.threshold(source, destination, 127, 255, Imgproc.THRESH_TOZERO);
        Imgcodecs.imwrite("C:/QuadPotroler/FinalPro/src/images/threshdold.jpg", destination);
        str = "C:/QuadPotroler/FinalPro/src/images/threshdold.jpg";
    } catch (Exception e) {
        System.out.println("error: " + e.getMessage());
    }
    return str;
}

From source file:fr.olympicinsa.riocognized.facedetector.tools.ImageConvertor.java

/**
 * Converts/writes a Mat into a BufferedImage.
 *
 * @param matrix Mat of type CV_8UC3 or CV_8UC1
 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
 *///  w  w  w  .j  a  v  a2s .  c o m
public static BufferedImage matToBufferedImage(Mat matrix) {
    log.debug("****** MatToBuffered Image **********");
    log.debug("input : " + matrix.toString());
    int cols = matrix.cols();
    int rows = matrix.rows();
    int elemSize = (int) matrix.elemSize();
    byte[] data = new byte[cols * rows * elemSize];
    int type;

    matrix.get(0, 0, data);

    switch (matrix.channels()) {
    case 1:
        type = BufferedImage.TYPE_BYTE_GRAY;
        break;

    case 3:
        type = BufferedImage.TYPE_3BYTE_BGR;

        // bgr to rgb
        byte b;
        for (int i = 0; i < data.length; i = i + 3) {
            b = data[i];
            data[i] = data[i + 2];
            data[i + 2] = b;
        }
        break;

    default:
        return null;
    }

    BufferedImage image = new BufferedImage(cols, rows, type);
    image.getRaster().setDataElements(0, 0, cols, rows, data);
    log.debug("type: " + type);
    log.debug("output:" + image.toString());
    log.debug("***********************************");
    return image;
}

From source file:gov.nasa.jpl.memex.pooledtimeseries.PoT.java

License:Apache License

static ArrayList<double[][]> computeGradients(Mat frame, int dim) {
    byte frame_array[] = new byte[(int) frame.total()];
    frame.get(0, 0, frame_array);// ww  w.  j  a va 2  s. c o m

    ArrayList<double[][]> gradients = new ArrayList<double[][]>();

    for (int k = 0; k < dim; k++) {
        double angle = Math.PI * (double) k / (double) dim;

        double dx = Math.cos(angle) * 0.9999999;
        double dy = Math.sin(angle) * 0.9999999;

        double[][] grad = new double[frame.width()][frame.height()];

        for (int i = 0; i < frame.cols(); i++) {
            for (int j = 0; j < frame.rows(); j++) {
                if (i <= 1 || j <= 1 || i >= frame.cols() - 2 || j >= frame.rows() - 2) {
                    grad[i][j] = 0;
                } else {
                    double f1 = interpolatePixel(frame_array, frame.cols(), (double) i + dx, (double) j + dy);
                    double f2 = interpolatePixel(frame_array, frame.cols(), (double) i - dx, (double) j - dy);

                    double diff = f1 - f2;
                    if (diff < 0)
                        diff = diff * -1;
                    if (diff >= 256)
                        diff = 255;

                    grad[i][j] = diff;
                }
            }
        }

        gradients.add(grad);
    }

    return gradients;
}

From source file:houghtransform.transform_process.MyTransform.java

@Override
public void houghTransform(Mat edges) {
    Mat _edges = edges.clone();

    double radian = Math.PI / 180;
    int degrees = (int) Math.floor(theta * 180 / Math.PI + 0.5);

    int w = _edges.cols();
    int h = _edges.rows();

    _edges.convertTo(_edges, CvType.CV_64FC3);
    int size = w * h;
    double[] img_data = new double[size];
    _edges.get(0, 0, img_data); // Gets all pixels

    _img_w = w; //Number of columns
    _img_h = h; //Number of lines

    //Create the accumulator
    double hough_h = ((Math.sqrt(2.0) * (double) (h > w ? h : w)) / 2.0);
    _accu_h = (int) (hough_h * 2.0); // -r -> +r
    _accu_w = 180;//  w w  w  . j a v  a  2  s .  com

    _accu = new int[_accu_h * _accu_w];
    for (int i = 0; i < _accu_h * _accu_w; i++) {
        _accu[i] = 0;
    }

    double center_x = w / 2;
    double center_y = h / 2;

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            if (img_data[(y * w) + x] > 250) {
                for (int t = 0; t < 180; t = t + degrees) {
                    // y = x * cos( theta ) + y * sin( theta )
                    double r = (((double) x - center_x) * Math.cos((double) t * radian))
                            + (((double) y - center_y) * Math.sin((double) t * radian));
                    _accu[(int) ((Math.floor(r + hough_h) * 180.0)) + t]++;
                }
            }
        }
    }

    ArrayList<Point> lines = new ArrayList<>();

    if (_accu.length == 0)
        try {
            throw new IOException("MyTransform: _accu == 0");
        } catch (IOException ex) {
            System.out.println(ex);
        }

    for (int r = 0; r < _accu_h; r++) {
        for (int t = 0; t < _accu_w; t++) {
            // Searching in the accumulator a value greater
            //or equal to the set threshold
            if (((int) _accu[(r * _accu_w) + t]) >= threshold) {
                // Is this point a local maxima (9x9)
                int max = _accu[(r * _accu_w) + t];
                ////////////////////////////////
                for (int ly = -4; ly <= 4; ly++) {
                    for (int lx = -4; lx <= 4; lx++) {
                        if (((ly + r) >= 0 && (ly + r) < _accu_h) && ((lx + t) >= 0 && (lx + t) < _accu_w)) {
                            if ((int) _accu[((r + ly) * _accu_w) + (t + lx)] > max) {
                                max = _accu[((r + ly) * _accu_w) + (t + lx)];
                                ly = lx = 5;
                            }
                        }
                    }
                }
                /////////////////////////////////
                if (max > (int) _accu[(r * _accu_w) + t])
                    continue;

                Point point = new Point();
                point.x = r;
                point.y = t * radian;
                lines.add(point);
            }
        }
    }
    _lines = lines;
}

From source file:i2r.snap2inspect.SamplePresentation.java

License:Apache License

public void setImageDynamic(Mat m) {
    // convert to bitmap:
    Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(m, bm);//  ww w  .  j av a2s  . c o  m
    mImageView.setImageBitmap(bm);
}

From source file:imagegame.Camera.java

public static BufferedImage mat2BufferedImage(Mat mat) {
    //        MatOfByte buffer = new MatOfByte();
    //        Imgcodecs.imencode(".png", mat, buffer);
    int type = mat.channels() > 1 ? BufferedImage.TYPE_3BYTE_BGR : BufferedImage.TYPE_BYTE_GRAY;
    BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
    mat.get(0, 0, ((DataBufferByte) image.getRaster().getDataBuffer()).getData());
    return image;
    //return new Image(new ByteArrayInputStream(buffer.toArray()));
}

From source file:imageprocess.HistogramProcessor.java

public static Mat applyLookUp(Mat image, Mat lookup) {
    // Set output image (always 1-channel)
    Mat result = new Mat(image.rows(), image.cols(), CV_8U);

    //        for (int i = 0; i < image.cols(); i++) {
    //            for (int j = 0; j < image.rows(); j++) {
    //                double[] data = image.get(j, i);
    //                double newIntensity = lookup.get((int)data[0], 0)[0];
    //                result.put(j, i, newIntensity);
    //            }
    //        }//  w ww . j ava 2 s  .c  o  m
    Core.LUT(image, lookup, result);
    return result;
}

From source file:imageprocess.ObjectFinder.java

public Mat find(final Mat image, MatOfInt channels, MatOfFloat ranges) {

    Mat result = new Mat();

    if (isIsSparse()) { // call the right function based on histogram type

        Imgproc.calcBackProject(Arrays.asList(image), channels, // vector specifying what histogram dimensions belong to what image channels
                ROIHistogram, // the histogram we are using
                result, // the resulting back projection image
                ranges, // the range of values, for each dimension
                255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
        );/*from  www . j  av a  2s. co m*/

    } else {
        Imgproc.calcBackProject(Arrays.asList(image), channels, // vector specifying what histogram dimensions belong to what image channels
                ROIHistogram, // the histogram we are using
                result, // the resulting back projection image
                ranges, // the range of values, for each dimension
                255.0 // the scaling factor is chosen such that a histogram value of 1 maps to 255
        );
    }

    // Threshold back projection to obtain a binary image
    Mat thresholded = new Mat(result.rows(), result.cols(), result.type());
    if (getThreshold() > 0.0) {
        Imgproc.threshold(result, thresholded, 255 * getThreshold(), 255, THRESH_BINARY);
    }

    return thresholded;
}

From source file:imageprocess.PixelProcessor.java

public void salt(Mat image, int n) {
    for (int k = 0; k < n; k++) {
        int i = (int) (Math.random() * image.cols());
        int j = (int) (Math.random() * image.rows());
        if (image.channels() == 1) {
            image.put(j, i, 255);//from   w w w. j  a v a 2 s .c  o m
        } else if (image.channels() == 3) {
            image.put(j, i, new byte[] { (byte) 255, (byte) 255, (byte) 255 });
        }
    }
}

From source file:interactivespaces.service.image.vision.opencv.MatUtils.java

License:Apache License

/**
 * Converts a {@link Mat} into a {@link BufferedImage}.
 *
 * @param matrix/*from w  ww  .  j  a  v  a2 s .c o m*/
 *          Mat of type CV_8UC3 or CV_8UC1
 *
 * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY
 *
 * @throws SimpleInteractiveSpacesException
 *           the OpenCV Mat type is not supported
 */
public static BufferedImage matToBufferedImage(Mat matrix) throws SimpleInteractiveSpacesException {
    int cols = matrix.cols();
    int rows = matrix.rows();
    int elemSize = (int) matrix.elemSize();
    byte[] data = new byte[cols * rows * elemSize];
    int type;
    matrix.get(0, 0, data);
    switch (matrix.channels()) {
    case 1:
        type = BufferedImage.TYPE_BYTE_GRAY;
        break;
    case 3:
        type = BufferedImage.TYPE_3BYTE_BGR;
        for (int i = 0; i < data.length; i = i + 3) {
            byte b = data[i];
            data[i] = data[i + 2];
            data[i + 2] = b;
        }
        break;
    default:
        throw new SimpleInteractiveSpacesException("The OpenCV Mat type is not supported");
    }

    BufferedImage image = new BufferedImage(cols, rows, type);
    image.getRaster().setDataElements(0, 0, cols, rows, data);

    return image;
}