List of usage examples for org.opencv.core Mat height
public int height()
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);//from w w w . j a va2 s.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:cv.FaceDetector.java
License:Open Source License
private BufferedImage matToBufferedImage(Mat matImage) { BufferedImage image = new BufferedImage(matImage.width(), matImage.height(), BufferedImage.TYPE_3BYTE_BGR); WritableRaster raster = image.getRaster(); DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer(); byte[] data = dataBuffer.getData(); matImage.get(0, 0, data);/*from w ww . j av a 2 s .com*/ return image; }
From source file:cv.recon.util.MatFXUtils.java
License:Open Source License
/** * Convert from OpenCV Mat to JavaFX WritableImage to be displayed in * ImageView./*w w w . j a v a2s . com*/ * @param mat Mat to be converted * @param writableImage Optional WritableImage, if non-null, the Mat will be * written in this WritableImage * @return A WritableImage to be used for JavaFX, return null if already * supplied with WritableImage */ public static WritableImage toFXImage(Mat mat, WritableImage writableImage) { int width = mat.width(); int height = mat.height(); int channels = mat.channels(); byte[] sourcePixels = new byte[width * height * channels]; mat.get(0, 0, sourcePixels); BufferedImage bufferedImage; if (mat.channels() > 1) { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); } else { bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); } final byte[] targetPixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData(); System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); if (writableImage == null) { WritableImage outputImage = SwingFXUtils.toFXImage(bufferedImage, null); return outputImage; } else { SwingFXUtils.toFXImage(bufferedImage, writableImage); return null; } }
From source file:cx.uni.jk.mms.iaip.mat.MatModel.java
License:Open Source License
/** * Loads an image from a file into this model. * //from ww w. j a va2 s. c o m * The image file type must be supported by ImageIO and must be 8 bit gray * scale due to limitations of the used methods. The image must be of even * width and even height in order to be processed by OpenCV's DCT/IDCT * methods. * * This implementation uses {@link Path} instead of {@link File} in order to * read the jar from the inside. * * @param path * @throws IllegalSizeException * @throws IOException * @throws UnsupportedImageTypeException */ public void loadImage(Path path) throws IllegalSizeException, IOException, UnsupportedImageTypeException { this.logger .fine(String.format("MatModel \"%s\" loading iamge from path %s", this.getName(), path.toString())); Mat matRead = null; matRead = this.loadAndDecodeImageWithJavaImageIO(path); // matRead = loadImageWithJavaImageIOAndDecodeWithOpenCV(path); // matRead = loadImageWithOpenCV(path); this.logger.finer("image type = " + matRead.type()); this.logger.finer("image channels = " + matRead.channels()); this.logger.finer("image depth = " + matRead.depth()); /** images must have size larger than 0x0 */ if (matRead.width() <= 0 || matRead.height() <= 0) { throw new IllegalSizeException("Image must have width and height > 0."); } /** dct images must have odd width or height */ if (matRead.width() % 2 == 1 || matRead.height() % 2 == 1) { throw new IllegalSizeException("Image must have even width and even height to perform DCT/IDCT."); } /** we need a float mat to do DCT/IDCT */ this.mat = matRead; // just a reference this.logger.finer("convert to internal format"); this.mat.convertTo(this.mat, MAT_TYPE); this.logger.finer("image type = " + this.mat.type()); this.logger.finer("image channels = " + this.mat.channels()); this.logger.finer("image depth = " + this.mat.depth()); /** remember last file loaded successfully */ this.lastPath = path; }
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.// w w w . j a va 2 s . co 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; }
From source file:depthDataFromStereoCamsOpenCV.ProcessImages.java
/** * Trims image by trimSize// w ww .j a v a 2 s .co m * @param image * @param trimSize * @return */ public static Mat cropImageHorizontal(Mat image, int trimSize) { // System.out.println("Initial image width "+image.width()); // System.out.println("Initial image height "+image.height()); Rect roi = new Rect(2 * trimSize, 0, image.width() - 4 * trimSize, image.height()); Mat result = image.submat(roi); // System.out.println("Trimmed image width "+ result.width()); // System.out.println("Trimmed image height "+result.height()); // displayImage(ProcessImages.Mat2BufferedImage(result),"Cropped Image"); return result; }
From source file:digitalassistant.Panel.java
public void capture() { try {/*www .ja v a 2s . co m*/ System.out.println("Hello, OpenCV"); // Load the native library. //System.loadLibrary("opencv_java244"); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); Thread.sleep(1000); camera.open(0); //Useless if (!camera.isOpened()) { System.out.println("Camera Error"); } else { System.out.println("Camera OK?"); } Mat frame = new Mat(); // camera.grab(); //System.out.println("Frame Grabbed"); // camera.retrieve(frame); //System.out.println("Frame Decoded"); System.out.println("Frame Obtained"); /* No difference camera.release(); */ System.out.println("Captured Frame Width " + frame.width()); JFrame frame1 = new JFrame("BasicPanel"); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // DetectFace f = new DetectFace(); int count = 15; while (count > 0) { camera.read(frame); //f.face_detect(frame); frame1.setSize(frame.width(), frame.height()); // Core.putText(frame,count+"", new Point(frame.width()/4,frame.height()/4), 3, 2,new Scalar(0, 255, 0),3); // Core.rectangle(frame, new Point(frame.width()/4,frame.height()/4), new Point(frame.width()/4+300,frame.height()/4 +300), new Scalar(0, 255, 0)); Panel panel1 = new Panel(frame); frame1.setContentPane(panel1); frame1.setVisible(true); Thread.sleep(100); count--; } // camera.read(frame); // Highgui.imwrite("camera.jpg", frame); // frame1.setVisible(false); // System.out.println("OK"); } catch (Exception e) { System.out.println(e); } }
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 .imread("D:\\OpenCV_Library\\resources\\Scan_Img\\image\\06-12-2015\\sc-03-145B.jpg"); if (null == inputImageMat) { System.out.println("[INPUT IMAGE NULL]"); }/*from w ww . j ava2 s .c om*/ 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.sust.cse.analysis.news.NewsAnalysis.java
private static int getLineHeight(Mat subMat) { int lineHeight = 0; float width = subMat.width(); float height = subMat.height(); if (height < 5 || width < 5) { return lineHeight; }/*from ww w .j a v a 2s. c o m*/ int start = -1, end = -1, biggest = -1; // String blacks= ""; for (int i = 0; i < height; i++) { int white = 0; for (int j = 0; j < width; j++) { if (subMat.get(i, j)[0] <= 140) { white++; if (start == -1) { start = i; } // blacks +="1"; // break; } else { // blacks +="0"; } } // blacks += "\n"; // if(white==0){ // for(int j=0; j<width; j++){ // double[] data = subMat.get(i, j); // if(data != null){ // data[0] = 0.0; // subMat.put(i, j, data); // } // } // } // if(biggest < white){ // biggest = white; // } // System.out.println(blacks); if (white == 0 && start != -1) { if ((i - 1 - start) < 5) { lineHeight = i - 1 - start; start = -1; continue; } if (end == -1) { end = i - 1; } lineHeight = end - start; break; } if (i == height - 1 && end == -1) { end = i; lineHeight = end - start; } } // System.out.println("start: "+start); // System.out.println("end: "+end); // if(lineHeight == 50){ // filewrile(blacks); // filewrile("\n\n\n\n\n\n\n\n"); // } return lineHeight; // Read image as before // Mat rgba = subMat.clone(); //// 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); // // return largestBlackBatch1(gray)[1]; }
From source file:edu.sust.cse.util.Histogram.java
public static Mat getHistogram(Mat image) { try {/*from w ww .j av a 2 s. c om*/ Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2); Imgproc.cvtColor(image, src, Imgproc.COLOR_RGB2GRAY); ArrayList<Mat> bgr_planes = new ArrayList<>(); Core.split(src, bgr_planes); MatOfInt histSize = new MatOfInt(256); final MatOfFloat histRange = new MatOfFloat(0f, 256f); boolean accumulate = false; Mat b_hist = new Mat(); Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), b_hist, histSize, histRange, accumulate); int hist_w = 512; int hist_h = 600; long bin_w; bin_w = Math.round((double) (hist_w / 256)); Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC1); Core.normalize(b_hist, b_hist, 3, histImage.rows(), Core.NORM_MINMAX); for (int i = 1; i < 256; i++) { Core.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(b_hist.get(i - 1, 0)[0])), new Point(bin_w * (i), hist_h - Math.round(Math.round(b_hist.get(i, 0)[0]))), new Scalar(255, 0, 0), 2, 8, 0); } return histImage; } catch (Exception ex) { System.out.println("[HISTOGRAM][ERROR][" + ex.getMessage() + "]"); return null; } }