List of usage examples for org.opencv.core Mat copyTo
public void copyTo(Mat m)
From source file:com.ttolley.pongbot.opencv.CvWorker.java
@Override protected Void doInBackground() throws Exception { try {/* w ww. ja va 2s .co m*/ //-- 2. Read the video stream Mat webcam_image = new Mat(); if (capture.isOpened()) { while (true) { capture.read(webcam_image); if (!webcam_image.empty()) { PublishObject publishObject = new PublishObject(webcam_image); for (Map.Entry<FilterType, Filter> entry : filters.entrySet()) { Mat hsv_image = new Mat(); Mat thresholded = new Mat(); Filter filter = entry.getValue(); // One way to select a range of colors by Hue Imgproc.cvtColor(webcam_image, hsv_image, Imgproc.COLOR_BGR2HSV); Core.inRange(hsv_image, filter.hsv_min, filter.hsv_max, thresholded); // Morph open final Size erodeSizeObj = new Size(filter.erodeSize, filter.erodeSize); final Size dilateSizeObj = new Size(filter.dilateSize, filter.dilateSize); Imgproc.erode(thresholded, thresholded, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, erodeSizeObj)); Imgproc.dilate(thresholded, thresholded, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, erodeSizeObj)); // Morph close Imgproc.dilate(thresholded, thresholded, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, dilateSizeObj)); Imgproc.erode(thresholded, thresholded, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, dilateSizeObj)); Mat temp = new Mat(); thresholded.copyTo(temp); List<MatOfPoint> contours = new ArrayList(); Mat heirarchy = new Mat(); Imgproc.findContours(temp, contours, heirarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); FilteredObject.Target largestTarget = findTarget(contours, webcam_image, filter); publishObject.addObject(entry.getKey(), new FilteredObject(largestTarget, thresholded)); } publish(publishObject); } else { System.out.println(" --(!) No captured frame -- Break!"); break; } } } } catch (Exception ex) { System.out.println("Unable to loop"); System.out.println(getStackTrace(ex)); } return null; }
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]; Mat in2 = matrix_list[1];//from ww w .ja va 2 s . c o m 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); in2.copyTo(Mat2);/*from w w w . j a va 2s. c o m*/ 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.compcellscope.MultiModeViewActivity.java
License:BSD License
public Mat generateMMFrame(Mat gridOut, Mat MatTL, Mat MatTR, Mat MatBL, Mat MatBR) { //gridOut = new Mat(100, 100, gridOut.type(), new Scalar(0,0,0)); Mat Mat1 = new Mat(MatTL.size(), MatTL.type()); Mat Mat2 = new Mat(MatTR.size(), MatTR.type()); Mat Mat3 = new Mat(MatBL.size(), MatBL.type()); Mat Mat4 = new Mat(MatBR.size(), MatBR.type()); // Ensure all of the mats are of the correct size since pyramid operation resizes Imgproc.resize(MatTL, MatTL, sz);//from w w w . j a v a 2 s .c om Imgproc.resize(MatTR, MatTR, sz); Imgproc.resize(MatBL, MatBL, sz); Imgproc.resize(MatBR, MatBR, sz); // Downsample by 2 for 2x2 grid Imgproc.pyrDown(MatBL, Mat1); Imgproc.pyrDown(MatBR, Mat2); Imgproc.pyrDown(MatTL, Mat3); Imgproc.pyrDown(MatTR, Mat4); /* Log.d(TAG,String.format("TLRect format is %.1f-%.1f",TLRect.size().width,TLRect.size().height)); Log.d(TAG,String.format("TRRect format is %.1f-%.1f",TRRect.size().width,TRRect.size().height)); Log.d(TAG,String.format("BLRect format is %.1f-%.1f",BLRect.size().width,BLRect.size().height)); Log.d(TAG,String.format("BRRect format is %.1f-%.1f",BRRect.size().width,BRRect.size().height)); Log.d(TAG,String.format("MatTL format is %.1f-%.1f",MatTL.size().width,MatTL.size().height)); Log.d(TAG,String.format("MatTR format is %.1f-%.1f",MatTR.size().width,MatTR.size().height)); Log.d(TAG,String.format("MatBL format is %.1f-%.1f",MatBL.size().width,MatBL.size().height)); Log.d(TAG,String.format("MatBR format is %.1f-%.1f",MatBR.size().width,MatBR.size().height)); */ Core.putText(Mat1, "DPC-LR", new Point(43, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0)); Core.putText(Mat2, "DPC-TB", new Point(43, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0)); Core.putText(Mat3, "BrightField", new Point(33, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0)); Core.putText(Mat4, "DarkField", new Point(37, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0)); Mat1.copyTo(gridOut.submat(BLRect)); Mat2.copyTo(gridOut.submat(BRRect)); Mat3.copyTo(gridOut.submat(TLRect)); Mat4.copyTo(gridOut.submat(TRRect)); Mat1.release(); Mat2.release(); Mat3.release(); Mat4.release(); return gridOut; }
From source file:com.wallerlab.processing.utilities.ImageUtils.java
License:BSD License
public static Mat circularShift(Mat mat, int x, int y) { int w = mat.cols(); int h = mat.rows(); Mat result = Mat.zeros(h, w, CvType.CV_32FC4); int shiftR = x % w; int shiftD = y % h; //java modulus gives negative results for negative numbers if (shiftR < 0) shiftR += w;//from w w w. j ava 2 s. c o m if (shiftD < 0) shiftD += h; /* extract 4 submatrices |---| shiftR ______________ | | | | 1 | 2 | |_________|___| ___ shiftD | | | | | 3 | 4 | | | | | | |_________|___| _|_ */ Mat shift1 = mat.submat(0, h - shiftD, 0, w - shiftR); Mat shift2 = mat.submat(0, h - shiftD, w - shiftR, w); Mat shift3 = mat.submat(h - shiftD, h, 0, w - shiftR); Mat shift4 = mat.submat(h - shiftD, h, w - shiftR, w); /* and rearrange ______________ | | | | 4 | 3 | | | | |___|_________| | | | | 2 | 1 | |___|_________| */ shift1.copyTo(new Mat(result, new Rect(shiftR, shiftD, w - shiftR, h - shiftD))); shift2.copyTo(new Mat(result, new Rect(0, shiftD, shiftR, h - shiftD))); shift3.copyTo(new Mat(result, new Rect(shiftR, 0, w - shiftR, shiftD))); shift4.copyTo(new Mat(result, new Rect(0, 0, shiftR, shiftD))); return result; }
From source file:cx.uni.jk.mms.iaip.tools.SimpleBrushTool.java
License:Open Source License
@Override public Rect apply(Mat mat, BrushModel brush, int x, int y, boolean inverseEffect) { Rect changedArea = null;//from ww w .ja v a 2s. c om try { this.logger.finer(String.format("apply mode=\"%s\" inverse=%s, size=%d, strength=%d", brush.getMode(), inverseEffect, brush.getSize(), brush.getValue())); this.logger.finest("mat = " + mat.toString()); /** where is brush going to work? this may reach outside the mat! */ int brushColStart = x - (brush.getSize() - 1) / 2; int brushColEnd = x + brush.getSize() / 2; int brushRowStart = y - (brush.getSize() - 1) / 2; int brushRowEnd = y + brush.getSize() / 2; if (brushColEnd >= 0 && brushColStart < mat.cols() && brushRowEnd >= 0 && brushRowStart < mat.rows()) { /** calculate bounds for roiMat to fit into original mat */ int subColStart = Math.max(0, brushColStart); int subColEnd = Math.min(brushColEnd, mat.cols() - 1); int subRowStart = Math.max(0, brushRowStart); int subRowEnd = Math.min(brushRowEnd, mat.rows() - 1); /** * the caller may want to know. Rect constructor interprets the * second point being outside of the Rect! a one pixel rectangle * Rect(Point(a,b), Point(a+1,b+1)) has height and width 1. see * * @link{http://docs.opencv.org/java/org/opencv/core/Rect.html */ changedArea = new Rect(new Point(subColStart, subRowStart), new Point(subColEnd + 1, subRowEnd + 1)); /** * get the part of original mat which going to be affected by * change */ Mat roiMat = mat.submat(subRowStart, subRowEnd + 1, subColStart, subColEnd + 1); this.logger.finest("matRoi = " + roiMat.toString()); /** does the brush fit into the roiMat we shall work on ? */ boolean brushFits = brushColStart == subColStart && brushColEnd == subColEnd && brushRowStart == subRowStart && brushRowEnd == subRowEnd; this.logger.finest("brush fits = " + brushFits); /** * make sure to have a working mat which matches the full brush * size */ Mat workMat, workRoi = null; if (brushFits) { /** just work in the original mat area defined by roi */ workMat = roiMat; } else { /** create a new mat as big as the brush */ workMat = Mat.zeros(brush.getSize(), brush.getSize(), MatModel.MAT_TYPE); this.logger.finest("workMat= " + workMat.toString()); /** * create an ROI in the workMat as big as the subMat, * correct offset for brushing in the middle */ int roiColStart = subColStart - brushColStart; int roiColEnd = roiColStart + roiMat.cols(); int roiRowStart = subRowStart - brushRowStart; int roiRowEend = roiRowStart + roiMat.rows(); workRoi = workMat.submat(roiRowStart, roiRowEend, roiColStart, roiColEnd); this.logger.finest("workRoi= " + workRoi.toString()); roiMat.copyTo(workRoi); this.logger.finest("workRoi= " + workRoi.toString()); // workRoi.put(0, 0, 1333.0d); this.logger.finest("roiMat dump1 " + roiMat.dump()); this.logger.finest("workRoi dump1 " + workRoi.dump()); this.logger.finest("workMat dump1 " + workMat.dump()); } /** the real action */ this.applyToWorkMat(brush, inverseEffect, workMat); this.logger.finest("workMat dump2 " + workMat.dump()); this.logger.finest("matRoi dump2 " + roiMat.dump()); if (brushFits) { /** * nothing to do, we have been working directly in original * mat */ } else { /** copy workMat back into original mat */ this.logger.finest("workRoi dump2 " + workRoi.dump()); // workRoi.put(0, 0, 1338); this.logger.finest("workRoi dump3 " + workRoi.dump()); /** * copy roi of changed workmat back into roi of original mat */ this.logger.finest("matRoi = " + roiMat.toString()); workRoi.copyTo(roiMat); this.logger.finest("matRoi = " + roiMat.toString()); } this.logger.finest("matRoi dump3 " + roiMat.dump()); } } catch (CvException e) { /** nevermind if the user does not notice */ this.logger.fine(e.getStackTrace().toString()); } /** let the caller know caller which area has potentially been changed */ return changedArea; }
From source file:depthDataFromStereoCamsOpenCV.ProcessImages.java
/** * /*from www .j a va2 s.co m*/ * @param image * @return */ public static Mat bringImageToStdSize(Mat image) { //create the square container int dstWidth = 300; int dstHeight = 500; Mat dst = new Mat(dstHeight, dstWidth, CvType.CV_8UC3, new Scalar(0, 0, 0)); //ProcessImages.displayImage(ProcessImages.Mat2BufferedImage(dst),"background"); //Put the image into the container, roi is the new position Rect roi = new Rect((dstWidth - image.cols()) / 2, (dstHeight - image.rows()) / 2, image.cols(), image.rows()); Mat targetROI = new Mat(dst, roi); image.copyTo(targetROI); // ProcessImages.displayImage(ProcessImages.Mat2BufferedImage(dst),"Standardized"); return dst; }
From source file:detectors.CCdetection.java
/** * Method to scan image with x classifier *///from ww w . j ava 2 s . c om public void scanImage(String path) { CascadeClassifier faceCC = new CascadeClassifier(); faceCC.load("res/haarcascade_frontalface_alt.xml"); //Matrix to hold image to be scanned Mat image = new Mat(); image = Highgui.imread(path, IMREAD_COLOR); Mat rgb = new Mat(); Mat gray = new Mat(); image.copyTo(rgb); image.copyTo(gray); //Create integral image from the grayscale version of frame Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_BGR2GRAY); Imgproc.equalizeHist(gray, gray); //MatOfRect detection window initialized MatOfRect detects = new MatOfRect(); faceCC.detectMultiScale(gray, detects); //Print out number of detected rect objects detected if (detects.toArray().length != 0) { System.out.printf("Detected %s\n", detects.toArray().length); } }
From source file:detectors.CCdetection.java
/** * Method to scan video with x classifier * NOT COMPLETE/*from w w w. j a va 2s .c om*/ */ public void scanVideo() { //Initialize video capture object to 0(default camera device) VideoCapture vc = new VideoCapture(0); //String path = ""; //Initialize and load in classifier CascadeClassifier faceCC = new CascadeClassifier(); faceCC.load("res/haarcascade_frontalface_default.xml"); //Matrix(opencv core image object) to hold the image frame coming from the capture stream Mat image = new Mat(); boolean isFound = false; while (!isFound) { //VideoCapture read combines grab and retrieve methods to decode and return the frame vc.read(image); if (image != null) { //Initialize new Mats to hold the original colored frame and the grayscaled frame Mat rgb = new Mat(); Mat gray = new Mat(); image.copyTo(rgb); image.copyTo(gray); //Create integral image from the grayscale version of frame Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_BGR2GRAY); Imgproc.equalizeHist(gray, gray); //MatOfRect detection window initialized MatOfRect detects = new MatOfRect(); faceCC.detectMultiScale(gray, detects); //Print out number of detected rect objects detected if (detects.toArray().length != 0) { System.out.printf("Detected %s\n", detects.toArray().length); //prodFound(UPC,"T"); isFound = true; } } else { break; } } }
From source file:es.ugr.osgiliart.drawer.OpenCVCollageDrawer.java
License:Open Source License
@Override public void draw(ArtisticIndividual artistic) { /*// w ww . j av a 2s.c om * */ int imageWidth = (Integer) this.getAlgorithmParameters().getParameter(ArtisticParameters.IMAGE_WIDTH); int imageHeight = (Integer) this.getAlgorithmParameters().getParameter(ArtisticParameters.IMAGE_HEIGHT); String imageType = (String) this.getAlgorithmParameters().getParameter(ArtisticParameters.IMAGE_TYPE); String folderPath = (String) this.getAlgorithmParameters().getParameter(ArtisticParameters.DATA_FOLDER); List<Primitive> primitives = ((ArtisticGenome) artistic.getGenome()).getPrimitives(); Mat orig = new Mat(imageWidth, imageHeight, CvType.CV_8UC3, new Scalar(255, 255, 255)); for (Primitive p : primitives) { Patch patch = (Patch) p; Mat pm = patch.getMat(); int posCol = (int) (patch.getLocation().x * orig.cols()); int posRow = (int) (patch.getLocation().y * orig.rows()); int finalCol = posCol + pm.cols(); int finalRow = posRow + pm.rows(); if (finalCol > orig.cols()) finalCol = orig.cols(); if (finalRow > orig.rows()) finalRow = orig.rows(); //System.out.println("Poniendo imagen de tamao "+pm.rows()+","+pm.cols()+" en "+posRow+","+posCol+" hasta "+finalRow+","+finalCol); Mat bSubmat = orig.submat(posRow, finalRow, posCol, finalCol); pm.copyTo(bSubmat); } /* * draw image ****************************************************/ /*save image */ String imageExtension = null; if (imageType.equalsIgnoreCase(IMAGE_TYPE_JPEG)) { imageExtension = "jpg"; } else if (imageType.equalsIgnoreCase(IMAGE_TYPE_PNG)) { imageExtension = "png"; } if (imageExtension != null) { String imagePath = String.format("%s/%s.%s", folderPath, artistic.getId(), imageExtension); //System.out.println("Saving... " + imagePath + " primitives: " + primitives.size()); //graphics.save(imagePath); //applet.save(imagePath); Highgui.imwrite(imagePath, orig); artistic.setImagePath(imagePath); } }