opencv中的csv文件要怎么做?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv中的csv文件要怎么做?相关的知识,希望对你有一定的参考价值。

static Mat norm_0_255(cv::InputArray _src) //0是灰度模式

Mat src = _src.getMat();
//创建一个归一化后的图像矩阵
Mat dst;

switch(src.channels())

case 1:
cv::normalize(_src, dst, 0, 255, cv::NORM_MINMAX, CV_8UC1);
break;
case 3:
cv::normalize(_src, dst, 0, 255, cv::NORM_MINMAX, CV_8UC3);
break;
default:
src.copyTo(dst);
break;


return dst;


static void read_csv(const string &filename, vector<Mat> &images, vector<int> &labels, char separator = ';') //使用csv文件去读文件和标签

std::ifstream file(filename.c_str(), ifstream::in);
if(!file)

string error_message = "No valid input file was given.";
CV_Error(CV_StsBadArg, error_message);


string line, path, classlabel;
while(getline(file, line))

stringstream liness(line);
getline(liness, path, separator); //遇到分号就结束
getline(liness, classlabel); //继续从分号后面开始,遇到换行结束
if(!path.empty() && !classlabel.empty())

images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));




int main(int argc, char *argv[])

string output_folder;
//output_folder = string("D:\\ORL\\result");
output_folder = string("D:\\data\\result");
//output_folder = string("D:\\Documents\\Visual Studio 2010\\Projects\\opencv\\picture");

//读取你的CSV文件路径
//string fn_csv = string("D:\\ORL\\to\\at.txt");
string fn_csv = string("D:\\data\\to\\at.txt");
//string fn_csv = string("D:\\Documents\\Visual Studio 2010\\Projects\\opencv\\picture\\to\\at.txt");

//两个容器来存放图像数据和对应的标签
vector<Mat> images;
vector<int> labels;
//输入读取数据如果文件不合法就会出错,输入的文件名是存在的
try

read_csv(fn_csv, images, labels);

catch(cv::Exception &e)

cerr<<"Error opening file "<<fn_csv<<". Reason: "<<e.msg<<endl; //调试到这里出现问题Microsoft C++ 异常:
//主要原因应该是csv文件设置出了问题

//文件有问题,我们啥也做不了了,退出了
exit(1);

主要是由图片转化成csv的时候我觉得应该我自己做的出错,可以的话,用图文演示一下

参考技术A 整个项目的结构图:

编写DetectFaceDemo.java,代码如下:

[java] view
plaincopyprint?

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()

System.out.println("\nRunning DetectFaceDemo");

System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());

// 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());

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

/*

* 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("lbpcascade_frontalface.xml").getPath().substring(1);

CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);

Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));

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

System.out.println(String.format("Writing %s", filename));

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()
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// 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());
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* 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("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// 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 = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);



3.编写测试类:

[java] view
plaincopyprint?

package com.njupt.zhb.test;

public class TestMain

public static void main(String[] args)

System.out.println("Hello, OpenCV");

// Load the native library.

System.loadLibrary("opencv_java246");

new DetectFaceDemo().run();





//运行结果:

//Hello, OpenCV

//

//Running DetectFaceDemo

///E:/eclipse_Jee/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(String[] args)
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();


//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png本回答被提问者和网友采纳

java导出 ,CSV格式文件,中文显示成问号,怎么解决?

已经试过编码格式转换,没有效果
response.setContentType("application/csv;charset=UTF-8");
刚刚查了一下,本身的编码环境就是UTF-8的,是不是导出的时候出了什么问题?

参考技术A 要加上UTF-8 BOM头,不然在windows下打开会乱码.本回答被提问者采纳 参考技术B 生成文件时,使用的编码不对应 。。。。。。。。。。

以上是关于opencv中的csv文件要怎么做?的主要内容,如果未能解决你的问题,请参考以下文章

vb6.0关于.csv文件的操作

gh读取csv文件

python中对csv文件某一列的每一行文本进行分词后再写到该文件另一列怎么做

如何把csv文件转换成“正常的”excel?

如何将csv文件转换成excel文件呢?

python生成csv文件一定要用vscode打开吗