最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于opencv+java實現(xiàn)簡單圖形識別程序

 更新時間:2022年01月26日 14:38:59   作者:x業(yè)精于勤x  
這篇文章主要給大家介紹了如何基于opencv+java實現(xiàn)簡單圖形識別程序的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

前言

OpenCV的 全稱是:Open Source Computer Vision Library。OpenCV是一個基于BSD許可(開源)發(fā)行的跨平臺計算機視覺庫,可以運行在Linux、Windows、Android和Mac OS操作系統(tǒng)上。它輕量級而且高效——由一系列 C 函數(shù)和少量 C++ 類 構(gòu)成,同時提供了Python、Ruby、MATLAB等語言的接口,實現(xiàn)了 圖像處理和計算機視覺方面的很多通用算法。

OpenCV用C++語言編寫,它的主要接口也是C++語言,但是依然保留了大量的C語言接口。該庫也有大量的Python, Java and MATLAB/OCTAVE (版本2.5)的接口。這些語言的API接口函數(shù)可以通過在線文檔獲得。如今也提供對于C#,Ch, Ruby的支持。

本文著重講述opencv+java的實現(xiàn)程序,關(guān)于opencv的如何引入dll庫等操作以及c的實現(xiàn)就不在這里概述了

方法如下

直接開始,首先下載opencv,引入opencv-246.jar包以及對應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);// 掩膜圖像復制
		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è)臉檢測不準確)

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è)臉檢測不準確)
 * @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());  
    //注意:源程序的路徑會多打印一個‘/',因此總是出現(xiàn)如下錯誤  
        /* 
         * 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_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下載地址:點擊這里,運行需自行引入opencv的dll文件

總結(jié)

到此這篇關(guān)于基于opencv+java實現(xiàn)簡單圖形識別程序的文章就介紹到這了,更多相關(guān)opencv+java圖形識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 中通過 key 獲取鎖的方法

    Java 中通過 key 獲取鎖的方法

    這篇文章主要介紹了Java 中通過 key 獲取鎖,本文演示如何對某個 key 加鎖,以保證對該 key 的并發(fā)操作限制,可以實現(xiàn)同一個 key 一個或者多個線程同時執(zhí)行,需要的朋友可以參考下
    2022-11-11
  • Java使用POI從Excel讀取數(shù)據(jù)并存入數(shù)據(jù)庫(解決讀取到空行問題)

    Java使用POI從Excel讀取數(shù)據(jù)并存入數(shù)據(jù)庫(解決讀取到空行問題)

    有時候需要在java中讀取excel文件的內(nèi)容,專業(yè)的方式是使用java POI對excel進行讀取,這篇文章主要給大家介紹了關(guān)于Java使用POI從Excel讀取數(shù)據(jù)并存入數(shù)據(jù)庫,文中介紹的辦法可以解決讀取到空行問題,需要的朋友可以參考下
    2023-12-12
  • springboot2版本無法加載靜態(tài)資源問題解決

    springboot2版本無法加載靜態(tài)資源問題解決

    這篇文章主要介紹了springboot2版本無法加載靜態(tài)資源問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • 關(guān)于Jsoup將相對路徑轉(zhuǎn)為絕對路徑的方法

    關(guān)于Jsoup將相對路徑轉(zhuǎn)為絕對路徑的方法

    這篇文章主要介紹了關(guān)于Jsoup將相對路徑轉(zhuǎn)為絕對路徑的方法,jsoup 是一款Java 的HTML解析器,可直接解析某個URL地址、HTML文本內(nèi)容,需要的朋友可以參考下
    2023-04-04
  • 深入理解JSON及其在Java中的應用小結(jié)

    深入理解JSON及其在Java中的應用小結(jié)

    json它是一種輕量級的數(shù)據(jù)交換格式,由于其易于閱讀和編寫,同時也易于機器解析和生成,因此廣泛應用于網(wǎng)絡(luò)數(shù)據(jù)交換和配置文件,這篇文章主要介紹了深入理解JSON及其在Java中的應用,需要的朋友可以參考下
    2023-12-12
  • java中金額元轉(zhuǎn)萬元工具類的實例

    java中金額元轉(zhuǎn)萬元工具類的實例

    這篇文章主要介紹了java中金額元轉(zhuǎn)萬元工具類的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Spring擴展之基于HandlerMapping實現(xiàn)接口灰度發(fā)布實例

    Spring擴展之基于HandlerMapping實現(xiàn)接口灰度發(fā)布實例

    這篇文章主要介紹了Spring擴展之基于HandlerMapping實現(xiàn)接口灰度發(fā)布實例,灰度發(fā)布是指在黑與白之間,能夠平滑過渡的一種發(fā)布方式,灰度發(fā)布可以保證整體系統(tǒng)的穩(wěn)定,在初始灰度的時候就可以發(fā)現(xiàn)、調(diào)整問題,以保證其影響度,需要的朋友可以參考下
    2023-08-08
  • SpringBoot如何優(yōu)雅實現(xiàn)接口參數(shù)驗證

    SpringBoot如何優(yōu)雅實現(xiàn)接口參數(shù)驗證

    為了保證參數(shù)的正確性,我們需要使用參數(shù)驗證機制,來檢測并處理傳入的參數(shù)格式是否符合規(guī)范,所以本文就來和大家聊聊如何優(yōu)雅實現(xiàn)接口參數(shù)驗證吧
    2023-08-08
  • 基于Java中Math類的常用函數(shù)總結(jié)

    基于Java中Math類的常用函數(shù)總結(jié)

    下面小編就為大家?guī)硪黄贘ava中Math類的常用函數(shù)總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Java超詳細整理講解各種排序

    Java超詳細整理講解各種排序

    這篇文章主要介紹了Java常用的排序算法及代碼實現(xiàn),在Java開發(fā)中,對排序的應用需要熟練的掌握,這樣才能夠確保Java學習時候能夠有扎實的基礎(chǔ)能力。那Java有哪些排序算法呢?本文小編就來詳細說說Java常見的排序算法,需要的朋友可以參考一下
    2022-07-07

最新評論

柳州市| 正安县| 辽源市| 荥阳市| 桦甸市| 高唐县| 巴林右旗| 繁昌县| 丹巴县| 彩票| 习水县| 贵定县| 东城区| 措勤县| 九江县| 柳林县| 县级市| 梅河口市| 莆田市| 禹城市| 安康市| 南部县| 长沙县| 万荣县| 祁阳县| 隆德县| 连城县| 墨竹工卡县| 神池县| 余庆县| 门源| 东乡县| 唐山市| 兴业县| 阿尔山市| 湖北省| 建水县| 公主岭市| 泗水县| 广汉市| 乌兰察布市|