0%

OpenCV(十一)-图像的分割与修复

一、图像分割的基本概念

  • 图像分割
    • 将前景物体从背景中分离出来
  • 分割方法
    • 传统的
      • 分水岭法
      • GrabCut法
      • MeanShift法
      • 背景扣除
    • 基于深度学习的

1.1、分水岭法

image-20241123220524748

image-20241123220624100

  • 分水岭法的处理步骤
    1. 标记背景
    2. 标记前景
    3. 标记未知域
    4. 进行分割
1
2
3
# 分水岭API
watershed(img, masker)
masker,前景、背景设置不同的值用以区分它们

image-20241123221143205

image-20241123221152881

image-20241123221200181

image-20241123221207886

image-20241123223521044

image-20241123223557854

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import cv2
import numpy as np

import matplotlib
matplotlib.use('Qt5Agg') # or 'Qt5Agg' if you prefer

import matplotlib.pyplot as plt



# 获取背景
# 1、通过二值法得到黑白图片
# 2、通过形态学获取背景

img = cv2.imread('water_coins.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 二值化
# 自适应性寻找阈值
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

# 开运算,膨胀,去除噪点
kernel = np.ones((3,3), np.int8)
open1 = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)

# 膨胀
bg = cv2.dilate(open1, kernel, iterations = 1)


# 获取前景物体
dist = cv2.distanceTransform(open1, cv2.DIST_L2, 5)

# 前景
ret, fg = cv2.threshold(dist, 0.7*dist.max(), 255, cv2.THRESH_BINARY)

# plt.imshow(dist, cmap='gray')
# plt.show()
# exit()

# 获取未知区域
fg = np.uint8(fg)
unknown = cv2.subtract(bg, fg)

# 创建连通域
ret, marker = cv2.connectedComponents(fg)
marker = marker + 1
marker[unknown==255] = 0

# 进行图像分割
result = cv2.watershed(img, marker)

img[result == -1] = [0, 0, 255]

cv2.imshow("img", img)
cv2.imshow("unknown", unknown)
cv2.imshow("fg", fg)
cv2.imshow("dist", dist)
cv2.imshow("bg", bg)
cv2.imshow("thresh", thresh)
cv2.waitKey(0)

image-20241123232911076

1.2、GrabCut

  • GrabCut
    • 通过交互的方式获得前景物体
  • ==原理==
    • 用户指定前景的大体区域,剩下的为背景区域
    • 用户还可以明确指定某些地方为前景或背景
    • GrabCut采用分段迭代的方法分析前景物体形成模型树
    • 最后根据权重决定某个像素是前景还是背景

image-20241124000257900

image-20241124000309094

image-20241124000330537

image-20241124000353370

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import cv2
import numpy as np

class App:

flag_rect = False
rect=(0, 0, 0, 0)
startX = 0
startY = 0

def onmouse(self, event, x, y, flags, param):

if event == cv2.EVENT_LBUTTONDOWN:
self.flag_rect = True
self.startX = x
self.startY = y
print("LBUTTIONDOWN")
elif event == cv2.EVENT_LBUTTONUP:
self.flag_rect = False
cv2.rectangle(self.img,
(self.startX, self.startY),
(x, y),
(0, 0, 255),
3)
self.rect = (min(self.startX, x), min(self.startY, y),
abs(self.startX - x),
abs(self.startY -y))

print("LBUTTIONUP")
elif event == cv2.EVENT_MOUSEMOVE:
if self.flag_rect == True:
self.img = self.img2.copy()
cv2.rectangle(self.img,
(self.startX, self.startY),
(x, y),
(255, 0, 0),
3)
print("MOUSEMOVE")

print("onmouse")

def run(self):
print("run...")

cv2.namedWindow('input')
cv2.setMouseCallback('input', self.onmouse)

self.img = cv2.imread('./lena.png')
self.img2 = self.img.copy()
self.mask = np.zeros(self.img.shape[:2], dtype=np.uint8)
self.output = np.zeros(self.img.shape, np.uint8)

while(1):
cv2.imshow('input', self.img)
cv2.imshow('output', self.output)
k = cv2.waitKey(100)
if k == 27:
break

if k == ord('g'):
bgdmodel = np.zeros((1, 65), np.float64)
fgdmodel = np.zeros((1, 65), np.float64)
cv2.grabCut(self.img2, self.mask, self.rect,
bgdmodel, fgdmodel,
1,
cv2.GC_INIT_WITH_RECT)
mask2 = np.where((self.mask==1)|(self.mask==3), 255, 0).astype('uint8')
self.output = cv2.bitwise_and(self.img2, self.img2, mask=mask2)

App().run()

image-20241124003322031

1.3、meanshift图像分割

image-20241124003451144

image-20241124003516370

image-20241124003545261

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 我一直显示报错,可能是opencv版本的原因吧
import cv2
import numpy as np

img = cv2.imread('key.png')

mean_img = cv2.pyrMeanShiftFiltering(img, 20, 30)

imgcanny = cv2.Canny(mean_img, 150, 300)

contours, _ = cv2.findContours(imgcanny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(img, contours, -1, (0, 0, 255), 2)

cv2.imshow('img', img)
cv2.imshow('mean_img', mean_img)
cv2.imshow('canny', imgcanny)
cv2.waitKey()

二、视频前后景分离

image-20241124155758857

image-20241124155826700

image-20241124155920693

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import cv2
import numpy as np

cap = cv2.VideoCapture('./vtest.avi')

mog = cv2.bgsegm.createBackgroundSubtractorMOG()

while(True):
ret, frame = cap.read()
fgmask = mog.apply(frame)

cv2.imshow('img', fgmask)

k = cv2.waitKey(10)
if k == 27:
break

cap.release()
cv2.destroyAllWindows()

image-20241124160636722

三、其他对视频前后影分离的方法

image-20241124160719062

image-20241124160726325

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import cv2
import numpy as np

cap = cv2.VideoCapture('./vtest.avi')

# mog = cv2.bgsegm.createBackgroundSubtractorMOG()
# 好处,可以计算出阴影部分
# 缺点,会产生很多噪点
mog = cv2.createBackgroundSubtractorMOG2()

while(True):
ret, frame = cap.read()
fgmask = mog.apply(frame)

cv2.imshow('img', fgmask)

k = cv2.waitKey(10)
if k == 27:
break

cap.release()
cv2.destroyAllWindows()

image-20241124161313470

image-20241124160757776

image-20241124160844845

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import cv2
import numpy as np

cap = cv2.VideoCapture('./vtest.avi')

# mog = cv2.bgsegm.createBackgroundSubtractorMOG()
# 好处,可以计算出阴影部分
# 缺点,会产生很多噪点
# mog = cv2.createBackgroundSubtractorMOG2()

# 好处,可以计算出阴影部分,同时减少了噪点
# 缺点,如果采用默认值,则在开始好长时间内没人作任何信息显示
# 解决办法,调整初始参考帧数量
mog = cv2.bgsegm.createBackgroundSubtractorGMG()

while(True):
ret, frame = cap.read()
fgmask = mog.apply(frame)

cv2.imshow('img', fgmask)

k = cv2.waitKey(10)
if k == 27:
break

cap.release()
cv2.destroyAllWindows()

image-20241124161440318

四、图形修复

image-20241124161737807

1
2
3
4
5
6
7
8
9
10
import cv2
import numpy as np

img = cv2.imread('inpaint.png')
mask = cv2.imread('inpaint_mask.png', 0)

dst = cv2.inpaint(img, mask, 5, cv2.INPAINT_TELEA)

cv2.imshow('dst', dst)
cv2.waitKey()

image-20241124162351550

-------------本文结束感谢您的阅读-------------