首页 > 编程知识 正文

opencv保存mat数据,opencv读相对路径图片

时间:2023-05-05 20:37:02 阅读:60603 作者:2888

扩大有富馀的可乐

项目总体结构图323131335323631343130323130331363533 e 4b 893 e 5b 19e 313337383961 :

将DetectFaceDemo.java编写如下:

[java] view

plaincopyprint?

package com.njupt.zhb.test;

import org.opencv.core.Core;

import org.opencv.core.Mat;

importorg.opencv.core.matofrect;

import org.opencv.core.Point;

import org.opencv.core.Rect;

import org.opencv.core.Scalar;

importorg.opencv.highgui.highgui;

importorg.opencv.obj detect.cascade classifier;

//

//Detects faces in an image,draws boxes around them,and writes the results

//to 'faceDetection.png '。

//

公共类检测功能演示{

公共void run (}

//createafacedetectorfromthecascadefileintheresources

//目录。

//cascadeclassifierfacedetector=newcascadeclassifier (getclass ).get resource (LBP cascade _ frontal face.XML ' ) )

//matimage=highgui.im read (getclass (.get resource (' Lena.png ' ).getPath ) );

//注意:由于源程序路径中多打印了一个“/”,因此总是出现以下错误

/*

//因此,删除第一个字符

stringxmlfilepath=getclass (.get resource (' LBP cascade _ frontal face.XML ' ).getPath ).substring(1) 1;

cascadeclassifierfacedetector=newcascadeclassifier (xmlfile路径);

//Detect faces in the image。

//matofrectisaspecialcontainerclassforrect。

face detector.detect multiscale (image,faceDetections;

//drawaboundingboxaroundeachface。

for (rect rect : face detections.to array () ) )。

core.rectangle(image,newpoint ) rect.y,rect.y ),newpoint ) rect.xrect.width,rect.y rect.height,news

}

//Save the visualized

detection.

String filename = "faceDetection.png";

Highgui.imwrite(filename, image);

}

}

package com.njupt.zhb.test;

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;

//

// Detects faces in an image, draws boxes around them, and writes the results

// to "faceDetection.png".

//

public class DetectFaceDemo {

public void run() {

// Create a face detector from the cascade file in the resources

// directory.

//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());

//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());

//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误

/*

//因此,我们将第一个字符去掉

String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);

CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);

// Detect faces in the image.

// MatOfRect is a special container class for Rect.

faceDetector.detectMultiScale(image, faceDetections);

// 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 = "faceDetection.png";

Highgui.imwrite(filename, image);

}

}

3.编写测试类:

[java] view

plaincopyprint?

package com.njupt.zhb.test;

public class TestMain {

public static void main(Stringwxdhn args) {

// Load the native library.

new DetectFaceDemo().run();

}

}

//运行结果:

//Hello, 受伤的玫瑰/p>

//

//Running wmdqyg/p>

///E:/eclipse笨笨的咖啡豆/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml

//Detected 8 faces

//Writing faceDetection.png

package com.njupt.zhb.test;

public class TestMain {

public static void main(Stringwxdhn args) {

// Load the native library.

new DetectFaceDemo().run();

}

}

//运行结果:

//Hello, 受伤的玫瑰/p>

//

//Running wmdqyg/p>

///E:/eclipse笨笨的咖啡豆/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml

//Detected 8 faces

//Writing faceDetection.png

已踩过<

你对这个回答的评价是?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。