opencv mat的数据类型CV_32FC1变成CV_8UC1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opencv mat的数据类型CV_32FC1变成CV_8UC1相关的知识,希望对你有一定的参考价值。

整个项目的结构图:

编写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
参考技术A 新建一个CV_8UC1类型的矩阵,直接把原矩阵赋值就行

OpenCV 并从 OutputArrays 或 (Mat) 读取数据

【中文标题】OpenCV 并从 OutputArrays 或 (Mat) 读取数据【英文标题】:OpenCV and reading data from OutputArrays or (Mat) 【发布时间】:2020-01-31 07:14:37 【问题描述】:

OpenCV 自己的 PnP 和 Rodrigues 公式函数存在一些问题。我认为它与cv::solvePnPRansac()有关

 cv::Mat w = cv::Mat::zeros(3,1,CV_32FC1);
 cv::Mat t = cv::Mat::zeros(3,1,CV_32FC1);
 std::vector<float> distortion = 0,0,0,0;

 std::vector<cv::Point3f> tmp1 = eig_vec_to_cv3(pts);
 std::vector<cv::Point2f> tmp2 =  eig_vec_to_cv2(pixels);

 cv::solvePnPRansac(tmp1, tmp2, eig_mat_2_cv(K),distortion, w, t,false, 100, 2.0f);

 cv::Mat R_ = cv::Mat::zeros(3,3,CV_32FC1);
 cv::Rodrigues(w,R_);

 std::cout<<"R_"<<std::endl;
 std::cout<<R_<<std::endl;
 std::cout<<R_.at<float>(0,0)<<std::endl;

对于std::cout&lt;&lt;R_&lt;&lt;std::endl,它看起来没问题,但R_.at&lt;float&gt;(0,0) 给出了一个垃圾号码,就像没有分配内存一样。 wt 也是如此。

但是,如果我这样做:

cv::Mat w_ = cv::Mat(3,1,cv_32FC1);
w.at<float>(0,0) = 0.2;
w.at<float>(0,1) = 0.4;
w.at<float>(0,2) = 0.3;

cv::Rodrigues(w_,R_);

std::cout<<"R_"<<std::endl;
std::cout<<R_<<std::endl;
std::cout<<R_.at<float>(0,0)<<std::endl;

效果很好。

这是一个最小(非)工作示例:

#include <opencv2/calib3d.hpp>
#include <opencv2/opencv.hpp>
#include <vector>

int main()

    cv::Mat w = cv::Mat(3,1,CV_32FC1);
    cv::Mat t = cv::Mat(3,1, CV_32FC1);

    std::vector<cv::Point3f> tmp1;
    std::vector<cv::Point2f> tmp2;

    for (int k = 0; k < 10; ++k)
    
        cv::Point3f p1(0.2f+k, 0.3f-k, 7.5f-k);
        cv::Point2f p2(3.2f*k, 4.5f/k);
        tmp1.push_back(p1);
        tmp2.push_back(p2);
    

    cv::Mat K = cv::Mat::zeros(3,3,CV_32FC1);
    K.at<float>(0,0) = 525.0;
    K.at<float>(0,2) = 234.5;
    K.at<float>(1,1) = 525;
    K.at<float>(1,2) = 312.5;
    K.at<float>(2,2) = 1.0f;

    std::vector<float> distortion = 0,0,0,0;

    cv::solvePnPRansac(tmp1, tmp2, K,distortion, w, t,false, 100, 2.0f);

    std::cout<<w<<std::endl;
    cv::Mat R = cv::Mat::zeros(3,3,CV_32FC1);
    cv::Rodrigues(w,R);
    std::cout<<R<<std::endl;

    std::cout<<R.at<float>(0,0)<<std::endl;
    return 0;

编译

g++ main.cpp -I /usr/local/include/opencv4/ -o test -L /usr/local/lib/ -lopencv_calib3d -lopencv_core

【问题讨论】:

【参考方案1】:

原因是函数cv::Rodrigues 创建了CV_64FC1 类型的输出矩阵。因此,值必须按如下方式读取:

std::cout<<R.at<double>(0,0)<<std::endl;

即使我们将输出矩阵预先分配为任何其他类型(比如CV_32FC1),它也会被cv::Rodrigues重新分配为类型CV_64FC1

在我看来,OpenCV 文档对许多函数的输入和输出类型缺乏明确性。在这种情况下,必须通过打印Mat::type() 函数的返回值来确定输出类型。

【讨论】:

非常感谢,我认为 OpenCV 文档在很多方面都存在问题。特别是对于 python。 @El_Loco.. 确实如此。即使是主要语言 (C++) 也有一些模棱两可的文档。我想是时候开始为代码库做出贡献并改进其文档了。 :) @El_Loco.. 另外,如果它解决了您的问题,您可以考虑接受答案。 @sgarizvi 现在是贡献力量的好时机——OpenCV 黑客马拉松本周末开始! opencv.org/opencv-hackathon-is-coming

以上是关于opencv mat的数据类型CV_32FC1变成CV_8UC1的主要内容,如果未能解决你的问题,请参考以下文章

如何安装opencv和cv

如何在opencv中将图像从CV_8UC1转换为CV_32FC1类型?

OpenCV 并从 OutputArrays 或 (Mat) 读取数据

了解opencv中的Mat

OpenCV 保存 CV_32FC1 图像

如何在 Opencv 中显示一个 Mat