0

Right now i am using this code to remove the black borders but i am still left with some black part in most of the cases after using this.

Am i doing something wrong in this ?

void cropImage(const vector<cv::Mat>& input, vector<cv::Mat>& output){
//  CHECK(!input.empty());
const int imgWidth = input[0].cols;
const int imgHeight = input[0].rows;
Mat cropMask(imgHeight, imgWidth, CV_32F, Scalar::all(0));
for(auto y=0; y<imgHeight; ++y){
    for(auto x=0; x<imgWidth; ++x){
        bool has_black = false;
        for(auto v=0; v<input.size(); ++v){
            if(input[v].at<Vec3b>(y,x) == Vec3b(0,0,0)){
                has_black = true;
                break;
            }
        }
        if(has_black)
            cropMask.at<float>(y,x) = -1000;
        else
            cropMask.at<float>(y,x) = 1;
    }
}
Mat integralImage;
cv::integral(cropMask, integralImage, CV_32F);
Vector4i roi;
//int x11=0,x22=0,y11=0,y22=0;
float optValue = -1000 * imgWidth * imgHeight;
const int stride = 20;
for(auto x1=0; x1<imgWidth; x1+=stride) {
    for (auto y1 = 0; y1 < imgHeight; y1+=stride) {
        for (auto x2 = x1 + stride; x2 < imgWidth; x2+=stride) {
            for (auto y2 = y1 + stride; y2 < imgHeight; y2+=stride) {
                float curValue = integralImage.at<float>(y2, x2) + 
                                 integralImage.at<float>(y1, x1)
                                 - integralImage.at<float>(y2, x1) - 
                                    integralImage.at<float>(y1, x2);
                if(curValue > optValue){
                    optValue = curValue;
                    roi = Vector4i(x1,y1,x2,y2);

                }
            }
        }
    }
}

output.resize(input.size());
for(auto i=0; i<output.size(); ++i){

    output[i] = input[i].colRange(roi[0],roi[2]).rowRange(roi[1], 
    roi[3]).clone();
    cv::resize(output[i], output[i], cv::Size(imgWidth, imgHeight));
}
}

Also this code seems to be slow. Is there any fast method to achieve the same ? Thanks in advance.

Himanshu Singla
  • 261
  • 3
  • 12
  • First of all, can you ensure that inside your region of interest has no black pixels? Maybe [this](https://stackoverflow.com/questions/13538748/crop-black-edges-with-opencv) could help – Daniel R. Jan 20 '18 at 13:22
  • 1
    Only code snippet is not enough to make the question clear. Please post your desired result and real result. – Kinght 金 Jan 20 '18 at 15:36

0 Answers0