c++字符串分割 和 c++读取文件内容

本文介绍了一种使用C++和OpenCV从特定目录读取图像并进行颜色判断的方法。通过遍历文本文件中记录的图像路径,利用OpenCV库加载图像,并对每个像素的颜色进行检查。如果像素颜色匹配预设值,则将结果图像写入到另一个指定目录。

用substr

#include "cv.h"  
#include "highgui.h"  
#include <iostream>
#include <fstream>
#include <string.h>

   
using namespace std;  
using namespace cv;  
   

int main()  
{  
    ifstream infile;
    infile.open("/media/hdc/xing/DeepLabV2/dataset/cityspace/gtFine/gtFine_color.txt");
    string tmp;
    while(getline(infile,tmp)){
        string str = tmp.substr(1);
        string gt_dir = "/media/hdc/xing/DeepLabV2/dataset/cityspace/gtFine" + str;
        string dis_dir = "/media/hdc/xing/DeepLabV2/cityspace" + str; 
        // cout << gt_dir << endl;
        // cout << dis_dir << endl;
        Mat img;      
        img = imread(gt_dir); 
        int col = img.cols;
        int row = img.rows;
        // cout << col << endl;
        // cout << row << endl;
        Mat result(1024, 2048, CV_8UC1, Scalar(0));
        for(int i = 0;i < col;i++){
            for(int j = 0;j < row;j++){
                //cout << (int)img.at<Vec3b>(j,i)[0] << " " << (int)img.at<Vec3b>(j,i)[1] << " " << (int)img.at<Vec3b>(j,i)[2] << "   " << j << " " << i << endl;
                int blue = img.at<Vec3b>(j,i)[0];
                int green = img.at<Vec3b>(j,i)[1];
                int red = img.at<Vec3b>(j,i)[2];
                //uchar number = img.at<Vec3b>(j,i)[0];
                //cout << number << endl;
                if(blue == 128 && green == 64 && red == 128){
                    result.at<uchar>(j,i) = 1;
                }
            }
        }
        imwrite(dis_dir,result);
    }
    
    return 0;  
}  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值