2017-08-13 12 views
0

を取得:OpenCVの:私はそうのような関心のある領域の周りに外接矩形を検出して描画することができる午前rect.brからのx座標()

enter image description here

今、私は "を見つける必要があります緑の境界の四角形の下の水平線の「x」値。私の主な目的は、バッテリーのベースの "x"値と、紙の青い帯の "x"値を見つけて、それらの間の距離を計算できるようにすることです。

rect.tl()rect.br()の2つの値しかありませんが、境界矩形Imgproc.rectangle(sourceMat, rect.tl(), rect.br(), green, 3);を描画するのに使用できます。私は境界矩形の右下の点からの "x"値がピクセル点のx座標になるという前提の下にあります。

E/BR::{793.0、1686.0}

私はそう Log.e("BR", rect.br().toString());よう rect.br()を印刷するとき

は、どのように私はこれを取得rect.br() からの 'x' の値を得るのですか

private Bitmap findRoi(Bitmap sourceBitmap) { Bitmap roiBitmap = null; Scalar green = new Scalar(0, 255, 0, 255); Mat sourceMat = new Mat(sourceBitmap.getWidth(), sourceBitmap.getHeight(), CvType.CV_8UC3); Utils.bitmapToMat(sourceBitmap, sourceMat); Mat roiTmp = sourceMat.clone(); final Mat hsvMat = new Mat(); sourceMat.copyTo(hsvMat); // convert mat to HSV format for Core.inRange() Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV); Scalar lowerb = new Scalar(85, 50, 40); // lower color border for BLUE Scalar upperb = new Scalar(135, 255, 255); // upper color border for BLUE Core.inRange(hsvMat, lowerb, upperb, roiTmp); // select only blue pixels // find contours List<MatOfPoint> contours = new ArrayList<>(); List<RotatedRect> boundingRects = new ArrayList<>(); Imgproc.findContours(roiTmp, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); // find appropriate bounding rectangles for (MatOfPoint contour : contours) { MatOfPoint2f areaPoints = new MatOfPoint2f(contour.toArray()); RotatedRect boundingRect = Imgproc.minAreaRect(areaPoints); double rectangleArea = boundingRect.size.area(); // test min ROI area in pixels if (rectangleArea > 40001) {//400000 Point rotated_rect_points[] = new Point[4]; boundingRect.points(rotated_rect_points); Rect rect = Imgproc.boundingRect(new MatOfPoint(rotated_rect_points)); // test horizontal ROI orientation if (rect.width > rect.height) { Imgproc.rectangle(sourceMat, rect.tl(), rect.br(), green, 3); } } } roiBitmap = Bitmap.createBitmap(sourceMat.cols(), sourceMat.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(sourceMat, roiBitmap); return roiBitmap; } 
+0

これはC++とよく似ていません。Javaとよく似ています。だから、['Rect'](http://docs.opencv.org/java/2.4.2/org/opencv/core/Rect.html)のドキュメントを見てください。' br() 'は' '' Point'](http://docs.opencv.org/java/2.4.2/org/opencv/core/Point.html)には、 'x'と' y'という2つのメンバがあります。 –

答えて

1

私はJavaについてはわかりませんが、C++と似ている必要があると思いますので、私のコードはC++で書かれています。

ので、私はOpenCVの

Rect r = Rect(20,20,30,20); // Rect(x,y,width,height) 

に構造上の書き込みと私はあなたがすることによって、コード

int x2=r.br().x ; 

下に欲しいものにアクセスすることができますが、画像下の enter image description here

のような長方形を持っていると仮定します下の画像はストーリー全体です

enter image description here

関連する問題