我编写代码来检测open cv中的矩形。我能够检测到很少的物体,但我无法检测物理门或大矩形。如果我错了,请检查我的代码并纠正我。另一个问题是这个代码不能不断检测矩形所以当我绘制矩形它来来去去时,它会看起来很糟糕。任何在每一帧中定期检测的方法。
Mat output= getGray(inputFrame.rgba(),inputFrame.rgba());
Imgproc.medianBlur(output, output, 5);
Imgproc.erode(output, output, new Mat());
Imgproc.dilate(output, output, new Mat());
Mat edges = new Mat();
Imgproc.Canny(output, output, 5, 50);
// Vector<MatOfPoint> vector=new Vector<MatOfPoint>();
// Imgproc.findContours(output, points, output, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
contours.clear();
Imgproc.findContours(output, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
MatOfPoint2f approxCurve = new MatOfPoint2f();
rgbImage=inputFrame.rgba();
mDrawnContours.clear();
> Blockquote
for(int i=0;i< contours.size();i++){
MatOfPoint tempContour=contours.get(i);
MatOfPoint2f newMat = new MatOfPoint2f( tempContour.toArray() );
int contourSize = (int)tempContour.total();
Imgproc.approxPolyDP(newMat, approxCurve, contourSize*0.15, true);
MatOfPoint points=new MatOfPoint(approxCurve.toArray());
if((Math.abs(Imgproc.contourArea(tempContour))<100) || !Imgproc.isContourConvex(points)){
Log.i(TAG, "::onCameraFrame:" + " too small");
appendLog("Too small");
continue;
}
else if(points.toArray().length >= 4 && points.toArray().length <= 6){
int vtc = points.toArray().length;
Vector<Double> cosList=new Vector<Double>();
for (int j = 2; j < vtc+1; j++){
cosList.add(angle(points.toArray()[j%vtc], points.toArray()[j-2], points.toArray()[j-1]));
}
double mincos = getMin(cosList);
double maxcos = getMax(cosList);
Log.i(TAG, "::onCameraFrame:" + "mincos:"+mincos+"maxcos:"+maxcos);
if (vtc == 4 && mincos >= -0.1 && maxcos <= 0.3)
{
mTotalSquare++;
Imgproc.drawContours(rgbImage, contours, i, new Scalar(0,0,255));
DrawnContours contours2=new DrawnContours();
contours2.setIndex(i);
mDrawnContours.add(contours2);
Log.i(TAG, "::onCameraFrame:" + "found");
appendLog("found");
}
else{
Log.i(TAG, "::onCameraFrame:" +" not found " +"mincos:"+mincos+"maxcos:"+maxcos);
appendLog("not found 1");
}
}
return rgbImage
如果您有任何问题,请与我们联系。
答案 0 :(得分:0)
我想,大轮廓有4个以上的边缘。它们的轮廓由大量短线段组成(取决于行中的近似函数参数
Imgproc.approxPolyDP(newMat, approxCurve, contourSize*0.15, true);
)。
你有检查边数的条件:
points.toArray().length <= 6