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

 更新时间:2022年01月26日 14:38:59   作者:x业精于勤x  
这篇文章主要给大家介绍了如何基于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 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.人脸检测技术 (靠边缘的和侧脸检测不准确)

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 人脸检测技术 (靠边缘的和侧脸检测不准确)
 * @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());  
    //注意:源程序的路径会多打印一个‘/',因此总是出现如下错误  
        /* 
         * 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文件

总结

到此这篇关于基于opencv+java实现简单图形识别程序的文章就介绍到这了,更多相关opencv+java图形识别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java 中通过 key 获取锁的方法

    Java 中通过 key 获取锁的方法

    这篇文章主要介绍了Java 中通过 key 获取锁,本文演示如何对某个 key 加锁,以保证对该 key 的并发操作限制,可以实现同一个 key 一个或者多个线程同时执行,需要的朋友可以参考下
    2022-11-11
  • Java使用POI从Excel读取数据并存入数据库(解决读取到空行问题)

    Java使用POI从Excel读取数据并存入数据库(解决读取到空行问题)

    有时候需要在java中读取excel文件的内容,专业的方式是使用java POI对excel进行读取,这篇文章主要给大家介绍了关于Java使用POI从Excel读取数据并存入数据库,文中介绍的办法可以解决读取到空行问题,需要的朋友可以参考下
    2023-12-12
  • springboot2版本无法加载静态资源问题解决

    springboot2版本无法加载静态资源问题解决

    这篇文章主要介绍了springboot2版本无法加载静态资源问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 关于Jsoup将相对路径转为绝对路径的方法

    关于Jsoup将相对路径转为绝对路径的方法

    这篇文章主要介绍了关于Jsoup将相对路径转为绝对路径的方法,jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址、HTML文本内容,需要的朋友可以参考下
    2023-04-04
  • 深入理解JSON及其在Java中的应用小结

    深入理解JSON及其在Java中的应用小结

    json它是一种轻量级的数据交换格式,由于其易于阅读和编写,同时也易于机器解析和生成,因此广泛应用于网络数据交换和配置文件,这篇文章主要介绍了深入理解JSON及其在Java中的应用,需要的朋友可以参考下
    2023-12-12
  • java中金额元转万元工具类的实例

    java中金额元转万元工具类的实例

    这篇文章主要介绍了java中金额元转万元工具类的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Spring扩展之基于HandlerMapping实现接口灰度发布实例

    Spring扩展之基于HandlerMapping实现接口灰度发布实例

    这篇文章主要介绍了Spring扩展之基于HandlerMapping实现接口灰度发布实例,灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式,灰度发布可以保证整体系统的稳定,在初始灰度的时候就可以发现、调整问题,以保证其影响度,需要的朋友可以参考下
    2023-08-08
  • SpringBoot如何优雅实现接口参数验证

    SpringBoot如何优雅实现接口参数验证

    为了保证参数的正确性,我们需要使用参数验证机制,来检测并处理传入的参数格式是否符合规范,所以本文就来和大家聊聊如何优雅实现接口参数验证吧
    2023-08-08
  • 基于Java中Math类的常用函数总结

    基于Java中Math类的常用函数总结

    下面小编就为大家带来一篇基于Java中Math类的常用函数总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Java超详细整理讲解各种排序

    Java超详细整理讲解各种排序

    这篇文章主要介绍了Java常用的排序算法及代码实现,在Java开发中,对排序的应用需要熟练的掌握,这样才能够确保Java学习时候能够有扎实的基础能力。那Java有哪些排序算法呢?本文小编就来详细说说Java常见的排序算法,需要的朋友可以参考一下
    2022-07-07

最新评论