美文网首页
c++读取文件中图像信息并用opencv展示

c++读取文件中图像信息并用opencv展示

作者: 送分童子笑嘻嘻 | 来源:发表于2019-12-18 16:12 被阅读0次
#include <iostream>
#include <fstream>
#include<sstream>
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
#include <thread>
//
using namespace std;
using namespace cv;

//字符串分割函数,
std::vector<int> split(std::string str, std::string pattern) {
    std::string::size_type pos;
    std::vector<int> result;
    str += pattern;//扩展字符串以方便操作
    int size = str.size();

    for (int i = 0; i < size; i++) {
        pos = str.find(pattern, i);
        if (pos < size) {
            std::string s = str.substr(i, pos - i);
            int number = 0;
            number = atoi(s.c_str());
            result.push_back(number);
            i = pos + pattern.size() - 1;
        }
    }
    return result;
}

int main() {
    string cai_filename = "/home/cai/bag/test1216bag/bigphoto.txt";
    //读txt文件
    ifstream infile;//定义文件变量
    infile.open(cai_filename, ios::in);//打开txt
    if (!infile) {
        std::cout << "读取txt文件失败!" << std::endl;
        return 0;
    }
    string temp;
    std::vector<int> image_vector;
    int width = 0;
    int height = 0;
    //行数
    int line = 0;
    //最后一行字符串
    string last_line_string;
    while (getline(infile, temp)) //读取一行, 直到所有行读完
    {
        //每隔1.5秒更新一次图片
        //std::this_thread::sleep_for(chrono::milliseconds(1500));
        std::cout << temp << std::endl;
        image_vector = split(temp, "-");
        //在数据传输协议里面已定义第2个为width,第3个为height,第一应该是"outfile_big_graph: "被spilit方法设置为0了
        width = image_vector[2];
        height = image_vector[1];
        image_vector.erase(image_vector.begin());
        image_vector.erase(image_vector.begin());
        image_vector.erase(image_vector.begin());
        std::cout << image_vector.size() << std::endl;
        std::cout << "width" << width << std::endl;
        std::cout << "height" << height << std::endl;
////////////opencv展示图片////////////////////////////////////////////////////////////////////////////////
////////////opencv展示图片////////////////////////////////////////////////////////////////////////////////
        cv::Mat src = Mat::zeros(height, width, CV_8UC1);
        int test = 0;
        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                int Gray = image_vector[i * width + j];
                src.at<uchar>(i, j) = (uchar) Gray;
                if (Gray > 200) {
                    test++;
                }
            }
        }
        //std::cout << "test=" << test << std::endl;
        cv::Mat dst;
        int new_width = width * 4;
        int new_height = height * 4;
        resize(src, dst, cv::Size(new_width, new_height));
        imshow("dst", dst);
        waitKey(1500);
////////////opencv展示图片////////////////////////////////////////////////////////////////////////////////
////////////opencv展示图片////////////////////////////////////////////////////////////////////////////////
    }

    destroyAllWindows();


    infile.close();
    return 0;
}

相关文章

网友评论

      本文标题:c++读取文件中图像信息并用opencv展示

      本文链接:https://www.haomeiwen.com/subject/cryunctx.html