List of usage examples for org.opencv.core Mat get
public int get(int row, int col, double[] data)
From source file:ch.zhaw.facerecognitionlibrary.Helpers.MatXml.java
License:Open Source License
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(' '); }/*from ww w . j a v a 2 s . c om*/ 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(); }
From source file:Clases.Segmentador.java
public BufferedImage Mat2BufferedImage(Mat m) { //source: http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/ //Fastest code //The output can be assigned either to a BufferedImage or to an Image int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; }/*from w w w.j a v a 2 s. c o m*/ int bufferSize = m.channels() * m.cols() * m.rows(); byte[] b = new byte[bufferSize]; m.get(0, 0, b); // get all the pixels BufferedImage image = new BufferedImage(m.cols(), m.rows(), type); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(b, 0, targetPixels, 0, b.length); return image; }
From source file:classes.Util.java
public static BufferedImage mat2Img(Mat in) { BufferedImage out;//from w w w . j av a2 s . c om byte[] data = new byte[in.width() * in.height() * (int) in.elemSize()]; int type; in.get(0, 0, data); if (in.channels() == 1) { type = BufferedImage.TYPE_BYTE_GRAY; } else { type = BufferedImage.TYPE_3BYTE_BGR; } out = new BufferedImage(in.width(), in.height(), type); out.getRaster().setDataElements(0, 0, in.width(), in.height(), data); return out; }
From source file:cmib_4_4.FeatureVector.java
public Mat removeNoisePixcels(Mat noiseGreyImage, Mat rbgImage) { Mat image = noiseGreyImage; Mat image1 = rbgImage;//from www . j a v a2s . c o m int size = (int) (image.total() * image.channels()); byte[] get = new byte[size]; byte[] temp1 = new byte[size]; int size1 = (int) (image1.total() * image1.channels()); byte[] rgb1 = new byte[size1]; for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { image.get(i, j, get); if (get[0] == -1) { image.put(i, j, 1); } image.get(i, j, get); //System.out.println(get[0]); if (get[0] == 1) { if (i == 0 & j == 0) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j + 1, a1); image.get(i + 1, j + 1, a2); image.get(i + 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 0.0); //System.out.println("1"); } else { image.put(i, j, 0); } } else if (i == 0 & j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j - 1, a1); image.get(i + 1, j - 1, a2); image.get(i + 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("2"); } else { image.put(i, j, 0); } } else if (i == image.rows() & j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { // image1.put(j, i, 1.0); // System.out.println("3"); } else { image.put(i, j, 0); } } else if (j == 0 & i == image.rows()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j + 1, a1); image.get(i - 1, j + 1, a2); image.get(i - 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("4"); } else { image.put(i, j, 0); } } else if (j == 0) { double[] a1 = image.get(i - 1, j); double[] a2 = image.get(i - 1, j + 1); double[] a3 = image.get(i, j + 1); double[] a4 = image.get(i + 1, j + 1); double[] a5 = image.get(i + 1, j); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("5"); } else { image.put(i, j, 0); } } else if (i == 0) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i, j - 1, a1); image.get(i + 1, j - 1, a2); image.get(i + 1, j, a3); image.get(i + 1, j + 1, a4); image.get(i, j + 1, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("6"); } else { image.put(i, j, 0); } } else if (j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); image.get(i + 1, j - 1, a4); image.get(i + 1, j, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("7"); } else { image.put(i, j, 0); } } else if (i == image.rows()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i, j + 1, a1); image.get(i - 1, j + 1, a2); image.get(i - 1, j, a3); image.get(i - 1, j - 1, a4); image.get(i, j - 1, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("8"); } else { image.put(i, j, 0); } } else { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; byte[] a6 = new byte[1]; byte[] a7 = new byte[1]; byte[] a8 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); image.get(i + 1, j - 1, a4); image.get(i + 1, j, a5); image.get(i + 1, j + 1, a6); image.get(i, j + 1, a7); image.get(i - 1, j + 1, a8); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == 1 & a4[0] == -1 & a5[0] == -1) | (a4[0] == -1 & a5[0] == -1 & a6[0] == -1) | (a5[0] == -1 & a6[0] == 1 & a7[0] == -1) | (a6[0] == -1 & a7[0] == -1 & a8[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("9"); } else { image.put(i, j, 0); } } } } } ////////////////////// Obtain RGB final blood cell image ///////////////////////////// for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { image.get(i, j, temp1); image1.get(i, j, rgb1); if (temp1[0] == -1) { image.put(i, j, 1); } image.get(i, j, temp1); //System.out.println(temp1[0]); byte r = (byte) (rgb1[0] * temp1[0]); byte g = (byte) (rgb1[1] * temp1[0]); byte b = (byte) (rgb1[2] * temp1[0]); image1.put(i, j, new byte[] { r, g, b }); } } ///////////////////////////////////////////////////////////////////////////////////// return image1; }
From source file:cmib_4_4.NoiseRemove.java
public Mat removeNoisePixcels(Mat noiseGreyImage, Mat rbgImage) { Mat image = noiseGreyImage; Mat image1 = rbgImage;//w ww . j av a 2 s . com int size = (int) (image.total() * image.channels()); byte[] get = new byte[size]; byte[] temp1 = new byte[size]; int size1 = (int) (image1.total() * image1.channels()); byte[] rgb1 = new byte[size1]; for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { image.get(i, j, get); if (get[0] == -1) { image.put(i, j, 1); } image.get(i, j, get); //System.out.println(get[0]); if (get[0] == 1) { if (i == 0 & j == 0) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j + 1, a1); image.get(i + 1, j + 1, a2); image.get(i + 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 0.0); //System.out.println("1"); } else { image.put(i, j, 0); } } else if (i == 0 & j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j - 1, a1); image.get(i + 1, j - 1, a2); image.get(i + 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("2"); } else { image.put(i, j, 0); } } else if (i == image.rows() & j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { // image1.put(j, i, 1.0); // System.out.println("3"); } else { image.put(i, j, 0); } } else if (j == 0 & i == image.rows()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; image.get(i, j + 1, a1); image.get(i - 1, j + 1, a2); image.get(i - 1, j, a3); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("4"); } else { image.put(i, j, 0); } } else if (j == 0) { double[] a1 = image.get(i - 1, j); double[] a2 = image.get(i - 1, j + 1); double[] a3 = image.get(i, j + 1); double[] a4 = image.get(i + 1, j + 1); double[] a5 = image.get(i + 1, j); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("5"); } else { image.put(i, j, 0); } } else if (i == 0) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i, j - 1, a1); image.get(i + 1, j - 1, a2); image.get(i + 1, j, a3); image.get(i + 1, j + 1, a4); image.get(i, j + 1, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("6"); } else { image.put(i, j, 0); } } else if (j == image.cols()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); image.get(i + 1, j - 1, a4); image.get(i + 1, j, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("7"); } else { image.put(i, j, 0); } } else if (i == image.rows()) { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; image.get(i, j + 1, a1); image.get(i - 1, j + 1, a2); image.get(i - 1, j, a3); image.get(i - 1, j - 1, a4); image.get(i, j - 1, a5); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == -1 & a4[0] == -1 & a5[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("8"); } else { image.put(i, j, 0); } } else { byte[] a1 = new byte[1]; byte[] a2 = new byte[1]; byte[] a3 = new byte[1]; byte[] a4 = new byte[1]; byte[] a5 = new byte[1]; byte[] a6 = new byte[1]; byte[] a7 = new byte[1]; byte[] a8 = new byte[1]; image.get(i - 1, j, a1); image.get(i - 1, j - 1, a2); image.get(i, j - 1, a3); image.get(i + 1, j - 1, a4); image.get(i + 1, j, a5); image.get(i + 1, j + 1, a6); image.get(i, j + 1, a7); image.get(i - 1, j + 1, a8); if ((a1[0] == -1 & a2[0] == -1 & a3[0] == -1) | (a2[0] == -1 & a3[0] == -1 & a4[0] == -1) | (a3[0] == 1 & a4[0] == -1 & a5[0] == -1) | (a4[0] == -1 & a5[0] == -1 & a6[0] == -1) | (a5[0] == -1 & a6[0] == 1 & a7[0] == -1) | (a6[0] == -1 & a7[0] == -1 & a8[0] == -1)) { //image1.put(j, i, 1.0); // System.out.println("9"); } else { image.put(i, j, 0); } } } } } ////////////////////// Obtain RGB final blood cell image ///////////////////////////// for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { image.get(i, j, temp1); image1.get(i, j, rgb1); if (temp1[0] == -1) { image.put(i, j, 1); } image.get(i, j, temp1); byte r = (byte) (rgb1[0] * temp1[0]); byte g = (byte) (rgb1[1] * temp1[0]); byte b = (byte) (rgb1[2] * temp1[0]); image1.put(i, j, new byte[] { r, g, b }); } } return image1; }
From source file:com.android.cts.verifier.sensors.RVCVXCheckAnalyzer.java
License:Apache License
/** * Analyze video frames using computer vision approach and generate a ArrayList<AttitudeRec> * * @param recs output ArrayList of AttitudeRec * @return total number of frame of the video *//* w ww .ja v a 2 s .co m*/ private int analyzeVideo(ArrayList<AttitudeRec> recs) { VideoMetaInfo meta = new VideoMetaInfo(new File(mPath, "videometa.json")); int decimation = 1; boolean use_timestamp = true; // roughly determine if decimation is necessary if (meta.fps > DECIMATION_FPS_TARGET) { decimation = (int) (meta.fps / DECIMATION_FPS_TARGET); meta.fps /= decimation; } VideoDecoderForOpenCV videoDecoder = new VideoDecoderForOpenCV(new File(mPath, "video.mp4"), decimation); Mat frame; Mat gray = new Mat(); int i = -1; Size frameSize = videoDecoder.getSize(); if (frameSize.width != meta.frameWidth || frameSize.height != meta.frameHeight) { // this is very unlikely return -1; } if (TRACE_VIDEO_ANALYSIS) { Debug.startMethodTracing("cvprocess"); } Size patternSize = new Size(4, 11); float fc = (float) (meta.frameWidth / 2.0 / Math.tan(meta.fovWidth / 2.0)); Mat camMat = cameraMatrix(fc, new Size(frameSize.width / 2, frameSize.height / 2)); MatOfDouble coeff = new MatOfDouble(); // dummy MatOfPoint2f centers = new MatOfPoint2f(); MatOfPoint3f grid = asymmetricalCircleGrid(patternSize); Mat rvec = new MatOfFloat(); Mat tvec = new MatOfFloat(); MatOfPoint2f reprojCenters = new MatOfPoint2f(); if (LOCAL_LOGV) { Log.v(TAG, "Camera Mat = \n" + camMat.dump()); } long startTime = System.nanoTime(); long[] ts = new long[1]; while ((frame = videoDecoder.getFrame(ts)) != null) { if (LOCAL_LOGV) { Log.v(TAG, "got a frame " + i); } if (use_timestamp && ts[0] == -1) { use_timestamp = false; } // has to be in front, as there are cases where execution // will skip the later part of this while i++; // convert to gray manually as by default findCirclesGridDefault uses COLOR_BGR2GRAY Imgproc.cvtColor(frame, gray, Imgproc.COLOR_RGB2GRAY); boolean foundPattern = Calib3d.findCirclesGrid(gray, patternSize, centers, Calib3d.CALIB_CB_ASYMMETRIC_GRID); if (!foundPattern) { // skip to next frame continue; } if (OUTPUT_DEBUG_IMAGE) { Calib3d.drawChessboardCorners(frame, patternSize, centers, true); } // figure out the extrinsic parameters using real ground truth 3D points and the pixel // position of blobs found in findCircleGrid, an estimated camera matrix and // no-distortion are assumed. boolean foundSolution = Calib3d.solvePnP(grid, centers, camMat, coeff, rvec, tvec, false, Calib3d.CV_ITERATIVE); if (!foundSolution) { // skip to next frame if (LOCAL_LOGV) { Log.v(TAG, "cannot find pnp solution in frame " + i + ", skipped."); } continue; } // reproject points to for evaluation of result accuracy of solvePnP Calib3d.projectPoints(grid, rvec, tvec, camMat, coeff, reprojCenters); // error is evaluated in norm2, which is real error in pixel distance / sqrt(2) double error = Core.norm(centers, reprojCenters, Core.NORM_L2); if (LOCAL_LOGV) { Log.v(TAG, "Found attitude, re-projection error = " + error); } // if error is reasonable, add it into the results. use ratio to frame height to avoid // discriminating higher definition videos if (error < REPROJECTION_THREASHOLD_RATIO * frameSize.height) { double[] rv = new double[3]; double timestamp; rvec.get(0, 0, rv); if (use_timestamp) { timestamp = (double) ts[0] / 1e6; } else { timestamp = (double) i / meta.fps; } if (LOCAL_LOGV) Log.v(TAG, String.format("Added frame %d ts = %f", i, timestamp)); recs.add(new AttitudeRec(timestamp, rodr2rpy(rv))); } if (OUTPUT_DEBUG_IMAGE) { Calib3d.drawChessboardCorners(frame, patternSize, reprojCenters, true); Imgcodecs.imwrite(Environment.getExternalStorageDirectory().getPath() + "/RVCVRecData/DebugCV/img" + i + ".png", frame); } } if (LOCAL_LOGV) { Log.v(TAG, "Finished decoding"); } if (TRACE_VIDEO_ANALYSIS) { Debug.stopMethodTracing(); } if (LOCAL_LOGV) { // time analysis double totalTime = (System.nanoTime() - startTime) / 1e9; Log.i(TAG, "Total time: " + totalTime + "s, Per frame time: " + totalTime / i); } return i; }
From source file:com.android.cts.verifier.sensors.RVCVXCheckAnalyzer.java
License:Apache License
private static Mat quat2rpy(Mat quat) { double[] q = new double[4]; quat.get(0, 0, q); double[] rpy = { Math.atan2(2 * (q[0] * q[1] + q[2] * q[3]), 1 - 2 * (q[1] * q[1] + q[2] * q[2])), Math.asin(2 * (q[0] * q[2] - q[3] * q[1])), Math.atan2(2 * (q[0] * q[3] + q[1] * q[2]), 1 - 2 * (q[2] * q[2] + q[3] * q[3])) }; Mat rpym = new Mat(3, 1, CvType.CV_64F); rpym.put(0, 0, rpy);/* www.j a v a 2 s. c o m*/ return rpym; }
From source file:com.android.cts.verifier.sensors.RVCVXCheckAnalyzer.java
License:Apache License
private static Mat rodr2quat(Mat rodr) { double t = Core.norm(rodr); double[] r = new double[3]; rodr.get(0, 0, r); double[] quat = { Math.cos(t / 2), Math.sin(t / 2) * r[0] / t, Math.sin(t / 2) * r[1] / t, Math.sin(t / 2) * r[2] / t }; Mat quatm = new Mat(4, 1, CvType.CV_64F); quatm.put(0, 0, quat);//from w w w.j a v a2 s . c om return quatm; }
From source file:com.astrocytes.core.ImageHelper.java
License:Open Source License
public static BufferedImage convertMatToBufferedImage(Mat in) { BufferedImage out;//from w w w. ja v a2s . com byte[] data = new byte[in.cols() * in.rows() * (int) in.elemSize()]; in.get(0, 0, data); int type = BufferedImage.TYPE_3BYTE_BGR; switch (in.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; } out = new BufferedImage(in.cols(), in.rows(), type); out.getRaster().setDataElements(0, 0, in.cols(), in.rows(), data); return out; }
From source file:com.example.afs.makingmusic.process.ImageGenerator.java
License:Open Source License
private BufferedImage toBufferedImage(Mat matrix) { int width = matrix.cols(); int height = matrix.rows(); int channels = matrix.channels(); int bitmapSize = height * width * channels; byte[] bitmap = new byte[bitmapSize]; matrix.get(0, 0, bitmap); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); WritableRaster raster = image.getRaster(); DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer(); System.arraycopy(bitmap, 0, dataBuffer.getData(), 0, bitmap.length); return image; }