List of usage examples for org.opencv.core Scalar Scalar
public Scalar(double[] vals)
From source file:qupath.opencv.processing.OpenCVTools.java
License:Open Source License
/** * Fill holes in a binary image (1-channel, 8-bit unsigned) with an area <= maxArea. * //from ww w .j av a 2s .c o m * @param matBinary * @param maxArea */ public static void fillSmallHoles(Mat matBinary, double maxArea) { Mat matHoles = new Mat(); invertBinary(matBinary, matHoles); List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(matHoles, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE); List<MatOfPoint> contoursTemp = new ArrayList<>(1); Scalar color = new Scalar(255); int ind = 0; for (MatOfPoint contour : contours) { // Only fill the small, inner contours if (hierarchy.get(0, ind)[3] >= 0 || Imgproc.contourArea(contour) > maxArea) { ind++; continue; } contoursTemp.clear(); contoursTemp.add(contour); Imgproc.drawContours(matBinary, contoursTemp, 0, color, -1); ind++; } }
From source file:qupath.opencv.processing.OpenCVTools.java
License:Open Source License
/** * Apply a watershed transform to refine a binary image, guided either by a distance transform or a supplied intensity image. * //from w w w . j a va 2s.c o m * @param matBinary - thresholded, 8-bit unsigned integer binary image * @param matIntensities - optional intensity image for applying watershed transform; if not set, distance transform of binary will be used * @param threshold */ public static void watershedIntensitySplit(Mat matBinary, Mat matWatershedIntensities, double threshold, int maximaRadius) { // Separate by intensity using the watershed transform // Find local maxima Mat matTemp = new Mat(); Mat strel = getCircularStructuringElement(maximaRadius); Imgproc.dilate(matWatershedIntensities, matTemp, strel); Core.compare(matWatershedIntensities, matTemp, matTemp, Core.CMP_EQ); Imgproc.dilate(matTemp, matTemp, getCircularStructuringElement(2)); Mat matWatershedSeedsBinary = matTemp; // Remove everything outside the thresholded region Core.min(matWatershedSeedsBinary, matBinary, matWatershedSeedsBinary); // Create labels for watershed Mat matLabels = new Mat(matWatershedIntensities.size(), CvType.CV_32F, new Scalar(0)); labelImage(matWatershedSeedsBinary, matLabels, Imgproc.RETR_CCOMP); // Do watershed // 8-connectivity is essential for the watershed lines to be preserved - otherwise OpenCV's findContours could not be used ProcessingCV.doWatershed(matWatershedIntensities, matLabels, threshold, true); // Update the binary image to remove the watershed lines Core.multiply(matBinary, matLabels, matBinary, 1, matBinary.type()); }
From source file:samples.LWF.java
private static void affine(Mat mat, double[][] from, double[][] to, double[][] coeficients, Mat lienzo, double escala, double gap) { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. // http://stackoverflow.com/questions/10100715/opencv-warping-from-one-triangle-to-another // https://www.learnopencv.com/warp-one-triangle-to-another-using-opencv-c-python/ // http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html MatOfPoint2f src_pf = new MatOfPoint2f(new Point(from[0][0], from[0][1]), new Point(from[1][0], from[1][1]), new Point(from[2][0], from[2][1])); MatOfPoint2f dst_pf = new MatOfPoint2f(new Point(to[0][0], to[0][1]), new Point(to[1][0], to[1][1]), new Point(to[2][0], to[2][1])); // https://www.learnopencv.com/warp-one-triangle-to-another-using-opencv-c-python/#download //how do I set up the position numbers in MatOfPoint2f here? // Mat perspective_matrix = Imgproc.getAffineTransform(src_pf, dst_pf); Rect r1 = Imgproc.boundingRect(new MatOfPoint(new Point(from[0][0], from[0][1]), new Point(from[1][0], from[1][1]), new Point(from[2][0], from[2][1]))); Rect r2 = Imgproc.boundingRect(new MatOfPoint(new Point(to[0][0], to[0][1]), new Point(to[1][0], to[1][1]), new Point(to[2][0], to[2][1]))); MatOfPoint2f tri1Cropped = new MatOfPoint2f(new Point(from[0][0] - r1.x, from[0][1] - r1.y), new Point(from[1][0] - r1.x, from[1][1] - r1.y), new Point(from[2][0] - r1.x, from[2][1] - r1.y)); MatOfPoint tri2CroppedInt = new MatOfPoint(new Point(to[0][0] - r2.x, to[0][1] - r2.y), new Point(to[1][0] - r2.x, to[1][1] - r2.y), new Point(to[2][0] - r2.x, to[2][1] - r2.y)); MatOfPoint2f tri2Cropped = new MatOfPoint2f(new Point((to[0][0] - r2.x), (to[0][1] - r2.y)), new Point((to[1][0] - r2.x), (to[1][1] - r2.y)), new Point((to[2][0] - r2.x), (to[2][1] - r2.y))); // for (int i = 0; i < 3; i++) { // // tri1Cropped.push_back(new MatOfPoint(new Point(from[i][0] - r1.x, from[i][1] - r1.y))); // new Point( from[i][0] - r1.x, from[i][1]- r1.y) ); // //tri2Cropped.push_back(new MatOfPoint(new Point(to[i][0] - r2.x, to[i][1] - r2.y))); ///* ww w .j a v a 2s . co m*/ // // fillConvexPoly needs a vector of Point and not Point2f // // tri2CroppedInt.push_back(new MatOfPoint2f(new Point((int) (to[i][0] - r2.x), (int) (to[i][1] - r2.y)))); // // } // Apply warpImage to small rectangular patches Mat img1Cropped = mat.submat(r1); //img1(r1).copyTo(img1Cropped); // Given a pair of triangles, find the affine transform. Mat warpMat = Imgproc.getAffineTransform(tri1Cropped, tri2Cropped); // Mat bbb = warpMat.mul(tri1Cropped); // // System.out.println( warpMat.dump() ); // System.out.println( tri2Cropped.dump() ); // System.out.println( bbb.dump() ); // Apply the Affine Transform just found to the src image Mat img2Cropped = Mat.zeros(r2.height, r2.width, img1Cropped.type()); Imgproc.warpAffine(img1Cropped, img2Cropped, warpMat, img2Cropped.size(), 0, Imgproc.INTER_LINEAR, new Scalar(Core.BORDER_TRANSPARENT)); //, 0, Imgproc.INTER_LINEAR, new Scalar(Core.BORDER_REFLECT_101)); // Get mask by filling triangle Mat mask = Mat.zeros(r2.height, r2.width, CvType.CV_8UC3); ///CV_8U CV_32FC3 Imgproc.fillConvexPoly(mask, tri2CroppedInt, new Scalar(1.0, 1.0, 1.0), 16, 0); // Copy triangular region of the rectangular patch to the output image // Core.multiply(img2Cropped,mask, img2Cropped); // // Core.multiply(mask, new Scalar(-1), mask); // Core.(mask,new Scalar(gap), mask); //Core.multiply(lienzo.submat(r2), (new Scalar(1.0,1.0,1.0)). - Core.multiply(mask,), lienzo.submat(r2)); // img2(r2) = img2(r2) + img2Cropped; // Core.subtract(Mat.ones(mask.height(), mask.width(), CvType.CV_8UC3), mask, mask); // Mat ff = ; // este Core.multiply(img2Cropped, mask, img2Cropped); //Core.multiply(lienzo.submat(r2), mask , lienzo.submat(r2)); Core.add(lienzo.submat(r2), img2Cropped, lienzo.submat(r2)); /* Mat bb = new Mat(mat, r2); bb.setTo(new Scalar(rnd.nextInt(),rnd.nextInt(),rnd.nextInt())); Core.multiply(bb,mask, bb); Core.multiply(lienzo.submat(r2), mask , lienzo.submat(r2)); Core.add(lienzo.submat(r2), bb, lienzo.submat(r2)); */ // lienzo.submat(r2).setTo(new Scalar(rnd.nextInt(),rnd.nextInt(),rnd.nextInt())); // // Imgproc.fillConvexPoly(lienzo, new MatOfPoint( // new Point(to[0][0] , to[0][1]), // new Point(to[1][0] , to[1][1]), // new Point(to[2][0] , to[2][1] )), new Scalar(1,1,1)); // img2Cropped.copyTo(lienzo); // return; // http://stackoverflow.com/questions/14111716/how-to-set-a-mask-image-for-grabcut-in-opencv // Imgproc.warpAffine(mat, lienzo, perspective_matrix, lienzo.size()); // Imgproc.getAffineTransform(null, null); /* // Find bounding rectangle for each triangle Rect r1 = boundingRect(tri1); Rect r2 = boundingRect(tri2); // Offset points by left top corner of the respective rectangles vector<Point2f> tri1Cropped, tri2Cropped; vector<Point> tri2CroppedInt; for(int i = 0; i < 3; i++) { tri1Cropped.push_back( Point2f( tri1[i].x - r1.x, tri1[i].y - r1.y) ); tri2Cropped.push_back( Point2f( tri2[i].x - r2.x, tri2[i].y - r2.y) ); // fillConvexPoly needs a vector of Point and not Point2f tri2CroppedInt.push_back( Point((int)(tri2[i].x - r2.x), (int)(tri2[i].y - r2.y)) ); } // Apply warpImage to small rectangular patches Mat img1Cropped; img1(r1).copyTo(img1Cropped); // Given a pair of triangles, find the affine transform. Mat warpMat = getAffineTransform( tri1Cropped, tri2Cropped ); // Apply the Affine Transform just found to the src image Mat img2Cropped = Mat::zeros(r2.height, r2.width, img1Cropped.type()); warpAffine( img1Cropped, img2Cropped, warpMat, img2Cropped.size(), INTER_LINEAR, BORDER_REFLECT_101); // Get mask by filling triangle Mat mask = Mat::zeros(r2.height, r2.width, CV_32FC3); fillConvexPoly(mask, tri2CroppedInt, Scalar(1.0, 1.0, 1.0), 16, 0); // Copy triangular region of the rectangular patch to the output image multiply(img2Cropped,mask, img2Cropped); multiply(img2(r2), Scalar(1.0,1.0,1.0) - mask, img2(r2)); img2(r2) = img2(r2) + img2Cropped;*/ }
From source file:samples.SimpleSample.java
public static void main(String[] args) { System.load("C:\\opencv\\build\\java\\x64\\opencv_java310.dll"); System.out.println(System.getProperty("java.library.path")); System.out.println("Welcome to OpenCV " + Core.VERSION); Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0)); System.out.println("OpenCV Mat: " + m); Mat mr1 = m.row(1);//w ww .java2s. c o m mr1.setTo(new Scalar(1)); Mat mc5 = m.col(5); mc5.setTo(new Scalar(5)); System.out.println("OpenCV Mat data:\n" + m.dump()); }
From source file:syncleus.dann.data.video.PatchGenerator.java
License:Apache License
/** * // w w w . ja va 2 s .co 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, Imgproc.BORDER_TRANSPARENT, null); } else { Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Imgproc.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:team492.GripPipeline.java
License:Open Source License
/** * This is the primary method that runs the entire pipeline and updates the * outputs.//ww w . jav a 2 s .co m */ public void process(Mat source0) { // Step HSV_Threshold0: Mat hsvThresholdInput = source0; double[] hsvThresholdHue = { 50.17985611510791, 99.69696969696967 }; double[] hsvThresholdSaturation = { 135.29676258992805, 255.0 }; double[] hsvThresholdValue = { 169.69424460431654, 255.0 }; hsvThreshold(hsvThresholdInput, hsvThresholdHue, hsvThresholdSaturation, hsvThresholdValue, hsvThresholdOutput); // Step CV_erode0: Mat cvErodeSrc = hsvThresholdOutput; Mat cvErodeKernel = new Mat(); Point cvErodeAnchor = new Point(-1, -1); double cvErodeIterations = 10.0; int cvErodeBordertype = Core.BORDER_CONSTANT; Scalar cvErodeBordervalue = new Scalar(-1); cvErode(cvErodeSrc, cvErodeKernel, cvErodeAnchor, cvErodeIterations, cvErodeBordertype, cvErodeBordervalue, cvErodeOutput); // Step Find_Contours0: Mat findContoursInput = cvErodeOutput; boolean findContoursExternalOnly = false; findContours(findContoursInput, findContoursExternalOnly, findContoursOutput); }
From source file:team492.GripPipeline.java
License:Open Source License
/** * Expands area of lower value in an image. * /*from w ww. j av a 2s . c o m*/ * @param src * the Image to erode. * @param kernel * the kernel for erosion. * @param anchor * the center of the kernel. * @param iterations * the number of times to perform the erosion. * @param borderType * pixel extrapolation method. * @param borderValue * value to be used for a constant border. * @param dst * Output Image. */ private void cvErode(Mat src, Mat kernel, Point anchor, double iterations, int borderType, Scalar borderValue, Mat dst) { if (kernel == null) { kernel = new Mat(); } if (anchor == null) { anchor = new Point(-1, -1); } if (borderValue == null) { borderValue = new Scalar(-1); } Imgproc.erode(src, dst, kernel, anchor, (int) iterations, borderType, borderValue); }
From source file:tv.danmaku.ijk.media.example.activities.VideoActivity.java
License:Apache License
@Override public void onCameraViewStarted(int width, int height) { mGray = new Mat(); mRgba = new Mat(); mIntermediateMat = new Mat(); /*/*w ww . j a va 2s . c o m*/ mResolutionList = mOpenCvCameraView.getResolutionList(); ListIterator<Size> resolutionItr = mResolutionList.listIterator(); while(resolutionItr.hasNext()) { Size element = resolutionItr.next(); Log.i(TAG, "Resolution Option ["+Integer.valueOf(element.width).toString() + "x" + Integer.valueOf(element.height).toString()+"]"); } Size resolution = mResolutionList.get(7); mOpenCvCameraView.setResolution(resolution); resolution = mOpenCvCameraView.getResolution(); String caption = "Resolution "+ Integer.valueOf(resolution.width).toString() + "x" + Integer.valueOf(resolution.height).toString(); Toast.makeText(this, caption, Toast.LENGTH_SHORT).show(); */ Camera.Size resolution = mOpenCvCameraView.getResolution(); String caption = "Resolution " + Integer.valueOf(resolution.width).toString() + "x" + Integer.valueOf(resolution.height).toString(); Toast.makeText(this, caption, Toast.LENGTH_SHORT).show(); Camera.Parameters cParams = mOpenCvCameraView.getParameters(); cParams.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); mOpenCvCameraView.setParameters(cParams); Toast.makeText(this, "Focus mode : " + cParams.getFocusMode(), Toast.LENGTH_SHORT).show(); mRgba = new Mat(height, width, CvType.CV_8UC4); mDetector = new ColorBlobDetector(); mSpectrum = new Mat(); mBlobColorRgba = new Scalar(255); mBlobColorHsv = new Scalar(255); SPECTRUM_SIZE = new Size(200, 64); CONTOUR_COLOR = new Scalar(255, 0, 0, 255); CONTOUR_COLOR_WHITE = new Scalar(255, 255, 255, 255); }
From source file:udp.server.ObjectTracker.java
private void createFrames() throws AWTException { //capture = new VideoCapture(1); //capture.set(3, 1280); //capture.set(3, 1366); // 500 //capture.set(4, 720); //capture.set(4, 768); // 400 //capture.set(15, -3); //capture.read(webcam_image); webcam_image = this.camCap.getFrame(); array255 = new Mat(480, 640, CvType.CV_8UC1); //array255 = new Mat(webcam_image.height(),webcam_image.width(),CvType.CV_8UC1); array255.setTo(new Scalar(255)); distance = new Mat(480, 640, CvType.CV_8UC1); //distance=new Mat(webcam_image.height(),webcam_image.width(),CvType.CV_8UC1); lhsv = new ArrayList<>(3); circles = new Mat(); }
From source file:udp.server.ObjectTracker.java
private void trackColors() { while (true) { //capture.read(webcam_image); //System.out.println(this.camCap.getFrame().size()); webcam_image = this.camCap.getFrame().clone(); if (!webcam_image.empty()) { //Adjusting brightness and contrast webcam_image.convertTo(webcam_image, -1, brightness, contrast); //Adding blur to remove noise Imgproc.blur(webcam_image, webcam_image, new Size(7, 7)); // converting to HSV image Imgproc.cvtColor(webcam_image, hsv_image, Imgproc.COLOR_BGR2HSV); //Checking if the hsv image is in range. Core.inRange(hsv_image, hsv_min, hsv_max, thresholded); Imgproc.erode(thresholded, thresholded, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8))); Imgproc.dilate(thresholded, thresholded, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8))); Core.split(hsv_image, lhsv); // We get 3 2D one channel Mats Mat S = lhsv.get(1);/* w ww . j av a 2 s .c o m*/ Mat V = lhsv.get(2); Core.subtract(array255, S, S); Core.subtract(array255, V, V); S.convertTo(S, CvType.CV_32F); V.convertTo(V, CvType.CV_32F); Core.magnitude(S, V, distance); Core.inRange(distance, new Scalar(0.0), new Scalar(200.0), thresholded2); Imgproc.GaussianBlur(thresholded, thresholded, new Size(9, 9), 0, 0); Imgproc.HoughCircles(thresholded, circles, Imgproc.CV_HOUGH_GRADIENT, 2, thresholded.height() / 8, 200, 100, 0, 0); Imgproc.findContours(thresholded, contours, thresholded2, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); //------Imgproc.drawContours(webcam_image, contours, -1, new Scalar(255, 0, 0), 2); //------Core.circle(webcam_image, new Point(210,210), 10, new Scalar(100,10,10),3); data = webcam_image.get(210, 210); //------Core.putText(webcam_image,String.format("("+String.valueOf(data[0])+","+String.valueOf(data[1])+","+String.valueOf(data[2])+")"),new Point(30, 30) , 3 //FONT_HERSHEY_SCRIPT_SIMPLEX //------ ,1.0,new Scalar(100,10,10,255),3); //ArrayList<Float> errorAngles = new ArrayList<>(); ArrayList<Float> errorAngles = getTargetError(); if (errorAngles != null) { try { semaphore.acquire(); } catch (InterruptedException ex) { Logger.getLogger(ObjectTracker.class.getName()).log(Level.SEVERE, null, ex); } float eXa = (errorAngles.get(0)); float eYa = (errorAngles.get(1)); this.dh.setPixyXvalue(eXa); this.dh.setPixyYvalue(eYa); //System.out.print("AngleErrorX: "+errorAngles.get(0)); //System.out.println(" AngleErrorY: "+ errorAngles.get(1)); semaphore.release(); } } /*else { System.out.println(" --(!) No captured frame -- Break!"); } */ } }