赞
踩
- import cv2
- import numpy as np
- import random
- # 首先读入img
- img = cv2.imread('0000003.jpg')
- label = open('0000003.txt','r',encoding='utf8')
-
- lines = label.readlines()
-
- for line in lines:
- points=[]
- point_ = line.split(',')[0:-1]
- for i in range(len(point_)//2):
- points.append((int(point_[2*i]),int(point_[2*i+1])))
- x,y,w,h = cv2.boundingRect(np.array(points))
- roi_img = img[y:y+h,x:x+w]
- cv2.imshow('roi_img',roi_img)
- cv2.waitKey()
- # 画上绿色的圆圈
- for i in range(len(points)):
- points[i] = (points[i][0]-x,points[i][1]-y)
- for point in points:
- cv2.circle(roi_img, point, 1, (0, 255, 0), 2)
-
- cv2.imshow('img',roi_img)
- cv2.waitKey()
-
- tps = cv2.createThinPlateSplineShapeTransformer()
-
- sourceshape = np.array(points,np.int32)
- sourceshape = sourceshape.reshape(1,-1,2)
-
- matches =[]
- N = len(points)
- for i in range(0,N):
- matches.append(cv2.DMatch(i,i,0))
-
- # 开始变动
- newpoints=[]
- N = N//2
- dx=int(w/(N-1))
- for i in range(0,N):
- newpoints.append((dx*i,2))
- for i in range(N-1,-1,-1):
- newpoints.append((dx*i,h-2))
-
- print(points,newpoints)
- targetshape = np.array(newpoints,np.int32)
- targetshape=targetshape.reshape(1,-1,2)
- tps.estimateTransformation(targetshape,sourceshape ,matches)
- roi_img_=tps.warpImage(roi_img)
-
- for point in newpoints:
- cv2.circle(roi_img_, (int(point[0]),int(point[1])), 1, (0, 0, 255), 2)
-
- cv2.namedWindow('img2',cv2.WINDOW_FREERATIO)
- cv2.imshow('img2',roi_img_)
- cv2.waitKey()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。