skip to Main Content

I wrote a program that uses the openCV and boost::filesystem libraries, and the program crops images to fit the object in the image. (Photoshop has already been used to replace most of the backgrounds with white). However, I have thousands and thousands of pictures that I need to sort through. I already know how to use the filesystem library and have no issue traversing the system’s directories. However, how do I detect images that have a non-white background (missed in the photoshop process)? This incorrect crop is formatted to have a margin and have a 1:1 aspect ratio, but it still has the odd grayish background. The image should end up looking like this correct crop. So, how do I determine if the image has a background like the incorrect crop?

2

Answers


  1. I that you can compute the gradient of an ROI of your image (all rows in column 10 to 15 for exemple).
    Then you compute the energy of your gradient (sum of all pixels of the gradient image).

    If the energy is very low, you have an uniform background (you can’t know the background color with this algorithm). Else you have a textured backgroud.

    This is a first approach. You can found in OpenCV all the functions required to do that.

    A second approach :
    If you are sure that your background is white, you can get the ROI of the first approach, then iterate over all pixels, and check for its color. If there are more than “n” pixels with a different color than “255,255,255”, you can mark your image as “non white Background”.

    Login or Signup to reply.
  2. could you try the code below

    ( to test the code you should create a directory c:/cropping and some subdirs on it. and put some images in the dirs you created.)

    hope it will be helpful

    #include "opencv2/imgproc.hpp"
    #include "opencv2/highgui.hpp"
    #include <iostream>
    using namespace cv;
    using namespace std;
    
    vector<Rect> divideHW(Mat src, int dim, double threshold1, double threshold2)
    {
        Mat gray, reduced, canny;
    
        if (src.channels() == 1)
        {
            gray = src;
        }
    
        if (src.channels() == 3)
        {
            Laplacian(src, gray, CV_8UC1);
            cvtColor(gray, gray, COLOR_BGR2GRAY);
            imshow("sobel", gray);
        }
    
        reduce(gray, reduced, dim, REDUCE_AVG);
    
        Canny(reduced, canny, threshold1, threshold2);
    
        vector<Point> pts;
        findNonZero(canny, pts);
    
        vector<Rect> rects;
    
        Rect rect(0, 0, gray.cols, gray.rows);
        if (!pts.size())
        {
            rects.push_back(rect);
        }
        int ref_x = 0;
        int ref_y = 0;
    
        for (size_t i = 0; i< pts.size(); i++)
        {
            if (dim)
            {
                rect.height = pts[i].y - ref_y;
                rects.push_back(rect);
                rect.y = pts[i].y;
                ref_y = rect.y;
                if (i == pts.size() - 1)
                {
                    rect.height = gray.rows - pts[i].y;
                    rects.push_back(rect);
                }
            }
    
            else
            {
                rect.width = pts[i].x - ref_x;
                rects.push_back(rect);
                rect.x = pts[i].x;
                ref_x = rect.x;
                if (i == pts.size() - 1)
                {
                    rect.width = gray.cols - pts[i].x;
                    rects.push_back(rect);
                }
            }
    
        }
        return rects;
    }
    
    int main( int argc, char** argv )
    {
        int wait_time = 0; // set this value > 0 for not waiting 
        vector<String> filenames;
        String folder = "c:/cropping/*.*"; // you can change this value or set it by argv[1]
    
        glob(folder, filenames, true);
    
        for (size_t i = 0; i < filenames.size(); ++i)
        {
    
            Mat src = imread(filenames[i]);
    
            if (src.data)
            {
                vector<Rect> rects = divideHW(src, 0, 0, 0);
                if (rects.size() < 3) continue;
                Rect border;
                border.x = rects[0].width;
                border.width = src.cols - rects[rects.size() - 1].width - border.x;
    
                rects = divideHW(src, 1, 0, 20);
                if (rects.size() < 3) continue;
                border.y = rects[0].height;
                border.height = src.rows - rects[rects.size() - 1].height - border.y;
    
                Mat cropped = src(border).clone();
                src(border).setTo(Scalar(255, 255, 255));
                Scalar _mean = mean(src);
                int mean_total = _mean[0] + _mean[1] + _mean[2];
    
                if (mean_total > 763)
                {
                    imwrite(filenames[i] + ".jpg", cropped);
                    imshow("cropped", cropped);
                    waitKey(wait_time);
                }
    
            }
        }
        return 0;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search