how can i crop particular Region of image as a Square, if x and y coordinate value of center of Region and radius of circle enclosing the Region are given. Example: x=595, y=160 and r=68

How can i crop particular Region of image as a Square, if x and y coordinate value of center of Region and radius of circle enclosing the Region. Example: x=595, y=160 and r=68

Answers (1)

ranjit - since the circle encloses the square, then let us assume that the four corners of the square intersect with the circle. Since we know the radius of the circle, then we know the distance from the centre of the square to each of its four corners. Using the trigonometric ratio, you can then determine the coordinates for each of the corners. For example, the top right corner coordinate can be computed as
r = 68;
xCtr = 595;
yCtr = 160;
x1 = floor(r*sind(45)) + xCtr;
y1 = floor(r*cosd(45)) + yCtr;
In the above, we know that the angle from the centre of the square to its top-right corner is 45 degrees. (What would be the angles for each of the other three corners?) We use floor because we want to ensure that we have an integer coordinate for our corner because we will use this when cropping the image. Since this is a homework question, I will leave you with the calculations for the remaining corners (though you will only need to do this for three - why?).
Once you have at least three corners, say top-right (x1,y1), bottom-right (x2,y2), and bottom-left (x3,y3), you will be able to crop your image, myImage, as
myCroppedImg = myImg(y2:y1,x3:x2,:);
Note how we use y2:y1 when accessing the rows of myImg and x3:x2 when accessing its columns. The above should work fine unless any of your corners fall outside of myImg because the radius of the circle extends beyond the borders of the image. You will need to consider this when writing the above code.
Try the above and see what happens!

1 Comment

Or you can use ceil() instead of floor() to compute x to make sure you capture the whole pixel. So if x1 would be 643.083261120685 then it would round to the right to be 644 instead of 643 which floor() would give you.
x1 = ceil(r*sind(45)) + xCtr
y1 = floor(r*cosd(45)) + yCtr
You have to decide at each corner whether to use floor() or ceil() to capture the circle completely. Also, remember for images that y=1 is at the top in increases going downwards, unlike the traditional direction for y in Cartesian cordinates, like what plot() assumes.
Also, there is lots of good info in the FAQ. You might like to look it over, for example this entry on plotting a circle: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F

Sign in to comment.

Categories

Tags

Asked:

on 6 Jan 2016

Commented:

on 9 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!