Commit 0fd25424 by “liusq”

增加人脸图片日志05

parent 0394c9ab
......@@ -5,9 +5,13 @@
#include <iostream>
#define cimg_display 0
#define cimg_use_jpeg
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "CImg.h"
#include "stb_image.h"
#include "stb_image_write.h"
using namespace cimg_library;
......@@ -41,8 +45,25 @@ cv::Mat FaceReconitionHandle::loadImage(const QString &path) {
qInfo() << "图像以OpenCV成功加载。";
return image;
}
return loadImageFromByteStream(path);
image= loadImageFromByteStream(path);
if (!image.empty()) {
qInfo() << "图像以CImg成功加载。";
return image;
}
image=loadImageWithSTB(path);
if (!image.empty()) {
qInfo() << "图像以stb_image成功加载。";
return image;
}
image=loadQImageFromByteStream(path);
if (!image.empty()) {
qInfo() << "图像以QImage成功加载。";
return image;
}
qInfo() << "四种加载方式都不行";
return cv::Mat();
}
void FaceReconitionHandle::initSourceImageMap(std::map<QString,QString>&maps,int numberFaces,float confidence){
......@@ -157,8 +178,55 @@ void FaceReconitionHandle::featureRemove(){
setImageChanged(false);
}
}
cv::Mat FaceReconitionHandle::loadImageWithSTB(const QString& filename) {
int width, height, channels;
QByteArray && b_filename = filename.toUtf8();
char* c_filename = b_filename.data();
// 使用 stb_image 加载图像,stbi_load 会根据图像格式返回数据
unsigned char* data = stbi_load(c_filename, &width, &height, &channels, 0);
if (!data) {
qInfo() << "tb_image无法加载图像 " << filename;
return cv::Mat();
}
// 检查图像通道数,创建对应的 cv::Mat
cv::Mat image;
if (channels == 1) {
// 灰度图像
image = cv::Mat(height, width, CV_8UC1, data);
} else if (channels == 3) {
// 彩色图像
image = cv::Mat(height, width, CV_8UC3, data);
// OpenCV 默认使用 BGR,因此需要将 RGB 转换为 BGR
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
} else if (channels == 4) {
// 带 Alpha 通道的图像
image = cv::Mat(height, width, CV_8UC4, data);
// RGB -> BGR(包括 Alpha 通道)
cv::cvtColor(image, image, cv::COLOR_RGBA2BGRA);
}
// 释放 stb_image 加载的内存
stbi_image_free(data);
return image;
}
cv::Mat FaceReconitionHandle::loadQImageFromByteStream(const QString& filePath) {
// 使用 QImage 加载图像
QImage qimage(filePath);
if (qimage.isNull()) {
qInfo() << "QImage Error: 无法加载图像 " << filePath;
return cv::Mat();
}
// 将 QImage 转换为 cv::Mat
cv::Mat mat(qimage.height(), qimage.width(), CV_8UC4, const_cast<uchar*>(qimage.bits()), qimage.bytesPerLine());
// 如果需要 BGR 格式
cv::cvtColor(mat, mat, cv::COLOR_RGBA2BGR);
return mat;
}
cv::Mat FaceReconitionHandle::loadImageFromByteStream(const QString& filePath) {
try {
......
......@@ -28,6 +28,11 @@ public:
cv::Mat loadImage(const QString &path);
cv::Mat loadImageFromByteStream(const QString& filePath);
cv::Mat loadQImageFromByteStream(const QString& filePath);
cv::Mat loadImageWithSTB(const QString& filename);
bool getImageChanged()const;
void setImageChanged(bool imageChanged);
......
......@@ -26,6 +26,8 @@ INCLUDEPATH+=/usr/local/include/XNetSDK
INCLUDEPATH+=/usr/local/include/human
INCLUDEPATH+=/usr/local/include/CImg
INCLUDEPATH+=/usr/local/include/mqtt
INCLUDEPATH+=/usr/local/include/stb_image
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment