List of usage examples for org.opencv.core Scalar Scalar
public Scalar(double v0, double v1, double v2)
From source file:fuzzycv.MainFrame.java
private Mat removeBG(Mat frame) { Mat hsvImg = new Mat(); List<Mat> hsvPlanes = new ArrayList<>(); Mat thresholdImg = new Mat(); //threshold the image with the histogram average value hsvImg.create(frame.size(), CvType.CV_8U); Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV); Core.split(hsvImg, hsvPlanes);/* ww w . j a v a 2s. c o m*/ double threshValue = getHistoAvg(hsvImg, hsvPlanes.get(0)); if (inverseCheckBox.isSelected()) { Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY_INV); } else { Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY); } Imgproc.blur(thresholdImg, thresholdImg, new Size(5, 5)); // dilate to fill gaps, erode to smooth edges Imgproc.dilate(thresholdImg, thresholdImg, new Mat(), new Point(-1, 1), 6); Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-1, 1), 6); Imgproc.threshold(thresholdImg, thresholdImg, threshValue, 179.0, Imgproc.THRESH_BINARY); // create the new image Mat foreground = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255, 255, 255)); frame.copyTo(foreground, thresholdImg); return foreground; }
From source file:fuzzycv.MainFrame.java
private Mat findAndDrawCrust(Mat maskedImage, Mat frame) { List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(maskedImage, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE); //if any contour exist... if (hierarchy.size().height > 0 && hierarchy.size().width > 0) { //for each contour, display it in blue for (int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0]) { Imgproc.drawContours(frame, contours, idx, new Scalar(160, 0, 0)); }//from w w w .ja va 2 s .c o m } return frame; }
From source file:hotgoaldetection.HotGoalDetection.java
/** * @param args the command line arguments */// w w w.j a v a2 s.co m public static void main(String[] args) throws IOException { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); img = Highgui.imread("..\\Robot-26ft.png", CV_LOAD_IMAGE_COLOR); //CameraWindow cWindow = new CameraWindow(); //cWindow.setVisible(true); Webcam.ImagePanel panel = Webcam.createPanel(img, "img"); Webcam.ImagePanel panel2 = Webcam.createPanel(img, "hsv"); //Webcam.ImagePanel panel3 = Webcam.createPanel(filter, "filter"); binary = new Mat(); hsv = new Mat(); filter = new Mat(); dst = new Mat(); GaussianBlur(img, img, new Size(3, 3), 2, 2); while (true) { Imgproc.cvtColor(img, hsv, Imgproc.COLOR_BGR2HSV); //Core.inRange(img, new Scalar(120, 170, 10), new Scalar(160, 240, 40), filter); //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()), hsv); Core.inRange(hsv, new Scalar(40, 52, 64), new Scalar(97, 255, 255), binary); erode(binary, binary, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3))); double[] condensedMat = new double[binary.width()]; for (int y = 0; y < binary.size().width; y++) { double totalX = 0; for (int x = 0; x < binary.size().height; x++) { if (binary.get(x, y)[0] == 255.0) totalX++; //System.out.println(java.util.Arrays.toString(binary.get(x,y))); } condensedMat[y] = totalX; } double[] rightOfMat = Arrays.copyOfRange(condensedMat, 0, (condensedMat.length / 2) - 1); double[] leftOfMat = Arrays.copyOfRange(condensedMat, condensedMat.length / 2, condensedMat.length); double totalRight = 0, totalLeft = 0; for (double s : rightOfMat) totalRight += s; for (double s : leftOfMat) totalLeft += s; //System.out.println(totalRight + ", " + totalLeft); if (totalRight > totalLeft) direction = true; else direction = false; totalLeft = 0; totalRight = 0; panel.updateImage(toBufferedImage(img)); panel2.updateImage(toBufferedImage(binary)); // panel3.updateImage(toBufferedImage(filter)); } }
From source file:houghtransform.transform_process.MyTransform.java
@Override public Mat drawLines(Mat scr) { for (Point pt : _lines) { double r = pt.x, t = pt.y; Point pt1 = new Point(); Point pt2 = new Point(); if (t / (Math.PI / 180) >= 45 && t / (Math.PI / 180) <= 135) { pt1.x = -1000;/*from w ww.j av a 2 s . com*/ pt2.x = 1000; pt1.y = ((r - _accu_h / 2) - (-1000 - _img_w / 2) * Math.cos(t)) / Math.sin(t) + _img_h / 2; pt2.y = ((r - _accu_h / 2) - (1000 - _img_w / 2) * Math.cos(t)) / Math.sin(t) + _img_h / 2; } else { pt1.y = -1000; pt2.y = 1000; pt1.x = ((r - _accu_h / 2) - (-1000 - _img_h / 2) * Math.sin(t)) / Math.cos(t) + _img_w / 2; pt2.x = ((r - _accu_h / 2) - (1000 - _img_h / 2) * Math.sin(t)) / Math.cos(t) + _img_w / 2; } //Drawing the lines Imgproc.line(scr, pt1, pt2, new Scalar(0, 0, 255), 1); } return scr; }
From source file:houghtransform.transform_process.OpenCV.java
@Override public Mat drawLines(Mat scr) { for (int x = 1; x < lines.rows(); x++) { double[] vec = lines.get(x, 0); double rho = vec[0], theta1 = vec[1]; Point pt1 = new Point(); Point pt2 = new Point(); double a = Math.cos(theta1), b = Math.sin(theta1); double x0 = a * rho, y0 = b * rho; pt1.x = Math.round(x0 + 1000 * (-b)); pt1.y = Math.round(y0 + 1000 * (a)); pt2.x = Math.round(x0 - 1000 * (-b)); pt2.y = Math.round(y0 - 1000 * (a)); Imgproc.line(scr, pt1, pt2, new Scalar(0, 0, 255), 1); }//from w ww .ja v a2 s . co m return scr; }
From source file:hu.unideb.fksz.VideoProcessor.java
License:Open Source License
/** * Processes {@code firstFrame} and {@code secondFrame}. * @param firstFrame the first frame of a cycle. *///w w w . j av a 2 s . c om private void processFrame(Mat firstFrame) { double contourArea = 0; int position = 0; try { /** * Resizes the {@code firstFrame} to {@code frameSize}. * */ Imgproc.resize(firstFrame, firstFrame, frameSize); /** * Convert the frame in grayscale color space. */ Imgproc.cvtColor(firstFrame, firstGrayImage, Imgproc.COLOR_BGR2GRAY); /** * {@code video} reads the second frame. */ video.read(secondFrame); Imgproc.resize(secondFrame, secondFrame, frameSize); Imgproc.cvtColor(secondFrame, secondGrayImage, Imgproc.COLOR_BGR2GRAY); /** * Getting the absolute per-pixel difference of the two frames into {@code differenceOfImages}. */ Core.absdiff(firstGrayImage, secondGrayImage, differenceOfImages); Imgproc.threshold(differenceOfImages, thresholdImage, 25, 255, Imgproc.THRESH_BINARY); Imgproc.blur(thresholdImage, thresholdImage, new Size(12, 12)); Imgproc.threshold(thresholdImage, thresholdImage, 20, 255, Imgproc.THRESH_BINARY); ///// for (int i = 0; i < contours.size(); ++i) { contours.get(i).release(); } contours.clear(); /** * The horizontal line. */ Imgproc.line(firstFrame, controlPoints.get(6), controlPoints.get(7), new Scalar(255, 0, 0), Imgproc.LINE_4); Imgproc.findContours(thresholdImage, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); for (int i = 0; i < hullPoints.size(); ++i) { hullPoints.get(i).release(); } hullPoints.clear(); for (int i = 0; i < contours.size(); i++) { MatOfInt tmp = new MatOfInt(); Imgproc.convexHull(contours.get(i), tmp, false); hullPoints.add(tmp); } /** * Searches for the contour with the greatest area. */ if (contours.size() > 0) { for (int i = 0; i < contours.size(); i++) { if (Imgproc.contourArea(contours.get(i)) > contourArea) { contourArea = Imgproc.contourArea(contours.get(i)); position = i; boundingRectangle = Imgproc.boundingRect(contours.get(i)); } } } secondFrame.release(); hierarchy.release(); secondGrayImage.release(); firstGrayImage.release(); thresholdImage.release(); differenceOfImages.release(); } catch (Exception e) { logger.error(e.getMessage()); } /** * Checking whether the control point on the left is * inside of {@code boundingRectangle}, which is a {@code Rect}, * bounding the greatest contour. */ if (controlPoints.get(6).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(0), controlPoints.get(1), new Scalar(0, 0, 255), 2); wasAtLeftPoint = true; } else if (!controlPoints.get(6).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(0), controlPoints.get(1), new Scalar(0, 255, 0), 2); } /** * Checking whether the control point on the middle is * inside of {@code boundingRectangle}, which is a {@code Rect}, * bounding the greatest contour. */ if (controlPoints.get(8).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(2), controlPoints.get(3), new Scalar(0, 0, 255), 2); wasAtCenterPoint = true; } else if (!controlPoints.get(8).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(2), controlPoints.get(3), new Scalar(0, 255, 0), 2); } /** * Checking whether the control point on the right is * inside of {@code boundingRectangle}, which is a {@code Rect}, * bounding the greatest contour. */ if (controlPoints.get(7).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(4), controlPoints.get(5), new Scalar(0, 0, 255), 2); wasAtRightPoint = true; } else if (!controlPoints.get(7).inside(boundingRectangle)) { Imgproc.line(frame, controlPoints.get(4), controlPoints.get(5), new Scalar(0, 255, 0), 2); } /** * If the three control points have were inside the {@code boundingRectangle}, * it means that a "car" has passed. */ if (wasAtCenterPoint && wasAtLeftPoint && wasAtRightPoint) { detectedCarsCount++; wasAtCenterPoint = false; wasAtLeftPoint = false; wasAtRightPoint = false; logger.info("Detected " + detectedCarsCount + " car(s)"); } /** * If the contour is big enough, draw it. */ if (contourArea > 3000) { Imgproc.drawContours(frame, contours, position, new Scalar(255, 255, 255)); } }
From source file:image.utils.Tagger.java
public static void main(String[] args) throws IOException, FileNotFoundException { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); File result;//from w w w. j av a 2 s .co m File image_dir = new File(args[1]); File result_dir = new File(args[0]); File img_output_dir = new File(args[2]); for (String s : result_dir.list()) { result = new File(result_dir.getAbsolutePath() + "/" + s); BufferedReader br = null; String line; br = new BufferedReader(new FileReader(result)); while ((line = br.readLine()) != null) { String[] parts = line.split("\t"); JSONArray a = (JSONArray) JSONValue.parse(parts[1]); Mat m = Imgcodecs.imread(parts[0]); for (int i = 0; i < a.size(); i++) { long x, y, w, h; JSONObject o = (JSONObject) a.get(i); x = (long) o.get("x"); y = (long) o.get("y"); w = (long) o.get("width"); h = (long) o.get("height"); System.out.println(String.format("%s %d %d %d %d", parts[0], x, y, w, h)); Rect r = new Rect((int) x, (int) y, (int) w, (int) h); Imgproc.rectangle(m, new Point(r.x, r.y), new Point(r.x + r.width, r.y + r.height), new Scalar(255, 0, 0), 2); Imgcodecs.imwrite(img_output_dir + "/" + new String(Base64.getEncoder().encode(parts[0].getBytes())) + ".jpg", m); } } } }
From source file:imageprocess.FaceDetector.java
public void run() { System.out.println("\nRunning DetectFaceDemo"); // Create a face detector from the cascade file in the resources // directory. CascadeClassifier faceDetector = new CascadeClassifier("D:\\backup\\lbpcascade_frontalface.xml"); Mat image = Highgui.imread("D:\\backup\\scarlett-800x600.jpg"); // Detect faces in the image. // MatOfRect is a special container class for Rect. MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); // Draw a bounding box around each face. for (Rect rect : faceDetections.toArray()) { Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); }//from ww w . j a v a2 s .co m // Save the visualized detection. String filename = "D:\\backup\\faceDetection.png"; System.out.println(String.format("Writing %s", filename)); Highgui.imwrite(filename, image); }
From source file:imageprocess.ObjectFinder.java
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat image = Highgui.imread("D:\\backup\\opencv\\baboon1.jpg"); // Define ROI Rect rect = new Rect(110, 260, 35, 40); Mat imageROI = new Mat(image, rect); Core.rectangle(image, new Point(110, 260), new Point(145, 300), new Scalar(0, 0, 255)); Imshow origIm = new Imshow("Origin"); origIm.showImage(image);//from www. ja v a 2 s .co m ObjectFinder finder = new ObjectFinder(false, 0.2f); // Get the Hue histogram int minSat = 65; Mat hist = finder.getHueHistogram(imageROI, minSat); Mat norm = new Mat(); Core.normalize(hist, norm, 1, 0, NORM_L2); finder.setROIHistogram(norm); // Convert to HSV space Mat hsv = new Mat(); Imgproc.cvtColor(image, hsv, CV_BGR2HSV); // Split the image List<Mat> v = new ArrayList<>(); Core.split(hsv, v); // Eliminate pixels with low saturation Imgproc.threshold(v.get(1), v.get(1), minSat, 255, THRESH_BINARY); Imshow satIm = new Imshow("Saturation"); satIm.showImage(v.get(1)); // Get back-projection of hue histogram Mat result = finder.find(hsv, new MatOfInt(0), new MatOfFloat(0.0f, 180.0f)); Imshow resultHueIm = new Imshow("Result Hue"); resultHueIm.showImage(result); Core.bitwise_and(result, v.get(1), result); Imshow resultHueAndIm = new Imshow("Result Hue and raw"); resultHueAndIm.showImage(result); // Second image Mat image2 = Highgui.imread("D:\\backup\\opencv\\baboon3.jpg"); // Display image Imshow img2Im = new Imshow("Imgage2"); img2Im.showImage(image2); // Convert to HSV space Imgproc.cvtColor(image2, hsv, CV_BGR2HSV); // Split the image Core.split(hsv, v); // Eliminate pixels with low saturation Imgproc.threshold(v.get(1), v.get(1), minSat, 255, THRESH_BINARY); Imshow satIm2 = new Imshow("Saturation2"); satIm2.showImage(v.get(1)); // Get back-projection of hue histogram finder.setThreshold(-1.0f); result = finder.find(hsv, new MatOfInt(0), new MatOfFloat(0.0f, 180.0f)); Imshow resultHueIm2 = new Imshow("Result Hue2"); resultHueIm2.showImage(result); Core.bitwise_and(result, v.get(1), result); Imshow resultHueAndIm2 = new Imshow("Result Hue and raw2"); resultHueAndIm2.showImage(result); Rect rect2 = new Rect(110, 260, 35, 40); Core.rectangle(image2, new Point(110, 260), new Point(145, 300), new Scalar(0, 0, 255)); TermCriteria criteria = new TermCriteria(TermCriteria.MAX_ITER | TermCriteria.EPS, 100, 0.01); int steps = Video.meanShift(result, rect2, criteria); Core.rectangle(image2, new Point(rect2.x, rect2.y), new Point(rect2.x + rect2.width, rect2.y + rect2.height), new Scalar(0, 255, 0)); Imshow meanshiftIm = new Imshow("Meanshift result"); meanshiftIm.showImage(image2); }
From source file:info.jmfavreau.bifrostcore.imageprocessing.ImageToColor.java
License:Open Source License
private Mat compute_roi(Mat original) { Mat roi = new Mat(); Imgproc.cvtColor(original, roi, Imgproc.COLOR_BGR2GRAY, 0); roi.setTo(new Scalar(0, 0, 0)); int x = original.width(); int y = original.height(); int cx = x / 2; int cy = y / 2; int r = Math.min(cx, cy) * 2 / 3; Core.circle(roi, new Point(cx, cy), r, new Scalar(255, 255, 255), -1, 8, 0); return roi;// w w w . ja v a 2 s . com }