如何使用多边形或蒙版裁剪图像?这是仅在图像的特定区域检测对象所必需的。
Example red area
例如,我认为在这里裁剪图像https://github.com/akuzyagin/video-detection-example/blob/2a92fe0a17c8c499f348fb1db8ace465c4d1e3b2/src/main/java/DetectionService.java#L45C1-L47C72:
Image image = ImageFactory.getInstance().fromImage(frame);
//TODO crop image by polygon before detect objects
DetectedObjects predict = predictor.predict(image);
完整代码:https://github.com/akuzyagin/video-detection-example/
我尝试使用ai.djl.modality.cv.Image.getMask
方法传递一个坐标数组,但我不太明白这个图像裁剪方法是如何工作的。我也尝试了ai.djl.modality.cv.util.NDImageUtils
的各种方法,但似乎没有什么适合实现的方法。
还尝试转换为BufferedImage https://github.com/deepjavalibrary/djl/issues/1432并进一步裁剪图像Crop image by polygon area in Java,但在日志中存在以下错误:BufferedImage bufferedImage = (BufferedImage) result.getWrappedImage();
堆栈跟踪:
2023-09-06 14:30:40,104 ERROR [io.qua.run.Application] (main) Failed to start application (with profile [prod]): java.lang.ClassCastException: class org.opencv.core.Mat cannot be cast to class java.awt.image.BufferedImage (org.opencv.core.Mat is in unnamed module of loader io.quarkus.bootstrap.runner.RunnerClassLoader @45283ce2; java.awt.image.BufferedImage is in module java.desktop of loader 'bootstrap')
at DetectionService.detect(DetectionService.java:82)
at DetectionService_Bean.create(Unknown Source)
at DetectionService_Bean.create(Unknown Source)
at io.quarkus.arc.impl.AbstractSharedContext.createInstanceHandle(AbstractSharedContext.java:113)
at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:37)
at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:34)
at io.quarkus.arc.impl.LazyValue.get(LazyValue.java:26)
at io.quarkus.arc.impl.ComputingCache.computeIfAbsent(ComputingCache.java:69)
at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:34)
at DetectionService_Bean.get(Unknown Source)
at DetectionService_Bean.get(Unknown Source)
at io.quarkus.arc.impl.ArcContainerImpl.beanInstanceHandle(ArcContainerImpl.java:476)
at io.quarkus.arc.impl.ArcContainerImpl.beanInstanceHandle(ArcContainerImpl.java:489)
at io.quarkus.arc.impl.ArcContainerImpl.instance(ArcContainerImpl.java:287)
at DetectionService_Observer_Synthetic_ebc203dc60d4841a41795405dc16fe346fbb52eb.notify(Unknown Source)
at io.quarkus.arc.impl.EventImpl$Notifier.notifyObservers(EventImpl.java:328)
at io.quarkus.arc.impl.EventImpl$Notifier.notify(EventImpl.java:310)
at io.quarkus.arc.impl.EventImpl.fire(EventImpl.java:78)
at io.quarkus.arc.runtime.ArcRecorder.fireLifecycleEvent(ArcRecorder.java:131)
at io.quarkus.arc.runtime.ArcRecorder.handleLifecycleEvents(ArcRecorder.java:100)
at io.quarkus.deployment.steps.LifecycleEventsBuildStep$startupEvent1144526294.deploy_0(Unknown Source)
at io.quarkus.deployment.steps.LifecycleEventsBuildStep$startupEvent1144526294.deploy(Unknown Source)
at io.quarkus.runner.ApplicationImpl.doStart(Unknown Source)
at io.quarkus.runtime.Application.start(Application.java:101)
at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:108)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:71)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:44)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:124)
at ApplicationMain.main(ApplicationMain.java:10)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at io.quarkus.bootstrap.runner.QuarkusEntryPoint.doRun(QuarkusEntryPoint.java:61)
at io.quarkus.bootstrap.runner.QuarkusEntryPoint.main(QuarkusEntryPoint.java:32)
2条答案
按热度按时间cczfrluj1#
如果您的目标是缩小检测条件(例如特定区域),您甚至不需要对其进行裁剪:检查supervision,它正是您需要的。
vbopmzt12#
我找到了我的问题的答案,下面的代码允许你从图像中剪切出一个多边形:Cropped image
完整代码:https://github.com/akuzyagin/video-detection-example/