opencv-python常用函数复制粘贴


#根据大边resize
largeborder = max(img.shape)    
(imgh,imgw) = img.shape[:2]
scalefactor = 1280.0/largeborder       
newh = int(imgh*scalefactor)
neww = int(imgw*scalefactor)      
img = cv2.resize(img,(neww,newh),interpolation=cv2.INTER_AREA)

#二值化
retval, dst = cv2.threshold(im_gray, 50, 255, cv2.THRESH_BINARY) 
dst = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 3)

#轮廓
img,contours, hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)    

cv2.drawContours(img_color,contours,-1,(0,0,255),3) ##thickness=cv2.FILLED填充轮廓

#轮廓外接矩形
rect = cv2.boundingRect(contour) 

#bounder
dst = cv2.copyMakeBorder(src, 50,50,50,50, cv2.BORDER_CONSTANT,value = [255,255,255]) 

#通道转换
img_color = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)


#旋转

img_ex = cv2.copyMakeBorder(img, 300,300,300,300, cv2.BORDER_REPLICATE)
(h, w) = img_ex.shape[:2]
center = (w / 2, h / 2)
M_rotate = cv2.getRotationMatrix2D(center, d, 1)
img_ex_rotated = cv2.warpAffine(img_ex, M_rotate, (w, h))
img_rotated = img_ex_rotated[299:299+imgh,299:299+imgw]


#写txt
traintxt = "train.txt"
ftrain = open(traintxt, "w")
ftrain.write(''+'\n')
ftrain.close()

#读txt
imagelist = open('train.txt', 'r').read().splitlines()

#esc退出
if cv2.waitKey(0)==27:
    cv2.destroyAllWindows()





你可能感兴趣的:(opencv-python常用函数复制粘贴)