List of usage examples for org.opencv.core Mat height
public int height()
From source file:main.Utils.java
public BufferedImage matToBufferedImage(Mat original) { // init//from ww w . ja v a 2 s.c o m BufferedImage image = null; int width = original.width(), height = original.height(), channels = original.channels(); byte[] sourcePixels = new byte[width * height * channels]; original.get(0, 0, sourcePixels); if (original.channels() > 1) { image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } else { image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); } final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); return image; }
From source file:mineshcvit.opendocscanner.CropImage.java
License:Apache License
private void makeDefault() { // minesh: finding the largest rect in the given image //Mat grayImage= Imgcodecs.imread(IMAGE_PATH, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE); ////////////////////// ////////////*w ww . j ava 2 s . com*/ Mat imgSource = new Mat(); Utils.bitmapToMat(mBitmap, imgSource); // Utils.bitmapToMat(bmp32, imgMAT); Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY); //Mat imgSource = Imgcodecs.imread(mImagePath,Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE); Log.w("myApp", "image path from isnde makedefault() is " + mImagePath); int matwidth = imgSource.width(); int matheight = imgSource.height(); Log.w("myApp", "mat image width, from makedefault() is " + matwidth); Log.w("myApp", "mat image height from, makedefault() is " + matheight); Mat imageBin = new Mat(); double threshold = Imgproc.threshold(imgSource, imageBin, 0, 255, Imgproc.THRESH_OTSU); Log.w("myApp", "otsu threshold is " + threshold); //for canny higher threshold is chosen as otsus threshold and lower threshold is half of the otsu threshold value Imgproc.Canny(imgSource.clone(), imgSource, threshold * 0.5, threshold); // Imgcodecs.imwrite(mImagePath, imgSource); // int canny_height=imgSource.height(); // int canny_width=imgSource.width(); // Log.w("myApp", "canny image height is "+canny_height); Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(3, 3), 3); // find the contours List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); //MatVector contours = new MatVector(); Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); double maxArea = -1; MatOfPoint temp_contour = contours.get(0); // the largest is at the // index 0 for starting // point MatOfPoint2f approxCurve = new MatOfPoint2f(); for (int idx = 0; idx < contours.size(); idx++) { temp_contour = contours.get(idx); double contourarea = Imgproc.contourArea(temp_contour); // compare this contour to the previous largest contour found if (contourarea > maxArea) { // check if this contour is a square MatOfPoint2f new_mat = new MatOfPoint2f(temp_contour.toArray()); int contourSize = (int) temp_contour.total(); MatOfPoint2f approxCurve_temp = new MatOfPoint2f(); Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize * 0.05, true); if (approxCurve_temp.total() == 4) { maxArea = contourarea; approxCurve = approxCurve_temp; } } } double[] temp_double; temp_double = approxCurve.get(0, 0); Point p1 = new Point(temp_double[0], temp_double[1]); // Core.circle(imgSource,p1,55,new Scalar(0,0,255)); // Imgproc.warpAffine(sourceImage, dummy, rotImage,sourceImage.size()); temp_double = approxCurve.get(1, 0); Point p2 = new Point(temp_double[0], temp_double[1]); // Core.circle(imgSource,p2,150,new Scalar(255,255,255)); temp_double = approxCurve.get(2, 0); Point p3 = new Point(temp_double[0], temp_double[1]); // Core.circle(imgSource,p3,200,new Scalar(255,0,0)); temp_double = approxCurve.get(3, 0); Point p4 = new Point(temp_double[0], temp_double[1]); // Core.circle(imgSource,p4,100,new Scalar(0,0,255)); ArrayList<Point> source = new ArrayList<Point>(); ArrayList<Point> topPoints = new ArrayList<Point>(); ArrayList<Point> bottomPoints = new ArrayList<Point>(); ArrayList<Point> sortedPoints = new ArrayList<Point>(); source.add(p1); source.add(p2); source.add(p3); source.add(p4); Collections.sort(source, new Comparator<Point>() { public int compare(Point o1, Point o2) { return Double.compare(o1.y, o2.y); } }); topPoints.add(source.get(0)); topPoints.add(source.get(1)); Collections.sort(topPoints, new Comparator<Point>() { public int compare(Point o1, Point o2) { return Double.compare(o1.x, o2.x); } }); bottomPoints.add(source.get(2)); bottomPoints.add(source.get(3)); Collections.sort(bottomPoints, new Comparator<Point>() { public int compare(Point o1, Point o2) { return Double.compare(o1.x, o2.x); } }); sortedPoints.add(topPoints.get(0));//top left sortedPoints.add(bottomPoints.get(0));//bottom left sortedPoints.add(bottomPoints.get(1));//bottom right sortedPoints.add(topPoints.get(1));//top right /* c++ code to sort the points void sortCorners(std::vector<cv::Point2f>& corners, cv::Point2f center) { std::vector<cv::Point2f> top, bot; for (int i = 0; i < corners.size(); i++) { if (corners[i].y < center.y) top.push_back(corners[i]); else bot.push_back(corners[i]); } cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0]; cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1]; cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0]; cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1]; corners.clear(); corners.push_back(tl); corners.push_back(tr); corners.push_back(br); corners.push_back(bl); } ... // Get mass center cv::Point2f center(0,0); for (int i = 0; i < corners.size(); i++) center += corners[i]; center *= (1. / corners.size()); sortCorners(corners, center); */ //p1 t0 p4 are in the anti clock wise order starting from top left // double s=source.get(0).x; ///////////////// ///////////////// int width = mBitmap.getWidth(); int height = mBitmap.getHeight(); Log.w("myApp", "bitmap width is " + width); Log.w("myApp", "bitmap height is " + height); Rect imageRect = new Rect(0, 0, width, height); // make the default size about 4/5 of the width or height /* int cropWidth = Math.min(width, height) * 4 / 5; int cropHeight = cropWidth; int x = (width - cropWidth) / 2; int y = (height - cropHeight) / 2; RectF cropRect = new RectF(x, y, x + cropWidth, y + cropHeight); */ /// To test the points order /* Point p1 = new Point(1.0*x,1.0*y ); Point p2 = new Point(1.0*x+150.0,1.0*y+1.0*cropHeight); Point p3 = new Point(1.0*x+1.0*cropWidth,1.0*y+1.0*cropHeight); Point p4 = new Point(1.0*x+1.0*cropWidth,1.0*y); ArrayList<Point> source = new ArrayList<Point>(); source.add(p1); source.add(p2); source.add(p3); source.add(p4); */ //////////////////////////// Log.w("myApp", "from inside makedeafult inside cropimage calss, default crop rect values are set and now highlight view will be initiated "); HighlightView hv = new HighlightView(mImageView, imageRect, sortedPoints); Log.w("myApp", "higlight view initiated; done"); mImageView.add(hv); Log.w("myApp", "add hv is done; done"); mImageView.invalidate(); mCrop = hv; Log.w("myApp", "mcrop=hv donee"); mCrop.setFocus(true); ; }
From source file:net.hydex11.opencvinteropexample.MainActivity.java
License:Open Source License
private void example() { RenderScript mRS = RenderScript.create(this); // Loads input image Bitmap inputBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.houseimage); // Puts input image inside an OpenCV mat Mat inputMat = new Mat(); Utils.bitmapToMat(inputBitmap, inputMat); Mat outputMat = new Mat(inputMat.size(), inputMat.type()); // Testing bitmap, used to test that the OpenCV mat actually has bitmap data inside Bitmap initialBitmap = Bitmap.createBitmap(inputMat.width(), inputMat.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(inputMat, initialBitmap); // Retrieve OpenCV mat data address long inputMatDataAddress = inputMat.dataAddr(); long outputMatDataAddress = outputMat.dataAddr(); // Creates a RS type that matches the input mat one. Element element = Element.RGBA_8888(mRS); Type.Builder tb = new Type.Builder(mRS, element); tb.setX(inputMat.width());/*from ww w .j av a 2 s. co m*/ tb.setY(inputMat.height()); Type inputMatType = tb.create(); // Creates a RenderScript allocation that uses directly the OpenCV input mat address Allocation inputAllocation = createTypedAllocationWithDataPointer(mRS, inputMatType, inputMatDataAddress); Allocation outputAllocation = createTypedAllocationWithDataPointer(mRS, inputMatType, outputMatDataAddress); // Define a simple convolve script // Note: here, ANY kernel can be applied! ScriptIntrinsicConvolve3x3 convolve3x3 = ScriptIntrinsicConvolve3x3.create(mRS, element); float convolveCoefficients[] = new float[9]; convolveCoefficients[0] = 1; convolveCoefficients[2] = 1; convolveCoefficients[5] = 1; convolveCoefficients[6] = 1; convolveCoefficients[8] = 1; convolve3x3.setCoefficients(convolveCoefficients); convolve3x3.setInput(inputAllocation); convolve3x3.forEach(outputAllocation); mRS.finish(); // Converts the result to a bitmap Bitmap cvOutputBitmap = Bitmap.createBitmap(outputMat.width(), outputMat.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(outputMat, cvOutputBitmap); // Testing bitmap, used to test the RenderScript ouput allocation contents // Note: it is placed here because the copyTo function clears the input buffer Bitmap rsOutputBitmap = Bitmap.createBitmap(outputMat.width(), outputMat.height(), Bitmap.Config.ARGB_8888); outputAllocation.copyTo(rsOutputBitmap); // Testing bitmap, used to test that RenderScript input allocation pointed to the OpenCV mat // Note: it is placed here because the copyTo function clears the input buffer Bitmap rsInitialBitmap = Bitmap.createBitmap(inputMat.width(), inputMat.height(), Bitmap.Config.ARGB_8888); inputAllocation.copyTo(rsInitialBitmap); // Display input and output ImageView originalImageIV = (ImageView) findViewById(R.id.imageView); ImageView inputRSImageIV = (ImageView) findViewById(R.id.imageView2); ImageView outputRSImageIV = (ImageView) findViewById(R.id.imageView3); ImageView outputCVIV = (ImageView) findViewById(R.id.imageView4); originalImageIV.setImageBitmap(initialBitmap); inputRSImageIV.setImageBitmap(rsInitialBitmap); outputRSImageIV.setImageBitmap(rsOutputBitmap); outputCVIV.setImageBitmap(cvOutputBitmap); }
From source file:news_analysis.headlinedetection.HeadLineDetection.java
public boolean isHeadLine(Mat image) { Size imageSize = image.size();// ww w.j a v a 2s .c o m int width = image.width(); int height = image.height(); if (height > 50 && height < 100) { return horizontalChecked(image, width, height); } //VerticleChecked( image, width, height); return false; }
From source file:news_analysis.isimage.IsImage.java
public boolean isImage(Mat image) { Size imageSize = image.size();/* ww w. ja va 2 s . c o m*/ int width = image.width(); int height = image.height(); borderDetection = new BorderDetection(); ArrayList<BorderItem> borderItems = borderDetection.getBorder(image, width, height); Mat[] subMat = new Mat[borderItems.size()]; for (int i = 0; i < borderItems.size(); i++) { BorderItem item = borderItems.get(i); if (item.getHeight() > 100 && item.getWidth() > 100) { item = canMaxiMizeBorder(item, item.getMinX(), item.getMaxX(), item.getMinY(), item.getMaxY(), height, width); subMat[i] = image.submat(item.getMinX(), item.getMaxX(), item.getMinY(), item.getMaxY()); //NewsAnalysis.imshow("Sub sub sub" + i, subMat[i]); int horizontal[] = horizontalChecked(subMat[i], item.getHeight() - 1, item.getWidth() - 1); int verticle[] = VerticleChecked(subMat[i], item.getHeight() - 1, item.getWidth() - 1); if (horizontal[0] + horizontal[1] > 110 && verticle[0] + verticle[1] > 110) { return true; } return true; } } return false; }
From source file:nz.ac.auckland.lablet.vision.CamShiftTracker.java
License:Open Source License
/** * Saves a Mat based image to /sdcard/ for debugging. * * @param frame The frame to save./*from w w w. j a va 2 s. c om*/ * @param name The name of the file (without a file type). */ public void saveFrame(Mat frame, String name) { Bitmap bmp = Bitmap.createBitmap(frame.width(), frame.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(frame, bmp); this.saveFrame(bmp, name); }
From source file:opencltest.YetAnotherTestT.java
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat kernel = new Mat(3, 3, CV_8UC1); kernel.put(0, 0, new double[] { 0, 1, 0, 1, 1, 1, 0, 1, 0 }); Mat source = Imgcodecs.imread("test.smaller.png"); Mat blur = new Mat(); Mat edges = new Mat(); Mat dilated = new Mat(); Imgproc.GaussianBlur(source, blur, new Size(13, 13), 0); Imgproc.Canny(blur, edges, 10, 30, 5, false); // Imgproc.cvtColor(edges, edges, Imgproc.COLOR_RGB2GRAY); // Imgproc.adaptiveThreshold(edges, edges, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 5, 2); // Core.bitwise_not(edges, edges); // Imgproc.dilate(edges, edges, kernel); // Imgproc.dilate(edges, dilated, kernel); dilated = edges;/*from ww w .j a va2 s . c o m*/ // Core.bitwise_not(edges, edges); Mat lines = new Mat(); Imgproc.HoughLinesP(dilated, lines, 1, Math.PI / 180, 300, 10, 70); Mat empty = new Mat(source.height(), source.width(), source.type()); // paintLines(empty, lines); List<MatOfPoint> contours = new ArrayList<>(); Mat hier = new Mat(); Imgproc.findContours(edges, contours, hier, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); Mat foundSquare = new Mat(source.height(), source.width(), CvType.CV_8UC4); source.copyTo(foundSquare); List<Double> hor = new ArrayList<>(); for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) { MatOfPoint next = iterator.next(); Rect bounding = Imgproc.boundingRect(next); int tr = 20; if (diffLessThan(bounding.size().width - 40, tr) && diffLessThan(bounding.size().height - 40, tr)) { Imgproc.rectangle(empty, bounding.tl(), bounding.br(), randomColor(), 3); // hor.add(bounding.x + 0.0); hor.add(bounding.x + bounding.width / 2.0 + 0.0); drawRect(bounding, foundSquare); } } Imgcodecs.imwrite("test_2.png", source); Imgcodecs.imwrite("test_3.png", dilated); Imgcodecs.imwrite("test_4.png", empty); Imgcodecs.imwrite("test_h.png", foundSquare); hor.sort(Double::compare); double low = hor.get(0); double hih = hor.get(hor.size() - 1); double n = hor.size(); Function<Double, Double> K = (d) -> (Math.abs(d) <= 1) ? ((3.0 / 4.0) * (1 - (d * d))) : 0;//epanechnikov kernel List<Double> result = new ArrayList<>(); double h = 10; for (int i = 0; i < source.width() + 1; i++) result.add(0.0); for (double d = low; d <= hih; d += 1) { double sum = 0; for (Double di : hor) { sum += K.apply((d - di) / h); } result.set((int) d, sum / (n * h)); System.out.println(sum / (n * h)); } normalize(result, 255); Mat test = new Mat(source.height(), source.width(), source.type()); source.copyTo(test); draw(result, test); Imgcodecs.imwrite("test_uwot.png", test); }
From source file:opencltest.YetAnotherTestT.java
private static void drawCross(double x, double y, Mat foundSquare) { Imgproc.line(foundSquare, new Point(x, 0), new Point(x, foundSquare.height()), new Scalar(255, 0, 0, 180), 1);/* w w w . jav a2s.com*/ Imgproc.line(foundSquare, new Point(0, y), new Point(foundSquare.width(), y), new Scalar(255, 0, 0, 180), 1); }
From source file:opencltest.YetAnotherTestT.java
private static void draw(List<Double> result, Mat test) { for (int i = 1; i < result.size() - 1; i++) { if (result.get(i) > result.get(i + 1) && result.get(i) > result.get(i - 1)) { Imgproc.line(test, new Point(i, 0), new Point(i, test.height()), // new Scalar(result.get(i), 0, 0), 1 new Scalar(0, 0, 255), 1); }/* ww w . j a v a 2s . co m*/ // Imgproc.line(test, // new Point(i, 0), new Point(i, test.height()), // new Scalar(result.get(i), 0, 0), 1 //// new Scalar(0,0,255),1 // ); } }
From source file:org.akvo.caddisfly.sensor.colorimetry.strip.util.ResultUtil.java
License:Open Source License
public static Bitmap makeBitmap(@NonNull Mat mat) { try {/*from ww w . j a va 2 s. c o m*/ Bitmap bitmap = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(mat, bitmap); //double max = bitmap.getHeight() > bitmap.getWidth() ? bitmap.getHeight() : bitmap.getWidth(); //double min = bitmap.getHeight() < bitmap.getWidth() ? bitmap.getHeight() : bitmap.getWidth(); //double ratio = min / max; //int width = (int) Math.max(600, max); //int height = (int) Math.round(ratio * width); return Bitmap.createScaledBitmap(bitmap, mat.width(), mat.height(), false); } catch (Exception e) { Timber.e(e); } return null; }