List of usage examples for org.opencv.core Mat release
public void release()
From source file:com.davidmiguel.gobees.monitoring.algorithm.AreaBeesCounter.java
License:Open Source License
@Override public int countBees(@NonNull Mat frame) { final long t0 = System.nanoTime(); Mat r0 = blur.process(frame); Mat r1 = bs.process(r0);/*from www . j a v a2 s. c o m*/ Mat r2 = morphology.process(r1); processedFrame = cf.process(r2); r0.release(); r1.release(); r2.release(); Log.d("countBees time: %d", (System.nanoTime() - t0) / 1000000); return cf.getNumBees(); }
From source file:com.davidmiguel.gobees.monitoring.algorithm.processors.ContoursFinder.java
License:Open Source License
@Override public Mat process(@NonNull Mat frame) { if (frame.empty()) { Log.e("Invalid input frame."); return null; }/*from w ww . ja v a 2 s . com*/ Mat tmp = frame.clone(); // Finding outer contours contourList.clear(); Imgproc.findContours(tmp, contourList, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // Filter bees Mat contours = new Mat(tmp.rows(), tmp.cols(), CvType.CV_8UC3); tmp.release(); double area; Scalar color; numBees = 0; for (int i = 0; i < contourList.size(); i++) { area = Imgproc.contourArea(contourList.get(i)); if (area > minArea && area < maxArea) { color = GREEN; numBees++; } else { color = RED; } // Draw contour Imgproc.drawContours(contours, contourList, i, color, -1); } return contours; }
From source file:com.mitzuli.core.ocr.OcrPreprocessor.java
License:Open Source License
/** * Binarizes and cleans the input image for OCR, saving debugging images in the given directory. * * @param input the input image, which is recycled by this method, so the caller should make a defensive copy of it if necessary. * @param debugDir the directory to write the debugging images to, or null to disable debugging. * @return the preprocessed image.//from w w w . ja v a 2 s . c om */ static Image preprocess(final Image input, final File debugDir) { // TODO Temporary workaround to allow to manually enable debugging (the global final variable should be used) boolean DEBUG = debugDir != null; // Initialization final Mat mat = input.toGrayscaleMat(); final Mat debugMat = DEBUG ? input.toRgbMat() : null; input.recycle(); final Mat aux = new Mat(mat.size(), CvType.CV_8UC1); final Mat binary = new Mat(mat.size(), CvType.CV_8UC1); if (DEBUG) Image.fromMat(mat).write(new File(debugDir, "1_input.jpg")); // Binarize the input image in mat through adaptive Gaussian thresholding Imgproc.adaptiveThreshold(mat, binary, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 51, 13); // Imgproc.adaptiveThreshold(mat, binary, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY, 31, 7); // Edge detection Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_OPEN, KERNEL_3X3); // Open Imgproc.morphologyEx(mat, aux, Imgproc.MORPH_CLOSE, KERNEL_3X3); // Close Core.addWeighted(mat, 0.5, aux, 0.5, 0, mat); // Average Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_GRADIENT, KERNEL_3X3); // Gradient Imgproc.threshold(mat, mat, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU); // Edge map if (DEBUG) Image.fromMat(mat).write(new File(debugDir, "2_edges.jpg")); // Extract word level connected-components from the dilated edge map Imgproc.dilate(mat, mat, KERNEL_3X3); if (DEBUG) Image.fromMat(mat).write(new File(debugDir, "3_dilated_edges.jpg")); final List<MatOfPoint> wordCCs = new ArrayList<MatOfPoint>(); Imgproc.findContours(mat, wordCCs, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // Filter word level connected-components individually and calculate their average attributes final List<MatOfPoint> individuallyFilteredWordCCs = new ArrayList<MatOfPoint>(); final List<MatOfPoint> removedWordCCs = new ArrayList<MatOfPoint>(); double avgWidth = 0, avgHeight = 0, avgArea = 0; for (MatOfPoint cc : wordCCs) { final Rect boundingBox = Imgproc.boundingRect(cc); if (boundingBox.height >= 6 // bounding box height >= 6 && boundingBox.area() >= 50 // bounding box area >= 50 && (double) boundingBox.width / (double) boundingBox.height >= 0.25 // bounding box aspect ratio >= 1:4 && boundingBox.width <= 0.75 * mat.width() // bounding box width <= 0.75 image width && boundingBox.height <= 0.75 * mat.height()) // bounding box height <= 0.75 image height { individuallyFilteredWordCCs.add(cc); avgWidth += boundingBox.width; avgHeight += boundingBox.height; avgArea += boundingBox.area(); } else { if (DEBUG) removedWordCCs.add(cc); } } wordCCs.clear(); avgWidth /= individuallyFilteredWordCCs.size(); avgHeight /= individuallyFilteredWordCCs.size(); avgArea /= individuallyFilteredWordCCs.size(); if (DEBUG) { Imgproc.drawContours(debugMat, removedWordCCs, -1, BLUE, -1); removedWordCCs.clear(); } // Filter word level connected-components in relation to their average attributes final List<MatOfPoint> filteredWordCCs = new ArrayList<MatOfPoint>(); for (MatOfPoint cc : individuallyFilteredWordCCs) { final Rect boundingBox = Imgproc.boundingRect(cc); if (boundingBox.width >= 0.125 * avgWidth // bounding box width >= 0.125 average width && boundingBox.width <= 8 * avgWidth // bounding box width <= 8 average width && boundingBox.height >= 0.25 * avgHeight // bounding box height >= 0.25 average height && boundingBox.height <= 4 * avgHeight) // bounding box height <= 4 average height { filteredWordCCs.add(cc); } else { if (DEBUG) removedWordCCs.add(cc); } } individuallyFilteredWordCCs.clear(); if (DEBUG) { Imgproc.drawContours(debugMat, filteredWordCCs, -1, GREEN, -1); Imgproc.drawContours(debugMat, removedWordCCs, -1, PURPLE, -1); removedWordCCs.clear(); } // Extract paragraph level connected-components mat.setTo(BLACK); Imgproc.drawContours(mat, filteredWordCCs, -1, WHITE, -1); final List<MatOfPoint> paragraphCCs = new ArrayList<MatOfPoint>(); Imgproc.morphologyEx(mat, aux, Imgproc.MORPH_CLOSE, KERNEL_30X30); Imgproc.findContours(aux, paragraphCCs, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // Filter paragraph level connected-components according to the word level connected-components inside final List<MatOfPoint> textCCs = new ArrayList<MatOfPoint>(); for (MatOfPoint paragraphCC : paragraphCCs) { final List<MatOfPoint> wordCCsInParagraphCC = new ArrayList<MatOfPoint>(); aux.setTo(BLACK); Imgproc.drawContours(aux, Collections.singletonList(paragraphCC), -1, WHITE, -1); Core.bitwise_and(mat, aux, aux); Imgproc.findContours(aux, wordCCsInParagraphCC, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); final Rect boundingBox = Imgproc.boundingRect(paragraphCC); final double center = mat.size().width / 2; final double distToCenter = center > boundingBox.x + boundingBox.width ? center - boundingBox.x - boundingBox.width : center < boundingBox.x ? boundingBox.x - center : 0.0; if (DEBUG) { System.err.println("****************************************"); System.err.println("\tArea: " + boundingBox.area()); System.err.println("\tDistance to center: " + distToCenter); System.err.println("\tCCs inside: " + wordCCsInParagraphCC.size()); } if ((wordCCsInParagraphCC.size() >= 10 || wordCCsInParagraphCC.size() >= 0.3 * filteredWordCCs.size()) && mat.size().width / distToCenter >= 4) { textCCs.addAll(wordCCsInParagraphCC); if (DEBUG) { System.err.println("\tText: YES"); Imgproc.drawContours(debugMat, Collections.singletonList(paragraphCC), -1, DARK_GREEN, 5); } } else { if (DEBUG) { System.err.println("\tText: NO"); Imgproc.drawContours(debugMat, Collections.singletonList(paragraphCC), -1, DARK_RED, 5); } } } filteredWordCCs.clear(); paragraphCCs.clear(); mat.setTo(WHITE); Imgproc.drawContours(mat, textCCs, -1, BLACK, -1); textCCs.clear(); if (DEBUG) Image.fromMat(debugMat).write(new File(debugDir, "4_filtering.jpg")); // Obtain the final text mask from the filtered connected-components Imgproc.erode(mat, mat, KERNEL_15X15); Imgproc.morphologyEx(mat, mat, Imgproc.MORPH_OPEN, KERNEL_30X30); if (DEBUG) Image.fromMat(mat).write(new File(debugDir, "5_text_mask.jpg")); // Apply the text mask to the binarized image if (DEBUG) Image.fromMat(binary).write(new File(debugDir, "6_binary.jpg")); binary.setTo(WHITE, mat); if (DEBUG) Image.fromMat(binary).write(new File(debugDir, "7_binary_text.jpg")); // Dewarp the text using Leptonica Pix pixs = Image.fromMat(binary).toGrayscalePix(); Pix pixsDewarp = Dewarp.dewarp(pixs, 0, Dewarp.DEFAULT_SAMPLING, 5, true); final Image result = Image.fromGrayscalePix(pixsDewarp); if (DEBUG) result.write(new File(debugDir, "8_dewarp.jpg")); // Clean up pixs.recycle(); mat.release(); aux.release(); binary.release(); if (debugMat != null) debugMat.release(); return result; }
From source file:com.oetermann.imageclassifier.ImageClassifier.java
License:Open Source License
public int match(String matcherName, Mat queryImage, int minMatches) { if (!flannMatchers.containsKey(matcherName)) { return UNKOWN_MATCHER; }/*from w w w.ja v a2 s . com*/ Imgproc.equalizeHist(queryImage, queryImage); // long t = System.currentTimeMillis(); Mat queryDescriptors = descriptorExtractorWrapper.detectAndCompute(queryImage); // System.out.println("SURF: "+(System.currentTimeMillis()-t)); // t = System.currentTimeMillis(); int match = flannMatchers.get(matcherName).bestMatch(queryDescriptors, minMatches); queryDescriptors.release(); // System.out.println("FLANN: "+(System.currentTimeMillis()-t)); return match; }
From source file:com.wallerlab.compcellscope.calcDPCTask.java
License:BSD License
protected Long doInBackground(Mat... matrix_list) { //int count = urls.length; Mat in1 = matrix_list[0];//w w w .j a v a 2 s. c om Mat in2 = matrix_list[1]; Mat outputMat = matrix_list[2]; Mat Mat1 = new Mat(in1.width(), in1.height(), in1.type()); Mat Mat2 = new Mat(in2.width(), in2.height(), in2.type()); in1.copyTo(Mat1); in2.copyTo(Mat2); Imgproc.cvtColor(Mat1, Mat1, Imgproc.COLOR_RGBA2GRAY, 1); Imgproc.cvtColor(Mat2, Mat2, Imgproc.COLOR_RGBA2GRAY, 1); Mat output = new Mat(Mat1.width(), Mat1.height(), CvType.CV_8UC4); Mat dpcSum = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); Mat dpcDifference = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); Mat dpcImgF = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); /* Log.d(TAG,String.format("Mat1 format is %.1f-%.1f, type: %d",Mat1.size().width,Mat1.size().height,Mat1.type())); Log.d(TAG,String.format("Mat2 format is %.1f-%.1f, type: %d",Mat2.size().width,Mat2.size().height,Mat2.type())); */ // Convert to Floats Mat1.convertTo(Mat1, CvType.CV_32FC1); Mat2.convertTo(Mat2, CvType.CV_32FC1); Core.add(Mat1, Mat2, dpcSum); Core.subtract(Mat1, Mat2, dpcDifference); Core.divide(dpcDifference, dpcSum, dpcImgF); Core.add(dpcImgF, new Scalar(1.0), dpcImgF); // Normalize to 0-2.0 Core.multiply(dpcImgF, new Scalar(110), dpcImgF); // Normalize to 0-255 dpcImgF.convertTo(output, CvType.CV_8UC1); // Convert back into RGB Imgproc.cvtColor(output, output, Imgproc.COLOR_GRAY2RGBA, 4); dpcSum.release(); dpcDifference.release(); dpcImgF.release(); Mat1.release(); Mat2.release(); Mat maskedImg = Mat.zeros(output.rows(), output.cols(), CvType.CV_8UC4); int radius = maskedImg.width() / 2 + 25; Core.circle(maskedImg, new Point(maskedImg.width() / 2, maskedImg.height() / 2), radius, new Scalar(255, 255, 255), -1, 8, 0); output.copyTo(outputMat, maskedImg); output.release(); maskedImg.release(); return null; }
From source file:com.wallerlab.compcellscope.MultiModeViewActivity.java
License:BSD License
public Mat calcDPC(Mat in1, Mat in2, Mat out) { Mat Mat1 = new Mat(in1.width(), in1.height(), in1.type()); Mat Mat2 = new Mat(in2.width(), in2.height(), in2.type()); in1.copyTo(Mat1);// ww w .j a v a2s . c o m in2.copyTo(Mat2); Imgproc.cvtColor(Mat1, Mat1, Imgproc.COLOR_RGBA2GRAY, 1); Imgproc.cvtColor(Mat2, Mat2, Imgproc.COLOR_RGBA2GRAY, 1); Mat output = new Mat(Mat1.width(), Mat1.height(), CvType.CV_8UC4); Mat dpcSum = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); Mat dpcDifference = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); Mat dpcImgF = new Mat(Mat1.width(), Mat1.height(), CvType.CV_32FC1); /* Log.d(TAG,String.format("Mat1 format is %.1f-%.1f, type: %d",Mat1.size().width,Mat1.size().height,Mat1.type())); Log.d(TAG,String.format("Mat2 format is %.1f-%.1f, type: %d",Mat2.size().width,Mat2.size().height,Mat2.type())); */ // Convert to Floats Mat1.convertTo(Mat1, CvType.CV_32FC1); Mat2.convertTo(Mat2, CvType.CV_32FC1); Core.add(Mat1, Mat2, dpcSum); Core.subtract(Mat1, Mat2, dpcDifference); Core.divide(dpcDifference, dpcSum, dpcImgF); Core.add(dpcImgF, new Scalar(1.0), dpcImgF); // Normalize to 0-2.0 Core.multiply(dpcImgF, new Scalar(110), dpcImgF); // Normalize to 0-255 dpcImgF.convertTo(output, CvType.CV_8UC1); // Convert back into RGB Imgproc.cvtColor(output, output, Imgproc.COLOR_GRAY2RGBA, 4); dpcSum.release(); dpcDifference.release(); dpcImgF.release(); Mat1.release(); Mat2.release(); Mat maskedImg = Mat.zeros(output.rows(), output.cols(), CvType.CV_8UC4); int radius = maskedImg.width() / 2 + 25; Core.circle(maskedImg, new Point(maskedImg.width() / 2, maskedImg.height() / 2), radius, new Scalar(255, 255, 255), -1, 8, 0); output.copyTo(out, maskedImg); output.release(); maskedImg.release(); return out; }
From source file:com.wallerlab.compcellscope.MultiModeViewActivity.java
License:BSD License
public Mat generateMMFrame(Mat gridOut, Mat MatTL, Mat MatTR, Mat MatBL, Mat MatBR) { //gridOut = new Mat(100, 100, gridOut.type(), new Scalar(0,0,0)); Mat Mat1 = new Mat(MatTL.size(), MatTL.type()); Mat Mat2 = new Mat(MatTR.size(), MatTR.type()); Mat Mat3 = new Mat(MatBL.size(), MatBL.type()); Mat Mat4 = new Mat(MatBR.size(), MatBR.type()); // Ensure all of the mats are of the correct size since pyramid operation resizes Imgproc.resize(MatTL, MatTL, sz);/*from w w w . j av a 2 s. c om*/ Imgproc.resize(MatTR, MatTR, sz); Imgproc.resize(MatBL, MatBL, sz); Imgproc.resize(MatBR, MatBR, sz); // Downsample by 2 for 2x2 grid Imgproc.pyrDown(MatBL, Mat1); Imgproc.pyrDown(MatBR, Mat2); Imgproc.pyrDown(MatTL, Mat3); Imgproc.pyrDown(MatTR, Mat4); /* Log.d(TAG,String.format("TLRect format is %.1f-%.1f",TLRect.size().width,TLRect.size().height)); Log.d(TAG,String.format("TRRect format is %.1f-%.1f",TRRect.size().width,TRRect.size().height)); Log.d(TAG,String.format("BLRect format is %.1f-%.1f",BLRect.size().width,BLRect.size().height)); Log.d(TAG,String.format("BRRect format is %.1f-%.1f",BRRect.size().width,BRRect.size().height)); Log.d(TAG,String.format("MatTL format is %.1f-%.1f",MatTL.size().width,MatTL.size().height)); Log.d(TAG,String.format("MatTR format is %.1f-%.1f",MatTR.size().width,MatTR.size().height)); Log.d(TAG,String.format("MatBL format is %.1f-%.1f",MatBL.size().width,MatBL.size().height)); Log.d(TAG,String.format("MatBR format is %.1f-%.1f",MatBR.size().width,MatBR.size().height)); */ Core.putText(Mat1, "DPC-LR", new Point(43, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0)); Core.putText(Mat2, "DPC-TB", new Point(43, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0)); Core.putText(Mat3, "BrightField", new Point(33, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0)); Core.putText(Mat4, "DarkField", new Point(37, 40), Core.FONT_ITALIC, 1, new Scalar(255, 255, 0)); Mat1.copyTo(gridOut.submat(BLRect)); Mat2.copyTo(gridOut.submat(BRRect)); Mat3.copyTo(gridOut.submat(TLRect)); Mat4.copyTo(gridOut.submat(TRRect)); Mat1.release(); Mat2.release(); Mat3.release(); Mat4.release(); return gridOut; }
From source file:edu.sust.cse.analysis.news.NewsAnalysis.java
public static void main(String[] args) throws IOException { // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\e-01.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\e-01-145.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\e-02.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\e-03.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\e-04.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\e-05.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\sc-01.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\sc-04_resized.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Google\\Thesis Work\\Camscanner Output\\normal_output_scan0007.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Google\\Thesis Work\\Camscanner Output\\normal_output_scan0007-01.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Google\\Thesis Work\\Camscanner Output\\normal_output_scan0001-01.bmp"); // Mat inputImageMat = Highgui.imread("D:\\Google\\Thesis Work\\scan-01-dec\\scan0007-300.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Google\\Thesis Work\\scan-01-dec\\scan0007-145.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Google\\Thesis Work\\scan-01-dec\\scan0007-145.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Google\\Thesis Work\\scan-01-dec\\scan0007-96.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Google\\Thesis Work\\scan-01-dec\\scan0001-145.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Thesis-4-1\\Previous Work\\OPenCv2\\eProthomAlo Sample I-O\\e-5-12.jpg"); // Mat inputImageMat = Highgui.imread("D:\\Thesis-4-1\\Previous Work\\OPenCv2\\eProthomAlo Sample I-O\\e-6-12.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\06-12-2015\\sc-03-145.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\06-12-2015\\sc-03-145.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\06-12-2015\\sc-03-300B.jpg"); Mat inputImageMat = Highgui//from w w w .j av a 2 s . c o m .imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\06-12-2015\\sc-03-145B.jpg"); if (null == inputImageMat) { System.out.println("[INPUT IMAGE NULL]"); } Mat image = new Mat();//normal_output_scan0002.jpg double ratio = 150 / 72.0; // 4.167 System.out.println("WIDTH: " + inputImageMat.width() + " HEIGHT:" + inputImageMat.height()); int inputWidth = (int) (inputImageMat.width() * ratio); int inputHeight = (int) (inputImageMat.height() * ratio); System.out.println("WIDTH: " + inputWidth + " HEIGHT:" + inputHeight); // inputImageMat = image; // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\sc-02.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\sc-03.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\sc-04.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\data1\\sc-05.jpg"); // Mat inputImageMat = Highgui.imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\web001.png"); Debug.debugLog("[Image [Cols, Rows]: [" + inputImageMat.cols() + ", " + inputImageMat.rows() + "]]"); // imshow("Original", inputImageMat); ViewerUI.show("Original", inputImageMat, ViewableUI.SHOW_ORIGINAL); // ViewerUI.show("Original-Histogram", Histogram.getHistogram(inputImageMat), ViewableUI.SHOW_HISTOGRAM_ORIGINAL); // Do some image processing on the image and display in another window. Mat filteredImage = new Mat(); /** * We have explained some filters which main goal is to smooth an input * image. However, sometimes the filters do not only dissolve the noise, * but also smooth away the edges */ // Imgproc.bilateralFilter(inputImageMat, m2, -1, 50, 10); /*Previous line for noise filtering*/ Imgproc.bilateralFilter(inputImageMat, filteredImage, -1, 50, 10); // Imgproc.bilateralFilter(inputImageMat, filteredImage, -1, 150, 11); ViewerUI.show("Noise Filter", filteredImage, ViewableUI.SHOW_NOISE_FILTER); // ViewerUI.show("Noise Filter-Histogram", Histogram.getHistogram(filteredImage), ViewableUI.SHOW_HISTOGRAM_NOISE_FILTER); Imgproc.Canny(filteredImage, filteredImage, 10, 150); // Imgproc.bilateralFilter(filteredImage, filteredImage, -1, 50, 10); // Imgproc.threshold(filteredImage, filteredImage, 250, 300,Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C ); //Imgproc.cvtColor(m1, m1, Imgproc.COLOR_RGB2GRAY, 0); // imshow("Edge Detected", m2); ViewerUI.show("Edge Detected", filteredImage, ViewableUI.SHOW_EDGE_DETECTION); // ViewerUI.show("Edge Detected-Histogram", Histogram.getHistogram(filteredImage), ViewableUI.SHOW_HISTOGRAM_EDGE_DETECTION); Size sizeA = filteredImage.size(); System.out.println("Width: " + sizeA.width + " Height: " + sizeA.height); int width = (int) sizeA.width; int height = (int) sizeA.height; int pointLength[][][] = new int[height][width][2]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { //double[] data = m2.get(i, j); if (filteredImage.get(i, j)[0] != 0) { pointLength[i][j][0] = 0; pointLength[i][j][1] = 0; continue; } if (j != 0 && filteredImage.get(i, j - 1)[0] == 0) { pointLength[i][j][0] = pointLength[i][j - 1][0]; } else { int count = 0; for (int k = j + 1; k < width; k++) { if (filteredImage.get(i, k)[0] == 0) { count++; } else { break; } } pointLength[i][j][0] = count; } if (i != 0 && filteredImage.get(i - 1, j)[0] == 0) { pointLength[i][j][1] = pointLength[i - 1][j][1]; } else { int count = 0; for (int k = i + 1; k < height; k++) { if (filteredImage.get(k, j)[0] == 0) { count++; } else { break; } } pointLength[i][j][1] = count; } } } String temp = ""; Mat convertArea = filteredImage.clone(); int[][] blackWhite = new int[height][width]; for (int i = 0; i < height; i++) { temp = ""; for (int j = 0; j < width; j++) { if (i == 0 || j == 0 || i == height - 1 || j == width - 1) { temp = temp + "@"; blackWhite[i][j] = 1; double[] data = filteredImage.get(i, j); data[0] = 255.0; convertArea.put(i, j, data); } else if (pointLength[i][j][0] > 150 && pointLength[i][j][1] > 6) { temp = temp + "@"; blackWhite[i][j] = 1; double[] data = filteredImage.get(i, j); data[0] = 255.0; convertArea.put(i, j, data); } else if (pointLength[i][j][0] > 7 && pointLength[i][j][1] > 200) { temp = temp + "@"; blackWhite[i][j] = 1; double[] data = filteredImage.get(i, j); data[0] = 255.0; convertArea.put(i, j, data); } else { temp = temp + " "; blackWhite[i][j] = 0; double[] data = filteredImage.get(i, j); data[0] = 0.0; convertArea.put(i, j, data); } } } ViewerUI.show("Convertion", convertArea, ViewableUI.SHOW_CONVERSION); // ViewerUI.show("Convertion-Histogram", Histogram.getHistogram(convertArea), ViewableUI.SHOW_HISTOGRAM_CONVERSION); ImageDetection isImage = new ImageDetection(); HeadlineDetection isHeadline = new HeadlineDetection(); ImageBorderDetectionBFS imgBFS = new ImageBorderDetectionBFS(); ArrayList<BorderItem> borderItems = imgBFS.getBorder(blackWhite, width, height, filteredImage, inputImageMat); // Mat[] subMat = new Mat[borderItems.size()]; // for (int i = 0; i < borderItems.size(); i++) { // subMat[i] = m2.submat(borderItems.get(i).getMinX(), borderItems.get(i).getMaxX(), // borderItems.get(i).getMinY(), borderItems.get(i).getMaxY()); // if (isImage.isImage(subMat[i])) { // System.out.println("subMat" + i + " is an image"); // imshow("Image" + i, subMat[i]); // // }else if(isHeadline.isHeadLine(subMat[i])){ // System.out.println("subMat" + i + " is an Headline"); // imshow("Headline" + i, subMat[i]); // }else{ // System.out.println("subMat" + i + " is an Column"); // imshow("Column" + i, subMat[i]); // } // //imshow("subMat" + i, subMat[i]); // bw.close(); // // } boolean[] imageIndexer = new boolean[borderItems.size()]; int[] lineHeight = new int[borderItems.size()]; int highestLinheight = -1, lowestLineHeight = 10000; int totalHeight = 0, notImage = 0; for (int i = 0; i < borderItems.size(); i++) { lineHeight[i] = 0; BorderItem borderItem = borderItems.get(i); // subMat[i] = m2.submat(borderItems.get(i).getMinX(), borderItems.get(i).getMaxX(), // borderItems.get(i).getMinY(), borderItems.get(i).getMaxY()); // if (isImage.isImage(subMat[i])) { // System.out.println("subMat" + i + " is an image"); // imshow("Image" + i, subMat[i]); // imageIndexer[i] = true; // continue; // }else{ // notImage++; // imageIndexer[i] = false; // } if (borderItem.getIsImage()) { System.out.println("subMat" + i + " is an image"); // imshow("Image" + i, borderItem.getBlock()); ViewerUI.show("Image" + i, borderItem.getBlock(), ViewableUI.SHOW_IMAGE); // ViewerUI.show("Image-Histogram" + i, Histogram.getHistogram(borderItem.getBlock()), ViewableUI.SHOW_HISTOGRAM_IMAGE); imageIndexer[i] = true; continue; } else { notImage++; imageIndexer[i] = false; } // totalHeight += lineHeight[i] = getLineHeight(subMat[i]); Mat fake = new Mat(); Imgproc.cvtColor(borderItem.getBlock(), fake, Imgproc.COLOR_RGB2GRAY, 0); totalHeight += lineHeight[i] = getLineHeight(fake); fake.release(); System.out.println("line height " + i + ": " + lineHeight[i]); // imshow("" + i, borderItems.get(i).getBlock()); if (lineHeight[i] > highestLinheight) { highestLinheight = lineHeight[i]; } if (lineHeight[i] < lowestLineHeight) { lowestLineHeight = lineHeight[i]; } // if(i==7) // break; } int avgLineHeight = totalHeight / notImage; for (int i = 0; i < borderItems.size(); i++) { if (!imageIndexer[i]) { if (lineHeight[i] - lowestLineHeight > 13 && lineHeight[i] >= 45) { // imshow("Headline" + i, subMat[i]); // imshow("Headline" + i, borderItems.get(i).getBlock()); ViewerUI.show("Headline" + i, borderItems.get(i).getBlock(), ViewableUI.SHOW_HEADING); // ViewerUI.show("Headline-Histogram" + i, Histogram.getHistogram(borderItems.get(i).getBlock()), ViewableUI.SHOW_HISTOGRAM_HEADING); } else if (lineHeight[i] - lowestLineHeight > 8 && lineHeight[i] >= 21 && lineHeight[i] < 45) { // imshow("Sub Headline" + i, borderItems.get(i).getBlock()); ViewerUI.show("Sub Headline" + i, borderItems.get(i).getBlock(), ViewableUI.SHOW_SUB_HEADING); // ViewerUI.show("Sub Headline-Histogram" + i, Histogram.getHistogram(borderItems.get(i).getBlock()), ViewableUI.SHOW_HISTOGRAM_SUB_HEADING); } else { // imshow("Column" + i, subMat[i]); // imshow("Column" + i, borderItems.get(i).getBlock()); ViewerUI.show("Column" + i, borderItems.get(i).getBlock(), ViewableUI.SHOW_COLUMN); // ViewerUI.show("Column-Histogram" + i, Histogram.getHistogram(borderItems.get(i).getBlock()), ViewableUI.SHOW_HISTOGRAM_COLUMN); } } } }
From source file:edu.ucue.tfc.Modelo.VideoProcessor.java
public Image convertCvMatToImage(Mat frameToConvert) throws IOException { if (!buffer.empty()) { buffer.release();/* ww w . j a va2 s . co m*/ } try { Imgproc.resize(frameToConvert, frameToConvert, frameSize); Imgcodecs.imencode(".png", frameToConvert, buffer, params); } catch (Exception e) { System.out.println(e.getMessage()); } image = ImageIO.read(new ByteArrayInputStream(buffer.toArray())); if (!frameToConvert.empty()) { frameToConvert.release(); } return image; }
From source file:edu.ucue.tfc.Modelo.VideoProcessor.java
public void processVideo() { do {/*from w w w . j a v a2s . c o m*/ Mat tmp = new Mat(); video.read(tmp); if (!tmp.empty()) { frame = tmp.clone(); tmp.release(); if (frameCounter < (getFrameCount() / 2) - 1) { frameCounter++; if (getMinutes() > 0) { carsPerMinute = getDetectedCarsCount() / getMinutes(); } processFrame(getFrame()); } else { frameCounter = 0; finished = true; System.out.println("Reiniciando.."); setFramePos(1); } } else { System.out.println("Imagen Vaca"); frameCounter = 0; finished = true; System.out.println("Reiniciando.."); setFramePos(1); } } while (frameCounter > (getFrameCount() / 2) - 2); }