将 Python 函数应用于图像的蒙版转换为 Java



我正在尝试将以下将蒙版应用于图像的Python函数转换为Java:

# Applies an image mask.
def region_of_interest(img, vertices):
    #defining a blank mask to start with
    mask = np.zeros_like(img)   
    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255
    #filling pixels inside the polygon defined by "vertices" with the fill color    
    cv2.fillPoly(mask, vertices, ignore_mask_color)
    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image

到目前为止,这就是我得到的:

public static opencv_core.Mat applyMask(opencv_core.Mat image, opencv_core.MatVector vertices) {
  opencv_core.Mat mask = opencv_core.Mat.zeros(image.size(), opencv_core.CV_8U).asMat();
  opencv_core.Scalar color = new opencv_core.Scalar(image.channels()); // 3
  double[] colors = new double[] {
    255.0, 255.0, 255.0, 255.0,
    255.0, 255.0, 255.0, 255.0,
    255.0, 255.0, 255.0, 255.0};
  color.put(colors, 0, colors.length);
  opencv_imgproc.fillPoly(mask, vertices, color);
  opencv_core.Mat dst = new opencv_core.Mat();
  opencv_core.bitwise_and(image, mask, dst);
  return dst;
}

但是,它不起作用。当我尝试调用此方法时,如以下示例所示:

opencv_core.MatVector points = new opencv_core.MatVector(
  new opencv_core.Mat(2, 3, opencv_core.CV_32F, new IntPointer(1, 2, 3, 4, 5, 6))
);
opencv_core.MatVector vertices = new opencv_core.MatVector(points);
opencv_core.Mat masked = LaneManager.applyMask(src, vertices);

(我假设这是构建一个由三个点组成的 2x3 矩阵的正确方法,每个点有两个坐标(1,2)(3, 4)(5,6)(

我得到一个例外:

java.lang.RuntimeException: std::bad_alloc
at org.bytedeco.javacpp.opencv_imgproc.fillPoly(Native Method)

我正在使用org.bytedeco.javacpp-presets:opencv-platform:3.2.0-1.3通过Maven Central提供的OpenCV。

必须承认,我在这里不知所措:与上面的 Python 函数做同样事情的惯用 Java 方式是什么?

好吧,我终于想通了。如果使用以下命令定义坐标:

int[] points = new int[] {x1, y1, x2, y2, ...};

然后,您可以使用以下代码简单地应用蒙版:

opencv_core.Mat mask = new opencv_core.Mat(image.size(), image.type());
// Array of polygons where each polygon is represented as an array of points
opencv_core.Point polygon = new opencv_core.Point();
polygon.put(points, 0, points.length);
opencv_imgproc.fillPoly(mask, polygon, new int[] {points.length / 2}, 1, new opencv_core.Scalar(255, 255, 255, 0));
opencv_core.Mat masked = new opencv_core.Mat(image.size(), image.type());
opencv_core.bitwise_and(image, mask, masked);

其中image是原始图像,masked是蒙版结果。

问题是原始的点列表没有正确定义。

也许一些奇才有完整的答案。以下是值得深思的:

  1. Java API 是 CPP API 的直接副本:http://opencv.org/opencv-java-api.html
  2. 当您

    无法分配所需的存储空间时,std::bad_alloc会发生错误。(http://answers.opencv.org/question/28959/cascade-training-killed-and-bad_alloc/(

  3. 有两种 CPP 方法:
    • C++:void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=LINE_8, int shift=0, Point offset=Point() )
    • C++:void fillPoly(InputOutputArray img, InputArrayOfArrays pts, const Scalar& color, int lineType=LINE_8, int shift=0, Point offset=Point() )
  4. 您不需要从 Mat 转换为 InputArray ,但您可以(并且应该(在请求InputArray的地方传递一个Mat对象(https://stackoverflow.com/a/32976883/1587329(

最新更新