import os
import cv2
import pandas as pd#不做平移或者旋转等变化的
path="E:\qichacha\data\zuobiao\\csv" #原始坐标保存路径
for i in os.listdir(path):back = os.path.join(path, i)data = pd.read_csv(back,header=None)#读取图片对应名称imgBgr=cv2.imread("E:\\qichacha\\img\\zengguang\\jiaoyan\\0{}.jpg".format(data.loc[0][8]))#打开对应csv的图片for i in range(0,int(len(data))):ptRectLeftUp=(data.loc[i][0],data.loc[i][1]) #左上ptRectRightDown=(data.loc[i][6],data.loc[i][7])imgRet = cv2.rectangle(imgBgr, ptRectLeftUp, ptRectRightDown, (0, 0, 255), 3)cv2.imwrite("E:\qichacha\img\\test\\0{}.jpg".format(data.loc[0][8]), imgRet)print("成功--{}".format(data.loc[0][8]))#平移变化的
path="E:\qichacha\data\zuobiao\\csv"#同上
for i in os.listdir(path):back = os.path.join(path, i)data = pd.read_csv(back,header=None)# print(data.loc[0][8])path1=data.loc[0][8]k="E:\qichacha\data\zuobiao\\csv1\\0"+path1+".csv"#读取对应图片平移的距离csv文件路径data1=pd.read_csv(k,header=None)imgBgr=cv2.imread("E:\\qichacha\\img\\zengguang\\pingyi\\0{}.jpg".format(data.loc[0][8]))#保存对应的图片for i in range(0,int(len(data))):ptRectLeftUp=(data.loc[i][0]+data1.loc[0][0],data.loc[i][1]+data1.loc[0][1]) #左上ptRectRightDown=(data.loc[i][6]+data1.loc[0][0],data.loc[i][7]+data1.loc[0][1])#右下imgRet = cv2.rectangle(imgBgr, ptRectLeftUp, ptRectRightDown, (0, 0, 255), 3)cv2.imwrite("E:\qichacha\img\\test1\\0{}.jpg".format(data.loc[0][8]), imgRet)print("成功--{}".format(data.loc[0][8]))#裁剪,移动裁剪的起始位置宽高
path="E:\qichacha\data\zuobiao\\csv"
for i in os.listdir(path):back = os.path.join(path, i)data = pd.read_csv(back,header=None)imgBgr=cv2.imread("E:\\qichacha\\img\\zengguang\\caijian\\0{}.jpg".format(data.loc[0][8]))for n in range(0,int(len(data))):ptRectLeftUp=(data.loc[n][0]-40,data.loc[n][1]-100) #左上ptRectRightDown=(data.loc[n][6]-40,data.loc[n][7]-100)imgRet = cv2.rectangle(imgBgr, ptRectLeftUp, ptRectRightDown, (0, 0, 255), 3)cv2.imwrite("E:\\qichacha\\img\\ceshi\\test3\\0{}.jpg".format(data.loc[0][8]), imgRet)print("成功--{}".format(data.loc[0][8]))#旋转,只是1:1缩放的只有旋转可用
import math
path="E:\qichacha\data\zuobiao\\csv"#同上
for i in os.listdir(path):back = os.path.join(path, i)#对应读取的原始坐标的路径data = pd.read_csv(back,header=None)path1=data.loc[0][8]#确定对应图片和旋转度数csv的路径k="E:\qichacha\data\zuobiao\\csv2\\0"+path1+".csv"data1=pd.read_csv(k,header=None)#读取旋转度数的csv文件img=cv2.imread("E:\qichacha\img\zengguang\\xuanzhuan\\0{}.jpg".format(data.loc[0][8]))for i in range(0, int(len(data))):# 原始左上x1 = int(data.loc[i][0]);y1 = int(data.loc[i][1])# 原始右上x1_1 = int(data.loc[i][2]);y1_1 = int(data.loc[i][3])# 原始左下x1_2 = int(data.loc[i][4]);y1_2 = int(data.loc[i][5])# 原始右下x1_3 = int(data.loc[i][6]);y1_3 = int(data.loc[i][7])# 按顺时针旋转cos_5 = math.cos(math.radians(-data1.loc[0][0]))sin_5 = math.sin(math.radians(-data1.loc[0][0]))height, width = img.shape[0:2]x2 = int(width / 2);y2 = int(height / 2)# x,y旋转后的点,x1,y1旋转前的点,x2,y2中心点# 旋转后左上点x_1 = int(((x1 - x2) * cos_5) - ((y1 - y2) * sin_5) + x2)y_1 = int(((y1 - y2) * cos_5) + ((x1 - x2) * sin_5) + y2)# 旋转后右上点x_2 = int(((x1_1 - x2) * cos_5) - ((y1_1 - y2) * sin_5) + x2)y_2 = int(((y1_1 - y2) * cos_5) + ((x1_1 - x2) * sin_5) + y2)# 旋转后左下点x_3 = int(((x1_2 - x2) * cos_5) - ((y1_2 - y2) * sin_5) + x2)y_3 = int(((y1_2 - y2) * cos_5) + ((x1_2 - x2) * sin_5) + y2)# 旋转后右下点x_4 = int(((x1_3 - x2) * cos_5) - ((y1_3 - y2) * sin_5) + x2)y_4 = int(((y1_3 - y2) * cos_5) + ((x1_3 - x2) * sin_5) + y2)cv2.line(img, (x_1, y_1), (x_2, y_2), (0, 0, 255), 2)cv2.line(img, (x_1, y_1), (x_3, y_3), (0, 0, 255), 2)cv2.line(img, (x_2, y_2), (x_4, y_4), (0, 0, 255), 2)cv2.line(img, (x_3, y_3), (x_4, y_4), (0, 0, 255), 2)cv2.imwrite("E:\qichacha\img\\test2\\0{}.jpg".format(data.loc[0][8]), img)print("成功--{}".format(data.loc[0][8]))