Find the new position of rectangle for resized image - image

I have a four element position vector [xmin ymin width hight] that specifies the size and position of crop rectangle from image I. How can i find the new position and size for the resized image I?

It is not entirely clear, what you want, as we don't know your coordinate system. Assuming x is the horizontal axis and y is the vertical axis and your point (1,1) is at the top left corner, you can use the following snippet:
p = [xmin ymin width height];
I = I_orig(p(2):p(2)+p(4)-1,p(1):p(1)+p(3)-1);
The size is of course your specified width and height.

You can convert your original bounding box to relative values (that is assuming the image size is 1x1)
[origH origW] = size( origI(:,:,1) );
relativeBB = [xmin / origW, ymin / origH, width / origW, hight / origH];
Now, no matter how you resized your origI, you can recover the bounding box w.r.t the new size from the relative representation:
[currH currW] = size(I(:,:,1));
currBB = relativeBB .* [currW, currH, currW, currH];
You might need to round things a bit: you might find floor better for xmin and ymin and ceil more suitable for width and height.

Related

How to scale a rotated rectangle to always fit another rectangle

the background of my question is the following.
I have a picture and a crop rectangle which describes how the picture should be cropped to produce the resulting picture. The crop rectangle is always smaller or at maximum the size of the picture.
Now it should be possible to rotate the crop rectangle.
This means that when rotating the crop regtanle inside the picture, the crop must be scaled in order that its extends does not exceed the photo.
Can anybode help me with a formula of how to compute the scale of the crop rectanlge based on the axis aligned photo regtancle?
My first attempt was to compute a axis aligned bounding box of the crop rectanlge and than make this fit it the photo rectangle. But somehow i get stuck with this approach,
Edited:
One more think to note:
- The crop rectangle can have other dimension and another center point inside the surrounding rectangle. This means the crop rectangle can be much smaller but for example is located at the lower left bound of the picture rectangle. So when rotating the smaller crop it will also exceed its limits
Thanks in advance
Sebastian
When you rotate an axis-aligned rectangle of width w and height h by an angle φ, the width and height of the rotated rectangle's axis-aligned bounding box are:
W = w·|cos φ| + h·|sin φ|
H = w·|sin φ| + h·|cos φ|
(The notation |x| denotes an absolute value.) This is the bounding box of the rotated crop rectangle which you can scale to fit the original rectangle of width wo and height ho with the factor
a = min(wo / W, ho / H)
if a is less than 1, the rotated crop rectangle fits inside the original rectangle and you don't have to scale. Otherwise, reduce the crop rectangle to the scaled dimensions
W′ = a·W
H′ = a·H
You could start checking if the dimension of the cropped rectangle fit in the old rectangle:
bound_x = a * cos(theta) + b * sin(theta)
bound_y = b * cos(theta) + a * sin(theta)
Where a and b are the new dimensions, theta us the angle and bound_x and bound_y should be smaller of the original rectangle.

Crop an image based on its binary - Matlab

I wish to crop this original image
to a new image which will contain only the bag with minimum white pixels (essentially reducing the size image to the bag borders)
.
Therefore I decided to first convert it to a binary image
but I do not know how to find the bag corner coordinates [xmin ymin width height] in order to use them with imcrop(I,rect).
Any help will be great.
The script:
clc;
close all;
url='http://oi65.tinypic.com/i19md1.jpg' ;
rgbImage = imread(url);
grayImage = rgb2gray(rgbImage);
binaryImage = grayImage < 250;
imshow(binaryImage);
That's a very easy task to perform. Since binaryImage contains a mask that you want to use to crop the image, you can find the top-left corner (xmin,ymin) of where you want to crop by finding the smallest column and row coordinate respectively that is non-zero in the mask, then to find width and height, find the bottom-right corner that is non-zero, then subtract the two x coordinates for the width and the two y coordinates for the height. You'll need to add 1 to each difference to account for self-distances (i.e. if you had a width that was 1 pixel wide, you should get a width of 1, not 0). You can use find to help you find the row and column locations that are non-zero. However, imcrop requires that the x coordinates reflect horizontal behaviour and y coordinates reflect vertical behaviour where find returns row and column locations respectively. That's why you'll have to flip them when you call find:
[y,x] = find(binaryImage); %// Find row and column locations that are non-zero
%// Find top left corner
xmin = min(x(:));
ymin = min(y(:));
%// Find bottom right corner
xmax = max(x(:));
ymax = max(y(:));
%// Find width and height
width = xmax - xmin + 1;
height = ymax - ymin + 1;
You can now go ahead and crop the image:
out = imcrop(rgbImage, [xmin ymin width height]);
imshow(out);
I get this for your cropped image:

How to crop rectangle box in image in matlab code

I want to crop a detected faces in my code. Here is my code.
function DisplayDetections(im, dets)
imshow(im);
k = size(dets,1);
hold on;
for i=1:k
rectangle('Position', dets(i,:),'LineWidth',2,'EdgeColor', 'r');
end
imcrop(rectangle);
hold off;
Their is syntax error in cropping.
Can anybody help in cropping rectangle box detected in above box.
That code only draws the rectangles in your image. If you actually want to crop out portions of the image with the defined rectangles, use imcrop.
As such, you would do something like this to store all of your cropped rectangles. This is assuming that im and dets are already defined in your code from your function:
k = size(dets,1);
cropped = cell(1,k);
for i=1:k
cropped{k} = imcrop(im, dets(i,:));
end
cropped would be a cell array where each element will store a cropped image defined by each rectangle within your dets array. This is assuming that dets is a 2D array where there are 4 columns, and the number of rows determines how many rectangles you have. Each row of dets should be structured like:
[xmin ymin width height]
xmin, ymin are the horizontal and vertical co-ordinate of the top-left corner of the rectangle, and width and height are the width and height of the rectangle.
If you want to access a cropped portion in the cell array, simply do:
crp = cropped{k};
k would be the kth rectangle detected in your image.

object dimensions in image according to camera distance faraway

I have a camera placed 10 meters faraway from a portrait (rectangle) having width = 50cm and height = 15cm, I want to get the dimensions of this portrait inside the image captured. The image captured has width=800 px and height=600 px.
How can I calculate the dimensions of the portrait inside the image? Any help please?
I am assuming the camera is located along the center normal of the portrait, looking straight at the portrait's center.
Let's define some variables.
Horizontal field of view: FOV (you need to specify)
Physical portrait width: PW = 50 cm
Width of portrait plane captured: CW cm (unknown)
Image width: IW = 800 px
Width of portrait in image space: X px (unknown)
Distance from camera to subject: D = 10 m
We know tan(FOV) = (CW cm) / (100 * D cm). Therefore CW = tan(FOV) * 100 * D cm.
We know PW / CW = X / IW. Therefore X = (IW * PW) / (tan(FOV) * 100 * D) px.
I agree with Timothy's answer, in that you need to the know the camera's field of view (FOV). I'm not sure I totally follow/agree with his method however. I think this is similar, but it differs, the FOV needs to be divided by two to split our view into two right-angled triangles. Use tan(x)=opposite/adjacent
tan(FOV/2) = (IW/2) / (Dist * 100)
where IW is the true image width (must divide by two as we are only finding finding half of the width with the right-angled triangle), Dist is the distance from the camera to the portrait (converted to cm).
Rearrange that to find the Width of the entire image (IW):
IW = tand(FOV/2) * (2*Dist*100)
You can now work out the width of each pixel (PW) using the number of pixels in the image width (800 for you).
PW = IW / NumPixels
PW = IW / 800
Now divide the true width by this value to find the number of pixels.
PixelWidth = TrueWidth / PW
The same can be done for the height, but you need your camera's field of view.
Im not sure this is the same a Timothy's answer, but I'm pretty sure this is correct.

A algorithm for scaling game world depending on character position

I am building a 2 player game and at the moment the scroll position is determined by the midpoint of the 2 characters positions. But i have a problem. When the characters goes to far apart the scroll position of the screen is in the middle of these characters and no one is visible because they moved out of view.
So I am looking for an algorithm to determine the scale that the world should be at for the characters to be in view.
Your time is valued.
Assuming that your screen (or window) is fixed size, (Screen_Width, Screen_Height).
Now you compute the X and Y distances between the two players.
xdist = abs(Player2.x - Player1.x);
ydist = abs(Player2.y - Player1.y);
Compute the x and y scaling factors:
xscale = Screen_Width / xdist;
yscale = Screen_Height / ydist;
And take the smaller value.
scale = min(xscale, yscale);
Now, if scale >= 1, then the players will fit on the screen. You don't have to do any scaling. You can just find the midpoint and draw.
If scale < 1, then you have to scale all of your coordinates by scale.
Without having any information as to the type of units, IDE, or language you are using here is a possible solution:
function object calcScale(screen, player1, player2, midpoint):
width = abs(player1.x-player2.x)
height = abs(player1.y-player2.y)
width = max(width,height)
height = width
x = min(player1.x, player2.x, midpoint.x-width/2)
y = max(player1.y, player2.y, midpoint.y+height/2)
s = (screen.width*screen.height) / (width*height)
newscreen= {startx:x, starty:y, scale:s}
return newscreen
This assumes a square viewport with the players centered at all time.

Resources