anzuwenden Ich versuche, die folgende Python-Funktion zu übersetzen, die eine Maske auf ein Bild bezieht, in Java:Übersetzen Python Funktion Maske Bild in 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
Bisher ist dies, was ich habe:
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;
}
Aber es funktioniert nicht. Beim Versuch, diese Methode aufgerufen wird, wie im folgenden Beispiel:
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);
(Ich gehe davon aus das der richtige Weg ist es, eine 2x3-Matrix von drei Punkten mit zwei Koordinaten jedes (1,2)
, (3, 4)
und (5,6)
zu bauen)
ich erhalte eine Ausnahme:
java.lang.RuntimeException: std::bad_alloc
at org.bytedeco.javacpp.opencv_imgproc.fillPoly(Native Method)
ich bin mit OpenCV wie org.bytedeco.javacpp-presets:opencv-platform:3.2.0-1.3
über Maven Zentrale zur Verfügung gestellt.
Ich muss zugeben, dass ich hier ratlos bin: Was ist die idiomatische Java-Art, das Gleiche wie die obige Python-Funktion zu machen?
Ich denke, was Sie suchen, ist mit diesem anderen Stackoverflow Post http: // Stackoverflow verwandt.com/questions/221830/set-bufferedimage-alpha-maske-in-java. Hoffe, das kann helfen. –
Wie initialisierst du die src-Variable, die du an applyMask sendest? – Daedalus
Eine verdächtige Sache ist, dass Sie offenbar eine Farbe für ein 3-Kanal-Bild mit 12 Farbkomponenten definieren. Das kann nicht stimmen. – Dave