In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use KNN algorithm to remove image background in Java OpenCV". The content in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use KNN algorithm to remove image background in Java OpenCV".
Implementation steps
1 get video
2 set morphological structure
3 create Video.createBackgroundSubtractorKNN ()
4. Extract model BS
5 to carry out morphological transformation
6 expansion
7 binarization
8 show the results
Sample 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.Size;import org.opencv.highgui.HighGui;import org.opencv.imgproc.Imgproc;import org.opencv.video.BackgroundSubtractorKNN 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_KNN () } / * OpenCV-4.1.0 Video Analysis and object tracking background removal KNN * * @ return: void * @ date: 22:10:14 * / public static void BSM_KNN () {/ / 1 create VideoCapture object VideoCapture capture = new VideoCapture (0) / 2 use the VideoCapture object to read the local video capture.open ("D:\\ BaiduNetdiskDownload\\ video_003.avi"); / / 4 use Mat video to save the image frames in the video for each frame processing Mat video = new Mat () / 3 set structural elements Mat kernel1 = Imgproc.getStructuringElement (Imgproc.MORPH_RECT, new Size (1,1), new Point (- 1,-1)); Mat kernel2 = Imgproc.getStructuringElement (Imgproc.MORPH_RECT, new Size (3,3), new Point (- 1,-1)); / / 4 KNN background elimination BackgroundSubtractorKNN knn = Video.createBackgroundSubtractorKNN (); Mat bitmask = new Mat () While (capture.read (video)) {/ / 5 extract model BSM knn.apply (video, bitmask,-1); / / 6 morphological transformation (closed operation) Imgproc.morphologyEx (bitmask, bitmask, Imgproc.MORPH_CLOSE, kernel1, new Point (- 1,-1)) / / 7 inflated Imgproc.dilate (bitmask, bitmask, kernel2, new Point (- 1,-1), 1); / 8 binary Imgproc.threshold (bitmask, bitmask, 20,255, Imgproc.THRESH_BINARY | Imgproc.THRESH_TRIANGLE); / / 9 set the background in the original image to Scalar (255,255,255) for (int I = 0, r = bitmask.rows () I < r; bitmask.cols +) {for (int j = 0, c = bitmask.cols (); j < c; Jake +) {if (bitmask.get (I, j) [0]
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.