Skip to content

Commit 7ce5aa4

Browse files
author
Gin
committed
fix image viewer script
1 parent d9f00bf commit 7ce5aa4

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

SerialPrograms/Scripts/image_viewer.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@
2727
import numpy as np
2828

2929
class ImageViewer:
30-
def __init__(self, image, highlight_list = []):
31-
self.image = image
32-
self.hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
30+
def __init__(self, image: np.ndarray, highlight_list = []):
31+
self.image = image # bgr or bgra channel order
32+
self.nc = image.shape[2] # num_channel
33+
if self.nc == 4:
34+
alpha_free_image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
35+
else:
36+
alpha_free_image = image
37+
self.hsv_image = cv2.cvtColor(alpha_free_image, cv2.COLOR_BGR2HSV)
3338
self.selected_pixel = (-1, -1)
3439
self.buffer = image.copy()
3540
self.window_name = 'image'
@@ -41,7 +46,6 @@ def __init__(self, image, highlight_list = []):
4146
self.cur_rect_index = -1
4247
self.mouse_down = False
4348
self.mouse_move_counter = 0
44-
self.nc = image.shape[2] # num_channel
4549
# The size of the cross used to highlight a selected pixel
4650
self.cross_size = max(1, min(self.width, self.height) // 200)
4751

@@ -105,7 +109,7 @@ def _print_pixel(self, coord):
105109
else:
106110
mgs += f"rgb=[{p[2]}, {p[1]}, {p[0]}]"
107111
p = self.hsv_image[coord[1], coord[0]]
108-
msg += f", hsv=[{p[2]}, {p[1]}, {p[0]}]"
112+
msg += f", hsv=[{p[0]}, {p[1]}, {p[2]}]"
109113
print(msg)
110114

111115
def _print_rect(self, i, rect):
@@ -250,6 +254,7 @@ def run(self):
250254

251255
filename = sys.argv[1]
252256

257+
# bgr or bgra channel order
253258
image = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
254259

255260
height = image.shape[0]

0 commit comments

Comments
 (0)