Java Code Examples for org.opencv.core.Mat#get()

The following examples show how to use org.opencv.core.Mat#get() . 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 6 votes vote down vote up
public static Mat LocalContrast(Mat img) {
	double[] h = { 1.0 / 16.0, 4.0 / 16.0, 6.0 / 16.0, 4.0 / 16.0, 1.0 / 16.0 };
	Mat mask = new Mat(h.length, h.length, img.type());
	for (int i = 0; i < h.length; i++) {
		for (int j = 0; j < h.length; j++) {
			mask.put(i, j, h[i] * h[j]);
		}
	}
	Mat localContrast = new Mat();
	Imgproc.filter2D(img, localContrast, img.depth(), mask);
	for (int i = 0; i < localContrast.rows(); i++) {
		for (int j = 0; j < localContrast.cols(); j++) {
			if (localContrast.get(i, j)[0] > Math.PI / 2.75)
				localContrast.put(i, j, Math.PI / 2.75);
		}
	}
	Core.subtract(img, localContrast, localContrast);
	return localContrast.mul(localContrast);
}
 
Example 2
Source File: Converters.java    From sudokufx with Apache License 2.0 6 votes vote down vote up
public static void Mat_to_vector_Mat(Mat m, List<Mat> mats) {
    if (mats == null)
        throw new IllegalArgumentException("mats == null");
    int count = m.rows();
    if (CvType.CV_32SC2 != m.type() || m.cols() != 1)
        throw new IllegalArgumentException(
                "CvType.CV_32SC2 != m.type() ||  m.cols()!=1\n" + m);

    mats.clear();
    int[] buff = new int[count * 2];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        long addr = (((long) buff[i * 2]) << 32) | (((long) buff[i * 2 + 1]) & 0xffffffffL);
        mats.add(new Mat(addr));
    }
}
 
Example 3
Source File: Converters.java    From FaceDetectDemo with Apache License 2.0 6 votes vote down vote up
public static void Mat_to_vector_KeyPoint(Mat m, List<KeyPoint> kps) {
    if (kps == null)
        throw new java.lang.IllegalArgumentException("Output List can't be null");
    int count = m.rows();
    if (CvType.CV_64FC(7) != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_64FC(7) != m.type() ||  m.cols()!=1\n" + m);

    kps.clear();
    double[] buff = new double[7 * count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        kps.add(new KeyPoint((float) buff[7 * i], (float) buff[7 * i + 1], (float) buff[7 * i + 2], (float) buff[7 * i + 3],
                (float) buff[7 * i + 4], (int) buff[7 * i + 5], (int) buff[7 * i + 6]));
    }
}
 
Example 4
Source File: Converters.java    From real_time_circle_detection_android with MIT License 6 votes vote down vote up
public static void Mat_to_vector_Mat(Mat m, List<Mat> mats) {
    if (mats == null)
        throw new java.lang.IllegalArgumentException("mats == null");
    int count = m.rows();
    if (CvType.CV_32SC2 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_32SC2 != m.type() ||  m.cols()!=1\n" + m);

    mats.clear();
    int[] buff = new int[count * 2];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        long addr = (((long) buff[i * 2]) << 32) | (((long) buff[i * 2 + 1]) & 0xffffffffL);
        mats.add(new Mat(addr));
    }
}
 
Example 5
Source File: Converters.java    From PixaToon with GNU General Public License v3.0 5 votes vote down vote up
public static void Mat_to_vector_float(Mat m, List<Float> fs) {
    if (fs == null)
        throw new java.lang.IllegalArgumentException("fs == null");
    int count = m.rows();
    if (CvType.CV_32FC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_32FC1 != m.type() ||  m.cols()!=1\n" + m);

    fs.clear();
    float[] buff = new float[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        fs.add(buff[i]);
    }
}
 
Example 6
Source File: Converters.java    From MOAAP with MIT License 5 votes vote down vote up
public static void Mat_to_vector_float(Mat m, List<Float> fs) {
    if (fs == null)
        throw new java.lang.IllegalArgumentException("fs == null");
    int count = m.rows();
    if (CvType.CV_32FC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_32FC1 != m.type() ||  m.cols()!=1\n" + m);

    fs.clear();
    float[] buff = new float[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        fs.add(buff[i]);
    }
}
 
Example 7
Source File: Converters.java    From SmartPaperScan with Apache License 2.0 5 votes vote down vote up
public static void Mat_to_vector_char(Mat m, List<Byte> bs) {
    if (bs == null)
        throw new java.lang.IllegalArgumentException("Output List can't be null");
    int count = m.rows();
    if (CvType.CV_8SC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_8SC1 != m.type() ||  m.cols()!=1\n" + m);

    bs.clear();
    byte[] buff = new byte[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        bs.add(buff[i]);
    }
}
 
Example 8
Source File: Converters.java    From ResistorScanner with MIT License 5 votes vote down vote up
public static void Mat_to_vector_int(Mat m, List<Integer> is) {
    if (is == null)
        throw new java.lang.IllegalArgumentException("is == null");
    int count = m.rows();
    if (CvType.CV_32SC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_32SC1 != m.type() ||  m.cols()!=1\n" + m);

    is.clear();
    int[] buff = new int[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        is.add(buff[i]);
    }
}
 
Example 9
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 5 votes vote down vote up
public  Mat quantizeImage(Mat image,String destinationFileName)
{
    //Mat image  = testGrabCut(imageFilePath);
    int rows = image.rows();
    int cols = image.cols();
    Mat newImage = new Mat(image.size(),image.type());
    for(int r = 0 ; r < rows ; r++)
    {
        for(int c =0; c< cols; c++)
        {
            double [] pixel_val = image.get(r, c);
            double [] pixel_data = new double[3];
            pixel_data[0] = reduceVal(pixel_val[0]);
            pixel_data[1] = reduceVal(pixel_val[1]);
            pixel_data[2] = reduceVal(pixel_val[2]);
         //   System.out.print("(" +pixel_data[0]+","+pixel_data[1]+","+pixel_data[2]+") *");
            newImage.put(r, c, pixel_data);
        }
      //  System.out.println();
    }
    /*
    MatOfInt params= new MatOfInt();
    int arr[] = new int[2];
    arr[0]= Imgcodecs.CV_IMWRITE_JPEG_QUALITY;
    arr[1]= 100;
    params.fromArray(arr);
    */
    Imgcodecs.imwrite(resultDirectory+destinationFileName, newImage);
    return newImage;
}
 
Example 10
Source File: Converters.java    From FtcSamples with MIT License 5 votes vote down vote up
public static void Mat_to_vector_char(Mat m, List<Byte> bs) {
    if (bs == null)
        throw new java.lang.IllegalArgumentException("Output List can't be null");
    int count = m.rows();
    if (CvType.CV_8SC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_8SC1 != m.type() ||  m.cols()!=1\n" + m);

    bs.clear();
    byte[] buff = new byte[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        bs.add(buff[i]);
    }
}
 
Example 11
Source File: MainActivity.java    From real_time_circle_detection_android with MIT License 5 votes vote down vote up
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat input = inputFrame.gray();
    Mat circles = new Mat();
    Imgproc.blur(input, input, new Size(7, 7), new Point(2, 2));
    Imgproc.HoughCircles(input, circles, Imgproc.CV_HOUGH_GRADIENT, 2, 100, 100, 90, 0, 1000);

    Log.i(TAG, String.valueOf("size: " + circles.cols()) + ", " + String.valueOf(circles.rows()));

    if (circles.cols() > 0) {
        for (int x=0; x < Math.min(circles.cols(), 5); x++ ) {
            double circleVec[] = circles.get(0, x);

            if (circleVec == null) {
                break;
            }

            Point center = new Point((int) circleVec[0], (int) circleVec[1]);
            int radius = (int) circleVec[2];

            Imgproc.circle(input, center, 3, new Scalar(255, 255, 255), 5);
            Imgproc.circle(input, center, radius, new Scalar(255, 255, 255), 2);
        }
    }

    circles.release();
    input.release();
    return inputFrame.rgba();
}
 
Example 12
Source File: Converters.java    From real_time_circle_detection_android with MIT License 5 votes vote down vote up
public static void Mat_to_vector_uchar(Mat m, List<Byte> us) {
    if (us == null)
        throw new java.lang.IllegalArgumentException("Output List can't be null");
    int count = m.rows();
    if (CvType.CV_8UC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_8UC1 != m.type() ||  m.cols()!=1\n" + m);

    us.clear();
    byte[] buff = new byte[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        us.add(buff[i]);
    }
}
 
Example 13
Source File: Converters.java    From SmartPaperScan with Apache License 2.0 5 votes vote down vote up
public static void Mat_to_vector_double(Mat m, List<Double> ds) {
    if (ds == null)
        throw new java.lang.IllegalArgumentException("ds == null");
    int count = m.rows();
    if (CvType.CV_64FC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_64FC1 != m.type() ||  m.cols()!=1\n" + m);

    ds.clear();
    double[] buff = new double[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        ds.add(buff[i]);
    }
}
 
Example 14
Source File: Color.java    From FTCVision with MIT License 5 votes vote down vote up
/**
 * Convert this color to a different colorspace and return a scalar
 *
 * @param to Colorspace to convert to
 * @return Scalar in other colorspace
 */
public Scalar convertColorScalar(ColorSpace to) {
    if (getColorSpace() == to)
        return getScalar();
    if (!getColorSpace().canConvertTo(to))
        throw new IllegalArgumentException("Cannot convert color to the desired color space.");

    Scalar output = this.getScalar();

    try {
        for (int i = 0; i < getColorSpace().getConversionsTo(to).length; i += 3) {
            int conversion = getColorSpace().getConversionsTo(to)[i];
            int inputDim = getColorSpace().getConversionsTo(to)[i + 1];
            int outputDim = getColorSpace().getConversionsTo(to)[i + 2];

            Mat pointMatTo = new Mat();
            Mat pointMatFrom = new Mat(1, 1, CvType.CV_8UC(inputDim), output);
            Imgproc.cvtColor(pointMatFrom, pointMatTo, conversion, outputDim);
            output = new Scalar(pointMatTo.get(0, 0));
            pointMatTo.release();
            pointMatFrom.release();
        }
    } catch (Exception ignored) {
        throw new IllegalArgumentException("Cannot convert color to the desired color space.");
    }

    return output;
}
 
Example 15
Source File: Converters.java    From LicensePlateDiscern with MIT License 5 votes vote down vote up
public static void Mat_to_vector_RotatedRect(Mat m, List<RotatedRect> rs) {
    if (rs == null)
        throw new java.lang.IllegalArgumentException("rs == null");
    int count = m.rows();
    if (CvType.CV_32FC(5) != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_32FC5 != m.type() ||  m.rows()!=1\n" + m);

    rs.clear();
    float[] buff = new float[5 * count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        rs.add(new RotatedRect(new Point(buff[5 * i], buff[5 * i + 1]), new Size(buff[5 * i + 2], buff[5 * i + 3]), buff[5 * i + 4]));
    }
}
 
Example 16
Source File: Converters.java    From SmartPaperScan with Apache License 2.0 5 votes vote down vote up
public static void Mat_to_vector_DMatch(Mat m, List<DMatch> matches) {
    if (matches == null)
        throw new java.lang.IllegalArgumentException("Output List can't be null");
    int count = m.rows();
    if (CvType.CV_64FC4 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_64FC4 != m.type() ||  m.cols()!=1\n" + m);

    matches.clear();
    double[] buff = new double[4 * count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        matches.add(new DMatch((int) buff[4 * i], (int) buff[4 * i + 1], (int) buff[4 * i + 2], (float) buff[4 * i + 3]));
    }
}
 
Example 17
Source File: Converters.java    From Android-Car-duino with GNU General Public License v2.0 5 votes vote down vote up
public static void Mat_to_vector_Rect(Mat m, List<Rect> rs) {
    if (rs == null)
        throw new java.lang.IllegalArgumentException("rs == null");
    int count = m.rows();
    if (CvType.CV_32SC4 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_32SC4 != m.type() ||  m.rows()!=1\n" + m);

    rs.clear();
    int[] buff = new int[4 * count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        rs.add(new Rect(buff[4 * i], buff[4 * i + 1], buff[4 * i + 2], buff[4 * i + 3]));
    }
}
 
Example 18
Source File: Converters.java    From pasm-yolov3-Android with GNU General Public License v3.0 5 votes vote down vote up
public static void Mat_to_vector_char(Mat m, List<Byte> bs) {
    if (bs == null)
        throw new java.lang.IllegalArgumentException("Output List can't be null");
    int count = m.rows();
    if (CvType.CV_8SC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_8SC1 != m.type() ||  m.cols()!=1\n" + m);

    bs.clear();
    byte[] buff = new byte[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        bs.add(buff[i]);
    }
}
 
Example 19
Source File: Converters.java    From Form-N-Fun with MIT License 5 votes vote down vote up
public static void Mat_to_vector_double(Mat m, List<Double> ds) {
    if (ds == null)
        throw new java.lang.IllegalArgumentException("ds == null");
    int count = m.rows();
    if (CvType.CV_64FC1 != m.type() || m.cols() != 1)
        throw new java.lang.IllegalArgumentException(
                "CvType.CV_64FC1 != m.type() ||  m.cols()!=1\n" + m);

    ds.clear();
    double[] buff = new double[count];
    m.get(0, 0, buff);
    for (int i = 0; i < count; i++) {
        ds.add(buff[i]);
    }
}
 
Example 20
Source File: MatXml.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 4 votes vote down vote up
private String dataStringBuilder(Mat mat) {
    StringBuilder sb = new StringBuilder();
    int rows = mat.rows();
    int cols = mat.cols();
    int type = mat.type();

    if( type == CvType.CV_32F ) {
        float fs[] = new float[1];
        for( int r=0 ; r<rows ; r++ ) {
            for( int c=0 ; c<cols ; c++ ) {
                mat.get(r, c, fs);
                sb.append( String.valueOf(fs[0]));
                sb.append( ' ' );
            }
            sb.append( '\n' );
        }
    }
    else if( type == CvType.CV_32S ) {
        int is[] = new int[1];
        for( int r=0 ; r<rows ; r++ ) {
            for( int c=0 ; c<cols ; c++ ) {
                mat.get(r, c, is);
                sb.append( String.valueOf(is[0]));
                sb.append( ' ' );
            }
            sb.append( '\n' );
        }
    }
    else if( type == CvType.CV_16S ) {
        short ss[] = new short[1];
        for( int r=0 ; r<rows ; r++ ) {
            for( int c=0 ; c<cols ; c++ ) {
                mat.get(r, c, ss);
                sb.append( String.valueOf(ss[0]));
                sb.append( ' ' );
            }
            sb.append( '\n' );
        }
    }
    else if( type == CvType.CV_8U ) {
        byte bs[] = new byte[1];
        for( int r=0 ; r<rows ; r++ ) {
            for( int c=0 ; c<cols ; c++ ) {
                mat.get(r, c, bs);
                sb.append( String.valueOf(bs[0]));
                sb.append( ' ' );
            }
            sb.append( '\n' );
        }
    }
    else {
        sb.append("unknown type\n");
    }

    return sb.toString();
}