List of usage examples for org.opencv.core Core inRange
public static void inRange(Mat src, Scalar lowerb, Scalar upperb, Mat dst)
From source file:abc.RomanCharacterPicture.java
public int evaluatePicture() { try {//www. j a va 2 s . c o m ITesseract instance = new Tesseract(); MatToBufImg webcamImageBuff = new MatToBufImg(); webcamImageBuff.setMatrix(webcam_image, ".jpg"); double heightRatio = (double) webcamImageBuff.getBufferedImage().getHeight() / (double) webcam_image.height(); double widthRatio = (double) webcamImageBuff.getBufferedImage().getWidth() / (double) webcam_image.width(); int x1 = this.leftRectangle.getxPos(); int y1 = this.leftRectangle.getyPos(); int x2 = this.rightRectangle.getxPos(); int y2 = this.rightRectangle.getyPos(); Rect rect = new Rect(leftRectangle.getxPos(), leftRectangle.getyPos(), (rightRectangle.getxPos() - leftRectangle.getxPos()), (rightRectangle.getyPos() - leftRectangle.getyPos())); //Rect rect = new Rect(new Point(leftRectangle.getxPos(), leftRectangle.getyPos()), new Point(leftRectangle.getxPos(), rightRectangle.getyPos()), , (rightRectangle.getxPos()-leftRectangle.getxPos())); Mat subImageMat = webcam_image.submat(rect); BufferedImage romanCharacter = webcamImageBuff.getBufferedImage().getSubimage((int) (x1 * widthRatio), (int) (y1 * heightRatio), (int) (widthRatio * (x2 - x1)), (int) (heightRatio * (y2 - y1))); //int[] pixels = ((DataBufferInt) romanCharacter.getRaster().getDataBuffer()).getData(); //Mat subImageMat = new Mat(romanCharacter.getHeight(), romanCharacter.getWidth(), CvType.CV_8UC3); //subImageMat.put(0, 0, pixels); Mat hsv_image = new Mat(); Imgproc.cvtColor(subImageMat, hsv_image, Imgproc.COLOR_BGR2HSV); Mat lower_black_hue_range = new Mat(); Mat upper_black_hue_range = new Mat(); Core.inRange(hsv_image, new Scalar(0, 0, 0), new Scalar(180, 255, 30), lower_black_hue_range); Core.inRange(hsv_image, new Scalar(0, 0, 20), new Scalar(180, 255, 40), upper_black_hue_range); Mat black_hue_image = new Mat(); Core.addWeighted(lower_black_hue_range, 1.0, upper_black_hue_range, 1.0, 0.0, black_hue_image); Imgproc.GaussianBlur(black_hue_image, black_hue_image, new Size(9, 9), 2, 2); MatToBufImg blackImageBuff = new MatToBufImg(); blackImageBuff.setMatrix(black_hue_image, ".jpg"); BufferedImage test = blackImageBuff.getBufferedImage(); //ImageIO.write(test, "PNG", new FileOutputStream((Math.round(Math.random()*1000))+"dst.png")); String result = instance.doOCR(test); int counterI = 0; for (int i = 0; i < result.length(); i++) { if (result.charAt(i) == 'I' || result.charAt(i) == 'l' || result.charAt(i) == '1' || result.charAt(i) == 'i' || result.charAt(i) == 'L' || result.charAt(i) == 'j' || result.charAt(i) == 'J') { counterI++; } } int counterV = 0; for (int i = 0; i < result.length(); i++) { if (result.charAt(i) == 'V' || result.charAt(i) == 'v' || result.charAt(i) == 'W' || result.charAt(i) == 'w' || result.contains("\\//")) { counterV++; } } //System.out.println("Result: "+result+ " calc:" + (counterI + (counterV * 5))); return (counterI + (counterV * 5)); } catch (Exception ex) { //System.out.println(ex.getMessage()); ex.printStackTrace(); return 0; } }
From source file:balldetection.BallDetection.java
/** * @param args the command line arguments * @throws java.io.IOException//from w w w .j av a 2 s.co m */ public static void main(String[] args) throws IOException { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CameraWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> CameraWindow cWindow = new CameraWindow(); cWindow.setVisible(true); int radius = 0; System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //intialization of matrices Mat circles = new Mat(); gray = new Mat(); hsv = new Mat(); filter = new Mat(); dst = new Mat(); camera = new VideoCapture(0); Mat frame = new Mat(); Webcam.ImagePanel panel = Webcam.createPanel(camera, "src"); Webcam.ImagePanel panel2 = Webcam.createPanel(camera, "filter"); Webcam.ImagePanel panel3 = Webcam.createPanel(camera, "dst"); while (true) { camera.read(frame); src = frame; GaussianBlur(src, src, new Size(3, 3), 2, 2); Imgproc.cvtColor(src, hsv, Imgproc.COLOR_BGR2HSV); Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY); Core.inRange(gray, new Scalar(20, 100, 100), new Scalar(30, 255, 255), gray); Core.inRange(hsv, new Scalar(cWindow.get_hLower(), cWindow.get_sLower(), cWindow.get_vLower()), new Scalar(cWindow.get_hUpper(), cWindow.get_sUpper(), cWindow.get_vUpper()), filter); Core.inRange(src, new Scalar(cWindow.get_hLower(), cWindow.get_sLower(), cWindow.get_vLower()), new Scalar(cWindow.get_hUpper(), cWindow.get_sUpper(), cWindow.get_vUpper()), dst); double[] temp = hsv.get(hsv.rows() / 2, hsv.cols() / 2); System.out.println(temp[0] + ", " + temp[1] + ", " + temp[2] + ", " + radius); //System.out.println("Current Distance from ball: " + ((2.5366*radius) - 123.02)); Imgproc.HoughCircles(filter, circles, CV_HOUGH_GRADIENT, cWindow.get_dp(), filter.rows() / 2, cWindow.get_param1(), cWindow.get_param2(), cWindow.get_minCircleSize(), cWindow.get_maxCircleSize()); for (int i = 0; i < circles.cols(); i++) { Point center = new Point(Math.round(circles.get(0, i)[0]), Math.round(circles.get(0, i)[1])); radius = (int) Math.round(circles.get(0, i)[2]); // draw the circle center Core.circle(src, center, 3, new Scalar(0, 255, 0), -1, 8, 0); // draw the circle outline Core.circle(src, center, radius, new Scalar(0, 0, 255), 3, 8, 0); //System.out.println("" + circles.get(0,0)[0] + ", " + circles.get(0,0)[1] + ", " + circles.get(0,0)[2]); } panel.updateImage(toBufferedImage(src)); panel2.updateImage(toBufferedImage(filter)); panel3.updateImage(toBufferedImage(dst)); } }
From source file:br.cefetmg.lsi.opencv.multipleObjectTracking.processing.MultipleObjectTracking.java
License:Open Source License
private Mat processImage(Mat hsvImage, Scalar hsvMin, Scalar hsvMax) { Mat thresholdedImage = new Mat(); Core.inRange(hsvImage, hsvMin, hsvMax, thresholdedImage); morphOps(thresholdedImage);//from ww w . j a v a 2 s . co m return thresholdedImage; }
From source file:classes.ObjectFinder.java
private void computeObjectHistogram() { // Converting the current fram to HSV color space Mat hsvImage = new Mat(this.objectImage.size(), CvType.CV_8UC3); System.out.println(this.objectImage); Imgproc.cvtColor(this.objectImage, hsvImage, Imgproc.COLOR_BGR2HSV); // Getting the pixels that are in te specified ranges Mat maskImage = new Mat(this.objectImage.size(), CvType.CV_8UC1); int hmin = thresholdsVector.get(0); int hmax = thresholdsVector.get(1); int smin = thresholdsVector.get(2); int smax = thresholdsVector.get(3); int vmin = thresholdsVector.get(4); int vmax = thresholdsVector.get(5); Core.inRange(hsvImage, new Scalar(hmin, smin, vmin), new Scalar(hmax, smax, vmax), maskImage); Mat hueImage = new Mat(hsvImage.size(), CvType.CV_8UC1); MatOfInt fromto = new MatOfInt(0, 0); Core.mixChannels(Arrays.asList(hsvImage), Arrays.asList(hueImage), fromto); MatOfInt sizes = new MatOfInt(16); MatOfFloat ranges = new MatOfFloat(0, 180); MatOfInt channels = new MatOfInt(0); Mat histogram = new Mat(); boolean accumulate = false; Imgproc.calcHist(Arrays.asList(hueImage), channels, maskImage, histogram, sizes, ranges, accumulate); Highgui.imwrite("histogram.png", histogram); // The resulting histogram is normalized and placed in the class variable Core.normalize(histogram, objectHistogram, 0, 255, Core.NORM_MINMAX); }
From source file:classes.ObjectFinder.java
private void backprojectObjectHistogram() { // Converting the current fram to HSV color space Mat hsvImage = new Mat(this.objectImage.size(), CvType.CV_8UC3); Imgproc.cvtColor(this.inputFrame, hsvImage, Imgproc.COLOR_BGR2HSV); // Getting the pixels that are in te specified ranges int hmin = this.thresholdsVector.get(0); int hmax = this.thresholdsVector.get(1); int smin = this.thresholdsVector.get(2); int smax = this.thresholdsVector.get(3); int vmin = this.thresholdsVector.get(4); int vmax = this.thresholdsVector.get(5); Mat maskImage = new Mat(this.objectImage.size(), CvType.CV_8UC1); Core.inRange(hsvImage, new Scalar(hmin, smin, vmin), new Scalar(hmax, smax, vmax), maskImage); // Taking the hue channel of the image Mat hueImage = new Mat(hsvImage.size(), hsvImage.depth()); MatOfInt fromto = new MatOfInt(0, 0); Core.mixChannels(Arrays.asList(hsvImage), Arrays.asList(hueImage), fromto); // Backprojecting the histogram over that hue channel image MatOfFloat ranges = new MatOfFloat(0, 180); MatOfInt channels = new MatOfInt(0); Imgproc.calcBackProject(Arrays.asList(hueImage), channels, this.objectHistogram, this.backprojectionImage, ranges, 1);// w w w . j a va 2 s . co m Core.bitwise_and(backprojectionImage, maskImage, backprojectionImage); }
From source file:com.astrocytes.core.operationsengine.CoreOperations.java
License:Open Source License
/** * Applies thresholding for color image. * * @param src - color source image./*from www . j a v a 2 s.co m*/ * @param r - the value for red value in threshold. * @param g - the value for green value in threshold. * @param b - the value for blue value in threshold. * @return thresholded color image. */ public static Mat threshold(Mat src, int r, int g, int b) { if (src.channels() < 3) return src; Mat dest = new Mat(); Mat srcBin = new Mat(); Imgproc.threshold(src, srcBin, 1, 255, Imgproc.THRESH_BINARY); Core.inRange(src, new Scalar(0), new Scalar(r, g, b), dest); dest = invert(dest); cvtColor(dest, dest, Imgproc.COLOR_GRAY2BGR); dest = xor(srcBin, dest); dest = and(src, dest); return dest; }
From source file:com.carver.paul.truesight.ImageRecognition.ImageTools.java
License:Open Source License
public static void MaskAColourFromImage(Mat image, Scalar lowerHsv, Scalar upperHsv, Mat mask) { Imgproc.cvtColor(image, mask, Imgproc.COLOR_BGR2HSV); Core.inRange(mask, lowerHsv, upperHsv, mask); }
From source file:com.example.colordetector.CamMainActivity.java
License:Apache License
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { // The frame currently captured by the camera, converted in the color RGBA rgbaFrame = inputFrame.rgba();/*from w w w .j a va 2 s . com*/ // Convert the frame in the HSV color space, to be able to identify the color with the thresholds Imgproc.cvtColor(rgbaFrame, rgbFrame, Imgproc.COLOR_RGBA2RGB); // Cant't convert directly rgba->hsv Imgproc.cvtColor(rgbFrame, hsvFrame, Imgproc.COLOR_RGB2HSV); // Create a mask with ONLY zones of the chosen color on the frame currently captured Core.inRange(hsvFrame, thresMin, thresMax, inRangeMask); filteredFrame.setTo(new Scalar(0, 0, 0)); rgbFrame.copyTo(filteredFrame, inRangeMask); // if the method of shooting image is set to manual, exit and return the filtered image... if (!methodAuto) { return filteredFrame; } //...else it was setted the automatic method, so continue with the method // Check the H channel of the image to see if the searched color is present on the frame Core.extractChannel(filteredFrame, hChannel, 0); /* There are two method to verify the color presence; below a little explanation */ /* checkRange: if almost one pixel of the searched color is found, continue with the countdown * Pro -> fast. * Versus -> less accurate, possible presence of false positive depending the quality of the camera * if(!Core.checkRange(hChannel, true, 0, 1)){ */ /* Percentage: count the pixel of the searched color, and if there are almost the * 0.1% of total pixel of the frame with the searched color, continue with the countdown * Pro: more accurate, lower risk of false positive * Versus: slower than checkRange * N.B.: the threshold percentage is imposted with a low value, otherwise small object will not be seen */ int perc = Core.countNonZero(hChannel); // Percentage if (perc > (frameDim * 0.001)) { // if the shooting method is setted to 'immediate', the photo is returned now; // otherwise continue with the countdown if (!countDown) { takePicture(); return rgbaFrame; } // 'point' is where the countdown will be visualized; in that case at // a quarter of height and width than left up angle Point point = new Point(rgbaFrame.cols() >> 2, rgbaFrame.rows() >> 2); // Update the osd countdown every 75*8 ms (if color searched is present) // Use the division in 75 ms cause a higher value would give the user the feeling of screen/app 'blocked'. if (timeToElapse % 8 == 0) { if (osdSecond.compareTo("") == 0) osdSecond = ((Integer) (timeToElapse >> 3)).toString(); else osdSecond = osdSecond.concat(".." + (((Integer) (timeToElapse >> 3)).toString())); Core.putText(rgbaFrame, osdSecond, point, 1, 3, Scalar.all(255)); } timeToElapse -= 1; // the user has framed an object for more than 3 seconds; shoot the photo if (timeToElapse <= 0) { timeToElapse = 24; takePicture(); } // the user has framed an object for less than 3 seconds; wait else { try { synchronized (this) { wait(75); } } catch (InterruptedException e) { e.printStackTrace(); } } } // the user has NOT framed a color searched object; reset osd else { timeToElapse = 24; osdSecond = ""; } return rgbaFrame; }
From source file:com.opencv.mouse.MouseMainFrame.java
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed try {/*from w w w. ja va 2 s . c o m*/ robot = new Robot(); } catch (AWTException e) { } t = new Thread() { public void run() { MatToBufImg matToBufferedImageConverter = new MatToBufImg(); //Utility class to convert Mat to Java's BufferedImage webCam = new VideoCapture(0); if (!webCam.isOpened()) { System.out.println("Kamera Ak Deil..!"); } else System.out.println("Kamera Ald --> " + webCam.toString()); Mat webcam_image = new Mat(480, 640, CvType.CV_8UC3); Mat hsv_image = new Mat(webcam_image.cols(), webcam_image.rows(), CvType.CV_8UC3); thresholded = new Mat(webcam_image.cols(), webcam_image.rows(), CvType.CV_8UC3, new Scalar(255, 255, 255)); if (webCam.isOpened()) { try { Thread.sleep(1000); } catch (InterruptedException ex) { } while (true) { try { webCam.read(webcam_image); } catch (Exception e) { System.out.println("Web Cam Kapal !"); } if (!webcam_image.empty()) { try { Thread.sleep(10); } catch (InterruptedException ex) { } // Mat inRangeResim = webcam_image.clone(); /* Mat inRangeResim = webcam_image.clone(); matToBufferedImageConverter.setMatrix(inRangeResim, ".jpg"); image =matToBufferedImageConverter.getBufferedImage(); Highgui.imwrite("D:\\bitirme.jpg", inRangeResim); */ // MatOfRect faceDetections = new MatOfRect(); Imgproc.cvtColor(webcam_image, hsv_image, Imgproc.COLOR_BGR2HSV); //siyah hsv range 0 0 0 - 180 45 100 //hsvmavi Core.inRange(webcam_image, new Scalar(75,63,40), new Scalar(118,255,255), webcam_image); //rgb mavi // Core.inRange(webcam_image, new Scalar(50,0,0), new Scalar(255,0,0), webcam_image); //turuncu hsv Core.inRange(webcam_image, new Scalar(5,50,50), new Scalar(15,255,255), webcam_image); //Core.inRange(webcam_image, new Scalar(80,50,50), new Scalar(140,255,255), webcam_image); // Core.inRange(webcam_image, new Scalar(29,0,24), new Scalar(30,155,155), webcam_image); //hsv mavi // jSliderHmin.setValue(75); // jSliderSmin.setValue(63); // jSliderVmin.setValue(40); // jSliderHmax.setValue(118); // jSliderSmax.setValue(255); // jSliderVmax.setValue(255); // // jSliderHmin.setValue(0); // jSliderSmin.setValue(0); // jSliderVmin.setValue(0); // jSliderHmax.setValue(179); // jSliderSmax.setValue(39); // jSliderVmax.setValue(120); Core.inRange(hsv_image, new Scalar(100, 97, 206), new Scalar(120, 255, 255), thresholded); Imgproc.dilate(thresholded, thresholded, element); Imgproc.erode(thresholded, thresholded, element); Imgproc.dilate(thresholded, thresholded, element); Imgproc.erode(thresholded, thresholded, element); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); Imgproc.drawContours(thresholded, contours, -1, new Scalar(255.0, 255.0, 255.0), 5); for (int i = 0; i < contours.size(); i++) { // System.out.println(Imgproc.contourArea(contours.get(i))); // if (Imgproc.contourArea(contours.get(i)) > 1 ){ Rect rect = Imgproc.boundingRect(contours.get(i)); kesit = thresholded.submat(rect); //System.out.println(rect.height); // if (rect.height > 20 && rect.height <30 && rect.width < 30 && rect.width >20){ // System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width); Core.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)); //} //} if (rect.height > 15 && rect.width > 15) { System.out.println(rect.x + "\n" + rect.y); Core.circle(webcam_image, new Point(rect.x, rect.y), i, new Scalar(0, 255, 0)); robot.mouseMove((int) (rect.x * 3), (int) (rect.y * 2.25)); } } // Imgproc.cvtColor(webcam_image, webcam_image, Imgproc.COLOR_HSV2BGR); // hsv_image.convertTo(hsv_image, CvType.CV_32F); // Imgproc.Canny(thresholded, thresholded, 10, 20); // Core.bitwise_and(thresholded, webcam_image, webcam_image); //ise yarar // Imgproc.cvtColor(thresholded, thresholded, Imgproc.COLOR_GRAY2BGR); // Core.bitwise_and(thresholded, webcam_image, webcam_image); // webcam_image.copyTo(hsv_image, thresholded); // System.out.println("<------------------------------>"); // System.out.println("BGR: " +webcam_image.channels()+" Size : "+webcam_image.size()); // System.out.println("HSV :"+hsv_image.channels()+" Size: "+hsv_image.size()); // System.out.println("Thresold :"+thresholded.channels()+" Size : "+thresholded.size()); // System.out.println("<------------------------------>"); // matToBufferedImageConverter.setMatrix(webcam_image, ".jpg"); image = matToBufferedImageConverter.getBufferedImage(); g.drawImage(image, 0, 0, webcam_image.cols(), webcam_image.rows(), null); } else { System.out.println("Grnt yok!"); break; } } // webCam.release(); } } }; threadDurum = true; t.start(); }
From source file:com.opencv.video.VideoCaptureMain.java
private void jButtonPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPlayActionPerformed Thread t = new Thread() { @Override/*ww w .j a va 2 s . co m*/ public void run() { MatToBufImg matToBufferedImageConverter = new MatToBufImg(); try { final VideoCapture videoCapture = new VideoCapture("D:\\colorTest.mp4"); // videoCapture = new VideoCapture(0); // Thread.sleep(3000); if (!videoCapture.isOpened()) { System.out.println("Video Alamad"); return; } double fps = videoCapture.get(5); System.out.println("FPS :" + fps); frame = new Mat(); Mat hsv_image = new Mat(); Mat thresholded = new Mat(); while (true) { boolean basarili = videoCapture.read(frame); if (!basarili) { System.out.println("Okunamyor"); break; } Imgproc.cvtColor(frame, hsv_image, Imgproc.COLOR_BGR2HSV); Core.inRange(hsv_image, new Scalar(170, 150, 60), new Scalar(179, 255, 255), thresholded); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); for (int i = 0; i < contours.size(); i++) { // System.out.println(Imgproc.contourArea(contours.get(i))); // if (Imgproc.contourArea(contours.get(i)) > 1 ){ Rect rect = Imgproc.boundingRect(contours.get(i)); kesit = frame.submat(rect); //System.out.println(rect.height); // if (rect.height > 20 && rect.height <30 && rect.width < 30 && rect.width >20){ // System.out.println(rect.x +","+rect.y+","+rect.height+","+rect.width); Core.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255)); // Core.circle(webcam_image, new Point(rect.x+rect.height/2, rect.y+rect.width/2), i, new Scalar(0, 0, 255)); //} //} } matToBufferedImageConverter.setMatrix(frame, ".jpg"); g.drawImage(matToBufferedImageConverter.getBufferedImage(), 0, 0, 640, 480, null); } } catch (Exception e) { System.out.println("Sorun Burda"); } } }; t.start(); }