2

I have taken the current frame under mRGBA in this fashion:

mRgba = inputFrame.rgba();

then created a rectangle object which takes the height and width of the current camera frame

rect = new Rect();
rect.width = mRgba.width();
rect.height = mRgba.height();

it takes the whole space of the frame but when i try to shrink this rectangle it got shrink in a one side(not as a whole which i need)

So i tried to find the rectangles center and then tried to create another rectangle according to that center and a predefined size

int x = (int) (rect.tl().x + rect.br().x)/2;
int y = (int) (rect.tl().y + rect.br().y)/2;

Rect rect1 = new Rect(x,y,280,280);

Imgproc.rectangle(mRgba, rect1.tl(), rect1.br(), new Scalar(255, 0, 0), 2, 8, 0);

But yet its not in the center!! i am not sure about the parameters the rectangle object takes i didn't find the document of opencv that much helpful. So how to overcome this situation i want the rectangle to be exactly at the center of the camera frame.

Rectangle not in the center

Fay007
  • 2,707
  • 3
  • 28
  • 58

1 Answers1

5

Base on my observation, the line below is actually creating a rectangle at a corner (x,y) with width 280 and height 280.

 Rect rect1 = new Rect(x,y,280,280);

 width(280)
<------(x,y)
         |
         |   height(280) 
         |
         V

so, your calculation for the point of center should be correct.

I hope the codes below may help you.

int width = 280;
int height = 280;
Rect rect1 = new Rect(x - width / 2, y - height / 2, width, height);

EDIT:

Thanks for Micka's reminder. My concept above is incorrect, although it works.

The Android camera is landscape in default. Usually, we rotate the image 90. (Ref: Android - Camera preview is sideways)

Your calculation is based on a landscape image and openCV's coordinate system which is different from what we usually use. (Ref: Reference coordinate system changes between OpenCV, OpenGL and Android Sensor)

It's difficult to explain in words, so I draw a picture for you :) enter image description here

Community
  • 1
  • 1
VoidExplorer
  • 220
  • 1
  • 5