简体   繁体   中英

How to detect contours width and height in android opencv

i created contours and draw a rectangle. now need to detect my rectangle width and height how do do it?

ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(ImageMatout, contours, new Mat(), Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_NONE);
Core.rectangle(ImageMatin, rect.tl(), rect.br(), new Scalar(255, 0, 0),2);  
//double h = ??
//double w = ??

try this method 1: rect.width for width and rect.height for height

OR Method 2:

double x1= rect.tl().x;
double y1= rect.tl().y;
double x2=  rect.br().x;
double y2= rect.br().y;

(x1,y1) is the coordinate point of the top-left corner of rectangle. (x2,y2) is the coordinate point of the Bottom-Right corner of rectangle. Height and width can be calculated as

double height=x2-x1;   //height or length
double width=y2-y1;    // width or breadth

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM