import pandas as pd
import cv2
def convert_to_top_left_origin(x, y, width, height):
# 图像中心点的x和y坐标
center_x = width // 2
center_y = height // 2
if x > 0:
new_x = x + center_x
if y > 0:
new_y = center_y - y
else:
new_y = center_y + abs(y)
else:
new_x = center_x - abs(x)
if y > 0:
new_y = center_y - y
else:
new_y = center_y + abs(y)
return new_x,new_y
img = cv2.imread(r'C:\autoMac\imgs1\Z66743KP1926_20240425_103334_11A600_.jpg',0)
_,bimg = cv2.threshold(img,10,255,cv2.THRESH_BINARY)
contours, _ = cv2.findContours(bimg, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
glass = img[y:y+h, x:x+w]
H, W = glass.shape
center_x = W//2
center_y = H//2
acc_x = W / 1850
acc_y = H / 1500
glass = cv2.cvtColor(glass,cv2.COLOR_GRAY2BGR)
datas = pd.read_excel(r'E:\412\MAC\670B110(HD080).xlsx')
left_top = datas['Panel左上角-a']
right_bottom = datas['Panel右下角-d']
for i in range(1,len(left_top)):
left_top_x = datas.iloc[i,3]
left_top_y = datas.iloc[i,4]
right_bottom_x = datas.iloc[i,9]
right_bottom_y = datas.iloc[i, 10]
x1 = int(left_top_x/1000 * acc_x)
y1 = int(left_top_y/1000 * acc_y)
x2 = int(right_bottom_x/1000 * acc_x)
y2 = int(right_bottom_y/1000 * acc_y)
x1_, y1_ = convert_to_top_left_origin(x1, y1, W, H)
x2_, y2_ = convert_to_top_left_origin(x2, y2, W, H)
if left_top_y == 41006:
y1_ += 70
y2_ += 70
cv2.rectangle(glass, (x1_, y1_), (x2_, y2_), (0, 0, 255), 20)
cv2.namedWindow('0', 0)
cv2.imshow('0', glass)
cv2.waitKey(0)
根据layout信息扣取panel图片
最新推荐文章于 2024-11-01 16:24:55 发布