OpenCV實(shí)現(xiàn)去除背景識(shí)別的方法總結(jié)
實(shí)現(xiàn)效果
效果如圖,只識(shí)別一定距離內(nèi)的物體
哈哈哈哈哈哈哈哈哈,但我不知道這有什么用
實(shí)現(xiàn)代碼
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以內(nèi) clipping_distance = clipping_distance_in_meters / depth_scale # 對(duì)齊圖像 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以以內(nèi)形成畫像 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) # 計(jì)算具有背景顏色的像素?cái)?shù) white_pic = np.sum(bg_removed == 255) # 當(dāng)背景顏色低于某個(gè)值時(shí)顯示“檢測(cè)到” if(threshold > white_pic): print("檢測(cè)到 {}".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()
補(bǔ)充
在opencv中有兩種方法可以進(jìn)行背景消除:
其一、基于機(jī)器學(xué)習(xí)(Knn–K個(gè)最近鄰)背景消除建模
其二、于圖像分割(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); //形態(tài)學(xué)操作--開操作,去除小的噪聲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) #創(chuàng)建窗口 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) #形態(tài)學(xué)處理 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實(shí)現(xiàn)去除背景識(shí)別的方法總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于OpenCV去除背景識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Python實(shí)現(xiàn)更改Word文檔的頁面大小
頁面大小確定文檔中每個(gè)頁面的尺寸和布局,有時(shí)我們會(huì)需要自定義頁面大小以滿足特定要求,下面我們就來看看如何使用Python實(shí)現(xiàn)這一效果吧2024-03-03python 的 scapy庫,實(shí)現(xiàn)網(wǎng)卡收發(fā)包的例子
今天小編就為大家分享一篇python 的 scapy庫,實(shí)現(xiàn)網(wǎng)卡收發(fā)包的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07pytorch tensor int型除法出現(xiàn)的問題
這篇文章主要介紹了pytorch tensor int型除法出現(xiàn)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04關(guān)于python3.7安裝matplotlib始終無法成功的問題的解決
這篇文章主要介紹了關(guān)于python3.7安裝matplotlib始終無法成功的問題的解決,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07在Python3中初學(xué)者應(yīng)會(huì)的一些基本的提升效率的小技巧
這篇文章主要介紹了在Python3中的一些基本的小技巧,有利于剛剛上手Python的初學(xué)者提升開發(fā)效率,需要的朋友可以參考下2015-03-03Python游戲開發(fā)之魔塔小游戲的實(shí)現(xiàn)
魔塔小游戲作為一款角色扮演RPG小游戲,一直深受大家的喜愛。本文將利用Python的cpgames模塊制作這一經(jīng)典小游戲,感興趣的可以跟隨小編一起動(dòng)手試一試2022-02-02python設(shè)置隨機(jī)種子實(shí)例講解
在本篇文章里小編給大家整理的是關(guān)于python設(shè)置隨機(jī)種子的相關(guān)知識(shí)點(diǎn)以及實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-09-09Python中關(guān)于面向?qū)ο蟾拍畹脑敿?xì)講解
要了解面向?qū)ο笪覀兛隙ㄐ枰戎缹?duì)象到底是什么玩意兒。關(guān)于對(duì)象的理解很簡(jiǎn)單,在我們的身邊,每一種事物的存在都是一種對(duì)象??偨Y(jié)為一句話也就是:對(duì)象就是事物存在的實(shí)體2021-10-10使用 Python 的 pprint庫格式化和輸出列表和字典的方法
pprint是"pretty-print"的縮寫,使用 Python 的標(biāo)準(zhǔn)庫 pprint 模塊,以干凈的格式輸出和顯示列表和字典等對(duì)象,這篇文章主要介紹了如何使用 Python 的 pprint庫格式化和輸出列表和字典,需要的朋友可以參考下2023-05-05