美文网首页
c++读取文件中指定一行的图像数据并用opencv显示

c++读取文件中指定一行的图像数据并用opencv显示

作者: 送分童子笑嘻嘻 | 来源:发表于2019-12-18 16:39 被阅读0次
    #include <iostream>
    #include <fstream>
    #include<sstream>
    #include <opencv2/opencv.hpp>
    #include <string>
    #include <vector>
    
    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 temp;
        bool find_i_need = false;
        string cai_filename = "/home/cai/bag/test1218bag/iamge_data.txt";
        //读txt文件
        ifstream infile;//定义文件变量
        infile.open(cai_filename, std::ios::app);
        int a = 0;
        while (getline(infile, temp)) //读取一行, 直到所有行读完
        {
            a++;
            unsigned long i = temp.find("1470");
           // std::cout<<i <<std::endl;
            if (temp.find("1470")==0) {
                std::cout << temp << std::endl;
                find_i_need = true;
                break;
            } else {
                continue;
            }
        }
        std::cout<<"a=" <<a<<std::endl;
        if (!find_i_need) {
            return 0;
        }
        std::vector<int> image_vector;
        int width = 0;
        int height = 0;
        image_vector = split(temp, "-");
        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;
        cv::Mat src = Mat::zeros(height, width, CV_8UC1);
        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;
            }
        }
    
        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(0);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:c++读取文件中指定一行的图像数据并用opencv显示

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