网络编程
位置:首页>> 网络编程>> Python编程>> OpenCV实现去除背景识别的方法总结

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

作者:小小小小能  发布时间:2021-01-06 23:04:10 

标签:OpenCV,去除,背景

实现效果

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

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

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

实现代码

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&ndash;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)

来源:https://blog.csdn.net/weixin_49828565/article/details/127257531

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com