欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程语言 > c# >内容正文

c#

c# opencv 轮廓检测-凯发ag旗舰厅登录网址下载

发布时间:2024/9/27 c# 31 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 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')

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的c# opencv 轮廓检测_基于opencv的区域分割、轮廓检测和阈值处理的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图