Skip to content

Commit e31136e

Browse files
committed
fix bug: add first box
1 parent e9c8ba2 commit e31136e

2 files changed

Lines changed: 44 additions & 33 deletions

File tree

mulimg_viewer.py

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,9 @@ def img_left_release(self, event):
471471

472472
show_scale = self.show_scale.GetLineText(0).split(',')
473473
show_scale = [float(x) for x in show_scale]
474-
self.xy_magnifier.append(
475-
self.ImgManager.sort_box_point([x, y, x_0, y_0])+show_scale)
474+
points = self.ImgManager.sort_box_point(
475+
[x_0, y_0, x, y], show_scale, first_point=True)
476+
self.xy_magnifier.append(points+show_scale)
476477
self.refresh(event)
477478

478479
def img_right_click(self, event):
@@ -488,9 +489,7 @@ def img_right_click(self, event):
488489
show_scale = self.show_scale.GetLineText(0).split(',')
489490
show_scale = [float(x) for x in show_scale]
490491
points = self.move_box_point(x, y, show_scale)
491-
points_show_scale = self.ImgManager.sort_box_point(
492-
points)+show_scale
493-
self.xy_magnifier.append(points_show_scale)
492+
self.xy_magnifier.append(points+show_scale)
494493
except:
495494
self.SetStatusText_(
496495
["-1", "Drawing a box need click left mouse button!", "-1", "-1"])
@@ -500,39 +499,16 @@ def move_box_point(self, x, y, show_scale):
500499
x_0, y_0, x_1, y_1 = self.xy_magnifier[0][0:4]
501500
show_scale_old = self.xy_magnifier[0][4:6]
502501
scale = [show_scale[0]/show_scale_old[0],
503-
show_scale[1]/show_scale_old[1]]
502+
show_scale[1]/show_scale_old[1]]
504503
x_0 = int(x_0*scale[0])
505504
x_1 = int(x_1*scale[0])
506505
y_0 = int(y_0*scale[1])
507506
y_1 = int(y_1*scale[1])
508-
x_center_old, y_center_old, width, height = self.get_center_box(
509-
[x_0, y_0, x_1, y_1], more=True)
507+
x_center_old, y_center_old = self.get_center_box(
508+
[x_0, y_0, x_1, y_1])
510509
delta_x = x-x_center_old
511510
delta_y = y-y_center_old
512-
513-
img_resolution = (np.array(self.ImgManager.img_resolution_origin) * np.array(show_scale)).astype(np.int)
514-
515-
if x_1+delta_x > img_resolution[0]:
516-
x = img_resolution[0]
517-
x_0 = img_resolution[0]-width
518-
elif x_0+delta_x < 0:
519-
x_0 = 0
520-
x = width
521-
else:
522-
x_0 = x_0+delta_x
523-
x = x_1+delta_x
524-
525-
if y_1+delta_y > img_resolution[1]:
526-
y = img_resolution[1]
527-
y_0 = img_resolution[1]-height
528-
elif y_0+delta_y < 0:
529-
y_0 = 0
530-
y = height
531-
else:
532-
y_0 = y_0+delta_y
533-
y = y_1+delta_y
534-
535-
return [x_0, y_0, x, y]
511+
return self.ImgManager.sort_box_point([x_0+delta_x, y_0+delta_y, x_1+delta_x, y_1+delta_y], show_scale)
536512

537513
def get_center_box(self, box, more=False):
538514
x_0, y_0, x_1, y_1 = box

utils.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,8 @@ def crop_points_process(self, crop_points, img_mode=0):
774774

775775
self.crop_points = crop_points_
776776

777-
def sort_box_point(self, box_point):
777+
def sort_box_point(self, box_point, show_scale, first_point=False):
778+
# box_point = [x_0,y_0,x_1,y_1]
778779
if box_point[2] < box_point[0]:
779780
temp = box_point[0]
780781
box_point[0] = box_point[2]
@@ -783,6 +784,40 @@ def sort_box_point(self, box_point):
783784
temp = box_point[1]
784785
box_point[1] = box_point[3]
785786
box_point[3] = temp
787+
788+
img_resolution = (np.array(self.img_resolution_origin) * np.array(show_scale)).astype(np.int)
789+
790+
width = abs(box_point[0]-box_point[2])
791+
height = abs(box_point[1]-box_point[3])
792+
793+
# limit box boundary
794+
if first_point:
795+
if box_point[2] > img_resolution[0]:
796+
box_point[2] = img_resolution[0]
797+
798+
if box_point[0] < 0:
799+
box_point[0] = 0
800+
801+
if box_point[3] > img_resolution[1]:
802+
box_point[3] = img_resolution[1]
803+
804+
if box_point[1]< 0:
805+
box_point[1] = 0
806+
else:
807+
if box_point[2] > img_resolution[0]:
808+
box_point[2] = img_resolution[0]
809+
box_point[0] = img_resolution[0]-width
810+
elif box_point[0] < 0:
811+
box_point[0] = 0
812+
box_point[2] = width
813+
814+
if box_point[3] > img_resolution[1]:
815+
box_point[3] = img_resolution[1]
816+
box_point[1] = img_resolution[1]-height
817+
elif box_point[1]< 0:
818+
box_point[1] = 0
819+
box_point[3] = height
820+
786821
return box_point
787822

788823
def magnifier_preprocessing(self, img, img_mode=0):

0 commit comments

Comments
 (0)