String inputFileName="simm_01"; String inputExtension = "jpg"; String inputDir = getCacheDir().getAbsolutePath(); // use the cache directory for i/o String outputDir = getCacheDir().getAbsolutePath(); String outputExtension = "png"; String inputFilePath = inputDir + File.separator + inputFileName + "." + inputExtension; Log.d (this.getClass().getSimpleName(), "loading " + inputFilePath + "..."); Mat image = Imgcodecs.imread(inputFilePath); Log.d (this.getClass().getSimpleName(), "width of " + inputFileName + ": " + image.width()); // if width is 0 then it did not read your image. // for the canny edge detection algorithm, play with these to see different results int threshold1 = 70; int threshold2 = 100; Mat im_canny = new Mat(); // you have to initialize output image before giving it to the Canny method Imgproc.Canny(image, im_canny, threshold1, threshold2); String cannyFilename = outputDir + File.separator + inputFileName + "_canny-" + threshold1 + "-" + threshold2 + "." + outputExtension; Log.d (this.getClass().getSimpleName(), "Writing " + cannyFilename); Imgcodecs.imwrite(cannyFilename, im_canny);
public Picture(BufferedImage bimg) { long start = new Date().getTime(); setContent(makeMat(bimg)); timeToLoad = new Date().getTime() - start; init(0, 0, getContent().width(), getContent().height()); setAttributes(); }
public Picture(Mat mat) { if (SX.isNull(mat)) { setContent(); } else { long start = new Date().getTime(); setContent(mat.clone()); timeToLoad = new Date().getTime() - start; } init(0, 0, getContent().width(), getContent().height()); setAttributes(); }
private void setContent(URL url) { setContent(); if (SX.isSet(url)) { urlImg = url; setName(getNameFromURL(urlImg)); if (urlImg != null) { long start = new Date().getTime(); String urlProto = urlImg.getProtocol(); if (urlProto.equals("file")) { File imgFile = new File(urlImg.getPath()); setContent(Imgcodecs.imread(imgFile.getAbsolutePath(), Imgcodecs.IMREAD_UNCHANGED)); } else { try { setContent(makeMat(ImageIO.read(urlImg))); } catch (IOException e) { log.error("load(): %s for %s", e.getMessage(), urlImg); } } timeToLoad = new Date().getTime() - start; if (isValid()) { setAttributes(); log.debug("getAll: loaded: (%dx%s) %s", getContent().width(), getContent().height(), urlImg); } else { log.error("getAll: not loaded: %s", urlImg); } } } }
public Picture(String fpImg) { setContent(fpImg); init(0, 0, getContent().width(), getContent().height()); }
public Picture(URL url) { setContent(url); init(0, 0, getContent().width(), getContent().height()); }
resizeFactor = Math.min(((double) getContent().width()) / resizeMinDownSample, ((double) getContent().height()) / resizeMinDownSample); resizeFactor = Math.max(1.0, resizeFactor);
int margin = ((int) target.getResizeFactor()) + 1; Rectangle rSub = new Rectangle(Math.max(0, maxLocX - margin), Math.max(0, maxLocY - margin), Math.min(target.w + 2 * margin, mBase.width()), Math.min(target.h + 2 * margin, mBase.height())); rSub = new Rectangle(0, 0, mBase.cols(), mBase.rows()).intersection(rSub);
Mat m = Highgui.imread(file.getAbsolutePath()); if (img.data) { showToast("Height: " + m.height() + " Width: " + m.width()); } else { // print error and abort execution }
Rect roi = Imgproc.boundingRect(c); Mat newMat = mRgba.submat(roi); Bitmap bmp1 = Bitmap.createBitmap(newMat.width(), newMat.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(newMat, bmp1);
public void writeImageToFile(Mat image, String filename) { File root = Environment.getExternalStorageDirectory(); File file = new File(root, filename); Highgui.imwrite(file.getAbsolutePath(), image); if (DEBUG) Log.d(TAG, "writing: " + file.getAbsolutePath() + " (" + image.width() + ", " + image.height() + ")"); }
private static Rect getSubMatRect(Mat mat, int x, int y, int w, int h, int margin) { x = Math.max(0, x - margin); y = Math.max(0, y - margin); w = Math.min(w + 2 * margin, mat.width() - x); h = Math.min(h + 2 * margin, mat.height()- y); return new Rect(x, y, w, h); }
// Read image as before Mat rgba = Highgui.imread(filePath); Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0); // Create an empty image in matching format BufferedImage gray = new BufferedImage(rgba.width(), rgba.height(), BufferedImage.TYPE_BYTE_GRAY); // Get the BufferedImage's backing array and copy the pixels directly into it byte[] data = ((DataBufferByte) gray.getRaster().getDataBuffer()).getData(); rgba.get(0, 0, data);
public BufferedImage captureDeviceScreen(int x, int y, int w, int h) { Mat matImage = captureDeviceScreenMat(x, y, w, h); BufferedImage bImage = null; if (matImage != null) { bImage = new BufferedImage(matImage.width(), matImage.height(), BufferedImage.TYPE_3BYTE_BGR); byte[] bImageData = ((DataBufferByte) bImage.getRaster().getDataBuffer()).getData(); matImage.get(0, 0, bImageData); } return bImage; }
public static ImageCV readImageWithCvException(File file) { if (!file.canRead()) { return null; } Mat img = Imgcodecs.imread(file.getPath()); if (img.width() < 1 || img.height() < 1) { throw new CvException("OpenCV cannot read " + file.getPath()); } return ImageCV.toImageCV(img); }
IplImage MatToIplImage(Mat m,int width,int heigth) { Bitmap bmp=Bitmap.createBitmap(m.width(), m.height(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(m, bmp); return BitmapToIplImage(bmp,width, heigth); }
public static void resizeImage(Mat image, double scaleFactor) { Imgproc.resize(image, image, new Size((int) (image.width() * scaleFactor), (int) (image.height() * scaleFactor))); } }
protected Bitmap processFrame(byte[] data) { Mat mYuv = new Mat( mFrameHeight + mFrameHeight/2, mFrameWidth, CvType.CV_8UC1); mYuv.put(0, 0, data); Mat mRGB = new Mat(); Imgproc.cvtColor(mYuv, mRGB, Imgproc.COLOR_YUV2RGB_NV21, 3); ProcessFrame(mRGB.getNativeObjAddr(), mRGB.width(), mRGB.height()); Utils.matToBitmap(mRGB, mBitmap); return mBitmap; }
/** * @param image the {@link Mat} to scale * @param scaleFactor the scale factor (<1 will scale down, >1 will scale up, 1 = 100%) * @return a {@link Mat} which is a version of the original image scaled by the given factor */ public Mat scaleImage(Mat image, double scaleFactor) { Mat resizedImage = new Mat(); Size sz = new Size(image.width() * scaleFactor, image.height() * scaleFactor); Imgproc.resize(image, resizedImage, sz); return resizedImage; } }