-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
61 lines (55 loc) · 2.51 KB
/
Copy pathapp.py
File metadata and controls
61 lines (55 loc) · 2.51 KB
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
"""
This Python script shows why we need to convert the image to grayscale
and why other single channel values like R, G, and B, do not work well for
contour detection.
"""
import os
import time
import cv2
import logging
def main():
# read the image
image = cv2.imread('image_2.jpg')
directory = "channel_experiment_output"
parent_dir = "/data/"
path = os.path.join(parent_dir, directory)
if os.path.exists(path):
print("{} folder exists".format(directory))
else:
os.mkdir(path)
# B, G, R channel splitting
blue, green, red = cv2.split(image)
# detect contours using blue channel and without thresholding
contours1, hierarchy1 = cv2.findContours(image=blue, mode=cv2.RETR_TREE,
method=cv2.CHAIN_APPROX_NONE)
# draw contours on the original image
image_contour_blue = image.copy()
cv2.drawContours(image=image_contour_blue, contours=contours1, contourIdx=-1,
color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
# see the results
logging.info("Generating blue channel image")
cv2.imwrite(os.path.join(path, 'blue_channel.jpg'), image_contour_blue)
# detect contours using green channel and without thresholding
contours2, hierarchy2 = cv2.findContours(image=green, mode=cv2.RETR_TREE,
method=cv2.CHAIN_APPROX_NONE)
# draw contours on the original image
image_contour_green = image.copy()
cv2.drawContours(image=image_contour_green, contours=contours2, contourIdx=-1,
color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
# see the results
logging.info("Generating green channel image")
cv2.imwrite(os.path.join(path, 'green_channel.jpg'), image_contour_green)
# detect contours using red channel and without thresholding
contours3, hierarchy3 = cv2.findContours(image=red, mode=cv2.RETR_TREE,
method=cv2.CHAIN_APPROX_NONE)
# draw contours on the original image
image_contour_red = image.copy()
cv2.drawContours(image=image_contour_red, contours=contours3, contourIdx=-1,
color=(0, 255, 0), thickness=2, lineType=cv2.LINE_AA)
# see the results
logging.info("Generating red channel image")
cv2.imwrite(os.path.join(path, 'red_channel.jpg'), image_contour_red)
print("sleeping for some time")
time.sleep(60)
if __name__ == '__main__':
main()