基于opencv+java實(shí)現(xiàn)簡(jiǎn)單圖形識(shí)別程序
前言
OpenCV的 全稱是:Open Source Computer Vision Library。OpenCV是一個(gè)基于BSD許可(開源)發(fā)行的跨平臺(tái)計(jì)算機(jī)視覺庫,可以運(yùn)行在Linux、Windows、Android和Mac OS操作系統(tǒng)上。它輕量級(jí)而且高效——由一系列 C 函數(shù)和少量 C++ 類 構(gòu)成,同時(shí)提供了Python、Ruby、MATLAB等語言的接口,實(shí)現(xiàn)了 圖像處理和計(jì)算機(jī)視覺方面的很多通用算法。
OpenCV用C++語言編寫,它的主要接口也是C++語言,但是依然保留了大量的C語言接口。該庫也有大量的Python, Java and MATLAB/OCTAVE (版本2.5)的接口。這些語言的API接口函數(shù)可以通過在線文檔獲得。如今也提供對(duì)于C#,Ch, Ruby的支持。
本文著重講述opencv+java的實(shí)現(xiàn)程序,關(guān)于opencv的如何引入dll庫等操作以及c的實(shí)現(xiàn)就不在這里概述了
方法如下
直接開始,首先下載opencv,引入opencv-246.jar包以及對(duì)應(yīng)dll庫
1.背景去除 簡(jiǎn)單案列,只適合背景單一的圖像
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
/**
* @Description 背景去除 簡(jiǎn)單案列,只適合背景單一的圖像
* @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
hsvImg.create(frame.size(), CvType.CV_8U);
Imgproc.cvtColor(frame, hsvImg, Imgproc.COLOR_BGR2HSV);
Core.split(hsvImg, hsvPlanes);
// get the average hue value of the image
Scalar average = Core.mean(hsvPlanes.get(0));
double threshValue = average.val[0];
Imgproc.threshold(hsvPlanes.get(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);
Imgproc.erode(thresholdImg, thresholdImg, new Mat(), new Point(-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));
thresholdImg.convertTo(thresholdImg, CvType.CV_8U);
frame.copyTo(foreground, thresholdImg);// 掩膜圖像復(fù)制
return foreground;
}
}2.邊緣檢測(cè)
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
/**
* @Description 邊緣檢測(cè)
* @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();
frame.copyTo(dest, detectedEdges);
return dest;
}
}3.人臉檢測(cè)技術(shù) (靠邊緣的和側(cè)臉檢測(cè)不準(zhǔn)確)
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
/**
*
* @Description 人臉檢測(cè)技術(shù) (靠邊緣的和側(cè)臉檢測(cè)不準(zhǔn)確)
* @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_frontalface_alt2.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("haarcascade_frontalface_alt2.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路徑會(huì)多打印一個(gè)‘/',因此總是出現(xiàn)如下錯(cuò)誤
/*
* 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
*/
//因此,我們將第一個(gè)字符去掉
String xmlfilePath=getClass().getResource("/haarcascade_frontalface_alt2.xml").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", 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));
}
// 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);
}
}人臉檢測(cè)需要自行下載haarcascade_frontalface_alt2.xml文件
附上demo下載地址:點(diǎn)擊這里,運(yùn)行需自行引入opencv的dll文件
總結(jié)
到此這篇關(guān)于基于opencv+java實(shí)現(xiàn)簡(jiǎn)單圖形識(shí)別程序的文章就介紹到這了,更多相關(guān)opencv+java圖形識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
elasticsearch?java客戶端action的實(shí)現(xiàn)簡(jiǎn)單分析
這篇文章主要為大家介紹了elasticsearch?java客戶端action的實(shí)現(xiàn)簡(jiǎn)單分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
java自動(dòng)根據(jù)文件內(nèi)容的編碼來讀取避免亂碼
這篇文章主要介紹了java自動(dòng)根據(jù)文件內(nèi)容的編碼來讀取避免亂碼,需要的朋友可以參考下2014-02-02
Java實(shí)現(xiàn)簡(jiǎn)單訂餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單訂餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
IntelliJ IDEA2020.1版本更新pom文件自動(dòng)導(dǎo)包的方法
這篇文章主要介紹了IntelliJ IDEA2020.1版本更新pom文件自動(dòng)導(dǎo)包的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
JAVA實(shí)現(xiàn)讀取txt文件內(nèi)容的方法
本篇文章主要介紹了JAVA實(shí)現(xiàn)讀取txt文件內(nèi)容的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Spring @value和@PropertySource注解使用方法解析
這篇文章主要介紹了Spring @value和@PropertySource注解使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
java發(fā)起http請(qǐng)求調(diào)用post與get接口的方法實(shí)例
在實(shí)際開發(fā)過程中,我們經(jīng)常需要調(diào)用對(duì)方提供的接口或測(cè)試自己寫的接口是否合適,下面這篇文章主要給大家介紹了關(guān)于java發(fā)起http請(qǐng)求調(diào)用post與get接口的相關(guān)資料,需要的朋友可以參考下2022-08-08

