-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider 1.2.py
149 lines (121 loc) · 6.29 KB
/
slider 1.2.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
Version 1.1 + tkinter file gui finder and grayscale threshold slider
Author: EYW and Addison Spiegel
Created: 9/21
"""
import cv2
import numpy
import os.path
# importing the gui module
import tkinter as tk
# importing the function that creates the file finder
from tkinter.filedialog import askopenfilename
# this function does nothing, it is for the creation of the cv2 createTrackbar, it requires a function as an argument
def nothing(x):
pass
class Application(tk.Frame):
# initializing the tkinter application
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
# creates the button to pick a file
def create_widgets(self):
self.quit = tk.Button(self, text="Pick a file", fg="black",
command=self.getFile)
self.quit.pack(side="bottom")
def getFile(self):
filename_valid = False
# keep asking for the correct file to be inputted
while filename_valid == False:
self.filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
# checks if file name is valid
if os.path.isfile(self.filename) == True:
filename_valid = True
else:
print ("Something was wrong with that filename. Please try again.")
self.createImage()
def createImage(self):
# returns a the image in rgb
original_image = cv2.imread(self.filename,1)
# returns the image in greyscale
grayscale_image_simple = cv2.imread(self.filename, 0)
# converts the greyscale image back to bgr
grayscale_image = cv2.cvtColor(grayscale_image_simple, cv2.COLOR_GRAY2BGR)
# Creates a new window on the display with the name you give it
# cv2.namedWindow('Original Image')
# cv2.namedWindow('Grayscale Image')
# cv2.namedWindow('Red Parts of Image')
# cv2.namedWindow('Yellow Parts of Image')
cv2.namedWindow('Customized Image')
cv2.namedWindow('sliderWindow')
# the height of the image
image_height = original_image.shape[0]
# the width of the image
image_width = original_image.shape[1]
# how many channels the image has, in this case, 3
image_channels = original_image.shape[2]
cv2.createTrackbar("threshold", "sliderWindow",100,255,nothing)
while True:
grayscale_break = cv2.getTrackbarPos('threshold', 'sliderWindow')
# creating an empty image for the red and yellow images
red_paper = numpy.zeros((image_height,image_width,image_channels), numpy.uint8)
yellow_paper = numpy.zeros((image_height,image_width,image_channels),
numpy.uint8)
# changing every pixel in the red image to red
red_paper[0:image_height,0:image_width, 0:image_channels] = [0,0,255]
# changing every pixel in the yellow image to yellow
yellow_paper[0:image_height,0:image_width, 0:image_channels] = [0,255,255]
#making the range of greyscales
min_grayscale_for_red = [0,0,0]
max_grayscale_for_red = [grayscale_break,grayscale_break,grayscale_break]
min_grayscale_for_yellow = [grayscale_break+1,grayscale_break+1,
grayscale_break+1]
max_grayscale_for_yellow = [255,255,255]
# converting these arrays to numpy arrays
min_grayscale_for_red = numpy.array(min_grayscale_for_red, dtype = "uint8")
max_grayscale_for_red = numpy.array(max_grayscale_for_red, dtype = "uint8")
min_grayscale_for_yellow = numpy.array(min_grayscale_for_yellow,
dtype = "uint8")
max_grayscale_for_yellow = numpy.array(max_grayscale_for_yellow,
dtype = "uint8")
# returns a mask that is made up of pure black and white pixels.
# Converts grayscale pixels within the range of the two arguments
# All the red parts turn white
block_all_but_the_red_parts = cv2.inRange(grayscale_image,
min_grayscale_for_red,
max_grayscale_for_red)
# converts all the yellow parts of the image to white
block_all_but_the_yellow_parts = cv2.inRange(grayscale_image,
min_grayscale_for_yellow,
max_grayscale_for_yellow)
# applies the mask onto the solid red and yellow images
# the black from the previous 'block' variables goes on top of the solid red and yellow images
red_parts_of_image = cv2.bitwise_or(red_paper, red_paper,
mask = block_all_but_the_red_parts)
yellow_parts_of_image = cv2.bitwise_or(yellow_paper, yellow_paper,
mask = block_all_but_the_yellow_parts)
# combines the two images, the black part on the yellow image is merged with the red from the other image
customized_image = cv2.bitwise_or(red_parts_of_image, yellow_parts_of_image)
# showing the image in the previously created windows
#cv2.imshow('Original Image', original_image)
#cv2.imshow('Grayscale Image',grayscale_image)
#cv2.imshow('Red Parts of Image',red_parts_of_image)
#cv2.imshow('Yellow Parts of Image',yellow_parts_of_image)
cv2.imshow('Customized Image',customized_image)
# if the key is pressed it will close all the windows
keypressed = cv2.waitKey(10)
if keypressed == 27:
cv2.destroyAllWindows()
break
# if s is pressed, it will save the images
elif keypressed == ord('s'):
cv2.imwrite('photo_GS_1.jpg',grayscale_image)
cv2.imwrite('photo_RY_1.jpg',customized_image)
cv2.destroyAllWindows()
break
quit()
root = tk.Tk()
app = Application(master=root)
app.mainloop()