List of usage examples for org.opencv.core Scalar Scalar
public Scalar(double[] vals)
From source file:com.trandi.opentld.tld.PatchGenerator.java
License:Apache License
/** * //w ww .jav a 2 s. c o m * @param image * @param T * @param patch OUTPUT * @param patchSize */ void generate(final Mat image, final Mat T, Mat patch, Size patchSize, final RNG rng) { patch.create(patchSize, image.type()); if (backgroundMin != backgroundMax) { Core.randu(patch, backgroundMin, backgroundMax); // TODO if that null scalar OK or should it be new Scalar(0) ? Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Core.BORDER_TRANSPARENT, null); } else { Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Core.BORDER_CONSTANT, new Scalar(backgroundMin)); } int ksize = randomBlur ? rng.nextInt() % 9 - 5 : 0; if (ksize > 0) { ksize = ksize * 2 + 1; Imgproc.GaussianBlur(patch, patch, new Size(ksize, ksize), 0, 0); } if (noiseRange > 0) { final Mat noise = new Mat(patchSize, image.type()); int delta = (image.depth() == CvType.CV_8U ? 128 : (image.depth() == CvType.CV_16U ? 32768 : 0)); Core.randn(noise, delta, noiseRange); // TODO this was different !! Core.addWeighted(patch, 1, noise, 1, -delta, patch); // if( backgroundMin != backgroundMax ) // addWeighted(patch, 1, noise, 1, -delta, patch); // else // { // for( int i = 0; i < patchSize.height; i++ ) // { // uchar* prow = patch.ptr<uchar>(i); // const uchar* nrow = noise.ptr<uchar>(i); // for( int j = 0; j < patchSize.width; j++ ) // if( prow[j] != backgroundMin ) // prow[j] = saturate_cast<uchar>(prow[j] + nrow[j] - delta); // } // } } }
From source file:com.trandi.opentld.tld.Tld.java
License:Apache License
/** * Output: resized zero-mean patch/pattern * @param inImg INPUT, outPattern OUTPUT * @return stdev/*w w w .java 2s . co m*/ */ private static double resizeZeroMeanStdev(final Mat inImg, Mat outPattern, int patternSize) { if (inImg == null || outPattern == null) { return -1; } Imgproc.resize(inImg, outPattern, new Size(patternSize, patternSize)); final MatOfDouble mean = new MatOfDouble(); final MatOfDouble stdev = new MatOfDouble(); Core.meanStdDev(outPattern, mean, stdev); outPattern.convertTo(outPattern, CvType.CV_32F); Core.subtract(outPattern, new Scalar(mean.toArray()[0]), outPattern); return stdev.toArray()[0]; }
From source file:com.wallerlab.compcellscope.calcDPCTask.java
License:BSD License
protected Long doInBackground(Mat... matrix_list) { //int count = urls.length; Mat in1 = matrix_list[0];/*from w ww . j a v a2 s . c o m*/ Mat in2 = matrix_list[1]; Mat outputMat = matrix_list[2]; Mat Mat1 = new Mat(in1.width(), in1.height(), in1.type()); Mat Mat2 = new Mat(in2.width(), in2.height(), in2.type()); in1.copyTo(Mat1); in2.copyTo(Mat2); Imgproc.cvtColor(Mat1, Mat1, Imgproc.COLOR_RGBA2GRAY, 1); Imgproc.cvtColor(Mat2, Mat2, Imgproc.COLOR_RGBA2GRAY, 1); Mat output = new Mat(Mat1.width(), Mat1.height(), CvType.CV_8UC4); Mat dpcSum = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); Mat dpcDifference = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); Mat dpcImgF = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); /* Log.d(TAG,String.format("Mat1 format is %.1f-%.1f, type: %d",Mat1.size().width,Mat1.size().height,Mat1.type())); Log.d(TAG,String.format("Mat2 format is %.1f-%.1f, type: %d",Mat2.size().width,Mat2.size().height,Mat2.type())); */ // Convert to Floats Mat1.convertTo(Mat1, CvType.CV_32FC1); Mat2.convertTo(Mat2, CvType.CV_32FC1); Core.add(Mat1, Mat2, dpcSum); Core.subtract(Mat1, Mat2, dpcDifference); Core.divide(dpcDifference, dpcSum, dpcImgF); Core.add(dpcImgF, new Scalar(1.0), dpcImgF); // Normalize to 0-2.0 Core.multiply(dpcImgF, new Scalar(110), dpcImgF); // Normalize to 0-255 dpcImgF.convertTo(output, CvType.CV_8UC1); // Convert back into RGB Imgproc.cvtColor(output, output, Imgproc.COLOR_GRAY2RGBA, 4); dpcSum.release(); dpcDifference.release(); dpcImgF.release(); Mat1.release(); Mat2.release(); Mat maskedImg = Mat.zeros(output.rows(), output.cols(), CvType.CV_8UC4); int radius = maskedImg.width() / 2 + 25; Core.circle(maskedImg, new Point(maskedImg.width() / 2, maskedImg.height() / 2), radius, new Scalar(255, 255, 255), -1, 8, 0); output.copyTo(outputMat, maskedImg); output.release(); maskedImg.release(); return null; }
From source file:com.wallerlab.compcellscope.MultiModeViewActivity.java
License:BSD License
public Mat calcDPC(Mat in1, Mat in2, Mat out) { Mat Mat1 = new Mat(in1.width(), in1.height(), in1.type()); Mat Mat2 = new Mat(in2.width(), in2.height(), in2.type()); in1.copyTo(Mat1);/*from w w w . j av a 2 s . co m*/ in2.copyTo(Mat2); Imgproc.cvtColor(Mat1, Mat1, Imgproc.COLOR_RGBA2GRAY, 1); Imgproc.cvtColor(Mat2, Mat2, Imgproc.COLOR_RGBA2GRAY, 1); Mat output = new Mat(Mat1.width(), Mat1.height(), CvType.CV_8UC4); Mat dpcSum = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); Mat dpcDifference = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); Mat dpcImgF = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); /* Log.d(TAG,String.format("Mat1 format is %.1f-%.1f, type: %d",Mat1.size().width,Mat1.size().height,Mat1.type())); Log.d(TAG,String.format("Mat2 format is %.1f-%.1f, type: %d",Mat2.size().width,Mat2.size().height,Mat2.type())); */ // Convert to Floats Mat1.convertTo(Mat1, CvType.CV_32FC1); Mat2.convertTo(Mat2, CvType.CV_32FC1); Core.add(Mat1, Mat2, dpcSum); Core.subtract(Mat1, Mat2, dpcDifference); Core.divide(dpcDifference, dpcSum, dpcImgF); Core.add(dpcImgF, new Scalar(1.0), dpcImgF); // Normalize to 0-2.0 Core.multiply(dpcImgF, new Scalar(110), dpcImgF); // Normalize to 0-255 dpcImgF.convertTo(output, CvType.CV_8UC1); // Convert back into RGB Imgproc.cvtColor(output, output, Imgproc.COLOR_GRAY2RGBA, 4); dpcSum.release(); dpcDifference.release(); dpcImgF.release(); Mat1.release(); Mat2.release(); Mat maskedImg = Mat.zeros(output.rows(), output.cols(), CvType.CV_8UC4); int radius = maskedImg.width() / 2 + 25; Core.circle(maskedImg, new Point(maskedImg.width() / 2, maskedImg.height() / 2), radius, new Scalar(255, 255, 255), -1, 8, 0); output.copyTo(out, maskedImg); output.release(); maskedImg.release(); return out; }
From source file:com.wallerlab.processing.tasks.ComputeRefocusTask.java
License:BSD License
private Bitmap[] computeFocus(float z) { int width = mDataset.WIDTH - 2 * mDataset.XCROP; int height = mDataset.HEIGHT - 2 * mDataset.YCROP; Mat result = new Mat(height, width, CvType.CV_32FC4); Mat result8 = new Mat(height, width, CvType.CV_8UC4); Mat dpc_result_tb = new Mat(height, width, CvType.CV_32FC4); Mat dpc_result_tb8 = new Mat(height, width, CvType.CV_8UC4); Mat dpc_result_lr = new Mat(height, width, CvType.CV_32FC4); Mat dpc_result_lr8 = new Mat(height, width, CvType.CV_8UC4); Mat img;/*from ww w.j a v a2s. com*/ Mat img32 = new Mat(height, width, CvType.CV_32FC4); Mat shifted; for (int idx = 0; idx < mDataset.fileCount; idx++) { img = ImageUtils.toMat(BitmapFactory.decodeByteArray(fileByteList[idx], 0, fileByteList[idx].length)); img = img.submat(mDataset.YCROP, mDataset.HEIGHT - mDataset.YCROP, mDataset.XCROP, mDataset.WIDTH - mDataset.XCROP); img.convertTo(img32, result.type()); // Grab actual hole number from filename String fName = mDataset.fileList[idx].toString(); String hNum = fName.substring(fName.indexOf("_scanning_") + 10, fName.indexOf(".jpeg")); int holeNum = Integer.parseInt(hNum); //Log.d(TAG,String.format("BF Scan Header is: %s", hNum)); // Calculate these based on array coordinates int xShift = (int) Math.round(z * tanh_lit[holeNum]); int yShift = (int) Math.round(z * tanv_lit[holeNum]); shifted = ImageUtils.circularShift(img32, yShift, xShift); if (mDataset.leftList.contains(holeNum)) //add LHS { Core.add(dpc_result_lr, shifted, dpc_result_lr); } else //subtract RHS { Core.subtract(dpc_result_lr, shifted, dpc_result_lr); } if (mDataset.topList.contains(holeNum)) //add Top { Core.add(dpc_result_tb, shifted, dpc_result_tb); } else //subtract bottom { Core.subtract(dpc_result_tb, shifted, dpc_result_tb); } Core.add(result, shifted, result); float progress = ((idx + 1) / (float) mDataset.fileCount); onProgressUpdate((int) (progress * 100), -1); Log.d(TAG, String.format("progress: %f", progress)); } Core.MinMaxLocResult minMaxLocResult1 = Core.minMaxLoc(result.reshape(1)); result.convertTo(result8, CvType.CV_8UC4, 255 / minMaxLocResult1.maxVal); Core.MinMaxLocResult minMaxLocResult2 = Core.minMaxLoc(dpc_result_lr.reshape(1)); dpc_result_lr.convertTo(dpc_result_lr8, CvType.CV_8UC4, 255 / (minMaxLocResult2.maxVal - minMaxLocResult2.minVal), -minMaxLocResult2.minVal * 255.0 / (minMaxLocResult2.maxVal - minMaxLocResult2.minVal)); Core.MinMaxLocResult minMaxLocResult3 = Core.minMaxLoc(dpc_result_tb.reshape(1)); dpc_result_tb.convertTo(dpc_result_tb8, CvType.CV_8UC4, 255 / (minMaxLocResult3.maxVal - minMaxLocResult3.minVal), -minMaxLocResult3.minVal * 255.0 / (minMaxLocResult3.maxVal - minMaxLocResult3.minVal)); /* Log.d(TAG,String.format("result_min: %f, max: %f",minMaxLocResult1.minVal,minMaxLocResult1.maxVal)); Log.d(TAG,String.format("lr_min: %f, max: %f",minMaxLocResult2.minVal,minMaxLocResult2.maxVal)); Log.d(TAG,String.format("tb_min: %f, max: %f",minMaxLocResult3.minVal,minMaxLocResult3.maxVal)); */ // remove transparency in DPC images Scalar alphaMask = new Scalar(new double[] { 1.0, 1.0, 1.0, 255.0 }); Core.multiply(dpc_result_lr8, alphaMask, dpc_result_lr8); Core.multiply(dpc_result_tb8, alphaMask, dpc_result_tb8); if (!mDataset.USE_COLOR_DPC) { Imgproc.cvtColor(dpc_result_lr8, dpc_result_lr8, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(dpc_result_tb8, dpc_result_tb8, Imgproc.COLOR_BGR2GRAY); } /* // Cut off edges in DPC images Point centerPt = new Point(); centerPt.x = Math.round((float)width/2.0); centerPt.y = Math.round((float)height/2.0); Mat circleMat = new Mat(dpc_result_lr8.size(), dpc_result_lr8.type()); Scalar color = new Scalar(255); Core.circle(circleMat, centerPt, 200, color); //Core.bitwise_and(circleMat, dpc_result_lr8, dpc_result_lr8); //Core.bitwise_and(circleMat, dpc_result_tb8, dpc_result_tb8); * * */ Bitmap[] outputBitmaps = new Bitmap[3]; outputBitmaps[0] = ImageUtils.toBitmap(result8); outputBitmaps[1] = ImageUtils.toBitmap(dpc_result_lr8); outputBitmaps[2] = ImageUtils.toBitmap(dpc_result_tb8); return outputBitmaps; }
From source file:cv.recon.controller.OutputDisplayController.java
License:Open Source License
/** * Subtract background using BackgroundSubtractorMOG2 * @param src Source Mat/* w w w . j a v a2 s .c om*/ */ private void subtractBackground(Mat src) { if (bsmog != null) { bsmog.apply(src, fgMask); Imgproc.erode(fgMask, fgMask, kernel); Imgproc.dilate(fgMask, fgMask, kernel); output.setTo(new Scalar(0)); src.copyTo(output, fgMask); if (isFirstFrame) { nonZeroCount = 0; isFirstFrame = false; } else { nonZeroCount = Core.countNonZero(fgMask); } nonZeroLabel.setText("" + nonZeroCount); } }
From source file:cv.recon.util.CannyDetector.java
License:Open Source License
/** * Apply Canny detector to Mat./* w w w.j a v a2 s . c o m*/ * @param src Input Mat * @param dst Output Mat */ public void detect(Mat src, Mat dst) { Mat src_gray = new Mat(); Mat detected_edges = new Mat(); Imgproc.cvtColor(src, src_gray, Imgproc.COLOR_RGB2GRAY); Imgproc.blur(src_gray, detected_edges, new Size(3, 3)); Imgproc.Canny(detected_edges, detected_edges, lowThreshold, lowThreshold * ratio, kernel_size, true); dst.setTo(new Scalar(0)); src.copyTo(dst, detected_edges); }
From source file:cx.uni.jk.mms.iaip.brush.BrushModel.java
License:Open Source License
private void updateValueMat() { this.valueMat = Mat.zeros(this.size, this.size, MatModel.MAT_TYPE); Core.add(this.valueMat, new Scalar(this.value), this.valueMat); }
From source file:cx.uni.jk.mms.iaip.brush.BrushModel.java
License:Open Source License
private void updateAlphaMat() { this.alphaMat = Mat.zeros(this.size, this.size, MatModel.MAT_TYPE); switch (this.shape) { case SQUARE:// w w w .j av a 2 s . c om Core.add(this.alphaMat, new Scalar(1.0f), this.alphaMat); break; case CIRCLE: Core.circle(this.alphaMat, new Point((this.size - 1) * 0.5d, (this.size - 1) * 0.5d), (this.size - 1) / 2, new Scalar(1.0f), -1); break; case SOFT_CIRCLE: { Mat temp = this.alphaMat.clone(); Core.circle(temp, new Point((this.size - 1) * 0.5d, (this.size - 1) * 0.5d), (this.size - 1) / 4, new Scalar(1.0f), -1); Imgproc.blur(temp, this.alphaMat, new Size(this.size * 0.5d, this.size * 0.5d)); } break; default: /** this must not happen, die */ throw new RuntimeException("unexpected BrushModel.Shape = " + this.shape); } }
From source file:cx.uni.jk.mms.iaip.brush.BrushModel.java
License:Open Source License
private void updateOneMinusAlphaMat() { this.oneMinusAlphaMat = this.alphaMat.clone(); Core.subtract(new Mat(this.alphaMat.rows(), this.alphaMat.cols(), MatModel.MAT_TYPE, new Scalar(1.0d)), this.alphaMat, this.oneMinusAlphaMat); }