Ask Your Question

OpenCV (Java) : Draw a rectangle region on Camera View

asked 2018-10-28 21:51:25 -0500

joyceeeen gravatar image

I 'm trying to create a bank card scanner on android using OpenCV, first, I'm creating a region where the user can scan their cards then crop it after, I'm struggling with the rectangle region and place it on the center, Any suggestion on how I can do it? Thank you so much

Here's what I've done so far:

It's doing a rectangle and its centered but the size isn't enough I tried to change the numbers but the position isn't centering.

 public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        Mat mrgba = inputFrame.rgba();

        int w = mrgba.width();
        int h = mrgba.height();

        Imgproc.rectangle(mrgba, new Point(w * 1 / 3, h * 1 / 3), new Point(
                w * 2 / 3, h * 2 /  3 ), new Scalar( 255, 0, 0 ), 5

        return mrgba;


image description

edit retag flag offensive close merge delete


hey, it's just a maths problem ..

berak gravatar imageberak ( 2018-10-29 02:47:38 -0500 )edit

well, can you help me with that? I don't really quite understand how rect works. I need it to be dynamically centered no matter what the screen size is

joyceeeen gravatar imagejoyceeeen ( 2018-10-29 04:20:42 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-29 04:26:27 -0500

berak gravatar image

updated 2018-10-29 04:27:55 -0500

again, it's only a simple maths problem.

assuming, your screen is SW, SH, and the desired rect size is RW,RH, then the center of the screen is:

(SW/2, SH/2)

and the rectangle coords are

tl : (SW/2 - RW/2, SH/2 - RH/2)  and  br : (SW/2 + RW/2, SH/2 + RH/2)
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-10-28 21:51:25 -0500

Seen: 82 times

Last updated: Oct 29 '18