Click here to Skip to main content
15,891,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I specify the Location by specified Pixel and select a particular Region?

case 0:
								
crop = img(Rect(0, 0, ((int)(img.cols / 2)), ((int)(img.rows / 2)))); // 
break;
case 1:
crop = img(Rect(((int)(img.cols / 2 + 1)), 0, ((int)(img.cols / 2 - 1)), ((int)(img.rows / 2))));
break;
case 2:
crop = img(Rect(0, ((int)(img.rows / 2 + 1)), ((int)(img.cols / 2)), ((int)(img.rows / 2 - 1))));
break;
case 3:
crop = img(Rect(((int)(img.cols / 2 + 1)), ((int)(img.rows / 2 + 1)), ((int)(img.cols / 2 - 1)), ((int)(img.rows / 2 - 1))));
break;


I wrote the cases to divide the image into 4 different parts, but it would be very hard to write for 64.

What I have tried:

I want to divide the 4 into 16 and then into 64, should i DEFINE each case specifically, if yes, how do I move around
Posted
Updated 7-Feb-20 11:10am
Comments
Richard MacCutchan 7-Feb-20 10:21am    
You need to refine your code to take the number of parts required and calculate the location and size for each part. It should be possible with a loop that is given the dimensions of the image, and the number of parts required.

1 solution

If you write one function to split a region into four pieces you can then use it to split a region as many times as you want. That is, to the limits of the data type you are using.

You are pretty much there already. You can simplify the code considerably if you calculate the half width and half height values first. Here is what that could look like :
C++
int halfhigh = img.rows / 2;
int halfwide = img.cols / 2;
switch( region )
{
case 0: crop = img( Rect( 0,            0,            halfwide, halfhigh ) ); break;
case 1: crop = img( Rect( halfwide + 1, 0,            halfwide, halfhigh ) ); break;
case 2: crop = img( Rect( 0,            halfhigh + 1, halfwide, halfhigh ) ); break;
case 3: crop = img( Rect( halfwide + 1, halfhigh + 1, halfwide, halfhigh ) ); break;
}
 
Share this answer
 
Comments
Member 14589606 7-Feb-20 20:42pm    
How do I use it to split it 64 times?
Do you I loop it for 64 times?
Member 14589606 7-Feb-20 20:51pm    
Or do u mean to call the function 3 times for 64?
Rick York 8-Feb-20 0:43am    
If you set up the code correctly, each call will split it into fourths. So, yes, call it three times.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900