# Divide an image into lower regions

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

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 close merge delete

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

( 2015-01-23 08:22:53 -0500 )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.

( 2015-01-24 06:35:28 -0500 )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.

( 2017-07-10 04:10:00 -0500 )edit
1

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

( 2017-07-10 08:05:05 -0500 )edit

Sort by ยป oldest newest most voted

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

/* Cloning the image to another for visualization later, if you do not want to visualize the result just comment every line related to visualization */
/* Checking if the clone image was cloned correctly */

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

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:

Output:

more

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

( 2016-11-16 01:20:25 -0500 )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 ;-).

( 2016-11-21 15:04:21 -0500 )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.

( 2017-07-10 03:58:44 -0500 )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 ;-)

( 2017-07-10 18:10:22 -0500 )edit

Official site

GitHub

Wiki

Documentation