基于opencv+java實(shí)現(xiàn)簡單圖形識別程序
前言
OpenCV的 全稱是:Open Source Computer Vision Library。OpenCV是一個(gè)基于BSD許可(開源)發(fā)行的跨平臺(tái)計(jì)算機(jī)視覺庫,可以運(yùn)行在Linux、Windows、Android和Mac OS操作系統(tǒng)上。它輕量級而且高效——由一系列 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ù)可以通過在線文檔獲得。如今也提供對于C#,Ch, Ruby的支持。
本文著重講述opencv+java的實(shí)現(xiàn)程序,關(guān)于opencv的如何引入dll庫等操作以及c的實(shí)現(xiàn)就不在這里概述了
方法如下
直接開始,首先下載opencv,引入opencv-246.jar包以及對應(yīng)dll庫
1.背景去除 簡單案列,只適合背景單一的圖像
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 背景去除 簡單案列,只適合背景單一的圖像
* @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.邊緣檢測
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 邊緣檢測
* @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.人臉檢測技術(shù) (靠邊緣的和側(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 人臉檢測技術(shù) (靠邊緣的和側(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);
}
}人臉檢測需要自行下載haarcascade_frontalface_alt2.xml文件
附上demo下載地址:點(diǎn)擊這里,運(yùn)行需自行引入opencv的dll文件
總結(jié)
到此這篇關(guān)于基于opencv+java實(shí)現(xiàn)簡單圖形識別程序的文章就介紹到這了,更多相關(guān)opencv+java圖形識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用POI從Excel讀取數(shù)據(jù)并存入數(shù)據(jù)庫(解決讀取到空行問題)
有時(shí)候需要在java中讀取excel文件的內(nèi)容,專業(yè)的方式是使用java POI對excel進(jìn)行讀取,這篇文章主要給大家介紹了關(guān)于Java使用POI從Excel讀取數(shù)據(jù)并存入數(shù)據(jù)庫,文中介紹的辦法可以解決讀取到空行問題,需要的朋友可以參考下2023-12-12
springboot2版本無法加載靜態(tài)資源問題解決
這篇文章主要介紹了springboot2版本無法加載靜態(tài)資源問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
關(guān)于Jsoup將相對路徑轉(zhuǎn)為絕對路徑的方法
這篇文章主要介紹了關(guān)于Jsoup將相對路徑轉(zhuǎn)為絕對路徑的方法,jsoup 是一款Java 的HTML解析器,可直接解析某個(gè)URL地址、HTML文本內(nèi)容,需要的朋友可以參考下2023-04-04
深入理解JSON及其在Java中的應(yīng)用小結(jié)
json它是一種輕量級的數(shù)據(jù)交換格式,由于其易于閱讀和編寫,同時(shí)也易于機(jī)器解析和生成,因此廣泛應(yīng)用于網(wǎng)絡(luò)數(shù)據(jù)交換和配置文件,這篇文章主要介紹了深入理解JSON及其在Java中的應(yīng)用,需要的朋友可以參考下2023-12-12
Spring擴(kuò)展之基于HandlerMapping實(shí)現(xiàn)接口灰度發(fā)布實(shí)例
這篇文章主要介紹了Spring擴(kuò)展之基于HandlerMapping實(shí)現(xiàn)接口灰度發(fā)布實(shí)例,灰度發(fā)布是指在黑與白之間,能夠平滑過渡的一種發(fā)布方式,灰度發(fā)布可以保證整體系統(tǒng)的穩(wěn)定,在初始灰度的時(shí)候就可以發(fā)現(xiàn)、調(diào)整問題,以保證其影響度,需要的朋友可以參考下2023-08-08
SpringBoot如何優(yōu)雅實(shí)現(xiàn)接口參數(shù)驗(yàn)證
為了保證參數(shù)的正確性,我們需要使用參數(shù)驗(yàn)證機(jī)制,來檢測并處理傳入的參數(shù)格式是否符合規(guī)范,所以本文就來和大家聊聊如何優(yōu)雅實(shí)現(xiàn)接口參數(shù)驗(yàn)證吧2023-08-08

