List of usage examples for org.opencv.core Mat get
public int get(int row, int col, double[] data)
From source file:com.orange.documentare.core.image.opencv.OpenCvImage.java
License:Open Source License
private static void fillByteArray(byte[] byteArray, Mat image, boolean raw) { int colsNb = image.cols(); int bytesPerPixel = image.channels(); int bytesPerRow = colsNb * bytesPerPixel + (raw ? 1 : 0); byte[] pixel = new byte[bytesPerPixel]; int magicNumberOffset = 0; for (int y = 0; y < image.rows(); y++) { for (int x = 0; x < colsNb; x++) { image.get(y, x, pixel); for (int z = 0; z < bytesPerPixel; z++) { byteArray[magicNumberOffset + y * bytesPerRow + x * bytesPerPixel + z] = pixel[z]; }/*w w w . j av a 2s.co m*/ } if (raw) { byteArray[magicNumberOffset + y * bytesPerRow + colsNb * bytesPerPixel] = SIMDOC_LINE_TERMINATION; } } }
From source file:com.shootoff.camera.Camera.java
License:Open Source License
public static BufferedImage matToBufferedImage(Mat matBGR) { BufferedImage image = new BufferedImage(matBGR.width(), matBGR.height(), BufferedImage.TYPE_3BYTE_BGR); final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); matBGR.get(0, 0, targetPixels); return image; }
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;//from ww w.j av a 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.shootoff.camera.shotdetection.PixelCluster.java
License:Open Source License
public double getColorDifference(final Mat workingFrame, final int[][] colorDistanceFromRed) { final Map<Pixel, byte[]> visited = new HashMap<Pixel, byte[]>(); int avgSaturation = 0; for (final Pixel pixel : this) { if (pixel.getConnectedness() < MAXIMUM_CONNECTEDNESS) { for (int h = -1; h <= 1; h++) { for (int w = -1; w <= 1; w++) { if (h == 0 && w == 0) continue; final int rx = pixel.x + w; final int ry = pixel.y + h; if (rx < 0 || ry < 0 || rx >= workingFrame.cols() || ry >= workingFrame.rows()) continue; final Pixel nearPoint = new Pixel(rx, ry); if (!visited.containsKey(nearPoint) && !this.contains(nearPoint)) { byte[] np = { 0, 0, 0 }; workingFrame.get(ry, rx, np); final int npSaturation = np[1] & 0xFF; avgSaturation += npSaturation; visited.put(nearPoint, np); }/*from w w w.j a v a 2s. c o m*/ } } } } final int pixelCount = visited.size(); if (pixelCount == 0) return 0; avgSaturation /= pixelCount; int colorDistance = 0; int avgColorDistance = 0; int tempColorDistance = 0; for (final Entry<Pixel, byte[]> pixelEntry : visited.entrySet()) { byte[] np = pixelEntry.getValue(); final int npSaturation = np[1] & 0xFF; if (npSaturation > avgSaturation) { final int npColor = np[0] & 0xFF; final int npLum = np[2] & 0xFF; final int thisDFromRed = Math.min(npColor, Math.abs(180 - npColor)) * npLum * npSaturation; final int thisDFromGreen = Math.abs(60 - npColor) * npLum * npSaturation; final int currentCol = thisDFromRed - thisDFromGreen; final Pixel pixel = pixelEntry.getKey(); colorDistance += currentCol - (int) (CURRENT_COLOR_BIAS_MULTIPLIER * colorDistanceFromRed[pixel.x][pixel.y]); if (logger.isTraceEnabled()) { tempColorDistance += currentCol; avgColorDistance += colorDistanceFromRed[pixel.x][pixel.y]; } } } if (logger.isTraceEnabled()) logger.trace("Pixels {} Color {} avg {} sum {}", pixelCount, colorDistance / pixelCount, avgColorDistance / pixelCount, tempColorDistance / pixelCount); return colorDistance; }
From source file:com.sikulix.core.Finder.java
License:Open Source License
private static void printMatI(Mat mat) { int[] data = new int[mat.channels()]; for (int r = 0; r < mat.rows(); r++) { for (int c = 0; c < mat.cols(); c++) { mat.get(r, c, data); log.trace("(%d, %d) %s", r, c, Arrays.toString(data)); }/* w w w.j ava 2s.c o m*/ } }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
static byte getByte(final int row, final int col, final Mat mat) { if (CvType.CV_8UC1 != mat.type()) throw new IllegalArgumentException( "Expected type is CV_8UC1, we found: " + CvType.typeToString(mat.type())); mat.get(row, col, _byteBuff1); return _byteBuff1[0]; }
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[]//from ww w.ja va 2s .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]; }/*from ww w. j a va 2 s . c o m*/ mat.get(0, 0, _intBuff); // 0 for row and col means the WHOLE Matrix return _intBuff; }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
static float[] getFloatArray(final Mat mat) { if (CvType.CV_32FC1 != mat.type()) throw new IllegalArgumentException( "Expected type is CV_32FC1, we found: " + CvType.typeToString(mat.type())); final int size = (int) (mat.total() * mat.channels()); if (_floatBuff.length != size) { _floatBuff = new float[size]; }/* ww w . j a v a2s . c om*/ mat.get(0, 0, _floatBuff); // 0 for row and col means the WHOLE Matrix return _floatBuff; }
From source file:com.trandi.opentld.tld.Util.java
License:Apache License
static double[] getDoubleArray(final Mat mat) { if (CvType.CV_64F != mat.type()) throw new IllegalArgumentException( "Expected type is CV_64F, we found: " + CvType.typeToString(mat.type())); final int size = (int) (mat.total() * mat.channels()); if (_doubleBuff.length != size) { _doubleBuff = new double[size]; }//w w w. ja va 2s .c o m mat.get(0, 0, _doubleBuff); // 0 for row and col means the WHOLE Matrix return _doubleBuff; }