Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use KNN algorithm to remove Image background in Java OpenCV

Shulou Source: shulou.com Published: 2022-06-01 19:43:54 10月01日 Update

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]

Tags: Background image algorithm video object shape morphology learning content model structure result transformation code element original image that is idea situation article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi macOS MySQL Linux Shulou Information