-
Notifications
You must be signed in to change notification settings - Fork 0
/
re.txt
100 lines (78 loc) · 2.94 KB
/
re.txt
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
import cv2
import numpy as np
import pandas as pd
import argparse
import tkinter as tk
from tkinter import filedialog
# Creating argument parser to take image path from command line
ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=False, help="Image Path")
args = vars(ap.parse_args())
root = tk.Tk()
root.withdraw()
# Open file dialog to select an image
file_path = filedialog.askopenfilename()
if file_path:
img_path = file_path
elif args["image"]:
img_path = args["image"]
else:
print("Please provide an image path")
# Reading the image with opencv
img = cv2.imread(img_path)
# Declaring global variables (are used later on)
clicked = False
r = g = b = xpos = ypos = 0
# Reading csv file with pandas and giving names to each column
csv = pd.read_csv('named_colors_output.csv')
# Function to calculate minimum distance from all colors and get the most matching color
def getColorName(R, G, B):
minimum = 10000
for i in range(len(csv)):
d = abs(R - int(csv.loc[i, "R"])) + abs(G - int(csv.loc[i, "G"])) + abs(B - int(csv.loc[i, "B"]))
if(d <= minimum):
minimum = d
cname = csv.loc[i, "color_name"]
return cname
# Function to get x,y coordinates of mouse click
def draw_function(event, x, y, flags, param):
if event == cv2.EVENT_MOUSEMOVE:
global b, g, r
b, g, r = img[y, x]
color_scanner = np.zeros((300, 300, 3), np.uint8)
color_scanner[:, :] = (b, g, r)
cv2.putText(color_scanner, "B: {} G: {} R: {}".format(b, g, r), (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.imshow('color_scanner', color_scanner)
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_function)
while True:
# Set maximum size of the image
max_size = 800
# Calculate the scale percent based on the maximum size and the image's original size
scale_percent = min(max_size / img.shape[1], max_size / img.shape[0]) * 100
# Calculate the new dimensions of the scaled image
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# Resize the image
scaled_img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
# Display the scaled image
cv2.imshow("image", scaled_img)
# Update the color rectangle and text when the user clicks on the image
text = ""
if clicked:
cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1)
text = getColorName(r, g, b) + ' R=' + str(r) + ' G=' + str(g) + ' B=' + str(b)
# Set font properties
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
thickness = 2
# Display the text on the image
cv2.putText(scaled_img, text, (50, 50), font, font_scale, (255, 255, 255), thickness, cv2.LINE_AA)
# Press 'esc' to exit the program
k = cv2.waitKey(20) & 0xFF
if k == 27:
break
# Press 'c' to capture the color and display it on the rectangle
if k == ord('c'):
clicked = True