如何將List<MatOfPoint> contours
轉換爲Rect[] contoursArray
,以便我可以使用Core.rectangle(mRgba, contoursArray[i].tl(), contoursArray[i].br(), (255,0,0,255), 3)
在其上繪製矩形?Android OpenCV - 在輪廓上繪製矩形
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
//Touch screen to track by color and draw rectangle on it
if (mItem_select && Color_Select){
mDetector.process(mRgba);
List <MatOfPoint> contours = mDetector.getContours();
Log.e(TAG, "Contours count: " + contours.size());
Imgproc.drawContours(mRgba, contours, - 1, Rectangle_Color);
//Draw rectangle on it
Rect[] contoursArray = contours.toArray(); //error:Type mismatch: cannot convert from Object[] to Rect[]
Core.rectangle(mRgba, contoursArray.tl(), contoursArray.br(), (255, 0, 0, 255), 3); //error:Cannot invoke tl() on the array type Rect[]、Cannot invoke br() on the array type Rect[]
Mat colorLabel = mRgba.submat(4, 68, 4, 68);
colorLabel.setTo(mBlobColorRgba);
Mat spectrumLabel = mRgba.submat(4, 4 + mSpectrum.rows(), 70, 70 + mSpectrum.cols());
mSpectrum.copyTo(spectrumLabel);
}
return mRgba;
}
啊,不要試圖將積分轉換爲矩形,而不是從輪廓採取外接矩形框,並繪製矩形! http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html#boundingRect(org.opencv.core.MatOfPoint) – berak
這可能有助於http://docs.opencv.org/doc/tutorials/ imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html#邊界,rects-圈 – user2727765