C/C++ 侵线检测

效果

在这里插入图片描述

#include <opencv2/opencv.hpp>
#include<iostream>
#include <typeinfo>
#include <numeric>
#define PI 3.14159265358979
using namespace std;
using namespace cv;


vector<Mat> read_images_in_folder(cv::String path);

cv::Mat draw_invade(cv::Mat& img, float& rho, float& theta){
    cv::Mat mask = cv::Mat::zeros(img.size().height,img.size().width, CV_8UC3);
    Point root_points[1][4];
    float append = mask.rows * 0.2;
	root_points[0][0] = Point(0, rho/sin(theta)+append);
	root_points[0][1] = Point(0, rho/sin(theta)-append);

	root_points[0][2] = Point(mask.cols, (rho - mask.cols * cos(theta))/sin(theta)+append);
	root_points[0][3] = Point(mask.cols, (rho - mask.cols * cos(theta))/sin(theta)-append);

	const Point* ppt[1] = { root_points[0] };
	int npt[] = { 4 };
    fillPoly(mask, ppt, npt, 1, Scalar(100, 255, 100));
    return mask;
}

cv::Mat get_invade_area_scene1(cv::Mat& img){
    float rate = 0.35;
    float x1, y1, w1, h1;
    float x2, y2, w2, h2;
    // mask size same as ori img size
    cv::Mat mask = cv::Mat::zeros(img.size().height,img.size().width, CV_8UC3);

    // cut 1
    x1 = 0;
    y1 = img.size().height*rate;
    w1 = img.size().width*rate;
    h1 = img.size().height*(1-rate) - y1;
    cv::Rect r1 = cv::Rect(x1, y1, w1, h1);

    // cut 2
    x2 = img.size().width*(1-rate);
    y2 = img.size().height*rate;
    w2 = img.size().width*rate;
    h2 = img.size().height*(1-rate) - y2;
    cv::Rect r2 = cv::Rect(x2, y2, w2, h2);


    cv::Mat cut1 = img(r1);
    cv::Mat cut2 = img(r2);

    cut1.copyTo(mask(r1));
    cut2.copyTo(mask(r2));
    // --------------------------------------preprocess------------------------------

    cvtColor(mask,mask,COLOR_BGR2GRAY);
    cv::GaussianBlur(mask, mask,cv::Size(11, 11), 0, 0);
    cv::Canny(mask, mask,10,70);
    std::vector<cv::Vec2f> lines;
    std::vector<cv::Vec2f> theta_filter_lines;
    cv::HoughLines(mask, lines, 1,PI/180, 50);
    cvtColor(mask,mask,COLOR_GRAY2BGR);
    std::vector<cv::Vec2f>::const_iterator it= lines.begin();
    std::vector<float> left_lines_rho;
    std::vector<float> left_lines_theta;
    std::vector<float> right_lines_rho;
    std::vector<float> right_lines_theta;


    while (it!=lines.end())
    {
        float rho= (*it)[0]; // first element is distance rho
        float theta= (*it)[1]; // second element is angle theta


        if (theta >PI/12 && theta < 11*PI/12)
        {
            if (theta > PI/2 + PI/7)
            {
                right_lines_rho.push_back(rho);
                right_lines_theta.push_back(theta);
            }
            if (theta < PI/2 - PI/7)
            {
                left_lines_rho.push_back(rho);
                left_lines_theta.push_back(theta);
            }
        }
        it++;
    }

    float R_rho = (accumulate(begin(right_lines_rho), end(right_lines_rho), 0.0))/right_lines_rho.size();
    float R_theta = (accumulate(begin(right_lines_theta), end(right_lines_theta), 0.0))/right_lines_theta.size();
    float L_rho = (accumulate(begin(left_lines_rho), end(left_lines_rho), 0.0))/left_lines_rho.size();
    float L_theta = (accumulate(begin(left_lines_theta), end(left_lines_theta), 0.0))/left_lines_theta.size();


    if (R_theta >PI/12 && R_theta < 11*PI/12 && R_theta > PI/2 + PI/7){cv::Mat mask1 = draw_invade(img,R_rho,R_theta);img = img + mask1 * 0.3;}
    if (L_theta >PI/12 && L_theta < 11*PI/12 && L_theta < PI/2 - PI/7){cv::Mat mask2 = draw_invade(img,L_rho,L_theta);img = img + mask2 * 0.3;}


    return img;
}

vector<Mat> read_images_in_folder(cv::String pattern)
{
  vector<cv::String> fn;
  glob(pattern, fn, false);

  vector<Mat> images;
  size_t count = fn.size(); //number of png files in images folder
  for (size_t i = 0; i < count; i++)
  {
    images.push_back(imread(fn[i]));
  }
  return images;
}

int main(){
    string imgpaths[] = {"scene1/0000079.jpg","scene1/0000061.jpg","scene1/0000012.jpg","scene1/0000095.jpg"};
    cout<<"1"<<endl;
    cv::String path = "scene1";
    vector<Mat> images = read_images_in_folder(path);
    cout<<images.size()<<endl;
    for(int i=0;i<images.size();i++){
        cv::Mat img = images[i];
        cout<<img.size()<<endl;
        cv::Mat img_invade = get_invade_area_scene1(img);
        imwrite(to_string(i)+".jpg",img_invade);
    }
    cout<<"2"<<endl;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值