opencv cv::imread无法使用中文路径的解决办法

https://github.com/opencv/opencv/issues/4292

#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

int main(int argc, char ** argv)
{
    FILE* f = fopen("人脸.jpg", "rb");
    fseek(f, 0, SEEK_END); // seek to end of file
    size_t buffer_size = ftell(f); // get current file pointer
    fseek(f, 0, SEEK_SET); // seek back to beginning of file

    std::vector<char> buffer(buffer_size);
    fread(&buffer[0], sizeof(char), buffer_size, f);
    fclose(f);

    cv::Mat frame = cv::imdecode(buffer, cv::IMREAD_COLOR);

    cv::imshow("Camera", frame);
    cv::waitKey();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将OpenCV中的`cv::Mat`类型转换为C++版ZXing库所需的`zxing::BinaryBitmap`类型,您需要进行一些额外的步骤。以下是一个示例代码,演示了如何进行这样的转换: ```cpp #include <iostream> #include <zxing/common/GlobalHistogramBinarizer.h> #include <zxing/Binarizer.h> #include <zxing/LuminanceSource.h> #include <zxing/MultiFormatReader.h> #include <zxing/DecodeHints.h> #include <zxing/BinaryBitmap.h> #include <opencv2/opencv.hpp> using namespace zxing; class OpenCVImageSource : public LuminanceSource { private: cv::Mat image; public: OpenCVImageSource(cv::Mat& image) : LuminanceSource(image.cols, image.rows), image(image) {} ArrayRef<char> getRow(int y, ArrayRef<char> row) const override { int width = getWidth(); if (!row) { row = ArrayRef<char>(width); } for (int x = 0; x < width; ++x) { // Assumes grayscale image, so all channels have the same value row[x] = image.at<uchar>(y, x); } return row; } ArrayRef<char> getMatrix() const override { cv::Mat gray; cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY); return ArrayRef<char>((char*)gray.data, gray.total()); } bool isCropSupported() const override { return false; } Ref<LuminanceSource> crop(int left, int top, int width, int height) const override { throw std::runtime_error("Crop not supported"); } bool isRotateSupported() const override { return false; } Ref<LuminanceSource> rotateCounterClockwise() const override { throw std::runtime_error("Rotate not supported"); } }; int main() { // 取图像文件,得到cv::Mat类型的图像数据 cv::Mat image = cv::imread("path_to_image.jpg"); // 创建OpenCVImageSource对象,将cv::Mat类型转换为zxing的LuminanceSource类型 OpenCVImageSource source(image); // 创建GlobalHistogramBinarizer对象,将LuminanceSource转换为zxing的Binarizer类型 Ref<Binarizer> binarizer = new GlobalHistogramBinarizer(source); // 创建BinaryBitmap对象,将Binarizer转换为zxing的BinaryBitmap类型 Ref<BinaryBitmap> bitmap = new BinaryBitmap(binarizer); // 创建DecodeHints对象,设置解码参数 DecodeHints hints(DecodeHints::DEFAULT_HINT); hints.setTryHarder(true); // 创建MultiFormatReader对象,用于解码二维码 MultiFormatReader reader(hints); try { // 解码二维码 Ref<Result> result = reader.decode(bitmap); // 输出解码结果 std::cout << "Decoded Result: " << result->getText()->getText() << std::endl; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; } ``` 在上面的示例代码中,我们首先使用OpenCV的`cv::imread`函数从图像文件中取图像数据,并得到一个`cv::Mat`类型的图像对象。 然后,我们定义了一个自定义的`OpenCVImageSource`类,它继承自ZXing库中的`LuminanceSource`类,并实现了其中的虚函数。 在`OpenCVImageSource`类中,我们将OpenCV的`cv::Mat`类型图像数据转换为ZXing库所需的灰度图像数据。 接下来,我们使用`OpenCVImageSource`对象创建了一个ZXing库的`Binarizer`对象,并将其传递给`BinaryBitmap`对象。 然后,我们创建了`DecodeHints`对象,并设置了解码参数。 最后,我们使用`MultiFormatReader`对象对二维码进行解码,并输出解码结果。 请注意,上述示例代码中的 `path_to_image.jpg` 是一个示例图像文件路径,您需要根据实际情况替换为您自己的图像文件路径。 希望这个示例代码能够帮助您将OpenCV中的`cv::Mat`类型转换为C++版ZXing库所需的`zxing::BinaryBitmap`类型!如有更多问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值