Calculating position in Bitmap Image - android

I have floor plan image and draw it into UI with bitmap in scaled example 500 x 500 pixel. The actual image is 700x700 pixel.
With normal size (700px x 700px) floorplan i could draw circle in direct position example (50px , 75px) and it give the corect result in normal size of floorplan.
This circle is for show current location of user.
The question is how to position that circle if the image is scaled into 500x500 pixel with correct position same as (50px ,75px) in (700px x 700px) floorplan ?

Dosn't this come down to a simple math problem?
The coordinates 50,75 in a 700*700 image would correspond to the coordinates 35(,71..), 53(,57..) in a 500*500 image. Fraction can be omitted, since, well, they're pixels.
The scaling factor would be 500/700 which is roughly 0,714. IE
ScalingFactor = NewDimension / OriginalDimension
ScaledPosition = OriginalPosition * ScalingFactor
This is obviously simplified because it is a square image, and scaling is the same among both sides.

Related

Get position of the circle in the image, circle is drawn on the camera while taking the image

I've an image which has a object(circle) placed on it while taking a picture in an android app. The circle center on the camera is the mid of the screen height and width and circle radius is 10% of the image.
Now, in opencv, I want to get exact position of the cirlce. Dividing number of cols and rows by 2 won't give exact circle center nor 10% of the cols gives exact radius.
How can I get circle center and radius in pixels in opencv?
For a particular example, camera width is 1080 and height is 1704. Image width and height taken by the camera in pixels is 3120 and 4160. By taking half of the width and height would give me the circle ceter, which should be 1560, 2080 and center should be 210(10% of the image width). But this calculations differs from exact circle values on the image.
This is the circle placed on camera while taking the image. Every image taken would also have the circle too. I want to get this circle parameters in the image.

How to convert coordinates on Bitmap to real coordiates on Image View displayed on screen

I have an Image View which displays an image (e.g 2000x1000 pixels) and I have a coordinate (X,Y) on that image (not the image view). The canvas of my Image View is 600x800 for example. How can I convert the point (X,Y) to screen coordinate so that I can draw a path with them on the OnDraw(...) method of Image View. Any help is appreciated! Thank you.
Update: If I use matrix to draw the path between coordinates, it works but the path and objects i draw become really small. Here is the code i used.
final Matrix matrix = canvas.getMatrix();
matrix.preConcat( _view.getImageMatrix() );
matrix.preScale( 1.0f /_inSampleSize, 1.0f / _inSampleSize);
canvas.setMatrix( matrix );
//I draw the path here
Update: I add a picture to show the effect when using matrix to draw the path. I would like to have the 4 line and the 4 corner balls to be in normal size. The red color is the boundary of the Image View which holds the picture.
I think that might depend on how exactly you are displaying your image. Your ImageView (600x800) is not the same aspect ratio as your bitmap (2000x1000).
You are keeping the bitmap's aspect ratio stable as you scale it down? If so, which part (height or width) takes up the full screen and which has black (or whatever else) as padding? This will help you determine your scale factor.
scale_factor = goal_height/height1; //if height is what you are scaling by
scale_factor = goal_width/width1; //if width is what you are scaling by.
I would try:
x_goal = x1 * scale_factor;
y_goal = y1 * scale_factor;
That is, if you have a point (1333, 900) in your image, and your image takes up the full width, you would multiply both x and y by 600/2000 to get (399.9, 270). (you might want to round that decimal).
If you are NOT keeping the bitmaps aspect ratio stable (that is, you're squeezing it to fit), then you'd have a height_scale_factor and a width_scale factor. So you'd take (1333,900) and multiply x by 600/2000 and y by 800/1000 to get (399.9,720).

Android converting touch points to actual bitmap points on an image

Here is my problem:
I have an Android application that displays an image. The image itself is resized to 480 x 640 regardless of size.
The user can click on multiple points of the image. Based on where the user clicks on the image, the bitmap itself has some warping applied to it.
So let's say the original image is 1000 x 2000 (using whole numbers to make it simpler).
Once the image is loaded into the ImageView, it scales to display properly in the imageview.
This is obviously different for different phones with different resolutions.
Now when the user clicks on different points, I ultimately want to pass those points to my WCF Service along with the bitmap data to perform some image manipulation.
So the problem for me is how to take the points where the user touched on the phone and convert those to points that are relative to the normal unscaled bitmap.
Summary:
Bitmap is scaled to fit. User Clicks at 100,100. 100,100 is the point relative to the scaled image...not the actual bitmap itself. I'm looking for guidance on how to convert that 100,100 to the point on the actual bitmap.
Thanks in advance for any help you can give.
ok, so the Android ImageView has a default ScaleType of FIT_CENTER, so that means:
public static final Matrix.ScaleToFit CENTER
Compute a scale that will maintain the original src aspect ratio, but
will also ensure that src fits entirely inside dst. At least one axis
(X or Y) will fit exactly. The result is centered inside dst.
so if you're whole image view has 480x640 to show the image, and for example your image is 1000x2000, then:
2000/640 = scaleFactor = 3.125/1
so width will scale down to 320 leaving 80 pixels on either side empty, so it can maintain the aspect ratio.
//this one will be 80.
int xBuffer= (imageViewWidth - (realImageWidth*scaleFactor))/2;
//this one will be zero in your example
int yBuffer = (imageViewHeight - (realImageHeight*scaleFactor))/2;
int imageViewX = 0;//x coord where on the image view it was clicked
int imageViewY = 0;//y coord where on the image view it was clicked
if (imageViewX < xBuffer || imageViewX > imageViewWidth-xBuffer)
{
//ignore the click, outside of your image.
}
else if (imageViewY < yBuffer || imageViewY > imageViewHeight-yBuffer)
{
//ignore the click, outside of your image.
}
else
{
realImageY = imageViewY * scaleFactor;
realImageX = (imageViewY - 80) * scaleFactor;
//save click somehow..
saveClick(realImageX,realImageY);
}

Android Opencv calculate size of rotated image inside rectangle

I want to calculate rotated image size, Image is inside rectangle. I have rectangle width, height and angle of rotated image. Any one tell me
how to calculate rotated image size?
So you have width, height and angle means you already got RotatedRect.
Now using the method
Rect RotatedRect::boundingRect();
you can easly calculate the bounding box for rotated rect.
for more info see RotatedRect.
Edit:
As per your comment below is the way how to find the width and height of rotated rect.
So you know the four corners of rectangle, lets say (x1,y1),(x2,y2),(x3,y3),(x4,y4), now you need to find the transformed point after rotation by the given angle, let it be (xT1,yT1),(xT2,yT2),etc...
where
xT = x0+(x-x0)*cos(theta)+(y-y0)*sin(theta)
yT = y0-(x-x0)*sin(theta)+(y-y0)*cos(theta)
here (x0,y0) is the center around which you are rotating. and theta = angle * CV_PI / 180.0
Using above calculate four transformed points, finally calculate the height and width by finding the distance between transformed points.

Bitmap coordinates after canvas.scale

I need help at getting the correct coordinates when touching the canvas.
I have an image that is 1240x1756
The user can scroll around on this bitmap. It is the bitmap attached to the canvas. I do this by translating the canvas. canvas.translate()
The user can also place a new bitmap and move that anywhere around on the canvas. up to 256x256.
Up until this point I have no problem calculating the touch coordinates at scale factor of 1.0. I take the screen touch coords and add the offset of the image and then check to see if the 256x256 moveable bitmap intersects the touch coords.
However my issue is getting the correct coordinates after the canvas has been scaled. 0.1-1.0 are my minimum and maximum scaling values.
Can anyone point me in the right direction for a working algorithm?
You should be able to just multiply the pixel offset by 1/scale factor.

Categories

Resources