List of usage examples for org.opencv.core Mat get
public double[] get(int row, int col)
From source file:OCV_GetRotationMatrix2D.java
License:Open Source License
@Override public void run(ImageProcessor ip) { Mat mat = Imgproc.getRotationMatrix2D(new Point(center_x, center_y), angle, scale); if (mat == null || mat.rows() <= 0 || mat.cols() <= 0) { IJ.showMessage("Output is null or error"); return;/*w w w . ja va 2 s . c o m*/ } ResultsTable rt = OCV__LoadLibrary.GetResultsTable(true); rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat.get(0, 0)[0])); rt.addValue("Column02", String.valueOf(mat.get(0, 1)[0])); rt.addValue("Column03", String.valueOf(mat.get(0, 2)[0])); rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat.get(1, 0)[0])); rt.addValue("Column02", String.valueOf(mat.get(1, 1)[0])); rt.addValue("Column03", String.valueOf(mat.get(1, 2)[0])); rt.show("Results"); }
From source file:OCV_InvertAffineTransform.java
License:Open Source License
@Override public void run(ImageProcessor ip) { if (isPerspective) { Mat mat_src = new Mat(3, 3, CvType.CV_64FC1); Mat mat_dst = new Mat(3, 3, CvType.CV_64FC1); mat_src.put(0, 0, new double[] { Double.valueOf(rt.getStringValue(0, 0).replaceAll("\"|'", "")) }); mat_src.put(0, 1, new double[] { Double.valueOf(rt.getStringValue(1, 0).replaceAll("\"|'", "")) }); mat_src.put(0, 2, new double[] { Double.valueOf(rt.getStringValue(2, 0).replaceAll("\"|'", "")) }); mat_src.put(1, 0, new double[] { Double.valueOf(rt.getStringValue(0, 1).replaceAll("\"|'", "")) }); mat_src.put(1, 1, new double[] { Double.valueOf(rt.getStringValue(1, 1).replaceAll("\"|'", "")) }); mat_src.put(1, 2, new double[] { Double.valueOf(rt.getStringValue(2, 1).replaceAll("\"|'", "")) }); mat_src.put(2, 0, new double[] { Double.valueOf(rt.getStringValue(0, 2).replaceAll("\"|'", "")) }); mat_src.put(2, 1, new double[] { Double.valueOf(rt.getStringValue(1, 2).replaceAll("\"|'", "")) }); mat_src.put(2, 2, new double[] { Double.valueOf(rt.getStringValue(2, 2).replaceAll("\"|'", "")) }); Imgproc.invertAffineTransform(mat_src, mat_dst); rt.reset();/*from w ww. jav a2 s . c o m*/ rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat_dst.get(0, 0)[0])); rt.addValue("Column02", String.valueOf(mat_dst.get(0, 1)[0])); rt.addValue("Column03", String.valueOf(mat_dst.get(0, 2)[0])); rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat_dst.get(1, 0)[0])); rt.addValue("Column02", String.valueOf(mat_dst.get(1, 1)[0])); rt.addValue("Column03", String.valueOf(mat_dst.get(1, 2)[0])); rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat_dst.get(2, 0)[0])); rt.addValue("Column02", String.valueOf(mat_dst.get(2, 1)[0])); rt.addValue("Column03", String.valueOf(mat_dst.get(2, 2)[0])); rt.show("Results"); } else { Mat mat_src = new Mat(2, 3, CvType.CV_64FC1); Mat mat_dst = new Mat(2, 3, CvType.CV_64FC1); mat_src.put(0, 0, new double[] { Double.valueOf(rt.getStringValue(0, 0).replaceAll("\"|'", "")) }); mat_src.put(0, 1, new double[] { Double.valueOf(rt.getStringValue(1, 0).replaceAll("\"|'", "")) }); mat_src.put(0, 2, new double[] { Double.valueOf(rt.getStringValue(2, 0).replaceAll("\"|'", "")) }); mat_src.put(1, 0, new double[] { Double.valueOf(rt.getStringValue(0, 1).replaceAll("\"|'", "")) }); mat_src.put(1, 1, new double[] { Double.valueOf(rt.getStringValue(1, 1).replaceAll("\"|'", "")) }); mat_src.put(1, 2, new double[] { Double.valueOf(rt.getStringValue(2, 1).replaceAll("\"|'", "")) }); Imgproc.invertAffineTransform(mat_src, mat_dst); rt.reset(); rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat_dst.get(0, 0)[0])); rt.addValue("Column02", String.valueOf(mat_dst.get(0, 1)[0])); rt.addValue("Column03", String.valueOf(mat_dst.get(0, 2)[0])); rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat_dst.get(1, 0)[0])); rt.addValue("Column02", String.valueOf(mat_dst.get(1, 1)[0])); rt.addValue("Column03", String.valueOf(mat_dst.get(1, 2)[0])); rt.show("Results"); } }
From source file:Questao2.java
void ruidoGaussiano() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat original_Bgr = image.clone(); // cria uma imagem e inicializa com valores aleatorios Mat mGaussian_noise = new Mat(original_Bgr.size(), original_Bgr.type()); System.out.print("Valor Principal: "); int mean = in.nextInt(); System.out.print("Desvio Padro: "); int desv = in.nextInt(); // randn(matriz destino, mean value, desvio padrao) randn(mGaussian_noise, mean, desv);/* w w w .j ava 2 s . c om*/ // aplicacao do ruido: original(clone) + mGaussian_noise 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(matriz entrada, matriz saida, valor minimo, valor maximo, tipo de normalizacao, tipo da imagem de saida) normalize(original_Bgr, original_Bgr, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3); // salva resultado do ruido gaussiano na imagem "gaussian.jpg" Imgcodecs.imwrite("gaussian.jpg", original_Bgr); showResult("gaussian.jpg"); }
From source file:Questao2.java
void ruidoSalPimenta() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // obtem clone da matriz original Mat saltPepper_img = image.clone();// w w w . ja v a 2 s . c o m // cria matriz para o ruido e inicializa com valor aleatorios Mat mSaltPepper_noise = new Mat(saltPepper_img.size(), saltPepper_img.type()); // randn(matriz destino, valor principal (espectativa), desvio padrao) randn(mSaltPepper_noise, 0, 255); System.out.print("Valor Mnimo: "); int min = in.nextInt(); System.out.print("Valor Mximo: "); int max = in.nextInt(); // utilizando da matriz de numeros aleatorios, verifica valores // muito baixos e os substituem por zero na matriz resultante (copia da original) // e os valores muito altos sao substituidos por 255 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(matriz entrada, matriz saida, valor minimo, valor maximo, tipo de normalizacao, tipo da imagem de saida) normalize(saltPepper_img, saltPepper_img, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3); /** * Salva imagem resultante em saltapepper.jpg */ Imgcodecs.imwrite("saltpepper.jpg", saltPepper_img); showResult("saltpepper.jpg"); }
From source file:MainBox.java
public static void main(String[] args) { try {//w w w. j a v a 2s. com int kernelSize = 9; //ao aumentar o valor a imagem fica mais embassada System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat source = Highgui.imread("D://teste_gray.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat destination = new Mat(source.rows(), source.cols(), source.type()); Mat kernel = Mat.ones(kernelSize, kernelSize, CvType.CV_32F); for (int i = 0; i < kernel.rows(); i++) { for (int j = 0; j < kernel.cols(); j++) { double[] m = kernel.get(j, j); for (int k = 0; k < m.length; k++) { m[k] = m[k] / (kernelSize * kernelSize); } kernel.put(i, j, m); } } Imgproc.filter2D(source, destination, -1, kernel); Highgui.imwrite("D://Box.jpg", destination); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } }
From source file:OctoEye.java
License:Open Source License
private void detectPupil() { // min and max pupil radius int r_min = 2; int r_max = 45; // min and max pupil diameter int d_min = 2 * r_min; int d_max = 2 * r_max; // min and max pupil area double area;//from ww w . jav a2 s. c o m double a_min = Math.PI * r_min * r_min; double a_max = Math.PI * r_max * r_max; // histogram stuff List<Mat> images; MatOfInt channels; Mat mask; Mat hist; MatOfInt mHistSize; MatOfFloat mRanges; // contour and circle stuff Rect rect = null; Rect rectMin; Rect rectMax; List<MatOfPoint> contours; MatOfPoint3 circles; // pupil center Point p; // ellipse test points Point v; Point r; Point s; // rect points Point tl; Point br; // pupil edge detection Vector<Point> pointsTest; Vector<Point> pointsEllipse; Vector<Point> pointsRemoved; // temporary variables double distance; double rad; double length; int x; int y; int tmp; byte buff[]; // ------------------------------------------------------------------------------------------------------------- // step 1 // blur the image to reduce noise Imgproc.medianBlur(src, tmp1, 25); // ------------------------------------------------------------------------------------------------------------- // step 2 // locate the pupil with feature detection and compute a histogram for each, // the best feature will be used as rough pupil location (rectMin) int score = 0; int winner = 0; // feature detection MatOfKeyPoint matOfKeyPoints = new MatOfKeyPoint(); FeatureDetector blobDetector = FeatureDetector.create(FeatureDetector.MSER); // Maximal Stable Extremal Regions blobDetector.detect(tmp1, matOfKeyPoints); List<KeyPoint> keyPoints = matOfKeyPoints.toList(); // histogram calculation for (int i = 0; i < keyPoints.size(); i++) { x = (int) keyPoints.get(i).pt.x; y = (int) keyPoints.get(i).pt.y; tl = new Point(x - 5 >= 0 ? x - 5 : 0, y - 5 >= 0 ? y - 5 : 0); br = new Point(x + 5 < WIDTH ? x + 5 : WIDTH - 1, y + 5 < HEIGHT ? y + 5 : HEIGHT - 1); images = new ArrayList<Mat>(); images.add(tmp1.submat(new Rect(tl, br))); channels = new MatOfInt(0); mask = new Mat(); hist = new Mat(); mHistSize = new MatOfInt(256); mRanges = new MatOfFloat(0f, 256f); Imgproc.calcHist(images, channels, mask, hist, mHistSize, mRanges); tmp = 0; for (int j = 0; j < 256 / 3; j++) { tmp += (256 / 3 - j) * (int) hist.get(j, 0)[0]; } if (tmp >= score) { score = tmp; winner = i; rect = new Rect(tl, br); } if (debug) { // show features (orange) Core.circle(dbg, new Point(x, y), 3, ORANGE); } } if (rect == null) { return; } rectMin = rect.clone(); if (debug) { // show rectMin (red) Core.rectangle(dbg, rectMin.tl(), rect.br(), RED, 1); } // ------------------------------------------------------------------------------------------------------------- // step 3 // compute a rectMax (blue) which is larger than the pupil int margin = 32; rect.x = rect.x - margin; rect.y = rect.y - margin; rect.width = rect.width + 2 * margin; rect.height = rect.height + 2 * margin; rectMax = rect.clone(); if (debug) { // show features (orange) Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE); } // ------------------------------------------------------------------------------------------------------------- // step 4 // blur the image again Imgproc.medianBlur(src, tmp1, 7); Imgproc.medianBlur(tmp1, tmp1, 3); Imgproc.medianBlur(tmp1, tmp1, 3); Imgproc.medianBlur(tmp1, tmp1, 3); // ------------------------------------------------------------------------------------------------------------- // step 5 // detect edges Imgproc.Canny(tmp1, tmp2, 40, 50); // ------------------------------------------------------------------------------------------------------------- // step 6 // from pupil center to maxRect borders, find all edge points, compute a first ellipse p = new Point(rectMin.x + rectMin.width / 2, rectMin.y + rectMin.height / 2); pointsTest = new Vector<Point>(); pointsEllipse = new Vector<Point>(); pointsRemoved = new Vector<Point>(); buff = new byte[tmp2.rows() * tmp2.cols()]; tmp2.get(0, 0, buff); length = Math.min(p.x - rectMax.x - 3, p.y - rectMax.y - 3); length = Math.sqrt(2 * Math.pow(length, 2)); Point z = new Point(p.x, p.y - length); for (int i = 0; i < 360; i += 15) { rad = Math.toRadians(i); x = (int) (p.x + Math.cos(rad) * (z.x - p.x) - Math.sin(rad) * (z.y - p.y)); y = (int) (p.y + Math.sin(rad) * (z.x - p.x) - Math.cos(rad) * (z.y - p.y)); pointsTest.add(new Point(x, y)); } if (debug) { for (int i = 0; i < pointsTest.size(); i++) { Core.line(dbg, p, pointsTest.get(i), GRAY, 1); Core.rectangle(dbg, rectMin.tl(), rectMin.br(), GREEN, 1); Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE, 1); } Core.rectangle(dbg, rectMin.tl(), rectMin.br(), BLACK, -1); Core.rectangle(dbg, rectMin.tl(), rectMin.br(), RED, 1); Core.rectangle(dbg, rectMax.tl(), rectMax.br(), BLUE); } // p: Ursprung ("Mittelpunkt" der Ellipse) // v: Zielpunkt (Testpunkt) // r: Richtungsvektor PV for (int i = 0; i < pointsTest.size(); i++) { v = new Point(pointsTest.get(i).x, pointsTest.get(i).y); r = new Point(v.x - p.x, v.y - p.y); length = Math.sqrt(Math.pow(p.x - v.x, 2) + Math.pow(p.y - v.y, 2)); boolean found = false; for (int j = 0; j < Math.round(length); j++) { s = new Point(Math.rint(p.x + (double) j / length * r.x), Math.rint(p.y + (double) j / length * r.y)); s.x = Math.max(1, Math.min(s.x, WIDTH - 2)); s.y = Math.max(1, Math.min(s.y, HEIGHT - 2)); tl = new Point(s.x - 1, s.y - 1); br = new Point(s.x + 1, s.y + 1); buff = new byte[3 * 3]; rect = new Rect(tl, br); try { (tmp2.submat(rect)).get(0, 0, buff); for (int k = 0; k < 3 * 3; k++) { if (Math.abs(buff[k]) == 1) { pointsEllipse.add(s); found = true; break; } } } catch (Exception e) { break; } if (found) { break; } } } double e_min = Double.POSITIVE_INFINITY; double e_max = 0; double e_med = 0; for (int i = 0; i < pointsEllipse.size(); i++) { v = pointsEllipse.get(i); length = Math.sqrt(Math.pow(p.x - v.x, 2) + Math.pow(p.y - v.y, 2)); e_min = (length < e_min) ? length : e_min; e_max = (length > e_max) ? length : e_max; e_med = e_med + length; } e_med = e_med / pointsEllipse.size(); if (pointsEllipse.size() >= 5) { Point[] points1 = new Point[pointsEllipse.size()]; for (int i = 0; i < pointsEllipse.size(); i++) { points1[i] = pointsEllipse.get(i); } MatOfPoint2f points2 = new MatOfPoint2f(); points2.fromArray(points1); pupil = Imgproc.fitEllipse(points2); } if (pupil.center.x == 0 && pupil.center.y == 0) { // something went wrong, return null reset(); return; } if (debug) { Core.ellipse(dbg, pupil, PURPLE, 2); } // ------------------------------------------------------------------------------------------------------------- // step 7 // remove some outlier points and compute the ellipse again try { for (int i = 1; i <= 4; i++) { distance = 0; int remove = 0; for (int j = pointsEllipse.size() - 1; j >= 0; j--) { v = pointsEllipse.get(j); length = Math.sqrt(Math.pow(v.x - pupil.center.x, 2) + Math.pow(v.y - pupil.center.y, 2)); if (length > distance) { distance = length; remove = j; } } v = pointsEllipse.get(remove); pointsEllipse.removeElementAt(remove); pointsRemoved.add(v); } } catch (Exception e) { // something went wrong, return null reset(); return; } if (pointsEllipse.size() >= 5) { Point[] points1 = new Point[pointsEllipse.size()]; for (int i = 0; i < pointsEllipse.size(); i++) { points1[i] = pointsEllipse.get(i); } MatOfPoint2f points2 = new MatOfPoint2f(); points2.fromArray(points1); pupil = Imgproc.fitEllipse(points2); Point[] vertices = new Point[4]; pupil.points(vertices); double d1 = Math .sqrt(Math.pow(vertices[1].x - vertices[0].x, 2) + Math.pow(vertices[1].y - vertices[0].y, 2)); double d2 = Math .sqrt(Math.pow(vertices[2].x - vertices[1].x, 2) + Math.pow(vertices[2].y - vertices[1].y, 2)); if (d1 >= d2) { pupilMajorAxis = (int) (d1 / 2); pupilMinorAxis = (int) (d2 / 2); axisA = new Point(vertices[1].x + (vertices[2].x - vertices[1].x) / 2, vertices[1].y + (vertices[2].y - vertices[1].y) / 2); axisB = new Point(vertices[0].x + (vertices[1].x - vertices[0].x) / 2, vertices[0].y + (vertices[1].y - vertices[0].y) / 2); } else { pupilMajorAxis = (int) (d2 / 2); pupilMinorAxis = (int) (d1 / 2); axisB = new Point(vertices[1].x + (vertices[2].x - vertices[1].x) / 2, vertices[1].y + (vertices[2].y - vertices[1].y) / 2); axisA = new Point(vertices[0].x + (vertices[1].x - vertices[0].x) / 2, vertices[0].y + (vertices[1].y - vertices[0].y) / 2); } } double ratio = (double) pupilMinorAxis / (double) pupilMajorAxis; if (ratio < 0.75 || 2 * pupilMinorAxis <= d_min || 2 * pupilMajorAxis >= d_max) { // something went wrong, return null reset(); return; } // pupil found if (debug) { Core.ellipse(dbg, pupil, GREEN, 2); Core.line(dbg, pupil.center, axisA, RED, 2); Core.line(dbg, pupil.center, axisB, BLUE, 2); Core.circle(dbg, pupil.center, 1, GREEN, 0); x = 5; y = 5; Core.rectangle(dbg, new Point(x, y), new Point(x + 80 + 4, y + 10), BLACK, -1); Core.rectangle(dbg, new Point(x + 2, y + 2), new Point(x + 2 + pupilMajorAxis, y + 4), RED, -1); Core.rectangle(dbg, new Point(x + 2, y + 6), new Point(x + 2 + pupilMinorAxis, y + 8), BLUE, -1); for (int i = pointsEllipse.size() - 1; i >= 0; i--) { Core.circle(dbg, pointsEllipse.get(i), 2, ORANGE, -1); } for (int i = pointsRemoved.size() - 1; i >= 0; i--) { Core.circle(dbg, pointsRemoved.get(i), 2, PURPLE, -1); } } Core.ellipse(dst, pupil, GREEN, 2); Core.circle(dst, pupil.center, 1, GREEN, 0); }
From source file:OCV_GetPerspectiveTransform.java
License:Open Source License
@Override public void run(ImageProcessor ip) { MatOfPoint2f matPt_src = new MatOfPoint2f(); MatOfPoint2f matPt_dst = new MatOfPoint2f(); matPt_src.fromList(lstPt_src);/*from w w w . ja v a2 s .c o m*/ matPt_dst.fromList(lstPt_dst); Mat mat = Imgproc.getPerspectiveTransform(matPt_src, matPt_dst); if (mat == null || mat.rows() <= 0 || mat.cols() <= 0) { IJ.showMessage("Output is null or error"); return; } ResultsTable rt = OCV__LoadLibrary.GetResultsTable(true); for (int i = 0; i < 3; i++) { rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat.get(i, 0)[0])); rt.addValue("Column02", String.valueOf(mat.get(i, 1)[0])); rt.addValue("Column03", String.valueOf(mat.get(i, 2)[0])); } rt.show("Results"); }
From source file:OCV_GetAffineTransform.java
License:Open Source License
@Override public void run(ImageProcessor ip) { MatOfPoint2f matPt_src = new MatOfPoint2f(); MatOfPoint2f matPt_dst = new MatOfPoint2f(); matPt_src.fromList(lstPt_src);/*from w w w. j av a 2 s.co m*/ matPt_dst.fromList(lstPt_dst); Mat mat = Imgproc.getAffineTransform(matPt_src, matPt_dst); if (mat == null || mat.rows() <= 0 || mat.cols() <= 0) { IJ.showMessage("Output is null or error"); return; } ResultsTable rt = OCV__LoadLibrary.GetResultsTable(true); for (int i = 0; i < 2; i++) { rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat.get(i, 0)[0])); rt.addValue("Column02", String.valueOf(mat.get(i, 1)[0])); rt.addValue("Column03", String.valueOf(mat.get(i, 2)[0])); } rt.show("Results"); }
From source file:KoImgProc.java
License:Open Source License
/** * Detects stones in the given image and returns an array list of KoCircle objects. * @param grey greyscale Mat of the board. * @param threshold threshold value for HoughCircles * @return an ArrayList of KoCircle objects. *///www . ja va 2s . c o m private static ArrayList<KoCircle> detectStones(Mat grey, int minRadius, int maxRadius, int minDist, int threshold) { Mat stones = new Mat(); ArrayList<KoCircle> highThresholdStones = new ArrayList<KoCircle>(); Imgproc.HoughCircles(grey, stones, Imgproc.CV_HOUGH_GRADIENT, DP, minDist, threshold, threshold / 2, minRadius, maxRadius); for (int i = 0; i < stones.cols(); i++) { highThresholdStones.add(new KoCircle(stones.get(0, i))); } System.out.println(highThresholdStones.size() + " stones detected. minRadius: " + minRadius + "\tmaxRadius: " + maxRadius + "\tminDist: " + minDist + "\tthreshold: " + threshold); return highThresholdStones; }
From source file:KoImgProc.java
License:Open Source License
public static double getAverageGreyscaleColor(int x, int y, int radius, Mat grey) { // TODO what if x/y +/- offset is outside the range of the Mat? Throws NullPointerException double sum = 0; int offset = radius / 5; sum += grey.get(y, x)[0]; sum += grey.get(y, x + offset)[0];/* w w w .ja va 2 s .c o m*/ sum += grey.get(y, x - offset)[0]; sum += grey.get(y + offset, x)[0]; sum += grey.get(y - offset, x)[0]; return sum / 5; }