opencv:mat、string、char、uchar转换

1060 篇文章 296 订阅

cv:mat读取图片并显示

int main()
{
    cv::Mat mat = cv::imread("/home/oceanstar/image/aa.jpg");
    if(mat.empty())
    {
        cout << "图像不能加载!"<< endl;
        return -1;
    }

    imshow("MyWindow", mat);
    //等待直到有键按下
    waitKey(0);
    return 0;
}

char*转为mat

cv::Mat char_to_mat(char *image_buf, int image_len){
    cv::Mat mat;
    string str2(image_buf, image_len);
    std::vector<char> vec_data1(str2.c_str(), str2.c_str() + str2.size());
    mat = cv::imdecode(vec_data1, -1).clone();
    return mat;
}

char[]与mat

#include <string>
#include <iostream>
#include <time.h>
#include <opencv2/highgui/highgui.hpp>
#include <fstream>

using namespace std;
using namespace cv;

const int MAX = 1024 * 1920 * 3;


bool write_image(const string& filename, char  *pBuffer, int length){
    ofstream fout(filename, ios::binary);
    if(!fout){
        printf("can't open  = %s\n", filename.c_str());
        return false;
    }
    fout.write(pBuffer,length);
    fout.close();
    return true;
}
int main()
{
    //3通道图像
    cv::Mat img2 = cv::imread("/home/oceanstar/image/aa.jpg");
    if(img2.empty())
    {
        cout << "图像不能加载!"<< endl;
        return -1;
    }

    char p[MAX]; //char数组
    std::vector<uchar> buff;
    cv::imencode(".jpg", img2, buff);
    memset(p, 0, MAX);
    memcpy(p, reinterpret_cast<char*>(&buff[0]), buff.size());
    int length = buff.size();

    char * buffer = reinterpret_cast<char *>(p);
    write_image("/home/oceanstar/image/vvvv.jpg", buffer, length );

    return 0;
}

uchar*与mat

#include <string>
#include <iostream>
#include <time.h>
#include <opencv2/highgui/highgui.hpp>
#include <fstream>

using namespace std;
using namespace cv;

const int img_width=1920; /** 图像的宽度*/
const int img_height = 1024; /**图像的高度*/


/**将uchar类型的转换为Mat类型*/
Mat ucharToMat(const uchar *p2)
{
    Mat img(Size(img_width, img_height),CV_8UC3);
    for (int i = 0; i < img_width * img_height * 3; i++)
    {
        img.at<Vec3b>(i / (img_width * 3), (i % (img_width * 3)) / 3)[i % 3] = p2[i];
    }
    return img;
}
/**数组,存放图片大小*/
///uchar p1[img_width * img_height * 3];
/**将Mat类型的数据转换为uchar类型*/
uchar* matToUchar(Mat img, uchar *p1)
{
    for (int i = 0; i < img_width * img_height * 3; i++)
    {
        p1[i]= (uchar)img.at<Vec3b>(i / (img_width * 3), (i % (img_width * 3)) / 3)[i % 3];
    }
    return p1;
}

int main()
{
    //3通道图像
    cv::Mat img2 = cv::imread("/home/oceanstar/image/aa.jpg");
    if(img2.empty())
    {
        cout << "图像不能加载!"<< endl;
        return -1;
    }

    /**定义一个数组存放matToUchar的返回值*/
    auto *p = new uchar[img_width * img_height * 3];
    matToUchar(img2, p);
    /**下面这两行代码目的是输出Mat转化为uchar的值是否正确
    @(int)目的是将uchar类型的数据转换为int可以直观的看到,
    uchar的数据输出的是乱码
    */
   for (int i = 0; i < img_width * img_height * 3; i++){
        cout << (int)p[i] << endl;
    }
    Mat img = ucharToMat(p);
    imshow("img",img);
    waitKey(0);

    return 0;
}

cv::Mat转std::string

cv::Mat mat = cv::imread("d:\\1.jpg");
std::string str;
std::vector<unsigned char> buff;
cv::imencode(".jpg", mat, buff);
str.resize(buff.size());
memcpy(&str[0], buff.data(), buff.size());

c/c++编程:读取文本&图片转为二进制流

uchar和Mat之间的相互转换
c++ string转char*

#include<string> #include"resource.h" #include<opencv2/opencv.hpp> #include<opencv2/core.hpp> #include <zxing/DecodeHints.h> #include <zxing/MultiFormatReader.h> #include <zxing/Result.h> #include <zxing/BinaryBitmap.h> #include <zxing/common/GlobalHistogramBinarizer.h> using namespace zxing; using namespace std; class OpenCVLuminanceSource : public zxing::LuminanceSource { private: cv::Mat image_; public: OpenCVLuminanceSource(cv::Mat image) : LuminanceSource(image.cols, image.rows), image_(image) {} zxing::ArrayRef<char> getRow(int y, zxing::ArrayRef<char> row) const { int width = getWidth(); if (!row || row->size() < width) { row = zxing::ArrayRef<char>(width); } const uchar* imgRow = image_.ptr<uchar>(y); memcpy(&row[0], imgRow, width); return row; } zxing::ArrayRef<char> getMatrix() const { int width = getWidth(); int height = getHeight(); zxing::ArrayRef<char> matrix = zxing::ArrayRef<char>(width * height); for (int y = 0; y < height; ++y) { const uchar* imgRow = image_.ptr<uchar>(y); memcpy(&matrix[y * width], imgRow, width); } return matrix; } }; int main() { cv::Mat image = cv::imread("path/to/your/image.jpg", cv::IMREAD_GRAYSCALE); cv::Mat image = cv::imread("path/to/your/image.jpg", cv::IMREAD_GRAYSCALE); OpenCVLuminanceSource source(image); zxing::Ref<zxing::LuminanceSource> luminanceSource(&source); zxing::Ref<zxing::Binarizer> binarizer = zxing::Binarizer::createBinarizer(luminanceSource); zxing::Ref<zxing::BinaryBitmap> bitmap = zxing::Ref<zxing::BinaryBitmap>(new zxing::BinaryBitmap(binarizer)); zxing::DecodeHints hints; hints.setTryHarder(true); zxing::MultiFormatReader reader; zxing::Ref<zxing::Result> result = reader.decode(bitmap, hints); std::string decodedData = result->getText()->getText(); std::cout << "Decoded data: " << decodedData << std::endl; }
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值