当前位置 : 主页 > 编程语言 > python >

OpenCV实现去除背景识别的方法总结

来源:互联网 收集:自由互联 发布时间:2023-01-30
目录 实现效果 实现代码 补充 实现效果 效果如图,只识别一定距离内的物体 哈哈哈哈哈哈哈哈哈,但我不知道这有什么用 实现代码 import pyrealsense2 as rsimport numpy as npimport cv2 # 排除背景
目录
  • 实现效果
  • 实现代码
  • 补充

实现效果

效果如图,只识别一定距离内的物体

哈哈哈哈哈哈哈哈哈,但我不知道这有什么用

实现代码

import pyrealsense2 as rs
import numpy as np
import cv2
 
# 排除背景色
WIDTH = 848
HEIGHT = 480
 
# 初始化
config = rs.config()
config.enable_stream(rs.stream.color, WIDTH, HEIGHT, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, WIDTH, HEIGHT, rs.format.z16, 30)
 
# 开始
pipeline = rs.pipeline()
profile = pipeline.start(config)
 
# 距离[m] = depth * depth_scale
depth_sensor = profile.get_device().first_depth_sensor()
depth_scale = depth_sensor.get_depth_scale()
clipping_distance_in_meters = 0.4  # 40cm以内
clipping_distance = clipping_distance_in_meters / depth_scale
 
# 对齐图像
align_to = rs.stream.color
align = rs.align(align_to)
 
threshold = (WIDTH * HEIGHT * 3) * 0.95
 
try:
    while True:
        frames = pipeline.wait_for_frames()
        aligned_frames = align.process(frames)
        color_frame = aligned_frames.get_color_frame()
        depth_frame = aligned_frames.get_depth_frame()
        if not depth_frame or not color_frame:
            continue
 
        color_image = np.asanyarray(color_frame.get_data())
        depth_image = np.asanyarray(depth_frame.get_data())
 
        # clipping_distance_in_metersm以以内形成画像
        white_color = 255 # 背景色
        depth_image_3d = np.dstack((depth_image, depth_image, depth_image))
        bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), white_color, color_image)
        # 计算具有背景颜色的像素数
        white_pic = np.sum(bg_removed == 255)
        # 当背景颜色低于某个值时显示“检测到”
        if(threshold > white_pic):
            print("检测到 {}".format(white_pic))
        else:
            print("{}".format(white_pic))
 
        images = np.hstack((bg_removed, color_image))
        cv2.imshow('Frames', images)
 
        if cv2.waitKey(1) & 0xff == 27:
            break
 
finally:
    # 停止
    pipeline.stop()
    cv2.destroyAllWindows()

补充

在opencv中有两种方法可以进行背景消除:

其一、基于机器学习(Knn–K个最近邻)背景消除建模

其二、于图像分割(GMM,抗干扰图像分割)背景消除建模BS ,Background Subtraction

c版

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
    VideoCapture capture;
    capture.open("D:/software/opencv1/picture/vtest.avi");
    if (!capture.isOpened()) {
        printf("could not load the video!");
        return -1;
    }
    Mat frame;
    Mat bsmaskMOG2,bsmaskKNN;
    namedWindow("input video", CV_WINDOW_AUTOSIZE);
    namedWindow("MOG2 Model",CV_WINDOW_AUTOSIZE);
    namedWindow("kKNNoutput Model", CV_WINDOW_AUTOSIZE);
    Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));
    //初始化BS
    Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
    Ptr<BackgroundSubtractor> pKNN = createBackgroundSubtractorKNN();

    
    while (capture.read(frame))
    {
        imshow("input video", frame);
        // MOG BS
        pMOG2->apply(frame, bsmaskMOG2);
        //形态学操作--开操作,去除小的噪声morphologyEx()
        morphologyEx(bsmaskMOG2, bsmaskMOG2, MORPH_OPEN, kernel, Point(-1, -1));
        imshow("MOG2 Model", bsmaskMOG2);
        // KNN BS mask
        pKNN->apply(frame, bsmaskKNN);
        imshow("KNNoutput Model", bsmaskKNN);
        char c = waitKey(100);
        if (c == 27) {
            break;
        }

    }

    capture.release();
    waitKey(0);
    return 0;

}

python

#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
# @Time : 2020/11/17 19:06
# @Author : ptg
# @Email : zhxwhchina@163.com
# @File : 去背景.py
# @Software: PyCharm

import cv2 as cv
import numpy as np
from cv2 import cv2

image = cv2.imread("mabaoguo2.jpg",cv2.IMREAD_GRAYSCALE)
binary = cv2.adaptiveThreshold(image,255,
        cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,25,15)
se = cv2.getStructuringElement(cv2.MORPH_RECT,(1,1))
se = cv2.morphologyEx(se, cv2.MORPH_CLOSE, (2,2))
mask = cv2.dilate(binary,se)
cv2.imshow("image",image)

mask1 = cv2.bitwise_not(mask)
binary =cv2.bitwise_and(image,mask)
result = cv2.add(binary,mask1)
cv2.imshow("reslut",result)
cv2.imwrite("reslut00.jpg",result)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
 
 
#读入图像
video = cv2.VideoCapture("E:\\video.avi")
videoIsOpen=video.isOpened
print(videoIsOpen)
width=int(video.get(cv2.CAP_PROP_FRAME_WIDTH))#宽度
height=int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))#高度
fps=video.get(cv2.CAP_PROP_FPS)#获取帧率
print(fps,width,height)
#创建窗口
cv2.namedWindow('MOG2')
cv2.namedWindow('MOG22')
cv2.namedWindow('input video')
#cv2.namedWindow('KNN')
bsmaskMOG2 = np.zeros([height,width],np.uint8)
bsmaskKnn = np.zeros([height,width],np.uint8)
#两种消除的方案
pMOG2 = cv2.createBackgroundSubtractorMOG2(detectShadows=True)
PKNN = cv2.createBackgroundSubtractorKNN(detectShadows=True)
#形态学处理
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
 
while videoIsOpen:
    (flag,frame)=video.read()
    if not flag:
        break
    cv2.imshow('input video',frame)
    # bsmaskKnn= PKNN.apply(frame)
    # cv2.imshow('KNN',bsmaskKnn)
    bsmaskMOG2 = pMOG2.apply(frame)
    cv2.imshow('MOG22',bsmaskMOG2)
    OPEND=cv2.morphologyEx(bsmaskMOG2,cv2.MORPH_OPEN,kernel)
    cv2.imshow('MOG2',OPEND)
    
    c = cv2.waitKey(40)
    if c==27:
        break
video.release()
 
cv2.waitKey(0)

以上就是OpenCV实现去除背景识别的方法总结的详细内容,更多关于OpenCV去除背景识别的资料请关注自由互联其它相关文章!

上一篇:如何将bag文件批量转成mp4
下一篇:没有了
网友评论