Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Common problems in using opencv

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "common problems in the use of opencv". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the common problems in the use of opencv.

Recently, the project encountered a problem, a problem in the packaging process of springboot2.

1. Refer to the local jar package, how to package it to the project

Pom.xml configuration dependencies tag configuration org.opencv opencv 0.0.1 system ${project.basedir} / src/main/resources/lib/opencv-440.jar where version is required, tried to omit and reported an error Plugins tag configuration org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 ${project.basedir} / src/main/resources/lib org.springframework.boot spring- Boot-maven-plugin true resources tag configuration ${project.basedir} / src/main/resources lib/*.jar ${project.basedir} / src/main/resources lib/*.dll ${project.basedir} / src/main/resources * .yml ${project.basedir} / src/main/resources * / * .xml ${project.basedir} / src/main/resources * / * .json

2. When opencv uses face recognition, you need to introduce the haarcascade_frontalface_alt2.xml file. If you put it in the lib file of the project, you can enter it into the package in the process of packaging, but in the process of dynamic reference, the file cannot be found due to the chaotic path of the file reference in the jar package. To find a compromise, the haarcascade_frontalface_alt2.xml file is placed in a fixed directory, and when referenced, you only need to read the fixed path.

Yml file configuration

Path:

ResourcePath: C:\ haarcascade_frontalface_alt2.xml # xml profile for face recognition

NativeConfig.java class

Package com.hake.smart.configration;import com.hake.smart.ymlconfig.YmlConfigUtil;import lombok.extern.slf4j.Slf4j;import org.opencv.core.*;import org.opencv.imgcodecs.Imgcodecs;import org.opencv.imgproc.Imgproc;import org.opencv.objdetect.CascadeClassifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.util.ClassUtils;import javax.annotation.Resource;import java.util.Arrays / * * @ Author: Liu Yue * @ Descripition: * @ Date; Create in 15:59 on 2020-8-25 * * / @ Configuration@Slf4jpublic class NativeConfig {@ Resource private YmlConfigUtil ymlConfigUtil; @ Value ("${path.resourcePath}") private static String resourcePath; @ Value (value = "${path.resourcePath}") private void setResourcePath (String resourcePath) {this.resourcePath = resourcePath } @ Bean public NativeConfig initFrontalface () {System.loadLibrary (Core.NATIVE_LIBRARY_NAME); log.error ("path: {}", resourcePath); faceDetector = new CascadeClassifier (resourcePath); return new NativeConfig ();} / * Test whether OpenCV can run: you need to modify the image location * @ throws Exception test is successful * / static CascadeClassifier faceDetector / * static {System.loadLibrary (Core.NATIVE_LIBRARY_NAME); / / String url = "C:/hake/gitremote/hakesmartacceptback/target/classes/lib/haarcascade_frontalface_alt2.xml"; / / ClassPathResource ("lib/haarcascade_frontalface_alt2.xml"); * / / * ClassPathResource resource = new ClassPathResource ("lib/haarcascade_frontalface_alt2.xml"); String path = resource.getPath (); path = basepath + path * / * URL urlClass = ClassLoader.getSystemResource ("lib/haarcascade_frontalface_alt2.xml"); String path = urlClass.getPath (); log.error ("path: {}", path); path = path.substring (1 mempath.length ()); faceDetector = new CascadeClassifier (path);} * / private Mat conv_Mat (String img_1) {Mat image0 = Imgcodecs.imread (img_1) Mat image = new Mat (); / / grayscale conversion Imgproc.cvtColor (image0, image, Imgproc.COLOR_BGR2GRAY); MatOfRect faceDetections = new MatOfRect (); / / detect face faceDetector.detectMultiScale (image, faceDetections) / / rect is the range of face images for (Rect rect: faceDetections.toArray ()) {/ / cut rect face Mat mat = new Mat (image, rect); return mat;} return null } / / compare picture similarity public Boolean compare_image (String img_1, String img_2) {Mat mat_1 = conv_Mat (img_1); Mat mat_2 = conv_Mat (img_2); Mat hist_1 = new Mat (); Mat hist_2 = new Mat (); / / Color range MatOfFloat ranges = new MatOfFloat (0f, 256f) / / histogram size, the larger the match, the more accurate (slower) MatOfInt histSize = new MatOfInt (1000); Imgproc.calcHist (Arrays.asList (mat_1), new MatOfInt (0), new Mat (), hist_1, histSize, ranges); Imgproc.calcHist (Arrays.asList (mat_2), new MatOfInt (0), new Mat (), hist_2, histSize, ranges) / / CORREL correlation coefficient double res = Imgproc.compareHist (hist_1, hist_2, Imgproc.CV_COMP_CORREL); log.info ("OPENCV face similarity: {}", res); int lifePhotostandard = ymlConfigUtil.getLifePhotostandard (); log.info ("recognition Standard: {}", lifePhotostandard); boolean bl = res * 100 > lifePhotostandard? True:false; return bl;}} at this point, I believe you have a deeper understanding of "common problems in the use of opencv". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report