私はandroidstudioでアプリ開発を行っています。
以下のプログラムは赤い領域を見つけ出し、その中心に点を描画するものです。下の方に書いてあるx,yをテキストとして表示させるにはどうすればいいでしょうか?
java
1 2import android.app.Activity; 3import android.os.Bundle; 4import android.util.Log; 5import android.view.MenuItem; 6import android.view.SurfaceView; 7import android.view.WindowManager; 8import org.opencv.android.BaseLoaderCallback; 9import org.opencv.android.CameraBridgeViewBase; 10import org.opencv.android.LoaderCallbackInterface; 11import org.opencv.android.OpenCVLoader; 12import org.opencv.core.Core; 13import org.opencv.core.CvType; 14import org.opencv.core.Mat; 15import org.opencv.core.MatOfPoint; 16import org.opencv.core.MatOfPoint2f; 17import org.opencv.core.Point; 18import org.opencv.core.RotatedRect; 19import org.opencv.core.Scalar; 20import org.opencv.core.Size; 21import org.opencv.imgproc.Imgproc; 22 23 24import java.util.ArrayList; 25import java.util.List; 26 27 28 29public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener { 30 private static final String TAG = "OCVSample::Activity"; 31 32 int i =0; 33 double x=0; 34 double y=0; 35 36 private CameraBridgeViewBase mOpenCvCameraView; 37 private boolean mIsJavaCamera = true; 38 private MenuItem mItemSwitchCamera = null; 39 40 41 private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { 42 @Override 43 public void onManagerConnected(int status) { 44 switch (status) { 45 // 読み込みが成功したらカメラプレビューを開始 46 case LoaderCallbackInterface.SUCCESS: 47 { 48 Log.i(TAG, "OpenCV loaded successfully"); 49 mOpenCvCameraView.enableView(); 50 } break; 51 default: { 52 super.onManagerConnected(status); 53 }break; 54 } 55 } 56 }; 57 58 public MainActivity() { 59 Log.i(TAG, "Instantiated new " + this.getClass()); 60 } 61 62 @Override 63 protected void onCreate(Bundle savedInstanceState) { 64 Log.i(TAG, "called onCreate"); 65 super.onCreate(savedInstanceState); 66 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 67 setContentView(R.layout.activity_main); 68 69 mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view); 70 71 mOpenCvCameraView.setVisibility(SurfaceView.INVISIBLE); 72 73 mOpenCvCameraView.setCvCameraViewListener(this); 74 } 75 76 @Override 77 protected void onPause() { 78 super.onPause(); 79 if (mOpenCvCameraView != null) 80 mOpenCvCameraView.disableView(); 81 } 82 83 @Override 84 protected void onResume() { 85 super.onResume(); 86 if (!OpenCVLoader.initDebug()) { 87 Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization"); 88 OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback); 89 } else { 90 Log.d(TAG, "OpenCV library found inside package. Using it!"); 91 mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS); 92 } 93 } 94 95 96 @Override 97 protected void onDestroy() { 98 super.onDestroy(); 99 if (mOpenCvCameraView != null) 100 mOpenCvCameraView.disableView(); 101 } 102 103 104 @Override 105 public void onCameraViewStarted(int width, int height) {// カメラプレビュー開始時に呼ばれる 106 } 107 108 @Override 109 public void onCameraViewStopped() {// カメラプレビュー終了時に呼ばれる 110 } 111 112 @Override 113 public Mat onCameraFrame(Mat inputFrame) { 114 Mat src = inputFrame; 115 Mat src1 = src.clone(); 116 Imgproc.cvtColor(src, src, Imgproc.COLOR_RGB2HSV);//HSVに変換 117 Imgproc.medianBlur(src, src, 5); 118 Core.inRange(src, new Scalar(150,100,100),new Scalar(180,255,255),src); 119 Mat hierarchy=Mat.zeros(new Size(5,5), CvType.CV_8UC1); 120 List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 121 Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1); 122 //Scalar color=new Scalar(200,20,100); 123 // Imgproc.drawContours(src1, contours, -1, color,10); 124 int i=0; 125 int index = -1; 126 double area = 0; 127 for(i=0;i<contours.size();i++) 128 { 129 double tmp = Imgproc.contourArea(contours.get(i)); 130 if (area < tmp) { 131 area = tmp; 132 index = i; 133 } 134 } 135 if (index != -1) { 136 MatOfPoint ptmat= contours.get(index); 137 Scalar color=new Scalar(0,200,0); 138 MatOfPoint2f ptmat2 = new MatOfPoint2f( ptmat.toArray() ); 139 RotatedRect bbox=Imgproc.minAreaRect(ptmat2); 140 Point point = bbox.center; 141 x = point.x; 142 y = point.y; 143 Point point2 = new Point(x,y); 144 Imgproc.circle(src1, bbox.center,5, color,-1); 145 } 146 return src1; 147 } 148 149} 150
あなたの回答
tips
プレビュー