基于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.背景去除简单案列,只适合背景单⼀的图像
import java.util.ArrayList;
import java.util.List;
import Core;
import CvType;
import Mat;
import Point;
import Scalar;
import Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
/**
* @Description 背景去除简单案列,只适合背景单⼀的图像
* @author XPY
* @date 2016年8⽉30⽇下午4:14:32
*/
public class demo1 {
public static void main(String[] args) {
System.loadLibrary("opencv_java246");
Mat img = Highgui.imread("E:\\opencv_img\\source\\1.jpg");//读图像
Mat new_img = doBackgroundRemoval(img);
Highgui.imwrite("E:\\opencv_img\\target\\1.jpg",new_img);//写图像
}
private static Mat doBackgroundRemoval(Mat frame) {
// init
Mat hsvImg = new Mat();
List<Mat> hsvPlanes = new ArrayList<>();
Mat thresholdImg = new Mat();
int thresh_type = Imgproc.THRESH_BINARY_INV;
// threshold the image with the average hue value
Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV);
Core.split(hsvImg, hsvPlanes);
// get the average hue value of the image
Scalar average = (0));
double threshValue = average.val[0];
Imgproc.(0), thresholdImg, threshValue, 179.0,
thresh_type);
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), 1);
3);
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));
return foreground;
}
}
2.边缘检测
import Core;
import Mat;
import Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
/**
* @Description 边缘检测
* @author XPY
* @date 2016年8⽉30⽇下午5:01:01
*/
public class demo2 {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat img = Highgui.imread("E:\\face7.jpg");//读图像
Mat new_img = doCanny(img);
Highgui.imwrite("E:\\opencv_img\\target\\2.jpg",new_img);//写图像
}
private static Mat doCanny(Mat frame)
{
/
/ init
Mat grayImage = new Mat();
Mat detectedEdges = new Mat();
double threshold = 10;
// convert to grayscale
Imgproc.cvtColor(frame, grayImage, Imgproc.COLOR_BGR2GRAY);
// reduce noise with a 3x3 kernel
Imgproc.blur(grayImage, detectedEdges, new Size(3, 3));
// canny detector, with ratio of lower:upper threshold of 3:1
Imgproc.Canny(detectedEdges, detectedEdges, threshold, threshold * 3);              // using Canny's output as a mask, display the result
Mat dest = new Mat();
return dest;
}
}
3.⼈脸检测技术(靠边缘的和侧脸检测不准确)
import Core;
import Mat;
import MatOfRect;
import Point;
import Rect;
import Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
/**
*
* @Description ⼈脸检测技术(靠边缘的和侧脸检测不准确)
* @author XPY
* @date 2016年9⽉1⽇下午4:47:33
*/
public class demo3 {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
/
/ Load the native library.
System.loadLibrary("opencv_java246");
new demo3().run();
}
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("/haarcascade_l").getPath());
// Create a face detector from the cascade file in the resources
// directory.
rectangle函数opencv//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("haarcascade_l").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路径会多打印⼀个‘/',因此总是出现如下错误
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我们将第⼀个字符去掉
String xmlfilePath=getClass().getResource("/haarcascade_l").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread("E:\\face2.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", Array().length));
// Draw a bounding box around each face.
for (Rect rect : Array()) {
}
// Save the visualized detection.
String filename = "E:\\faceDetection.png";
System.out.println(String.format("Writing %s", filename));
System.out.println(filename);
Highgui.imwrite(filename, image);
}
}
⼈脸检测需要⾃⾏下载haarcascade_l⽂件
附上demo下载地址:,运⾏需⾃⾏引⼊opencv的dll⽂件
总结
到此这篇关于基于opencv+java实现简单图形识别程序的⽂章就介绍到这了,更多相关opencv+java图形识别内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。