c++杂记一工程常用

在图像上做标记

工程中常常将输出的结果信息在图像上显示,例如:目标检测、车牌检测
、车牌分类、车牌识别,会将输出结果在图像上显示。
c++中常用opencv 中的两个函数:
第一个

void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

此函数用于在图像上画出矩形,参数:Rect:需要标记的矩形,color:矩形的线条颜色,thickness
:线条的粗细(thickness=-1)代表将整个矩形进行填充,lineType:线条的类型,shift:坐标点的小数点位数。
常用设置:


cv::rectangle(temptMat,virOutput.JudgeInfo.PDCInfo.vcarLoc[i].CarLoc[j],cv::Scalar(0,0,255),3,8,0);

第二个函数常用设置,用于在图片上的相应位置加入字符信息,不能添加汉字


cv::putText(img, "T:"+std::to_string(carPlateInfo[i].type)+" "+"S:"+carPlateInfo[i].plate_number,
                    cv::Point(temptRect.x-10, temptRect.y-10), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(255, 23, 0), 3, 3);
             

常用字符串提取处理

工程中经常会有以下需求:
提取imgList中的图片名字不带.jpg,某图片路径,放在imgList.txt中:
/data_1/VIRProject/data/3603230771@2019年02月14日04时49分42秒@01@赣AE5793@@360300@0@12@0@1.jpg
第一种,使用filename()和find()、substr():


std::string imgList = "/data_1/Working/project/addNewViolate/VIRProject/data/imgList.txt";
std::string imgRow;
std::ifstream fileList(imgList); //读取imgList中的内容
int index=1;
while (fileList>>imgRow) //一次读取一个字符串,以空格作为字符串的分隔符,如果一次读一行则使用
{                        // getline(fileList,imgRow)
  boost::filesystem::path orgPath = imgRow;
  std::string imgName = orgPath.filename().string(); //提取图片名字,名字中带有.jpg
  									      // boost::filesystem::path 定义的路径转换为字符,一定要使用 *.string()* 进行转换
  int pos = imgName.find(".");  //发现字符“."在字符串imgRow中的位置,位置从0开始
  std::string temptStr = imgName.substr(0,pos);//按照位置提取子字符串,不包含pos位置的字符
  std::cout << "index: " << std::to_string(index) << temptStr << std::endl;
  index++;
  }
  

第二种,两次使用split()
工程中还有需求:
//从“/data/3603230774@2019年02月11日17时27分06秒@01@赣J66720@1.jpg 0”中读取图像的名字。
代码:

#include <iostream> //cout使用
#include <string>
#include <boost/filesystem.hpp> //关于对文件或文件夹进行操作的头文件
#include <fstream> //.txt 中读写头文件
#include <sstream> //字符流
#include <boost/algorithm/string.hpp>//函数split使用

#define PROCESS_SUCCESS 1
#define FILE_ERRO -1
#define DATA_ERRO -2

using namespace std;
using namespace boost::filesystem;
//从“/data/3603230774@2019年02月11日17时27分06秒@01@赣J66720@1.jpg 0”中读取图像的名字
int main() {

    std::string filePath = "/data_1/LearningStep/LearningC++/LearningExam/data/imgList.txt";
    std::vector<std::string> temptStr;
    std::string rowStr,imgPath,imgLabel;
    ifstream fileData(filePath);
    if(!fileData.is_open())
        return FILE_ERRO;

    while (getline(fileData,rowStr)) // 按照行读取.txt文件内容
    {
         temptStr.clear();
         if(rowStr.empty())          // vector和string都具有empty()方法。
             return DATA_ERRO;

         istringstream lineStr(rowStr); // 以空格作为分割符,将一行的内容分在不同变量内。
         lineStr>>imgPath>>imgLabel;  //获得空格分割后的内容
         boost::split(temptStr,imgPath,boost::is_any_of("/")); // "/"前算作一个
         std::string imgName = temptStr[2];
         temptStr.clear();
         boost::split(temptStr,imgName,boost::is_any_of("."));
         imgName = temptStr[0];
         std::cout<<"图像名字:"<<imgName<<std::endl;
    }
    return PROCESS_SUCCESS;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值