我在OpenCV中有点新鲜,我想练习一个简单的面部检测和图像裁剪。
具体来说,我使用return false;
从文件夹加载图像,然后检测到面部,在检测到的面部上绘制一个矩形,然后仅裁剪检测到的面部区域。
一切正常,检测到脸部,在当场绘制矩形。除了最后一部分:种植。我收到臭名昭着的 Restcomm
错误。以下是我的代码和错误:
2017-08-21 11:22:15,581 ERROR [org.mobicents.slee.runtime.sbbentity.SbbEntityImpl] (SLEE-EventRouterExecutor-1-thread-1) Failed to assign and create sbb object
java.lang.ClassCastException: org.mobicents.slee.resource.jdbc.JdbcResourceAdaptorSbbInterfaceImpl cannot be cast to org.restcomm.slee.resource.jdbc.JdbcResourceAdaptorSbbInterface
at sn.pmf.jdbc.jdbcSbb.setSbbContext(jdbcSbb.java:147)
at org.mobicents.slee.runtime.sbb.SbbObjectImpl.<init>(SbbObjectImpl.java:124)
at org.mobicents.slee.runtime.sbb.SbbObjectPoolFactory.makeObject(SbbObjectPoolFactory.java:146)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:974)
at org.mobicents.slee.runtime.sbb.SbbObjectPoolImpl.borrowObject(SbbObjectPoolImpl.java:68)
at org.mobicents.slee.runtime.sbbentity.SbbEntityImpl.assignSbbObject(SbbEntityImpl.java:744)
at org.mobicents.slee.runtime.eventrouter.routingtask.EventRoutingTaskImpl.routeQueuedEvent(EventRoutingTaskImpl.java:362)
at org.mobicents.slee.runtime.eventrouter.routingtask.EventRoutingTaskImpl.run(EventRoutingTaskImpl.java:128)
at org.mobicents.slee.runtime.eventrouter.EventRouterExecutorImpl$EventRoutingTaskStatsCollector.run(EventRouterExecutorImpl.java:72)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
错误:
OpenCV错误:断言失败(0&lt; = roi.x&amp;&amp; 0&lt; = roi.width&amp;&amp; roi.x + roi.width&lt; = m.cols&amp;&amp; 0&lt; cv :: Mat :: Mat中的; = roi.y&amp;&amp; 0&lt; = roi.height&amp;&amp; roi.y + roi.height&lt; = m.rows),文件C:\ build \ master_winpack -build-win64-vc14 \ opencv \ modules \ core \ src \ matrix.cpp,第522行
现在我明白错误显示了,因为public void setSbbContext(SbbContext context) {
this.tracer = context.getTracer("JdbcExampleSbb");
this.contextExt = (SbbContextExt) context;
this.jdbcRA = (JdbcResourceAdaptorSbbInterface) this.contextExt
.getResourceAdaptorInterface(JdbcResourceAdaptorSbbInterface.RATYPE_ID, "JDBCRA");
this.jdbcACIF = (JdbcActivityContextInterfaceFactory) this.contextExt
.getActivityContextInterfaceFactory(JdbcActivityContextInterfaceFactory.RATYPE_ID);
}
超出范围。这里有什么麻烦我。
当我尝试首先绘制矩形时,我不应该收到此错误吗?为什么我会在cv::glob
上收到错误但不会在我正在绘制的矩形上出现错误?
为什么Assertion Failed
超出范围?我用它上面绘制的矩形显示图像,一切看起来都很好。当void faceDetectFolder()
{
Mat source;
CascadeClassifier face_cascade;
face_cascade.load("C:/OpenCV-3.2.0/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml");
String path(path on my PC);
std::vector<cv::String> fn;
glob(path, fn, true);
for (size_t i = 0; i < fn.size(); i++)
{
source = imread(fn[i]);
if (source.empty()) continue;
std::string imgname = fn[i].substr(45, std::string::npos); //File name
std::vector<Rect> faces;
face_cascade.detectMultiScale(source, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
if (faces[i].width > 80 && faces[i].height*0.5 > 80) //Threshold, some detections are false
{
int x = faces[i].x;
int y = faces[i].y;
int h = y + faces[i].height;
int w = x + faces[i].width;
rectangle(source, Point(x, y), Point(w, h), Scalar(255, 0, 0), 2, 8, 0); //Drawing rectangle on detected face
imshow(imgname, source);
Rect roi;
roi.x = x;
roi.y = y;
roi.height = h;
roi.width = w;
Mat detectedface = source(roi);
imshow("cropped image", detectedface);
waitKey(0);
}
}
}
}
与绘制的矩形具有相同的值时,为什么会出现此错误?
请原谅我任何新手的错误,我们都从某个地方开始。感谢您阅读并度过美好的一天!
答案 0 :(得分:2)
在roi.height
和roi.width
中,尝试提供faces[i].height
和faces[i].width
分别。实际上你认为错误应该在之前出现,但它适用于绘图,因为矩形在两个对角线相对的顶点上作为参数,而在Rect roi
的情况下不是宽度/高度。您可以使用Rect
和Point(x, y)
初始化Point(w,h)
,它应该可以正常工作。