效果
#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;
cv::Mat mask = cv::Mat::zeros(img.size().height,img.size().width, CV_8UC3);
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);
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));
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];
float theta= (*it)[1];
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();
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;
}