List of usage examples for org.opencv.core Mat total
public long total()
From source file:cmib_4_4.Feature.java
public int[] featureVector(Mat image1) { Mat image = new Mat(); Size sz = new Size(30, 30); Imgproc.resize(image1, image, sz);// www .j a v a 2 s. c o m int size = (int) (image1.total() * image1.channels()); int size2 = (image.width() * image.height()); double[][] spec1 = new double[size2][3]; FeatureVector A = new FeatureVector(); int k = 0; for (int i = 0; i < image.rows(); i++) { for (int j = 0; j < image.cols(); j++) { //image.get(i, j, rgb); double[] rgb = image.get(i, j); double[] a = A.cartToSpec(rgb[0], rgb[1], rgb[2]); double x = Math.toRadians(90); spec1[k][0] = a[0] / x; spec1[k][1] = a[1] / x; spec1[k][2] = a[2] / x; //System.out.println(rgb[0]); //System.out.println(spec1[k][2]); k++; } } int[][] b = new int[11][11]; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { b[i][j] = 0; } } for (int i = 0; i < 900; i++) { int x1 = (int) (Math.round(spec1[i][1] * 10)); int y1 = (int) (Math.round(spec1[i][2] * 10)); b[x1][y1] = b[x1][y1] + 1; //System.out.println(x1+"and"+y1); } int l = 0; int[] c = new int[121]; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { c[l] = b[i][j]; l++; //System.out.println(c[l-1]); } } return c; }
From source file:cmib_4_4.FeatureVector.java
public Mat removeNoisePixcels(Mat noiseGreyImage, Mat rbgImage) { Mat image = noiseGreyImage; Mat image1 = rbgImage;//from w ww . jav a 2 s. 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;/*from w ww .jav a 2 s. 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); 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.lauszus.facerecognitionapp.TinyDB.java
License:Apache License
public void putListMat(String key, ArrayList<Mat> objArray) { checkForNullKey(key);/*from w w w .jav a2 s.c o m*/ ArrayList<String> objStrings = new ArrayList<String>(); for (Mat mat : objArray) { int size = (int) (mat.total() * mat.channels()); byte[] data = new byte[size]; mat.get(0, 0, data); String dataString = new String(Base64.encode(data, Base64.DEFAULT)); objStrings.add(dataString); } putListString(key, objStrings); }
From source file:com.minio.io.alice.MatVideoWriter.java
License:Open Source License
private byte[] captureRawBytes(Mat mat) { int length = (int) (mat.total() * mat.elemSize()); matByteArray = new byte[length]; mat.get(0, 0, matByteArray);/* w ww .j av a2 s.c o m*/ return matByteArray; }
From source file:com.oetermann.imageclassifier.Util.java
License:Open Source License
public static void saveMat(String path, Mat mat) { File file = new File(path).getAbsoluteFile(); file.getParentFile().mkdirs();/*from www . java2 s . c om*/ try { int rows = mat.rows(); int cols = mat.cols(); int type = mat.type(); Object data; switch (mat.type()) { case CvType.CV_8S: case CvType.CV_8U: data = new byte[(int) mat.total() * mat.channels()]; mat.get(0, 0, (byte[]) data); break; case CvType.CV_16S: case CvType.CV_16U: data = new short[(int) mat.total() * mat.channels()]; mat.get(0, 0, (short[]) data); break; case CvType.CV_32S: data = new int[(int) mat.total() * mat.channels()]; mat.get(0, 0, (int[]) data); break; case CvType.CV_32F: data = new float[(int) mat.total() * mat.channels()]; mat.get(0, 0, (float[]) data); break; case CvType.CV_64F: data = new double[(int) mat.total() * mat.channels()]; mat.get(0, 0, (double[]) data); break; default: data = null; } try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))) { oos.writeObject(rows); oos.writeObject(cols); oos.writeObject(type); oos.writeObject(data); oos.close(); } } catch (IOException | ClassCastException ex) { System.err.println("ERROR: Could not save mat to file: " + path); // Logger.getLogger(ImageClassifier.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.orange.documentare.core.image.opencv.OpenCvImage.java
License:Open Source License
private static long matBytesCount(Mat mat) { long pixelBytesCount = mat.elemSize(); return pixelBytesCount * mat.total(); }
From source file:com.shootoff.camera.shotdetection.JavaShotDetector.java
License:Open Source License
private Set<Pixel> findThresholdPixelsAndUpdateFilter(final Mat workingFrame, final boolean detectShots) { dynamicallyThresholded = 0;/*w w w . java 2s . c om*/ final Set<Pixel> thresholdPixels = Collections.synchronizedSet(new HashSet<Pixel>()); if (!cameraManager.isDetecting()) return thresholdPixels; final int subWidth = workingFrame.cols() / SECTOR_COLUMNS; final int subHeight = workingFrame.rows() / SECTOR_ROWS; final int cols = workingFrame.cols(); final int channels = workingFrame.channels(); final int size = (int) (workingFrame.total() * channels); final byte[] workingFramePrimitive = new byte[size]; workingFrame.get(0, 0, workingFramePrimitive); // In this loop we accomplish both MovingAverage updates AND threshold // pixel detection Parallel.forIndex(0, (SECTOR_ROWS * SECTOR_COLUMNS), 1, new Operation<Integer>() { public void perform(Integer sector) { final int sectorX = sector.intValue() % SECTOR_COLUMNS; final int sectorY = sector.intValue() / SECTOR_ROWS; if (!cameraManager.isSectorOn(sectorX, sectorY)) return; final int startX = subWidth * sectorX; final int startY = subHeight * sectorY; for (int y = startY; y < startY + subHeight; y++) { final int yOffset = y * cols; for (int x = startX; x < startX + subWidth; x++) { final int currentH = workingFramePrimitive[(yOffset + x) * channels] & 0xFF; final int currentS = workingFramePrimitive[(yOffset + x) * channels + 1] & 0xFF; final int currentV = workingFramePrimitive[(yOffset + x) * channels + 2] & 0xFF; final Pixel pixel = updateFilter(currentH, currentS, currentV, x, y, detectShots); if (pixel != null) thresholdPixels.add(pixel); } } } }); return thresholdPixels; }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
/** * The corresponding Java primitive array type depends on the Mat type: * CV_8U and CV_8S -> byte[]//w w w. ja v a 2 s. c o m * CV_16U and CV_16S -> short[] * CV_32S -> int[] * CV_32F -> float[] * CV_64F-> double[] */ static byte[] getByteArray(final Mat mat) { if (CvType.CV_8UC1 != mat.type()) throw new IllegalArgumentException( "Expected type is CV_8UC1, we found: " + CvType.typeToString(mat.type())); final int size = (int) (mat.total() * mat.channels()); if (_byteBuff.length != size) { _byteBuff = new byte[size]; } mat.get(0, 0, _byteBuff); // 0 for row and col means the WHOLE Matrix return _byteBuff; }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
static int[] getIntArray(final Mat mat) { if (CvType.CV_32SC1 != mat.type()) throw new IllegalArgumentException( "Expected type is CV_32SC1, we found: " + CvType.typeToString(mat.type())); final int size = (int) (mat.total() * mat.channels()); if (_intBuff.length != size) { _intBuff = new int[size]; }/*w ww . jav a 2 s .co m*/ mat.get(0, 0, _intBuff); // 0 for row and col means the WHOLE Matrix return _intBuff; }