Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

have a look in this function. It is in C++ but I think you will not have any problem porting it to python.

// function for splitting image into multiple blocks. rowDivisor and colDivisor specify the number of blocks in rows and cols respectively
        int imgDivision(const cv::Mat &img, const int rowDivisor, const int colDivisor, std::vector<cv::Mat> &blocks)
        {        
            /* Checking if the image was passed correctly */
            if(!img.data || img.empty())
                std::cerr << "Problem Loading Image" << std::endl;

            /* Cloning the image to another for visualization later, if you do not want to visualize the result just comment every line related to visualization */
            cv::Mat maskImg = img.clone();
            /* Checking if the clone image was cloned correctly */
            if(!maskImg.data || maskImg.empty())
                std::cerr << "Problem Loading Image" << std::endl;

            // check if divisors fit to image dimensions
            if(img.cols % colDivisor == 0 && img.rows % rowDivisor == 0)
            {
                for(int y = 0; y < img.cols; y += img.cols / colDivisor)
                {
                    for(int x = 0; x < img.rows; x += img.rows / rowDivisor)
                    {
                        blocks.push_back(img(cv::Rect(y, x, (img.cols / colDivisor), (img.rows / rowDivisor))).clone());
                        rectangle(maskImg, Point(y, x), Point(y + (maskImg.cols / colDivisor) - 1, x + (maskImg.rows / rowDivisor) - 1), CV_RGB(255, 0, 0), 1); // visualization

                        imshow("Image", maskImg); // visualization
                        waitKey(0); // visualization
                    }
                }
            }else if(img.cols % colDivisor != 0)
            {
                cerr << "Error: Please use another divisor for the column split." << endl;
                exit(1);
            }else if(img.rows % rowDivisor != 0)
            {
                cerr << "Error: Please use another divisor for the row split." << endl;
                exit(1);
            }
        return EXIT_SUCCESS;
    }

have a look in this function. It is in C++ but I think you will not have any problem porting it to python.

// function for splitting image into multiple blocks. rowDivisor and colDivisor specify the number of blocks in rows and cols respectively
        int imgDivision(const cv::Mat &img, const int rowDivisor, const int colDivisor, std::vector<cv::Mat> &blocks)
        {        
            /* Checking if the image was passed correctly */
            if(!img.data || img.empty())
                std::cerr << "Problem Loading Image" << std::endl;

            /* Cloning the image to another for visualization later, if you do not want to visualize the result just comment every line related to visualization */
            cv::Mat maskImg = img.clone();
            /* Checking if the clone image was cloned correctly */
            if(!maskImg.data || maskImg.empty())
                std::cerr << "Problem Loading Image" << std::endl;

            // check if divisors fit to image dimensions
            if(img.cols % colDivisor == 0 && img.rows % rowDivisor == 0)
            {
                for(int y = 0; y < img.cols; y += img.cols / colDivisor)
                {
                    for(int x = 0; x < img.rows; x += img.rows / rowDivisor)
                    {
                        blocks.push_back(img(cv::Rect(y, x, (img.cols / colDivisor), (img.rows / rowDivisor))).clone());
                        rectangle(maskImg, Point(y, x), Point(y + (maskImg.cols / colDivisor) - 1, x + (maskImg.rows / rowDivisor) - 1), CV_RGB(255, 0, 0), 1); // visualization

                        imshow("Image", maskImg); // visualization
                        waitKey(0); // visualization
                    }
                }
            }else if(img.cols % colDivisor != 0)
            {
                cerr << "Error: Please use another divisor for the column split." << endl;
                exit(1);
            }else if(img.rows % rowDivisor != 0)
            {
                cerr << "Error: Please use another divisor for the row split." << endl;
                exit(1);
            }
        return EXIT_SUCCESS;
    }

Input:

image description

Output:

image description

have a look in this function. It is in C++ but I think you will not have any problem porting it to python.

// function for splitting image into multiple blocks. rowDivisor and colDivisor specify the number of blocks in rows and cols respectively
        int imgDivision(const subdivide(const cv::Mat &img, const int rowDivisor, const int colDivisor, std::vector<cv::Mat> &blocks)
        {        
            /* Checking if the image was passed correctly */
            if(!img.data || img.empty())
                std::cerr << "Problem Loading Image" << std::endl;

            /* Cloning the image to another for visualization later, if you do not want to visualize the result just comment every line related to visualization */
            cv::Mat maskImg = img.clone();
            /* Checking if the clone image was cloned correctly */
            if(!maskImg.data || maskImg.empty())
                std::cerr << "Problem Loading Image" << std::endl;

            // check if divisors fit to image dimensions
            if(img.cols % colDivisor == 0 && img.rows % rowDivisor == 0)
            {
                for(int y = 0; y < img.cols; y += img.cols / colDivisor)
                {
                    for(int x = 0; x < img.rows; x += img.rows / rowDivisor)
                    {
                        blocks.push_back(img(cv::Rect(y, x, (img.cols / colDivisor), (img.rows / rowDivisor))).clone());
                        rectangle(maskImg, Point(y, x), Point(y + (maskImg.cols / colDivisor) - 1, x + (maskImg.rows / rowDivisor) - 1), CV_RGB(255, 0, 0), 1); // visualization

                        imshow("Image", maskImg); // visualization
                        waitKey(0); // visualization
                    }
                }
            }else if(img.cols % colDivisor != 0)
            {
                cerr << "Error: Please use another divisor for the column split." << endl;
                exit(1);
            }else if(img.rows % rowDivisor != 0)
            {
                cerr << "Error: Please use another divisor for the row split." << endl;
                exit(1);
            }
        return EXIT_SUCCESS;
    }

Input:

image description

Output:

image description