static int disColor(Vec3b color1, Vec3b color2) {
int rmean = (color1[0] + color2[0]) / 2;
int R = color1[0] - color2[0];
int G = color1[1] - color2[1];
int B = color1[2] - color2[2];
int distance = (2 + rmean / 256.0) * R * R + 4 * G * G + (2 + (255-rmean) / 256.0) * B * B;
int dis;
dis = sqrt(distance);
return dis;
}
int black_Detect(std::vector<cv::Mat>& imgs){
cv::Mat imgdata= imgs[0];
int w = imgdata.cols;
int h = imgdata.rows;
//std::cout << "w,h is" << w << " " << h << std::endl;
//h = cv2.GetSize(img)
int updo_offset = h/20;
int updow_offset = h/60; //notice
int updow_init = h/3;
int leri_offset = w/20;
int videoLength = imgs.size();
typedef Vec<uchar, 3> Vec3b;
int black_score = 0;
Vec3b blackColor = {0,0,0};
// for video
for (int i = 0; i< videoLength; i++){
int updown_dis = 0;
int leftright_dis = 0;
for (int i = 0; i < 20; i ++){
Vec3b curColor = imgdata.at<Vec3b>(updo_offset, i*leri_offset);
int curDis = disColor(curColor,blackColor);
updown_dis += curDis;
}
for (int j = 0; j < 20; j ++){
Vec3b curColor = imgdata.at<Vec3b>(j*updow_offset + updow_init, leri_offset);
int curDis = disColor(curColor,blackColor);
leftright_dis += curDis;
}
//std::cout << "left,up" << leftright_dis << " " << updown_dis <<std::endl;
//up-down black
if (leftright_dis >= updown_dis){
black_score +=1;
}
}
if (black_score > videoLength/2){
return 3; //up-down black
}
else{
return 5; //left-right black
}
}
2020-11-24
最新推荐文章于 2020-11-26 12:38:02 发布