我是OpenCV的新手,我不知道为什么会发生这种异常。
if (!frame.empty())
{
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
// convert the image to gray scale
Mat dilateElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(24,24));
Mat erodeElement = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,12));
Imgproc.cvtColor(frame, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(gray, gray,128, 255, Imgproc.THRESH_BINARY);
Imgproc.blur(gray, gray, new Size(7,7));
Imgproc.erode(gray, gray, erodeElement);
Imgproc.dilate(gray, gray, dilateElement);
Imgproc.findContours(gray, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
while(contours != null)
{
MatOfPoint thisContour = new MatOfPoint();
for (int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0]
{
MatOfPoint2f thisContour2f = new MatOfPoint2f();
MatOfPoint approxContour = new MatOfPoint();
MatOfPoint2f approxContour2f = new MatOfPoint2f();
thisContour.convertTo(thisContour2f, CvType.CV_32FC2);
Imgproc.approxPolyDP(thisContour2f,approxContour2f,0.02,true);
approxContour2f.convertTo(approxContour, CvType.CV_32S);
if(approxContour.size().height==4)
{
Rect ret = Imgproc.boundingRect(approxContour);
text_field.setText("nice");
}
我已经从c ++转换了代码。这就是错误。
OpenCV Error: Assertion failed (npoints >= 0 && (depth == CV_32S || depth == CV_32F)) in cv::approxPolyDP, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\approx.cpp, line 679 Exception during the image elaboration: CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\approx.cpp:679: error: (-215) npoints >= 0 && (depth == CV_32S || depth == CV_32F) in function cv::approxPolyDP]
我已经尝试了一切来解决它但没有成功。