In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This "Java OpenCV how to achieve background elimination" article knowledge points most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Java OpenCV how to achieve background elimination" article.
Implementation steps
1. Get video
two。 Set morphological structure
3. Create Video.createBackgroundSubtractorMOG2 ()
4. Extract model BS
5. Perform morphological transformation
6. Show the results
Main code package com.xu.opencv;import java.util.ArrayList;import java.util.List;import java.util.Objects;import java.util.Optional;import java.util.stream.Collectors;import org.opencv.core.Core;import org.opencv.core.Mat;import org.opencv.core.MatOfPoint;import org.opencv.core.Point;import org.opencv.core.Rect;import org.opencv.core.Scalar;import org.opencv.core.Size;import org.opencv.highgui.HighGui;import org.opencv.imgproc.Imgproc Import org.opencv.video.BackgroundSubtractorMOG2;import org.opencv.video.Video;import org.opencv.videoio.VideoCapture / * @ Title: BSM.java * @ Package com.xu.opencv * @ Description: OpenCV-4.1.0 background elimination * @ author: hyacinth * @ date: 22:10:14 * @ version: Vmur1.0 * @ Copyright: 2019 hyacinth * / public class BSM {static {System.loadLibrary (Core.NATIVE_LIBRARY_NAME) } public static void main (String [] args) {BSM_MOG2 () } / * OpenCV-4.1.0 Video Analysis and object tracking background removal GMM * * @ return: void * @ date: 22:10:14 * / public static void BSM_MOG2 () {/ / 1 create VideoCapture object VideoCapture capture = new VideoCapture (0) / 2 use VideoCapture object to read local video capture.open ("D:\\ BaiduNetdiskDownload\\ video_003.avi"); / / 3 get keyboard input for video processing. I am here to exit video object tracking int index = 0 if you press Esc to exit video processing. / / 4 use Mat video to save image frames in video and do processing for each frame Mat video = new Mat (); / / 5 get morphological structure Mat kernel = Imgproc.getStructuringElement (Imgproc.MORPH_RECT, new Size (3,3), new Point (- 1,-1)); / / 6 GMM BackgroundSubtractorMOG2 subtractor = Video.createBackgroundSubtractorMOG2 (); Mat fgmask = new Mat () While (capture.read (video)) {/ / 7 extract model BSM subtractor.apply (video, fgmask); / / 8 morphological transformation Imgproc.morphologyEx (fgmask, fgmask, Imgproc.MORPH_OPEN, kernel, new Point (- 1,-1)) / / 9 effect display Optional.ofNullable (process (fgmask)) .orElse (new ArrayList ()) .stream () .filter (Objects::nonNull) .forEach (rect-> {Imgproc.rectangle (fgmask, new Point (rect.x, rect.y), new Point (rect.x + rect.width, rect.y + rect.height), new Scalar (255,0,0), 1 Imgproc.LINE_AA, 0) }); HighGui.imshow ("GMM background Elimination", fgmask); index = HighGui.waitKey; if (index = = 27) {capture.release (); break } / * * OpenCV-4.0.0 * * Imgproc.findContours () function mode and method parameter interpretation * Mode input parameter interpretation * RETR_EXTERNAL detects only the outermost contour, and the inner contour contained in the outer contour is ignored * RETR_LIST detects all contours, including the inner and outer contours However, the detected contours do not establish a hierarchical relationship, independent of each other, and have no hierarchical relationship, which means that there are no parent contours or embedded contours in this retrieval mode, so the third and fourth components of all elements in the hierarchy vector will be set to-1 * RETR_CCOMP to detect all contours, but only two hierarchical relationships will be established for all contours, with the periphery at the top level. If the inner contour of the perimeter also contains other contour information, then all contours in the inner perimeter belong to the top level * RETR_TREE detects all contours, and all contours establish a hierarchical tree structure. The outer profile contains the inner profile, and the inner profile can continue to contain embedded contours. * Mthod input parameter interpretation * CHAIN_APPROX_NONE saves all continuous contour points on the object boundary into the contours vector * CHAIN_APPROX_SIMPLE only saves the inflection point information of the contour, and saves all the inflection points of the contour into the contours vector. The information points on the line segment between the inflection point and the inflection point are not retained * CHAIN_APPROX_TC89_L1 uses teh-Chinl chain near * CHAIN_APPROX_TC89_KCOS uses teh-Chinl chain near * * @ param video Mat * @ return: List * @ date 19 July 2019 22:10:14 * / public static List process (Mat video) {/ / 1 follow The position of the tracking object in the image List contours = new ArrayList () / / 2 find out the position of the object in the image Imgproc.findContours (video, contours, new Mat (), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point (2,2)); return Optional.ofNullable (contours) .orElse (new ArrayList ()) .stream () .filter (Objects::nonNull) .map (item-> Imgproc.boundingRect (item)) .location (Collectors.toList ()) }} effect picture
The above is about the content of this article on "how to achieve background elimination in Java OpenCV". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.