Ask Your Question
4

Divide an image into lower regions

asked 2015-01-23 07:00:48 -0600

spinter696 gravatar image

updated 2015-01-23 07:10:32 -0600

berak gravatar image

Hi,

I explain my problem, I have an image of MxN and I want ti create regions of lower dimensions. for example I want to divide the image into 4x2, so 8 regions I have the next code:

# Numbers of rows
nRows = 4
# Number of columns
mCols = 2

# Reading image
img = cv2.imread('XXXX')
print img
# Dimensions of the image
sizeX = img.shape[0]
sizeY = img.shape[1]

print sizeX,sizeY
# Tam of Y region
regionY=sizeY/nRows

# Tam of X region 
regionX = sizeX/mCols

# Total of regions
totalRegions = nRows* mCols
name = str(totalRegions)
for v in range (0,totalRegions):
    for i in range(0,mCols):
        for j in range(0, nRows):
            name =  str(totalRegions)
            globals()[name]=img[0+mCols*v:mCols+(mCols*v)-1,
                               0+nRows*(v/nRows):nRows+(nRows*(v/nRows))-1]
            totalRegions += -1
edit retag flag offensive close merge delete

Comments

And now explain what is going wrong? All I see is a piece of code and an explanation of your desired result.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-23 08:22:53 -0600 )edit
1

Sorry, I hadn't time so I wrote the problem without explaining the problem, I 'll do it right now:

the thing is, I don't know if there is an optimal method in openCV to do the job I wrote, I think the code is not too good. Just asking for another idea.

Thanks.

spinter696 gravatar imagespinter696 ( 2015-01-24 06:35:28 -0600 )edit

Could you please share the python piece of code if it worked for you, mine even after applying the C++ algorithm, is not working displaying a red screen, i.e red colored filled rectangles all over the image.

Gigi gravatar imageGigi ( 2017-07-10 04:10:00 -0600 )edit
1

@Gigi, it's not working like this. all those persons moved on, and won't ever respond.

ask your own question, show, what you've tried so far.

berak gravatar imageberak ( 2017-07-10 08:05:05 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
7

answered 2015-01-29 07:21:01 -0600

theodore gravatar image

updated 2015-09-24 07:49:43 -0600

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 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

edit flag offensive delete link more

Comments

Hi, Could you please tell what to pass as &Blocks Kindly revert as soon as possible

vidya7s gravatar imagevidya7s ( 2016-11-16 01:20:25 -0600 )edit

in blocks just pass an empty vector<Mat> that you have initialized from where ever you are calling the function. Couldn't be more simple ;-).

theodore gravatar imagetheodore ( 2016-11-21 15:04:21 -0600 )edit

Hey, can you brief if it is necessary to clone (mask the image) in python? I've written the code in python but it is displaying a red screen as a result. Could you please help as soon as possible. Thanks.

Gigi gravatar imageGigi ( 2017-07-10 03:58:44 -0600 )edit

the cloning is only needed in order to visualize the result nothing more nothing less. try to debug your code line by line or you could also post it here, someone with python experience will for sure help ;-)

theodore gravatar imagetheodore ( 2017-07-10 18:10:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-01-23 07:00:48 -0600

Seen: 12,405 times

Last updated: Sep 24 '15