日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c# opencv 轮廓检测_基于OpenCV的区域分割、轮廓检测和阈值处理

發布時間:2024/9/27 C# 101 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# opencv 轮廓检测_基于OpenCV的区域分割、轮廓检测和阈值处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

OpenCV是一個巨大的開源庫,廣泛用于計算機視覺,人工智能和圖像處理領域。它在現實世界中的典型應用是人臉識別,物體檢測,人類活動識別,物體跟蹤等。

現在,假設我們只需要從整個輸入幀中檢測到一個對象。因此,代替處理整個框架,如果可以在框架中定義一個子區域并將其視為要應用處理的新框架,該怎么辦。我們要完成一下三個步驟:

? 定義興趣區

? 在ROI中檢測輪廓

? 閾值檢測輪廓輪廓線

什么是ROI?

簡而言之,我們感興趣的對象所在的幀內的子區域稱為感興趣區域(ROI)。

我們如何定義ROI?

在輸入幀中定義ROI的過程稱為ROI分割。

在“ ROI細分”中,(此處)我們選擇框架中的特定區域,并以矩形方法提供其尺寸,以便它將在框架上繪制矩形的ROI。

(輸出)藍色矩形覆蓋的區域是我們的投資回報率

現在,如果您也想綁定感興趣的對象,那么我們可以通過在ROI中找到輪廓來實現。

什么是輪廓?

輪廓線是 表示或說是限制對象形狀的輪廓。

如何在框架中找到輪廓?

對我而言,在將ROI框架設為閾值后,找到輪廓效果最佳。因此,要找到輪廓,手上的問題是-

什么是閾值?

閾值不過是圖像分割的一種簡單形式。這是將灰度或rgb圖像轉換為二進制圖像的過程。例如

(這是RGB幀)

(這是二進制閾值幀)

因此,在對rgb幀進行閾值處理后,程序很容易找到輪廓,因為由于ROI中感興趣對象的顏色將是黑色(在簡單的二進制脫粒中)或白色(在如上所述的反向二進制脫粒中),因此分割(將背景與前景即我們的對象分開)將很容易完成。

在對框架進行閾值處理并檢測到輪廓之后,我們應用凸包技術對圍繞對象點的緊密擬合凸邊界進行設置。實施此步驟后,框架應如下所示-

我們可以做的另一件事是,我們可以遮蓋ROI以僅顯示被檢測到的輪廓本身覆蓋的對象。再次-

什么是圖像MASK?

圖像MASK是隱藏圖像的某些部分并顯示某些部分的過程。這是圖像編輯的非破壞性過程。在大多數情況下,它使您可以在以后根據需要調整和調整遮罩。通常,它是一種有效且更具創意的圖像處理方式。

因此,基本上在這里我們將掩蓋ROI的背景。為此,首先我們將修復ROI的背景。然后,在固定背景之后,我們將從框架中減去背景,并用wewant背景(這里是一個簡單的黑色框架)替換它。

實施上述技術,我們應該得到如下輸出:

(背景被遮罩以僅捕獲對象)

這是所說明技術的理想實現的完整代碼。

import cv2import numpy as npimport copyimport mathx=0.5 # start point/total widthy=0.8 # start point/total widththreshold = 60 # BINARY thresholdblurValue = 7 # GaussianBlur parameterbgSubThreshold = 50learningRate = 0# variablesisBgCaptured = 0 # whether the background captureddef removeBG(frame): #Subtracting the background fgmask = bgModel.apply(frame,learningRate=learningRate) kernel = np.ones((3, 3), np.uint8) fgmask = cv2.erode(fgmask, kernel, iterations=1) res = cv2.bitwise_and(frame, frame, mask=fgmask) return res# Cameracamera = cv2.VideoCapture(0)camera.set(10,200)while camera.isOpened(): ret, frame = camera.read() frame = cv2.bilateralFilter(frame, 5, 50, 100) # smoothening filter frame = cv2.flip(frame, 1) # flip the frame horizontally cv2.rectangle(frame, (int(x * frame.shape[1]), 0), (frame.shape[1], int(y * frame.shape[0])), (255, 0, 0), 2) #drawing ROI cv2.imshow('original', frame) # Main operation if isBgCaptured == 1: # this part wont run until background captured img = removeBG(frame) img = img[0:int(y * frame.shape[0]), int(x * frame.shape[1]):frame.shape[1]] # clip the ROI cv2.imshow('mask', img) # convert the image into binary image gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (blurValue, blurValue), 0) cv2.imshow('blur', blur) ret, thresh = cv2.threshold(blur, threshold, 255, cv2.THRESH_BINARY) #thresholding the frame cv2.imshow('ori', thresh) # get the coutours thresh1 = copy.deepcopy(thresh) contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #detecting contours length = len(contours) maxArea = -1 if length > 0: for i in range(length): # find the biggest contour (according to area) temp = contours[i] area = cv2.contourArea(temp) if area > maxArea: maxArea = area ci = i res = contours[ci] hull = cv2.convexHull(res) #applying convex hull technique drawing = np.zeros(img.shape, np.uint8) cv2.drawContours(drawing, [res], 0, (0, 255, 0), 2) #drawing contours cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3) #drawing convex hull cv2.imshow('output', drawing) # Keyboard OP k = cv2.waitKey(10) if k == 27: camera.release() cv2.destroyAllWindows() break elif k == ord('b'): # press 'b' to capture the background bgModel = cv2.createBackgroundSubtractorMOG2(0, bgSubThreshold) isBgCaptured = 1 print( 'Background Captured') elif k == ord('r'): # press 'r' to reset the background bgModel = None isBgCaptured = 0 print ('Reset BackGround')

總結

以上是生活随笔為你收集整理的c# opencv 轮廓检测_基于OpenCV的区域分割、轮廓检测和阈值处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。