1
00:00:00,240 --> 00:00:06,390
Hi and welcome back to Chapter 22, a Section 22, where we take a look at the watershed algorithm,

2
00:00:06,390 --> 00:00:12,240
which is basically a segmentation algorithm similar to find contours, which we'll explore now.

3
00:00:13,380 --> 00:00:14,790
So let's do this stuff.

4
00:00:15,060 --> 00:00:16,050
So now that's open.

5
00:00:16,200 --> 00:00:21,190
Let's load a little files and libraries and the image and show a function.

6
00:00:22,140 --> 00:00:23,670
And while loads.

7
00:00:23,700 --> 00:00:26,040
I'll explain the watershed algorithm in principle.

8
00:00:26,670 --> 00:00:33,840
So imagine we have an image like this where it's a bunch of coins here what what digital your algorithm

9
00:00:33,840 --> 00:00:34,220
does.

10
00:00:34,860 --> 00:00:40,260
The reason it's called watershed is because it looks for low points or dark areas in an image.

11
00:00:40,650 --> 00:00:47,070
And those pictures dark areas to be valleys so the valleys will fill with water if you were to actually

12
00:00:47,070 --> 00:00:50,040
apply water to that, that geographical scene.

13
00:00:50,790 --> 00:00:56,220
So similarly, with these types of images, the darker areas have become filled with water.

14
00:00:56,580 --> 00:00:59,340
And that's how you actually have a segmentation between this.

15
00:00:59,520 --> 00:01:07,730
So what this allows us to do is, firstly, before I even go into that, imagine we're trying to do

16
00:01:07,740 --> 00:01:10,620
use to get the contours around each coin here.

17
00:01:11,040 --> 00:01:14,400
Now we could use something like half circles to find this.

18
00:01:14,820 --> 00:01:18,210
But what if we want to do something else faster and quicker and more resilient?

19
00:01:18,690 --> 00:01:23,160
So we wanted to use fine contours, but fine contours wouldn't work because look what happens?

20
00:01:24,120 --> 00:01:24,940
You can actually get.

21
00:01:24,960 --> 00:01:27,360
You actually have these images, these moving centers here.

22
00:01:28,410 --> 00:01:29,520
So that's going to be a problem.

23
00:01:30,000 --> 00:01:32,400
So how do we remove those touching mass?

24
00:01:32,880 --> 00:01:36,030
Well, one way we can do it is by dilation as well.

25
00:01:36,510 --> 00:01:42,090
So we can use them dilation and then we can using something called finding to show foreground here you.

26
00:01:42,480 --> 00:01:47,100
Which basically means that what we're trying to find, the area that we're showing is to foreground.

27
00:01:47,880 --> 00:01:49,420
So we actually what we do here.

28
00:01:49,480 --> 00:01:59,040
Firstly, we perform some morphology x here, which which applies simple blur or noise removal to the

29
00:01:59,040 --> 00:01:59,610
image here.

30
00:02:00,420 --> 00:02:03,840
So the next step is we need to find a sure background area.

31
00:02:04,140 --> 00:02:10,230
And to do that, we just actually have to sort of dilate this image here with a kernel of tree by tree.

32
00:02:10,230 --> 00:02:14,730
And we do have tree times, which is why this is a show foreground, which I'll explain shortly.

33
00:02:14,730 --> 00:02:18,240
But this is why it looks like this and this short background, which you don't have to.

34
00:02:18,420 --> 00:02:19,650
I don't have to show a background split.

35
00:02:20,190 --> 00:02:21,500
But we can take a look at it.

36
00:02:21,510 --> 00:02:24,690
No, actually, it would be good for us to do.

37
00:02:25,770 --> 00:02:28,290
So let's plot as well.

38
00:02:28,800 --> 00:02:29,640
So let's not run.

39
00:02:29,640 --> 00:02:35,030
This could or didn't do the stuff beforehand.

40
00:02:35,040 --> 00:02:41,340
Actually, make sure you run these cellblocks before running to code the images out there because it

41
00:02:41,340 --> 00:02:45,450
saves your lasso, but so often make that mistake.

42
00:02:47,100 --> 00:02:47,520
All right.

43
00:02:47,640 --> 00:02:53,880
So you can see this is a threshold image here, which actually should be sure.

44
00:02:54,090 --> 00:02:58,020
Background Let's run this again with the appropriate titles.

45
00:02:59,100 --> 00:02:59,850
OK, good.

46
00:02:59,970 --> 00:03:05,520
So now we have these images here, so we have to show the image here, which is our short foreground,

47
00:03:05,520 --> 00:03:10,710
which actually lets us called Show F G for future reference.

48
00:03:13,030 --> 00:03:15,060
And we have a show background on knowing.

49
00:03:15,730 --> 00:03:20,710
So I'll explain the rest of the code, but just to show you illustrating what we're getting so far.

50
00:03:21,220 --> 00:03:22,920
We have to show foreground here.

51
00:03:22,930 --> 00:03:28,240
We have the sure background here and then we have the unknown areas.

52
00:03:28,580 --> 00:03:31,000
So there is a basically to mask here as well.

53
00:03:31,960 --> 00:03:33,550
So let's go back up to just good.

54
00:03:34,180 --> 00:03:40,400
So to deter it, to get to show off foreground, we used it silly to distance transform as well.

55
00:03:40,420 --> 00:03:46,120
Here we use the parameters that open if we specifies in the documentation for this application.

56
00:03:46,180 --> 00:03:47,590
This is a watershed application.

57
00:03:48,310 --> 00:03:54,640
And then we multiply it by the MAX by 0.7 and we apply the threshold here.

58
00:03:55,060 --> 00:03:59,860
So that's how we get our threshold value and that's what we get to show foreground.

59
00:04:00,640 --> 00:04:06,130
So we can put the region here and then we subtract the show background from the show foreground to get

60
00:04:06,130 --> 00:04:07,300
the unknown area here.

61
00:04:07,630 --> 00:04:11,800
So that's basically what we've done so far, and that's really annoying area looks like this.

62
00:04:12,670 --> 00:04:16,720
So the whole point of that was actually to obtain this image.

63
00:04:16,900 --> 00:04:17,980
This new image here.

64
00:04:19,120 --> 00:04:26,230
So what we do, we actually run the connected components, which is basically it's an algorithm that

65
00:04:26,540 --> 00:04:30,400
shows the connectivity of blood like regions in a binary image.

66
00:04:31,270 --> 00:04:37,300
So now that we have that, we have the regional markers that unknown eliminated here.

67
00:04:38,110 --> 00:04:40,780
So then we can now apply the watershed algorithm.

68
00:04:40,780 --> 00:04:45,040
So it seems like a lot of work to get to this point, and it's actually not that intuitive.

69
00:04:45,580 --> 00:04:49,330
However, just follow these steps in these guidelines to implement this if you need to.

70
00:04:49,990 --> 00:04:53,350
And what we do here markers that is minus one.

71
00:04:53,350 --> 00:04:55,840
We actually just make them this value as well.

72
00:04:56,350 --> 00:04:58,210
So that's how we can.

73
00:04:58,300 --> 00:05:00,640
Actually, this is blue, so we actually make the markers blue.

74
00:05:00,640 --> 00:05:01,120
I should say.

75
00:05:01,420 --> 00:05:02,140
Sorry for that.

76
00:05:02,830 --> 00:05:06,100
So no, we can draw circles around the coins.

77
00:05:06,100 --> 00:05:06,640
Exactly.

78
00:05:07,120 --> 00:05:08,560
And we don't have that.

79
00:05:08,560 --> 00:05:12,320
We do have some of them joining like this bit that it's actually not fully joined.

80
00:05:12,320 --> 00:05:13,450
It actually has the boundary here.

81
00:05:13,930 --> 00:05:17,530
So they're all not joined, which is what we wanted and wanted to extract.

82
00:05:17,740 --> 00:05:19,420
These are objects here separately.

83
00:05:19,990 --> 00:05:21,400
So this achieves the purpose here.

84
00:05:21,400 --> 00:05:28,330
So whenever you need to do a segmentation of of things that do overlap quite a bit, you can use this

85
00:05:28,330 --> 00:05:31,690
algorithm to implement that segmentation.

86
00:05:32,110 --> 00:05:38,230
So hopefully from this lesson in useful will then move on now to background and foreground subtraction.

87
00:05:38,710 --> 00:05:39,070
Thank you.
