Example usage for org.opencv.core Mat get

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

Introduction

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

Prototype

public double[] get(int row, int col) 

Source Link

Usage

From source file:news_analysis.headlinedetection.HeadLineDetection.java

private boolean horizontalChecked(Mat image, int width, int height) {
    int countHisto = 0;
    int countLine = 0;
    int maxTAG = -1;
    int minTAG = 100000;

    int freq[] = new int[height];
    for (int i = 0; i < height; i++) {
        countHisto = 0;/*from  w w w. ja va2  s .c  o m*/
        for (int j = 0; j < width; j++) {
            if (image.get(i, j)[0] == 255) {
                countHisto++;
                //System.out.print("*");
            }

        }
        freq[i] = countHisto;
        if (freq[i] > maxTAG)
            maxTAG = freq[i];
        if (freq[i] < minTAG)
            minTAG = freq[i];
        if (freq[i] == 0)
            countLine++;
        //System.out.println("Line: "+countLine);
    }

    boolean any = true;
    for (int i = 0; i < height * .3; i++) {
        if (freq[i] == maxTAG) {
            for (int j = freq[height - 1]; j > height * .9; j--) {
                if (freq[i] <= minTAG + 5) {

                } else {
                    any = false;
                }
            }
        }
    }

    if (countLine < 4)
        return any;

    return false;

}

From source file:news_analysis.headlinedetection.HeadLineDetection.java

private int VerticleChecked(Mat image, int width, int height) {

    int CHEACKED_LINE = (height / 2) % 6;
    int countHisto = 0;
    int maxTAG = -1;
    int start_H_HV = 0;
    int End_H_HV = 0;

    for (int i = 0; i < width; i++) {
        if (i > CHEACKED_LINE && i < width - CHEACKED_LINE) {
            maxTAG = -1;/*from ww  w  . j a  va2  s.  co  m*/
            continue;
        }
        countHisto = 0;
        for (int j = 0; j < height; j++) {
            if (image.get(j, i)[0] == 255) {
                countHisto++;
                //System.out.print("*");
            }
        }
        if (countHisto > maxTAG)
            maxTAG = countHisto;
        if (i == CHEACKED_LINE - 1)
            start_H_HV = maxTAG;
        else if (i > CHEACKED_LINE)
            End_H_HV = maxTAG;
        // System.out.println("");
    }

    //System.out.println("start_H_HV : "+start_H_HV+" End_H_HV : "+End_H_HV);

    return 0;

}

From source file:news_analysis.isimage.IsImage.java

private int[] horizontalChecked(Mat image, int width, int height) {
    int CHEACKED_LINE = (width / 2) % 6;
    int countHisto = 0;
    int maxTAG = -1;
    int start_H_HV = 0;
    int End_H_HV = 0;

    for (int i = 0; i < height; i++) {
        if (i > CHEACKED_LINE && i < height - CHEACKED_LINE) {
            maxTAG = -1;//from   w w w  .  j  a v a 2  s  .  c o m
            continue;
        }
        countHisto = 0;
        for (int j = 0; j < width; j++) {
            if (image.get(i, j)[0] == 255) {
                countHisto++;
                // System.out.print("*");
            }
        }
        if (countHisto > maxTAG)
            maxTAG = countHisto;
        if (i == CHEACKED_LINE - 1)
            start_H_HV = maxTAG;
        else if (i > CHEACKED_LINE)
            End_H_HV = maxTAG;
        //System.out.println("");
    }

    int res[] = new int[2];
    res[0] = compareWithWidth(width, start_H_HV);
    res[1] = compareWithWidth(width, End_H_HV);

    //System.out.println("start_H_HV : "+start_H_HV+" End_H_HV : "+End_H_HV);

    return res;

}

From source file:news_analysis.isimage.IsImage.java

private int[] VerticleChecked(Mat image, int width, int height) {

    int CHEACKED_LINE = (height / 2) % 6;
    int countHisto = 0;
    int maxTAG = -1;
    int start_H_HV = 0;
    int End_H_HV = 0;

    for (int i = 0; i < width; i++) {
        if (i > CHEACKED_LINE && i < width - CHEACKED_LINE) {
            maxTAG = -1;/*from www  .  j  a  v a2  s.c  o m*/
            continue;
        }
        countHisto = 0;
        for (int j = 0; j < height; j++) {
            if (image.get(j, i)[0] == 255) {
                countHisto++;
                //System.out.print("*");
            }
        }
        if (countHisto > maxTAG)
            maxTAG = countHisto;
        if (i == CHEACKED_LINE - 1)
            start_H_HV = maxTAG;
        else if (i > CHEACKED_LINE)
            End_H_HV = maxTAG;
        // System.out.println("");
    }
    int res[] = new int[2];
    res[0] = compareWithWidth(height, start_H_HV);
    res[1] = compareWithWidth(height, End_H_HV);

    //System.out.println("start_H_HV : "+start_H_HV+" End_H_HV : "+End_H_HV);

    return res;

}

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();/*from   www .  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:nz.ac.auckland.lablet.vision.CamShiftTracker.java

License:Open Source License

/**
 * Finds the dominant colour in an image, and returns two values in HSV colour space to represent similar colours,
 * e.g. so you can keep all colours similar to the dominant colour.
 *
 * How the algorithm works://from  ww w.  j  a  va 2  s.c o m
 *
 * 1. Scale the frame down so that algorithm doesn't take too long.
 * 2. Segment the frame into different colours (number of colours determined by k)
 * 3. Find dominant cluster (largest area) and get its central colour point.
 * 4. Get range (min max) to represent similar colours.
 *
 * @param bgr The input frame, in BGR colour space.
 * @param k The number of segments to use (2 works well).
 * @return The min and max HSV colour values, which represent the colours similar to the dominant colour.
 */
private Pair<Scalar, Scalar> getMinMaxHsv(Mat bgr, int k) {
    //Convert to HSV
    Mat input = new Mat();
    Imgproc.cvtColor(bgr, input, Imgproc.COLOR_BGR2BGRA, 3);

    //Scale image
    Size bgrSize = bgr.size();
    Size newSize = new Size();

    if (bgrSize.width > CamShiftTracker.KMEANS_IMG_SIZE || bgrSize.height > CamShiftTracker.KMEANS_IMG_SIZE) {

        if (bgrSize.width > bgrSize.height) {
            newSize.width = CamShiftTracker.KMEANS_IMG_SIZE;
            newSize.height = CamShiftTracker.KMEANS_IMG_SIZE / bgrSize.width * bgrSize.height;
        } else {
            newSize.width = CamShiftTracker.KMEANS_IMG_SIZE / bgrSize.height * bgrSize.width;
            newSize.height = CamShiftTracker.KMEANS_IMG_SIZE;
        }

        Imgproc.resize(input, input, newSize);
    }

    //Image quantization using k-means, see here for details of k-means algorithm: http://bit.ly/1JIvrlB
    Mat clusterData = new Mat();

    Mat reshaped = input.reshape(1, input.rows() * input.cols());
    reshaped.convertTo(clusterData, CvType.CV_32F, 1.0 / 255.0);
    Mat labels = new Mat();
    Mat centres = new Mat();
    TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 50, 1);
    Core.kmeans(clusterData, k, labels, criteria, 1, Core.KMEANS_PP_CENTERS, centres);

    //Get num hits for each category
    int[] counts = new int[k];

    for (int i = 0; i < labels.rows(); i++) {
        int label = (int) labels.get(i, 0)[0];
        counts[label] += 1;
    }

    //Get cluster index with maximum number of members
    int maxCluster = 0;
    int index = -1;

    for (int i = 0; i < counts.length; i++) {
        int value = counts[i];

        if (value > maxCluster) {
            maxCluster = value;
            index = i;
        }
    }

    //Get cluster centre point hsv
    int r = (int) (centres.get(index, 2)[0] * 255.0);
    int g = (int) (centres.get(index, 1)[0] * 255.0);
    int b = (int) (centres.get(index, 0)[0] * 255.0);
    int sum = (r + g + b) / 3;

    //Get colour range
    Scalar min;
    Scalar max;

    int rg = Math.abs(r - g);
    int gb = Math.abs(g - b);
    int rb = Math.abs(r - b);
    int maxDiff = Math.max(Math.max(rg, gb), rb);

    if (maxDiff < 35 && sum > 120) { //white
        min = new Scalar(0, 0, 0);
        max = new Scalar(180, 40, 255);
    } else if (sum < 50 && maxDiff < 35) { //black
        min = new Scalar(0, 0, 0);
        max = new Scalar(180, 255, 40);
    } else {
        Mat bgrColour = new Mat(1, 1, CvType.CV_8UC3, new Scalar(r, g, b));
        Mat hsvColour = new Mat();

        Imgproc.cvtColor(bgrColour, hsvColour, Imgproc.COLOR_BGR2HSV, 3);
        double[] hsv = hsvColour.get(0, 0);

        int addition = 0;
        int minHue = (int) hsv[0] - colourRange;
        if (minHue < 0) {
            addition = Math.abs(minHue);
        }

        int maxHue = (int) hsv[0] + colourRange;

        min = new Scalar(Math.max(minHue, 0), 60, Math.max(35, hsv[2] - 30));
        max = new Scalar(Math.min(maxHue + addition, 180), 255, 255);
    }

    return new Pair<>(min, max);
}

From source file:opencltest.YetAnotherTestT.java

private static void paintLines(Mat targetImg, Mat lines) {
    for (int x = 0; x < lines.rows(); x++) {
        double[] vec = lines.get(x, 0);
        double x1 = vec[0], y1 = vec[1], x2 = vec[2], y2 = vec[3];
        Point start = new Point(x1, y1);
        Point end = new Point(x2, y2);
        Imgproc.line(targetImg, start, end, new Scalar(255, 0, 0), 1);
    }//from   w w w  . j a  va  2 s . c o  m
}

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 .j a  v a 2 s . c  o m
    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();

    //   /* w  w  w  . j av a  2s  .  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   www  .ja  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;
}