图形识别-基于opencv+java简单程序

OpenCV的全称是:Open Source Computer Vision Library。OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

OpenCV用C++语言编写,它的主要接口也是C++语言,但是依然保留了大量的C语言接口。该库也有大量的Python, Java and MATLAB/OCTAVE (版本2.5)的接口。这些语言的API接口函数可以通过在线文档获得。如今也提供对于C#,Ch, Ruby的支持。

本文着重讲述opencv+java的实现程序,关于opencv的如何引入dll库等操作以及c的实现就不在这里概述了

直接开始,首先下载opencv,引入opencv-246.jar包以及对应dll库

1.背景去除 简单案列,只适合背景单一的图像

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.opencv.core.Core;
  4. import org.opencv.core.CvType;
  5. import org.opencv.core.Mat;
  6. import org.opencv.core.Point;
  7. import org.opencv.core.Scalar;
  8. import org.opencv.core.Size;
  9. import org.opencv.highgui.Highgui;
  10. import org.opencv.imgproc.Imgproc;
  11. /**
  12. * @Description 背景去除 简单案列,只适合背景单一的图像
  13. * @author XPY
  14. * @date 2016年8月30日下午4:14:32
  15. */
  16. public class demo1 {
  17. public static void main(String[] args) {
  18. System.loadLibrary("opencv_java246");
  19. Mat img = Highgui.imread("E:\opencv_img\source\1.jpg");//读图像
  20. Mat new_img = doBackgroundRemoval(img);
  21. Highgui.imwrite("E:\opencv_img\target\1.jpg",new_img);//写图像
  22. }
  23. private static Mat doBackgroundRemoval(Mat frame) {
  24. // init
  25. Mat hsvImg = new Mat();
  26. List<Mat> hsvPlanes = new ArrayList<>();
  27. Mat thresholdImg = new Mat();
  28. int thresh_type = Imgproc.THRESH_BINARY_INV;
  29. // threshold the image with the average hue value
  30. hsvImg.create(frame.size(), CvType.CV_8U);
  31. Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV);
  32. Core.split(hsvImg, hsvPlanes);
  33. // get the average hue value of the image
  34. Scalar average = Core.mean(hsvPlanes.get(0));
  35. double threshValue = average.val[0];
  36. Imgproc.threshold(hsvPlanes.get(0), thresholdImg, threshValue, 179.0,
  37. thresh_type);
  38. Imgproc.blur(thresholdImg, thresholdImg, new Size(5, 5));
  39. // dilate to fill gaps, erode to smooth edges
  40. Imgproc.dilate(thresholdImg, thresholdImg, new Mat(),
  41. new Point(-1, -1), 1);
  42. Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-1, -1),
  43. 3);
  44. Imgproc.threshold(thresholdImg, thresholdImg, threshValue, 179.0,
  45. Imgproc.THRESH_BINARY);
  46. // create the new image
  47. Mat foreground = new Mat(frame.size(), CvType.CV_8UC3, new Scalar(255,
  48. 255, 255));
  49. thresholdImg.convertTo(thresholdImg, CvType.CV_8U);
  50. frame.copyTo(foreground, thresholdImg);// 掩膜图像复制
  51. return foreground;
  52. }
  53. }

2.边缘检测

  1. import org.opencv.core.Core;
  2. import org.opencv.core.Mat;
  3. import org.opencv.core.Size;
  4. import org.opencv.highgui.Highgui;
  5. import org.opencv.imgproc.Imgproc;
  6. /**
  7. * @Description 边缘检测
  8. * @author XPY
  9. * @date 2016年8月30日下午5:01:01
  10. */
  11. public class demo2 {
  12. public static void main(String[] args) {
  13. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  14. Mat img = Highgui.imread("E:\face7.jpg");//读图像
  15. Mat new_img = doCanny(img);
  16. Highgui.imwrite("E:\opencv_img\target\2.jpg",new_img);//写图像
  17. }
  18. private static Mat doCanny(Mat frame)
  19. {
  20. // init
  21. Mat grayImage = new Mat();
  22. Mat detectedEdges = new Mat();
  23. double threshold = 10;
  24. // convert to grayscale
  25. Imgproc.cvtColor(frame, grayImage, Imgproc.COLOR_BGR2GRAY);
  26. // reduce noise with a 3x3 kernel
  27. Imgproc.blur(grayImage, detectedEdges, new Size(3, 3));
  28. // canny detector, with ratio of lower:upper threshold of 3:1
  29. Imgproc.Canny(detectedEdges, detectedEdges, threshold, threshold * 3);
  30. // using Canny's output as a mask, display the result
  31. Mat dest = new Mat();
  32. frame.copyTo(dest, detectedEdges);
  33. return dest;
  34. }
  35. }

3.人脸检测技术 (靠边缘的和侧脸检测不准确)

  1. import org.opencv.core.Core;
  2. import org.opencv.core.Mat;
  3. import org.opencv.core.MatOfRect;
  4. import org.opencv.core.Point;
  5. import org.opencv.core.Rect;
  6. import org.opencv.core.Scalar;
  7. import org.opencv.highgui.Highgui;
  8. import org.opencv.objdetect.CascadeClassifier;
  9. /**
  10. *
  11. * @Description 人脸检测技术 (靠边缘的和侧脸检测不准确)
  12. * @author XPY
  13. * @date 2016年9月1日下午4:47:33
  14. */
  15. public class demo3 {
  16. public static void main(String[] args) {
  17. System.out.println("Hello, OpenCV");
  18. // Load the native library.
  19. System.loadLibrary("opencv_java246");
  20. new demo3().run();
  21. }
  22. public void run() {
  23. System.out.println("Running DetectFaceDemo");
  24. System.out.println(getClass().getResource("/haarcascade_frontalface_alt2.xml").getPath());
  25. // Create a face detector from the cascade file in the resources
  26. // directory.
  27. //CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("haarcascade_frontalface_alt2.xml").getPath());
  28. //Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
  29. //注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
  30. /*
  31. * Detected 0 faces Writing faceDetection.png libpng warning: Image
  32. * width is zero in IHDR libpng warning: Image height is zero in IHDR
  33. * libpng error: Invalid IHDR data
  34. */
  35. //因此,我们将第一个字符去掉
  36. String xmlfilePath=getClass().getResource("/haarcascade_frontalface_alt2.xml").getPath().substring(1);
  37. CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
  38. Mat image = Highgui.imread("E:\face2.jpg");
  39. // Detect faces in the image.
  40. // MatOfRect is a special container class for Rect.
  41. MatOfRect faceDetections = new MatOfRect();
  42. faceDetector.detectMultiScale(image, faceDetections);
  43. System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
  44. // Draw a bounding box around each face.
  45. for (Rect rect : faceDetections.toArray()) {
  46. Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
  47. }
  48. // Save the visualized detection.
  49. String filename = "E:\faceDetection.png";
  50. System.out.println(String.format("Writing %s", filename));
  51. System.out.println(filename);
  52. Highgui.imwrite(filename, image);
  53. }
  54. }

图形识别-基于opencv+java简单程序

相关推荐