@Override protected void releaseData() { synchronized (this) { // Explicitly deallocate Mats if (mRgba != null) mRgba.release(); if (mMarkerImage != null) mMarkerImage.release(); if (mGray != null) mGray.release(); mRgba = null; mGray = null; } markerDetector = null; }
public void onCameraViewStopped() { if (img != null) img.release(); if (img_hue != null) img_hue.release(); if (temp != null) temp.release(); if (img_bw1 != null) img_bw1.release(); if (img_bw2 != null) img_bw2.release(); img = null; img_hue = null; temp = null; img_bw1 = null; img_bw2 = null; }
@Override public void run() { super.run(); synchronized (this) { // Explicitly deallocate Mats if (mRgba != null) mRgba.release(); if (mGray != null) mGray.release(); if (mIntermediateMat != null) mIntermediateMat.release(); mRgba = null; mGray = null; mIntermediateMat = null; } }
// javadoc: buildOpticalFlowPyramid(img, pyramid, winSize, maxLevel, withDerivatives, pyrBorder, // derivBorder, tryReuseInputImage) public static int buildOpticalFlowPyramid( Mat img, List<Mat> pyramid, Size winSize, int maxLevel, boolean withDerivatives, int pyrBorder, int derivBorder, boolean tryReuseInputImage) { Mat pyramid_mat = new Mat(); int retVal = buildOpticalFlowPyramid_0( img.nativeObj, pyramid_mat.nativeObj, winSize.width, winSize.height, maxLevel, withDerivatives, pyrBorder, derivBorder, tryReuseInputImage); Converters.Mat_to_vector_Mat(pyramid_mat, pyramid); pyramid_mat.release(); return retVal; }
private void displayMarkersDebug(Mat imgMat, Scalar contourColor, Scalar codesColor) { ArrayList<MatOfPoint> components = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); DtouchMarker marker = new DtouchMarker(); boolean markerFound = findMarkers(imgMat, marker, components, hierarchy); if (markerFound) { String code = codeArrayToString(marker.getCode()); Point codeLocation = new Point(imgMat.cols() / 4, imgMat.rows() / 8); Core.putText(mRgba, code, codeLocation, Core.FONT_HERSHEY_COMPLEX, 1, codesColor, 3); Imgproc.drawContours( mRgba, components, marker.getComponentIndex(), contourColor, 3, 8, hierarchy, 2, new Point(0, 0)); } if (components != null) components.clear(); if (hierarchy != null) hierarchy.release(); components = null; hierarchy = null; }
@Override public void run() { super.run(); synchronized (this) { // Explicitly deallocate Mats if (mRgba != null) mRgba.release(); if (mGray != null) mGray.release(); if (mCascadeFile != null) mCascadeFile.delete(); if (mNativeDetector != null) mNativeDetector.release(); mRgba = null; mGray = null; mCascadeFile = null; } }
private void processFrameForMarkersDebug(VideoCapture capture) { ArrayList<MatOfPoint> components = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); // Get original image. capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); // Get gray scale image. capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME); // Get image segment to detect marker. Mat imgSegmentMat = cloneMarkerImageSegment(mGray); Mat thresholdedImgMat = new Mat(imgSegmentMat.size(), imgSegmentMat.type()); applyThresholdOnImage(imgSegmentMat, thresholdedImgMat); copyThresholdedImageToRgbImgMat(thresholdedImgMat, mRgba); Scalar contourColor = new Scalar(0, 0, 255); Scalar codesColor = new Scalar(255, 0, 0, 255); displayMarkersDebug(thresholdedImgMat, contourColor, codesColor); // displayThresholds(mRgba, codesColor, localThresholds); displayRectOnImageSegment(mRgba, false); if (components != null) components.clear(); if (hierarchy != null) hierarchy.release(); components = null; hierarchy = null; }
// javadoc: buildOpticalFlowPyramid(img, pyramid, winSize, maxLevel) public static int buildOpticalFlowPyramid( Mat img, List<Mat> pyramid, Size winSize, int maxLevel) { Mat pyramid_mat = new Mat(); int retVal = buildOpticalFlowPyramid_1( img.nativeObj, pyramid_mat.nativeObj, winSize.width, winSize.height, maxLevel); Converters.Mat_to_vector_Mat(pyramid_mat, pyramid); pyramid_mat.release(); return retVal; }
public static Mat[] getMatArray(byte[] data, Camera camera) { Camera.Parameters parameters = camera.getParameters(); int width = parameters.getPreviewSize().width; int height = parameters.getPreviewSize().height; Mat yuv = new Mat(height + height / 2, width, CvType.CV_8UC1); Mat rgba = new Mat(height, width, CvType.CV_8UC1); yuv.put(0, 0, data); Imgproc.cvtColor(yuv, rgba, Imgproc.COLOR_YUV2RGB_NV21); Mat rgbaResult = rotateMat(rgba); Mat grayResult = new Mat(rgbaResult.height(), rgbaResult.width(), CvType.CV_8UC1); Imgproc.cvtColor(rgbaResult, grayResult, Imgproc.COLOR_BGR2GRAY); yuv.release(); rgba.release(); Mat[] result = {grayResult, rgbaResult}; return result; }
private void copyThresholdedImageToRgbImgMat(Mat thresholdedImgMat, Mat dest) { // convert thresholded image segment to RGB. Mat smallRegionImg = new Mat(); Imgproc.cvtColor(thresholdedImgMat, smallRegionImg, Imgproc.COLOR_GRAY2BGRA, 4); // find location of image segment to be replaced in the destination image. Rect rect = calculateImageSegmentArea(dest); Mat destSubmat = dest.submat(rect.y, rect.y + rect.height, rect.x, rect.x + rect.width); // copy image. smallRegionImg.copyTo(destSubmat); smallRegionImg.release(); }
// javadoc: Subdiv2D::getVoronoiFacetList(idx, facetList, facetCenters) public void getVoronoiFacetList( MatOfInt idx, List<MatOfPoint2f> facetList, MatOfPoint2f facetCenters) { Mat idx_mat = idx; Mat facetList_mat = new Mat(); Mat facetCenters_mat = facetCenters; getVoronoiFacetList_0( nativeObj, idx_mat.nativeObj, facetList_mat.nativeObj, facetCenters_mat.nativeObj); Converters.Mat_to_vector_vector_Point2f(facetList_mat, facetList); facetList_mat.release(); return; }
public Mat onCameraFrame(Mat inputFrame) { if (takeScanBase) { if (scanBase != null) { scanBase.release(); } scanBase = new Mat(); scanningMat = new Mat(); Imgproc.cvtColor(inputFrame, scanBase, Imgproc.COLOR_RGB2GRAY); Imgproc.cvtColor(scanBase, scanningMat, Imgproc.COLOR_GRAY2RGB); scanBase.release(); hasScanBase = true; takeScanBase = false; } if (hasScanBase) { Mat ret; Point p = findLaser(inputFrame); inputFrame.release(); if (p != null) { if (hasThermal) { double temp = mThermal.read(); data[(int) p.y][(int) p.x] = (float) temp; Core.circle(scanningMat, p, 3, new Scalar(255, 0, 0), -3); } else { // Log.v(TAG, "No thermal!"); } } return scanningMat; } else return inputFrame; }
protected void releaseCamera() { synchronized (this) { if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); } mCamera = null; if (mBaseMat != null) mBaseMat.release(); if (mFrameChain != null) { mFrameChain[0].release(); mFrameChain[1].release(); } } }
private ArrayList<Double> applyThresholdOnImage(Mat srcImgMat, Mat outputImgMat) { double localThreshold; int startRow; int endRow; int startCol; int endCol; ArrayList<Double> localThresholds = new ArrayList<Double>(); int numberOfTiles = mPreference.getNumberOfTiles(); int tileWidth = (int) srcImgMat.size().height / numberOfTiles; int tileHeight = (int) srcImgMat.size().width / numberOfTiles; // Split image into tiles and apply threshold on each image tile separately. // process image tiles other than the last one. for (int tileRowCount = 0; tileRowCount < numberOfTiles; tileRowCount++) { startRow = tileRowCount * tileWidth; if (tileRowCount < numberOfTiles - 1) endRow = (tileRowCount + 1) * tileWidth; else endRow = (int) srcImgMat.size().height; for (int tileColCount = 0; tileColCount < numberOfTiles; tileColCount++) { startCol = tileColCount * tileHeight; if (tileColCount < numberOfTiles - 1) endCol = (tileColCount + 1) * tileHeight; else endCol = (int) srcImgMat.size().width; Mat tileThreshold = new Mat(); Mat tileMat = srcImgMat.submat(startRow, endRow, startCol, endCol); // localThreshold = Imgproc.threshold(tileMat, tileThreshold, 0, 255, Imgproc.THRESH_BINARY // | Imgproc.THRESH_OTSU); // RNM: Adaptive threshold rules! localThreshold = 0x80; Imgproc.adaptiveThreshold( tileMat, tileThreshold, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 91, 2); Mat copyMat = outputImgMat.submat(startRow, endRow, startCol, endCol); tileThreshold.copyTo(copyMat); tileThreshold.release(); localThresholds.add(localThreshold); } } return localThresholds; }
private void processFrameForMarkersFull(VideoCapture capture, DtouchMarker marker) { ArrayList<MatOfPoint> components = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); // Get original image. capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); // Get gray scale image. capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME); // Get image segment to detect marker. markerPosition = calculateImageSegmentArea(mGray); Mat imgSegmentMat = cloneMarkerImageSegment(mGray); // apply threshold. Mat thresholdedImgMat = new Mat(imgSegmentMat.size(), imgSegmentMat.type()); applyThresholdOnImage(imgSegmentMat, thresholdedImgMat); imgSegmentMat.release(); // find markers. boolean markerFound = findMarkers(thresholdedImgMat, marker, components, hierarchy); thresholdedImgMat.release(); // Marker detected. if (markerFound) { setMarkerDetected(true); // if marker is found then copy the marker image segment. mMarkerImage = cloneMarkerImageSegment(mRgba); // display codes on the original image. // displayMarkerCodes(mRgba, markers); // display rect with indication that a marker is identified. displayRectOnImageSegment(mRgba, true); // display marker image displayMarkerImage(mMarkerImage, mRgba); } else displayRectOnImageSegment(mRgba, false); if (components != null) components.clear(); if (hierarchy != null) hierarchy.release(); components = null; hierarchy = null; }
public void onCameraViewStopped() { // Explicitly deallocate Mats if (mZoomWindow != null) mZoomWindow.release(); if (mZoomCorner != null) mZoomCorner.release(); if (mBlurWindow != null) mBlurWindow.release(); if (mGrayInnerWindow != null) mGrayInnerWindow.release(); if (mRgbaInnerWindow != null) mRgbaInnerWindow.release(); if (mRgba != null) mRgba.release(); if (mGray != null) mGray.release(); if (mIntermediateMat != null) mIntermediateMat.release(); mRgba = null; mGray = null; mIntermediateMat = null; mRgbaInnerWindow = null; mGrayInnerWindow = null; mBlurWindow = null; mZoomCorner = null; mZoomWindow = null; }
private boolean findMarkers( Mat imgMat, DtouchMarker marker, ArrayList<MatOfPoint> components, Mat hierarchy) { boolean markerFound = false; // holds all the markers identified in the camera. List<DtouchMarker> markersDetected = new ArrayList<DtouchMarker>(); Mat contourImg = imgMat.clone(); // Find blobs using connect component. Imgproc.findContours( contourImg, components, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE); // No need to use contourImg so release it. contourImg.release(); List<Integer> code = new ArrayList<Integer>(); for (int i = 0; i < components.size(); i++) { // clean this list. code.clear(); if (markerDetector.verifyRoot(i, hierarchy, code)) { // if marker found then add in the list. DtouchMarker markerDetected = new DtouchMarker(); markerDetected.setCode(code); markerDetected.setComponentIndex(i); markersDetected.add(markerDetected); } } // if markers are found then decide which marker code occurred most. if (markersDetected.size() > 0) { DtouchMarker markerSelected = markerDetector.compareDetectedMarkers(markersDetected); if (markerSelected != null) { marker.setCode(markerSelected.getCode()); marker.setComponentIndex(markerSelected.getComponentIndex()); markerFound = true; } } return markerFound; }
public void onCameraViewStopped() { Log.i(TAG, "camera view stop"); mGray.release(); mRgba.release(); xface.stopFacedetect(); }
public Point findLaser(Mat inputFrame) { Mat mHsv = new Mat(); Imgproc.cvtColor(inputFrame, mHsv, Imgproc.COLOR_RGB2HSV); // Find laser center Mat center = new Mat(); Core.inRange(mHsv, new Scalar(0, 0, 250), new Scalar(180, 16, 255), center); Mat h = new Mat(); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(center, contours, h, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); center.release(); Mat center_mask = Mat.zeros(inputFrame.rows(), inputFrame.cols(), CvType.CV_8U); if (contours.size() > 0) { for (int i = 0; i < contours.size(); i++) { int radius = 10; // Point[] cont_pos = contours.get(i).toArray(); Moments m = Imgproc.moments(contours.get(i)); Point p = new Point(); p.x = m.get_m10() / m.get_m00(); p.y = m.get_m01() / m.get_m00(); Core.circle(center_mask, p, radius * 2, new Scalar(255), -1); } } // Find halo Mat ranged = new Mat(); Core.inRange(mHsv, new Scalar(100, 32, 225), new Scalar(150, 255, 255), ranged); mHsv.release(); // Mat f_frame =ranged.clone(); // Find halo around bright dot Core.bitwise_and(ranged, center_mask, ranged); center_mask.release(); // Find biggest resulting contour for (int i = 1; i < contours.size(); i++) { contours.get(i).release(); } contours.clear(); Imgproc.findContours(ranged, contours, h, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); h.release(); ranged.release(); if (contours.size() > 0) { MatOfPoint biggest_cont = contours.get(0); double cont_size = Imgproc.contourArea(biggest_cont); for (int i = 1; i < contours.size(); i++) { MatOfPoint cur = contours.get(i); if (Imgproc.contourArea(cur) > cont_size) { biggest_cont = cur; cont_size = Imgproc.contourArea(cur); } } Moments m = Imgproc.moments(biggest_cont); Point p = new Point(); p.x = m.get_m10() / m.get_m00(); p.y = m.get_m01() / m.get_m00(); for (int i = 1; i < contours.size(); i++) { contours.get(i).release(); } biggest_cont.release(); return p; } else { return null; } }
public boolean onTouch(View v, MotionEvent event) { int cols = mRgba.cols(); int rows = mRgba.rows(); int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2; int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2; int x = (int) event.getX() - xOffset; int y = (int) event.getY() - yOffset; Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")"); if ((x < 0) || (y < 0) || (x > cols) || (y > rows)) return false; Rect touchedRect = new Rect(); touchedRect.x = (x > 4) ? x - 4 : 0; touchedRect.y = (y > 4) ? y - 4 : 0; touchedRect.width = (x + 4 < cols) ? x + 4 - touchedRect.x : cols - touchedRect.x; touchedRect.height = (y + 4 < rows) ? y + 4 - touchedRect.y : rows - touchedRect.y; Mat touchedRegionRgba = mRgba.submat(touchedRect); Mat touchedRegionHsv = new Mat(); Imgproc.cvtColor(touchedRegionRgba, touchedRegionHsv, Imgproc.COLOR_RGB2HSV_FULL); // Calculate average color of touched region mBlobColorHsv = Core.sumElems(touchedRegionHsv); int pointCount = touchedRect.width * touchedRect.height; for (int i = 0; i < mBlobColorHsv.val.length; i++) mBlobColorHsv.val[i] /= pointCount; mBlobColorRgba = converScalarHsv2Rgba(mBlobColorHsv); Log.i( TAG, "Touched rgba color: (" + mBlobColorRgba.val[0] + ", " + mBlobColorRgba.val[1] + ", " + mBlobColorRgba.val[2] + ", " + mBlobColorRgba.val[3] + ")"); mDetector.setHsvColor(mBlobColorHsv); Imgproc.resize(mDetector.getSpectrum(), mSpectrum, SPECTRUM_SIZE); mIsColorSelected = true; final Handler handler = new Handler(); Runnable runable = new Runnable() { @Override public void run() { try { // do your code here hehe.setText(abc.toString()); if (abc > 16000 && abc < 25000) { imageView.setImageResource(R.drawable.green1); } else { imageView.setImageResource(R.drawable.red1); } // also call the same runnable handler.postDelayed(this, 1000); } catch (Exception e) { // TODO: handle exception } finally { // also call the same runnable // handler.postDelayed(this, 1000); } } }; handler.postDelayed(runable, 1000); touchedRegionRgba.release(); touchedRegionHsv.release(); return false; // don't need subsequent touch events }
public void onCameraViewStopped() { mGray.release(); mRgba.release(); }
@Override public void onCameraViewStopped() { mRgba.release(); }
public void destroy() { if (srcImage != null) srcImage.release(); if (spherifiedImage != null) spherifiedImage.release(); }
public void releaseMat() { new_image.release(); }
/** * @param inputImg * @return Mat */ public static Mat kmeans(Mat inputImg) { Mat rgba = inputImg; Mat tempMat = inputImg; rgba = new Mat(inputImg.cols(), inputImg.rows(), CvType.CV_8UC3); inputImg.copyTo(rgba); List<Mat> hsv_planes_temp = new ArrayList<Mat>(3); Core.split(tempMat, hsv_planes_temp); double threshValue1 = PreProcessingOperation.getHistAverage(inputImg, hsv_planes_temp.get(0)); sample.util.Estimate.setFirstHistAverageValue(threshValue1); System.out.println("Defore eqau " + threshValue1); System.out.println( Estimate.getBlueAverage() + " ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"); if (threshValue1 > 140) { if (Estimate.getBlueAverage() > 110) { rgba.convertTo(rgba, -1, 10d * 31 / 100, 0); System.out.println("11"); } else { rgba.convertTo(rgba, -1, 10d * 40 / 100, 0); System.out.println("12"); } } else if (threshValue1 > 135) { rgba.convertTo(rgba, -1, 10d * 32 / 100, 0); System.out.println("21"); } else if (threshValue1 > 125) { if (Estimate.getBlueAverage() > 110) { rgba.convertTo(rgba, -1, 10d * 30 / 100, 0); rgba = PreProcessing.Dilate(rgba, 5); System.out.println("31"); } else { rgba.convertTo(rgba, -1, 10d * 37 / 100, 0); System.out.println("32"); } } else if (threshValue1 > 120) { rgba.convertTo(rgba, -1, 10d * 35 / 100, 0); System.out.println("41"); } else if (threshValue1 > 110) { if (Estimate.getBlueAverage() > 110) { rgba.convertTo(rgba, -1, 10d * 35 / 100, 0); rgba = PreProcessing.Dilate(rgba, 5); System.out.println("51"); } } else if (threshValue1 > 100) { if (Estimate.getBlueAverage() > 107) { rgba.convertTo(rgba, -1, 10d * 24 / 100, 0); rgba = PreProcessing.Dilate(rgba, 5); System.out.println("61"); } else if (Estimate.getBlueAverage() > 90) { rgba.convertTo(rgba, -1, 10d * 30 / 100, 0); rgba = PreProcessing.Dilate(rgba, 5); System.out.println("62"); } } else if (threshValue1 > 50) { if (Estimate.getBlueAverage() > 160) { rgba.convertTo(rgba, -1, 10d * 30 / 100, 0); rgba = PreProcessing.Dilate(rgba, 3); System.out.println("81"); } else if (Estimate.getBlueAverage() > 160) { rgba.convertTo(rgba, -1, 10d * 27 / 100, 0); rgba = PreProcessing.Dilate(rgba, 9); System.out.println("82"); } else if (Estimate.getBlueAverage() > 130) { rgba.convertTo(rgba, -1, 10d * 30 / 100, 0); rgba = PreProcessing.Dilate(rgba, 9); System.out.println("83"); } else if (Estimate.getBlueAverage() > 70) { rgba.convertTo(rgba, -1, 10d * 29 / 100, 0); rgba = PreProcessing.Dilate(rgba, 9); System.out.println("84"); } } else if (threshValue1 > 30) { if (Estimate.getBlueAverage() > 190) { rgba.convertTo(rgba, -1, 10d * 25 / 100, 0); System.out.println("91"); } else if (Estimate.getBlueAverage() > 160) { rgba.convertTo(rgba, -1, 10d * 35 / 100, 0); System.out.println("92"); } } else { if (Estimate.getBlueAverage() > 240) { rgba.convertTo(rgba, -1, 10d * 24 / 100, 0); System.out.println("7"); } else { rgba.convertTo(rgba, -1, 10d * 17 / 100, 0); System.out.println("7"); } } tempMat.release(); Mat mHSV = new Mat(); Imgproc.cvtColor(rgba, mHSV, Imgproc.COLOR_RGBA2RGB, 3); Imgproc.cvtColor(rgba, mHSV, Imgproc.COLOR_RGB2HSV, 3); List<Mat> hsv_planes = new ArrayList<Mat>(3); Core.split(mHSV, hsv_planes); Mat channel = hsv_planes.get(0); channel = Mat.zeros(mHSV.rows(), mHSV.cols(), CvType.CV_8UC1); hsv_planes.set(2, channel); Core.merge(hsv_planes, mHSV); mHSV.convertTo(mHSV, CvType.CV_8UC1); mHSV = Histogram(mHSV); /* Mat clusteredHSV = new Mat(); mHSV.convertTo(mHSV, CvType.CV_32FC3); TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1); Core.kmeans(mHSV, 1, clusteredHSV, criteria, 20, Core.KMEANS_PP_CENTERS); Mat hsvImg = new Mat(); List<Mat> hsvPlanes = new ArrayList<>(); Mat thresholdImg = new Mat(); int thresh_type = Imgproc.THRESH_BINARY_INV; hsvImg.create(mHSV.size(), CvType.CV_8U); Imgproc.cvtColor(mHSV, hsvImg, Imgproc.COLOR_BGR2HSV); Core.split(hsvImg, hsvPlanes); Imgproc.threshold(hsvPlanes.get(1), thresholdImg, 0 , 200 , thresh_type); double threshValue = PreProcessingOperation.getHistAverage(hsvImg, hsvPlanes.get(0)); Estimate.setSecondHistAverageValue(threshValue); System.out.println("After equa " + Estimate.getSecondHistAverageValue());*/ Imgproc.threshold(mHSV, mHSV, 0, 150, Imgproc.THRESH_BINARY_INV); // mHSV.convertTo(mHSV, CvType.CV_8UC1); return mHSV; }
public void onCameraViewStopped() { mGray.release(); mRgba.release(); mZoomWindow.release(); mZoomWindow2.release(); }