List of usage examples for org.opencv.core Mat rows
public int rows()
From source file:ctPrincipal.Ruidos.java
private String ruidoGaussiano(int mean, int desv) { Mat original_Bgr = image.clone(); Mat mGaussian_noise = new Mat(original_Bgr.size(), original_Bgr.type()); randn(mGaussian_noise, mean, desv);//from w w w .j a va 2s. c om for (int m = 0; m < original_Bgr.rows(); m++) { for (int n = 0; n < original_Bgr.cols(); n++) { double[] val = new double[3]; for (int i = 0; i < original_Bgr.get(m, n).length; i++) { val[i] = original_Bgr.get(m, n)[i] + mGaussian_noise.get(m, n)[i]; } original_Bgr.put(m, n, val); } } normalize(original_Bgr, original_Bgr, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3); Imgcodecs.imwrite("OutputImg/gaussian.jpg", original_Bgr); return "OutputImg/gaussian.jpg"; }
From source file:ctPrincipal.Ruidos.java
private String ruidoSalPimenta(int min, int max) { Mat saltPepper_img = image.clone(); Mat mSaltPepper_noise = new Mat(saltPepper_img.size(), saltPepper_img.type()); randn(mSaltPepper_noise, 0, 255);//from w w w.j a v a2 s. c o m for (int m = 0; m < saltPepper_img.rows(); m++) { for (int n = 0; n < saltPepper_img.cols(); n++) { double[] val = new double[3]; if (mSaltPepper_noise.get(m, n)[0] < min && mSaltPepper_noise.get(m, n)[1] < min && mSaltPepper_noise.get(m, n)[2] < min) { for (int i = 0; i < saltPepper_img.get(m, n).length; i++) { val[i] = 0; } saltPepper_img.put(m, n, val); } if (mSaltPepper_noise.get(m, n)[0] > max && mSaltPepper_noise.get(m, n)[1] > max && mSaltPepper_noise.get(m, n)[2] > max) { for (int i = 0; i < saltPepper_img.get(m, n).length; i++) { val[i] = 255; } saltPepper_img.put(m, n, val); } } } normalize(saltPepper_img, saltPepper_img, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3); Imgcodecs.imwrite("OutputImg/saltpepper.jpg", saltPepper_img); return "OutputImg/saltpepper.jpg"; }
From source file:cx.uni.jk.mms.iaip.filter.LogRedBlue.java
License:Open Source License
@Override public Mat convert(Mat mat) { MinMaxLocResult negativeMmlr, positiveMmlr; double min, max, alpha, beta; /** negative values to positive and log */ Mat negativeMat = mat.clone();/* w w w . jav a 2 s.c o m*/ Core.min(negativeMat, new Scalar(0.0d), negativeMat); Core.multiply(negativeMat, new Scalar(-1.0d), negativeMat); Core.add(negativeMat, new Scalar(1.0d), negativeMat); Core.log(negativeMat, negativeMat); /** positve values log */ Mat positiveMat = mat.clone(); Core.max(positiveMat, new Scalar(0.0d), positiveMat); Core.add(positiveMat, new Scalar(1.0d), positiveMat); Core.log(positiveMat, positiveMat); /** find common contrast and brightness to fit into 8 bit */ negativeMmlr = Core.minMaxLoc(negativeMat); positiveMmlr = Core.minMaxLoc(positiveMat); min = 0; max = Math.max(negativeMmlr.maxVal, positiveMmlr.maxVal); alpha = 256.0d / (max - min); beta = -min * alpha; /** conversion of both matrices to 8 bit */ negativeMat.convertTo(negativeMat, CvType.CV_8UC1, alpha, beta); positiveMat.convertTo(positiveMat, CvType.CV_8UC1, alpha, beta); /** combine both matrices into one 8 bit 3 channel rgb picture */ Mat tempMat = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC3); List<Mat> mixSrcMats = new ArrayList<>(); mixSrcMats.add(negativeMat); // 1 channel: 0 mixSrcMats.add(positiveMat); // 1 channel: 1 List<Mat> mixDstMats = new ArrayList<>(); mixDstMats.add(tempMat); // 3 channels: 0-2 MatOfInt fromToMat = new MatOfInt(0, 0 /* neg->red */, -1, 1/* * null->green */, 1, 2 /* * pos- * > * blue */); Core.mixChannels(mixSrcMats, mixDstMats, fromToMat); return tempMat; }
From source file:cx.uni.jk.mms.iaip.filter.LogYellowCyan.java
License:Open Source License
@Override public Mat convert(Mat mat) { MinMaxLocResult negativeMmlr, positiveMmlr; double min, max, alpha, beta; /** negative values to positive and log */ Mat negativeMat = mat.clone();//from ww w . j av a2 s.c om Core.min(negativeMat, new Scalar(0.0d), negativeMat); Core.multiply(negativeMat, new Scalar(-1.0d), negativeMat); Core.add(negativeMat, new Scalar(1.0d), negativeMat); Core.log(negativeMat, negativeMat); /** positve values log */ Mat positiveMat = mat.clone(); Core.max(positiveMat, new Scalar(0.0d), positiveMat); Core.add(positiveMat, new Scalar(1.0d), positiveMat); Core.log(positiveMat, positiveMat); /** find common contrast and brightness to fit into 8 bit */ negativeMmlr = Core.minMaxLoc(negativeMat); positiveMmlr = Core.minMaxLoc(positiveMat); min = 0; max = Math.max(negativeMmlr.maxVal, positiveMmlr.maxVal); alpha = 256.0d / (max - min); beta = -min * alpha; /** conversion of both matrices to 8 bit */ negativeMat.convertTo(negativeMat, CvType.CV_8UC1, alpha, beta); positiveMat.convertTo(positiveMat, CvType.CV_8UC1, alpha, beta); /** create additional mat for saturated green */ Mat brightMat = negativeMat.clone(); Core.max(negativeMat, positiveMat, brightMat); // Core.absdiff(brightMat, new Scalar(255.0d), brightMat); // Core.multiply(brightMat, new Scalar(1.0d/3.0d), brightMat); /** combine all matrices into one 8 bit 3 channel rgb picture */ Mat tempMat = new Mat(mat.rows(), mat.cols(), CvType.CV_8UC3); List<Mat> mixSrcMats = new ArrayList<>(); mixSrcMats.add(negativeMat); // 1 channel: 0 mixSrcMats.add(positiveMat); // 1 channel: 1 mixSrcMats.add(brightMat); // 1 channel: 2 List<Mat> mixDstMats = new ArrayList<>(); mixDstMats.add(tempMat); // 3 channels: 0-2 MatOfInt fromToMat = new MatOfInt(0, 0 /* neg->red */, 2, 1/* * avg->green */, 1, 2 /* * pos- * > * blue */); Core.mixChannels(mixSrcMats, mixDstMats, fromToMat); return tempMat; }
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 w w w . j av 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:de.hhn.android.licenseplatedecoder.decoder.CountryExtractor.java
/** * Constructor// w w w . ja v a2 s .c o m * @param nativeAddress input image pointer address * @param withoutStripInputAddr image pointer address in order to store the cropped image without the blue strip */ public CountryExtractor(long nativeAddress, long withoutStripInputAddr) { this.nativeInputAddr = nativeAddress; this.withoutStripInputAddr = withoutStripInputAddr; this.withoutBlueStrip = new Mat(); /** OCR ENGINE INIT */ this.baseApi = new TessBaseAPI(); this.baseApi.setDebug(true); this.baseApi.init("/sdcard/", "blueBand"); // myDir + "/tessdata/eng.traineddata" must be present this.baseApi.setVariable("tessedit_char_whitelist", "ABCDFGHIKLOPRSTZ"); this.baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_CHAR); ArrayList<Mat> countryCode = getCharacters(); StringBuffer strb = new StringBuffer(); for (Mat elem : countryCode) { Bitmap pass = Bitmap.createBitmap(elem.cols(), elem.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(elem, pass, true); baseApi.setImage(pass); String recognizedText = baseApi.getUTF8Text(); strb.append(recognizedText); baseApi.clear(); pass.recycle(); } this.result = strb.toString(); baseApi.end(); baseApi = null; countryCode = null; }
From source file:de.hhn.android.licenseplatedecoder.decoder.DecodingEngine.java
/** * Get the country code using CountryExtractor * @return the country code/* w w w . j a v a2 s . c o m*/ */ public String getCountryCode() { Mat nativeOpencv = new Mat(); org.opencv.android.Utils.bitmapToMat(inputImg, nativeOpencv); Mat res = new Mat(nativeOpencv.rows(), nativeOpencv.cols(), nativeOpencv.type()); org.opencv.imgproc.Imgproc.cvtColor(nativeOpencv, res, Imgproc.COLOR_RGBA2BGR); nativeOpencv = null; Mat withoutStrip = new Mat(); CountryExtractor ce = new CountryExtractor(res.nativeObj, withoutStrip.nativeObj); return ce.getResult(); }
From source file:de.hhn.android.licenseplatedecoder.decoder.DecodingEngine.java
/** * Get the license plate using LPSegmenter * @return the license plate//from www . java2 s . c o m */ public String getLicensePlate() { Mat nativeOpencv = new Mat(); org.opencv.android.Utils.bitmapToMat(inputImg, nativeOpencv); Mat res = new Mat(nativeOpencv.rows(), nativeOpencv.cols(), nativeOpencv.type()); org.opencv.imgproc.Imgproc.cvtColor(nativeOpencv, res, Imgproc.COLOR_RGBA2BGR); nativeOpencv = null; LPSegmenter lp = new LPSegmenter(res.nativeObj, this.countryCodeNonAutomatic); return lp.getResult(); }
From source file:de.hhn.android.licenseplatedecoder.decoder.LPSegmenter.java
/** * Constructor//from ww w . jav a2s . co m * @param inputImageAddr input image pointer address * @param countryCode input country code */ public LPSegmenter(long inputImageAddr, int countryCode) { this.nativeInputAddr = inputImageAddr; this.countryCode = countryCode; Log.d("Segmenter", "Country Code: " + countryCode); /** OCR ENGINE INIT */ this.baseApi = new TessBaseAPI(); // INITIALIZATION DEPENDING OF THE COUNTRY CODE String languageDataset; String whitelist = null; String[] datasetWhitelist = getDatasetAndWhiteList(countryCode); languageDataset = datasetWhitelist[0]; whitelist = datasetWhitelist[1]; this.baseApi.init("/sdcard/", languageDataset); // myDir + "/tessdata/eng.traineddata" must be present this.baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_CHAR); this.baseApi.setVariable("tessedit_char_whitelist", whitelist); ArrayList<Mat> segChars = getCharacters(); StringBuffer strb = new StringBuffer(); for (Mat elem : segChars) { Imgproc.cvtColor(elem, elem, Imgproc.COLOR_BGR2GRAY); Bitmap pass = Bitmap.createBitmap(elem.cols(), elem.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(elem, pass, true); baseApi.setImage(pass); String recognizedText = baseApi.getUTF8Text(); // Log or otherwise display this string... strb.append(recognizedText); baseApi.clear(); pass.recycle(); } this.result = strb.toString(); baseApi.end(); baseApi = null; segChars = null; }
From source file:de.hu_berlin.informatik.spws2014.mapever.entzerrung.CornerDetector.java
License:Open Source License
/** * Guesses the most likly corners of a distorted map within an image. * Expects OpenCV to be initialized.//from ww w. j a v a 2s . c o m * The results are already pretty good but could propably be improved * via tweaking the parameters or adding some additional line filtering * criteria(like them being kind of parallel for instance...) * * @param gray_img A grayscale image in OpenCVs Mat format. * @return An array of propable corner points in the following form: {x0,y0,x1,y1,x2,y2,x3,y3} or null on error. **/ public static Point[] guess_corners(Mat gray_img) { Mat lines = new Mat(); Imgproc.Canny(gray_img, gray_img, THRESHOLD0, THRESHOLD1, APERTURE_SIZE, false); Imgproc.HoughLinesP(gray_img, lines, RHO, THETA, HOUGH_THRESHOLD, Math.min(gray_img.cols(), gray_img.rows()) / MIN_LINE_LENGTH_FRACTION, MAX_LINE_GAP); double[][] edge_lines = filter_lines(lines, gray_img.size()); Point[] ret_val = new Point[4]; ret_val[0] = find_intercept_point(edge_lines[0], edge_lines[2]); ret_val[1] = find_intercept_point(edge_lines[0], edge_lines[3]); ret_val[2] = find_intercept_point(edge_lines[1], edge_lines[3]); ret_val[3] = find_intercept_point(edge_lines[1], edge_lines[2]); // do sanity checks and return null on invalid coordinates for (int i = 0; i < 4; i++) { // check if coordinates are outside image boundaries if (ret_val[i].x < 0 || ret_val[i].y < 0 || ret_val[i].x > gray_img.width() || ret_val[i].y > gray_img.height()) { return null; } // check if point equal to other point for (int j = i + 1; j < 4; j++) { if (ret_val[j].x == ret_val[i].x && ret_val[j].y == ret_val[i].y) { return null; } } } return ret_val; }