Skip to content

Commit 58b6314

Browse files
committed
[milestone] finish the runnable first version of this program
Finish the program containing the following works: 1. add file "canny_watershed.py" as main code 2. add two images "coins.jpg" and "四破魚(藍圓鰺)2.jpg" for testing 3. create conda environment named "canny_watershed_classic" and export the environment settings to "environment.yml" 4. modify "README.md" to let other people the development tools I use
1 parent bd50326 commit 58b6314

5 files changed

+119
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# canny_watershed_classic
22
an image segmentation practice using canny edge detection and watershed algorithm
3+
4+
# Note
5+
I use Anaconda to develop this project, and the requirements are written in ```environment.yml```

canny_watershed.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
import cv2 as cv
3+
import numpy as np
4+
5+
# first, read the image
6+
#image = cv.imread('coins.jpg')
7+
image = cv.imread('四破魚(藍圓鰺)2.jpg')
8+
cv.imshow('Original image', image)
9+
10+
'''
11+
part of misc
12+
'''
13+
# change image from BGR to grayscale
14+
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
15+
# apply thresholding to convert the image to binary
16+
ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
17+
# erode the image
18+
foreground = cv.erode(thresh, None, iterations = 1)
19+
# Dilate the image
20+
backgroundTemp = cv.dilate(thresh, None, iterations = 1)
21+
# Apply thresholding
22+
ret, background = cv.threshold(backgroundTemp, 1, 128, 1)
23+
# Add foreground and background
24+
marker = cv.add(foreground, background)
25+
26+
'''
27+
part of canny
28+
'''
29+
# apply (Gaussian) filter for canny edge detector preprocessing
30+
gaussian = cv.GaussianBlur(marker, (5, 5), 0)
31+
# apply canny edge detection
32+
canny = cv.Canny(gaussian, 100, 300)
33+
34+
'''
35+
part of watershed
36+
'''
37+
# Finding the contors in the image using chain approximation
38+
new, contours, hierarchy = cv.findContours(canny, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
39+
# converting the marker to float 32 bit
40+
marker32 = np.int32(marker)
41+
# Apply watershed algorithm
42+
cv.watershed(image, marker32)
43+
# Apply thresholding on the image to convert to binary image
44+
m = cv.convertScaleAbs(marker32)
45+
ret, thresh = cv.threshold(m, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
46+
# Invert the thresh
47+
thresh_inv = cv.bitwise_not(thresh)
48+
# Bitwise and with the image mask thresh
49+
res = cv.bitwise_and(image, image, mask = thresh)
50+
# Bitwise and the image with mask as threshold invert
51+
res3 = cv.bitwise_and(image, image, mask = thresh_inv)
52+
# Take the weighted average
53+
res4 = cv.addWeighted(res, 1, res3, 1, 0)
54+
# Draw the contours on the image with green color and pixel width is 1
55+
final = cv.drawContours(res4, contours, -1, (0, 255, 0), 1)
56+
57+
# Display the image
58+
cv.imshow("Watershed", final)
59+
# Wait for key stroke
60+
cv.waitKey()

coins.jpg

15.9 KB
Loading

environment.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: canny_watershed_classic
2+
channels:
3+
- defaults
4+
dependencies:
5+
- blas=1.0=mkl
6+
- bzip2=1.0.6=h1de35cc_5
7+
- ca-certificates=2018.03.07=0
8+
- cairo=1.14.12=hc4e6be7_4
9+
- certifi=2018.4.16=py36_0
10+
- ffmpeg=3.4=h8a2ae75_0
11+
- fontconfig=2.13.0=h5d5b041_1
12+
- freetype=2.9.1=hb4e5f40_0
13+
- gettext=0.19.8.1=h15daf44_3
14+
- glib=2.56.1=h35bc53a_0
15+
- graphite2=1.3.11=h2098e52_2
16+
- harfbuzz=1.8.4=hb8d4a28_0
17+
- hdf5=1.10.2=hfa1e0ec_1
18+
- icu=58.2=h4b95b61_1
19+
- intel-openmp=2018.0.3=0
20+
- jasper=2.0.14=h636a363_1
21+
- jpeg=9b=he5867d9_2
22+
- libcxx=4.0.1=h579ed51_0
23+
- libcxxabi=4.0.1=hebd6815_0
24+
- libedit=3.1.20170329=hb402a30_2
25+
- libffi=3.2.1=h475c297_4
26+
- libgfortran=3.0.1=h93005f0_2
27+
- libiconv=1.15=hdd342a3_7
28+
- libopencv=3.4.1=h14a57ad_3
29+
- libopus=1.2.1=h169cedb_0
30+
- libpng=1.6.34=he12f830_0
31+
- libprotobuf=3.5.2=h2cd40f5_0
32+
- libtiff=4.0.9=hcb84e12_1
33+
- libvpx=1.7.0=h378b8a2_0
34+
- libxml2=2.9.8=hab757c2_1
35+
- mkl=2018.0.3=1
36+
- mkl_fft=1.0.4=py36h5d10147_1
37+
- mkl_random=1.0.1=py36h5d10147_1
38+
- ncurses=6.1=h0a44026_0
39+
- numpy=1.15.0=py36h648b28d_0
40+
- numpy-base=1.15.0=py36h8a80b8c_0
41+
- opencv=3.4.1=py36h6fd60c2_3
42+
- openssl=1.0.2o=h26aff7b_0
43+
- pcre=8.42=h378b8a2_0
44+
- pip=10.0.1=py36_0
45+
- pixman=0.34.0=hca0a616_3
46+
- py-opencv=3.4.1=py36h4b7e113_3
47+
- python=3.6.6=hc167b69_0
48+
- readline=7.0=hc1231fa_4
49+
- setuptools=39.2.0=py36_0
50+
- sqlite=3.24.0=ha441bb4_0
51+
- tk=8.6.7=h35a86e2_3
52+
- wheel=0.31.1=py36_0
53+
- xz=5.2.4=h1de35cc_4
54+
- zlib=1.2.11=hf3cbc9b_2
55+
prefix: /usr/local/anaconda3/envs/canny_watershed_classic
56+

四破魚(藍圓鰺)2.jpg

88.9 KB
Loading

0 commit comments

Comments
 (0)