Example usage for org.opencv.core Mat put

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

Introduction

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

Prototype

public int put(int row, int col, byte[] data) 

Source Link

Usage

From source file:net.semanticmetadata.lire.imageanalysis.features.local.opencvfeatures.CvSurfExtractor.java

License:Open Source License

public LinkedList<CvSurfFeature> computeSurfKeypoints(BufferedImage img) {
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    List<KeyPoint> myKeys;//from   w  w w .  j  ava  2  s . c o m
    //        Mat img_object = Highgui.imread(image, 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
    //        detector.detect(img_object, keypoints);
    byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
    Mat matRGB = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC3);
    matRGB.put(0, 0, data);
    Mat matGray = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
    Imgproc.cvtColor(matRGB, matGray, Imgproc.COLOR_BGR2GRAY); //TODO: RGB or BGR?
    byte[] dataGray = new byte[matGray.rows() * matGray.cols() * (int) (matGray.elemSize())];
    matGray.get(0, 0, dataGray);

    detector.detect(matGray, keypoints);
    myKeys = keypoints.toList();

    LinkedList<CvSurfFeature> myKeypoints = new LinkedList<CvSurfFeature>();
    KeyPoint key;
    CvSurfFeature feat;
    for (Iterator<KeyPoint> iterator = myKeys.iterator(); iterator.hasNext();) {
        key = iterator.next();
        feat = new CvSurfFeature(key.pt.x, key.pt.y, key.size, null);
        myKeypoints.add(feat);
    }

    return myKeypoints;
}

From source file:news_analysis.NewsAnalysis.java

public static void main(String[] args) throws IOException {
    file = new File("F:\\AbcFile\\filename.txt");
    if (!file.exists()) {
        file.createNewFile();// w ww.  j a  v  a 2  s .  c  o m
    }
    fw = new FileWriter(file.getAbsoluteFile());
    bw = new BufferedWriter(fw);
    bw.flush();
    // Load an image file and display it in a window.
    Mat m1 = Highgui.imread("E:\\Raw Java Project\\Thesis\\test6.jpg");
    //imshow("Original", m1);

    // Do some image processing on the image and display in another window.
    Mat m2 = new Mat();
    Imgproc.bilateralFilter(m1, m2, -1, 50, 10);
    Imgproc.Canny(m2, m2, 10, 200);
    imshow("Edge Detected", m2);
    Size sizeA = m2.size();
    System.out.println("width: " + sizeA.width + " Height: " + sizeA.height);
    int width = (int) sizeA.width;
    int hight = (int) sizeA.height;
    int pointLength[][][] = new int[hight][width][2];
    for (int i = 0; i < hight; i++) {
        for (int j = 0; j < width; j++) {
            double[] data = m2.get(i, j);
            if (m2.get(i, j)[0] != 0) {
                pointLength[i][j][0] = 0;
                pointLength[i][j][1] = 0;
                continue;
            }
            if (j != 0 && m2.get(i, j - 1)[0] == 0) {
                pointLength[i][j][0] = pointLength[i][j - 1][0];
            } else {
                int count = 0;
                for (int k = j + 1; k < width; k++) {
                    if (m2.get(i, k)[0] == 0) {
                        count++;
                    } else {
                        break;
                    }
                }
                pointLength[i][j][0] = count;
            }
            if (i != 0 && m2.get(i - 1, j)[0] == 0) {
                pointLength[i][j][1] = pointLength[i - 1][j][1];
            } else {
                int count = 0;
                for (int k = i + 1; k < hight; k++) {
                    if (m2.get(k, j)[0] == 0) {
                        count++;
                    } else {
                        break;
                    }
                }
                pointLength[i][j][1] = count;
            }

            //System.out.println(data[0]);
        }
    }
    String temp = "";
    Mat convertArea = m2.clone();

    int[][] balckWhite = new int[hight][width];

    for (int i = 0; i < hight; i++) {
        temp = "";
        for (int j = 0; j < width; j++) {
            if (i == 0 || j == 0 || i == hight - 1 || j == width - 1) {
                temp = temp + "@";
                balckWhite[i][j] = 1;

                double[] data = m2.get(i, j);
                data[0] = 255.0;
                convertArea.put(i, j, data);
            } else if (pointLength[i][j][0] > 150 && pointLength[i][j][1] > 6) {
                temp = temp + "@";
                balckWhite[i][j] = 1;

                double[] data = m2.get(i, j);
                data[0] = 255.0;
                convertArea.put(i, j, data);
            } else if (pointLength[i][j][0] > 7 && pointLength[i][j][1] > 200) {
                temp = temp + "@";
                balckWhite[i][j] = 1;

                double[] data = m2.get(i, j);
                data[0] = 255.0;
                convertArea.put(i, j, data);
            } else {
                temp = temp + " ";
                balckWhite[i][j] = 0;

                double[] data = m2.get(i, j);
                data[0] = 0.0;
                convertArea.put(i, j, data);
            }

        }
        //filewrile(temp);
    }
    imshow("Convertion", convertArea);
    IsImage isImage = new IsImage();
    HeadLineDetection isHeadline = new HeadLineDetection();

    ImageBorderDetectionBFS imgBFS = new ImageBorderDetectionBFS();
    ArrayList<BorderItem> borderItems = imgBFS.getBorder(balckWhite, width, hight);
    Mat[] subMat = new Mat[borderItems.size()];
    for (int i = 0; i < borderItems.size(); i++) {
        subMat[i] = m2.submat(borderItems.get(i).getMinX(), borderItems.get(i).getMaxX(),
                borderItems.get(i).getMinY(), borderItems.get(i).getMaxY());
        if (isImage.isImage(subMat[i])) {
            System.out.println("subMat" + i + " is an image");
            //imshow("subMat" + i, subMat[i]);

        } else if (isHeadline.isHeadLine(subMat[i])) {
            System.out.println("subMat" + i + " is an Headline");
            //imshow("Headline" + i, subMat[i]);
        } else {
            System.out.println("subMat" + i + " is an Column");
            imshow("Column" + i, subMat[i]);
        }
        //imshow("subMat" + i, subMat[i]);
        bw.close();

    }

}

From source file:opencltest.YetAnotherTestT.java

public static void main(String[] args) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat kernel = new Mat(3, 3, CV_8UC1);
    kernel.put(0, 0, new double[] { 0, 1, 0, 1, 1, 1, 0, 1, 0 });

    Mat source = Imgcodecs.imread("test.smaller.png");
    Mat blur = new Mat();
    Mat edges = new Mat();
    Mat dilated = new Mat();

    Imgproc.GaussianBlur(source, blur, new Size(13, 13), 0);
    Imgproc.Canny(blur, edges, 10, 30, 5, false);
    //        Imgproc.cvtColor(edges, edges, Imgproc.COLOR_RGB2GRAY);
    //        Imgproc.adaptiveThreshold(edges, edges, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 5, 2);
    //        Core.bitwise_not(edges, edges);
    //        Imgproc.dilate(edges, edges, kernel);
    //        Imgproc.dilate(edges, dilated, kernel);
    dilated = edges;/*from  ww w .  j  a va  2 s.c o  m*/
    //        Core.bitwise_not(edges, edges);

    Mat lines = new Mat();
    Imgproc.HoughLinesP(dilated, lines, 1, Math.PI / 180, 300, 10, 70);

    Mat empty = new Mat(source.height(), source.width(), source.type());
    //        paintLines(empty, lines);

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hier = new Mat();
    Imgproc.findContours(edges, contours, hier, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    Mat foundSquare = new Mat(source.height(), source.width(), CvType.CV_8UC4);
    source.copyTo(foundSquare);

    List<Double> hor = new ArrayList<>();
    for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) {
        MatOfPoint next = iterator.next();
        Rect bounding = Imgproc.boundingRect(next);
        int tr = 20;
        if (diffLessThan(bounding.size().width - 40, tr) && diffLessThan(bounding.size().height - 40, tr)) {
            Imgproc.rectangle(empty, bounding.tl(), bounding.br(), randomColor(), 3);
            //                hor.add(bounding.x + 0.0);
            hor.add(bounding.x + bounding.width / 2.0 + 0.0);
            drawRect(bounding, foundSquare);
        }
    }

    Imgcodecs.imwrite("test_2.png", source);
    Imgcodecs.imwrite("test_3.png", dilated);
    Imgcodecs.imwrite("test_4.png", empty);
    Imgcodecs.imwrite("test_h.png", foundSquare);

    hor.sort(Double::compare);
    double low = hor.get(0);
    double hih = hor.get(hor.size() - 1);
    double n = hor.size();
    Function<Double, Double> K = (d) -> (Math.abs(d) <= 1) ? ((3.0 / 4.0) * (1 - (d * d))) : 0;//epanechnikov kernel
    List<Double> result = new ArrayList<>();
    double h = 10;
    for (int i = 0; i < source.width() + 1; i++)
        result.add(0.0);
    for (double d = low; d <= hih; d += 1) {
        double sum = 0;
        for (Double di : hor) {
            sum += K.apply((d - di) / h);
        }
        result.set((int) d, sum / (n * h));
        System.out.println(sum / (n * h));
    }
    normalize(result, 255);
    Mat test = new Mat(source.height(), source.width(), source.type());
    source.copyTo(test);
    draw(result, test);
    Imgcodecs.imwrite("test_uwot.png", test);
}

From source file:opencv.CaptchaDetection.java

private static Mat thres_rgb(Mat src) {
    Mat gray = Mat.zeros(src.size(), CvType.CV_8UC1);

    //  , ?//from w  w  w.ja  v a 2  s  . c  om
    int thres = 150;
    double gamma = 2.5;

    for (int row = 0; row < src.rows(); row++) {
        for (int col = 0; col < src.cols(); col++) {
            double[] s_data = src.get(row, col);

            byte[] s_buff = new byte[3];
            byte[] g_buff = new byte[1];

            double color_sum = s_data[0] + s_data[1] + s_data[2];

            if (color_sum / 3 > thres) {
                for (int channel = 0; channel < 3; channel++)
                    s_buff[channel] = (byte) 255;

                g_buff[0] = 0;
            } else {
                //   gamma 
                for (int channel = 0; channel < 3; channel++) {
                    double tmp = s_data[channel];
                    tmp = Math.pow(tmp / 255, gamma) * 255;

                    if (tmp < 0)
                        s_buff[channel] = 0;
                    else if (tmp > 255)
                        s_buff[channel] = (byte) 255;
                    else
                        s_buff[channel] = (byte) tmp;
                }

                g_buff[0] = (byte) 255;
            }
            src.put(row, col, s_buff);
            gray.put(row, col, g_buff);
        }
    }
    return gray;
}

From source file:opencv.CaptchaDetection.java

private static Mat k_means_spilter(Mat src) {
    Mat dst = Mat.zeros(src.size(), CvType.CV_8UC1);

    int width = src.cols();
    int height = src.rows();
    int dims = src.channels();

    //   /*from   w  w w .  j ava  2  s. c  om*/
    int clusterCount = 3;

    Mat points = new Mat(width * height, dims, CvType.CV_32F, new Scalar(0));
    Mat centers = new Mat(clusterCount, dims, CvType.CV_32F);
    Mat labels = new Mat(width * height, 1, CvType.CV_32S);

    //    points
    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            int index = row * width + col;
            double[] s_data = src.get(row, col);

            for (int channel = 0; channel < 3; channel++) {
                float[] f_buff = new float[1];
                f_buff[0] = (float) s_data[channel];

                points.put(index, channel, f_buff);
            }
        }
    }

    //  knn ?
    TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER, 10, 0.1);
    Core.kmeans(points, clusterCount, labels, criteria, 3, Core.KMEANS_PP_CENTERS, centers);

    //  ??? label index
    Map<Integer, Integer> tmp = new TreeMap<>();
    for (int i = 0; i < clusterCount; i++) {
        int sum = 0;
        for (int j = 0; j < dims; j++) {
            sum += centers.get(i, j)[0];
        }
        while (tmp.containsKey(sum))
            sum++;
        tmp.put(sum, i);
    }

    int count = 0;
    int[] label_order = new int[clusterCount];
    for (Map.Entry<Integer, Integer> iter : tmp.entrySet()) {
        label_order[count++] = iter.getValue();
    }

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            int index = row * width + col;
            int label = (int) labels.get(index, 0)[0];

            if (label == label_order[1]) {
                byte[] d_buff = new byte[1];
                d_buff[0] = (byte) 255;
                dst.put(row, col, d_buff);
            }
        }
    }

    return dst;
}

From source file:opencv.CaptchaDetection.java

private static Mat check_is_line(Mat src) {
    Mat dst = Mat.zeros(src.size(), CvType.CV_8UC1);

    int min_length = 3;

    //  ?//from   w  w  w.j a  v  a  2  s  .c  o  m
    for (int row = 0; row < src.rows(); row++) {
        for (int col = 0; col < src.cols(); col++) {
            if (src.get(row, col)[0] == 0)
                continue;

            //  ??
            boolean left_black = false, right_black = false;

            if (col == 0 || src.get(row, col - 1)[0] == 0)
                left_black = true;

            if (col == src.cols() - 1 || src.get(row, col + 1)[0] == 0)
                right_black = true;

            if (!left_black || !right_black)
                continue;

            //   
            int length = col_length(src, row, col);
            if (length > min_length) {
                byte[] d_buff = new byte[1];
                d_buff[0] = (byte) 255;
                dst.put(row, col, d_buff);
            }

        }
    }

    //  ?
    for (int row = 0; row < src.rows(); row++) {
        for (int col = 0; col < src.cols(); col++) {
            if (src.get(row, col)[0] == 0)
                continue;

            //  ?
            boolean up_black = false, down_black = false;

            if (row == 0 || src.get(row - 1, col)[0] == 0)
                up_black = true;

            if (row == src.rows() - 1 || src.get(row + 1, col)[0] == 0)
                down_black = true;

            if (!up_black || !down_black)
                continue;

            //  
            int length = row_length(src, row, col);
            if (length > min_length) {
                byte[] d_buff = new byte[1];
                d_buff[0] = (byte) 255;
                dst.put(row, col, d_buff);
            }
        }
    }

    return dst;
}

From source file:opencv.CaptchaDetection.java

/***
 *  src  dst_0  dst_1//from  w  w w.  j a va  2s  .com
 * @param src
 * @param dst_0
 * @param dst_1 
 */
private static void delete_target(Mat src, Mat dst_0, Mat dst_1) {
    for (int row = 0; row < src.rows(); row++) {
        for (int col = 0; col < src.cols(); col++) {
            if (dst_0.get(row, col)[0] == 255 && dst_1.get(row, col)[0] == 255) {
                byte[] d_buff = new byte[1];
                d_buff[0] = (byte) 0;
                src.put(row, col, d_buff);
            }
        }
    }
}

From source file:opencv.CaptchaDetection.java

/***
 * //  w w w.ja va  2  s  . c  om
 * @param src 
 */
private static void delete_point(Mat src) {
    for (int row = 0; row < src.rows(); row++) {
        for (int col = 0; col < src.cols(); col++) {
            if (src.get(row, col)[0] == 255) {
                int count = 0;

                if (row == 0 || src.get(row - 1, col)[0] == 0)
                    count++;
                if (row == src.rows() - 1 || src.get(row + 1, col)[0] == 0)
                    count++;
                if (col == 0 || src.get(row, col - 1)[0] == 0)
                    count++;
                if (col == src.cols() - 1 || src.get(row, col + 1)[0] == 0)
                    count++;

                if (count == 4) {
                    byte[] d_buff = new byte[1];
                    d_buff[0] = (byte) 0;
                    src.put(row, col, d_buff);
                }

            }
        }
    }
}

From source file:opencv.fark.ResimSecMainFrame.java

private void jButtonKarsilastirV1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonKarsilastirV1ActionPerformed

    Mat resim1hsv = new Mat();
    Mat resim2hsv = new Mat();
    Imgproc.cvtColor(resim1, resim1hsv, Imgproc.COLOR_BGR2HSV);
    Imgproc.cvtColor(resim2, resim2hsv, Imgproc.COLOR_BGR2HSV);

    Mat image = resim1.clone();

    double data2[] = new double[3];
    for (int i = 0; i < image.rows(); i++) {
        for (int j = 0; j < image.cols(); j++) {
            double[] data = resim1.get(i, j);
            double[] data1 = resim2.get(i, j);

            data2[0] = Math.abs(data[0] - data1[0]);
            data2[1] = Math.abs(data[1] - data1[1]);
            data2[2] = Math.abs(data[2] - data1[2]);
            image.put(i, j, data2);

        }/* www  .  ja  v a 2 s.  c  om*/

    }

    Core.inRange(image, new Scalar(1, 0, 0), new Scalar(255, 255, 255), image);
    //Imgproc.threshold(image, image, 0, 256, Imgproc.THRESH_BINARY);

    MatToBufImg buf = new MatToBufImg();
    buf.setMatrix(image, ".png");
    g.drawImage(buf.getBufferedImage(), 0, 0, null);

}

From source file:opencv_ext.TGG_OpenCV_Util.java

private static Mat bufferedImage2Mat(BufferedImage bufferedImage) {
    byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
    Mat im = null;
    if (bufferedImage.getType() == BufferedImage.TYPE_INT_ARGB) {
        //convert argb to rgba
        for (int i = 0; i < pixels.length / 4; i++) {
            byte temp = pixels[i * 4];
            pixels[i * 4] = pixels[i * 4 + 3];
            pixels[i * 4 + 3] = temp;//from   www.j av  a 2s  .  co  m
        }
        im = new Mat(bufferedImage.getWidth(), bufferedImage.getHeight(), CvType.CV_8UC4);
    } else if (bufferedImage.getType() == BufferedImage.TYPE_BYTE_GRAY) {
        im = new Mat(bufferedImage.getWidth(), bufferedImage.getHeight(), CvType.CV_8UC1);
    }
    im.put(0, 0, pixels);
    return im;
}