图像旋转,背景指定像素填充
20190802更新: 统计待旋转图像画面中像素值最多的为背景点
#coding=utf-8
import cv2
import os
from math import *
import numpy as np
def get_background(srcimg):
gray = cv2.cvtColor(srcimg,cv2.COLOR_BGR2GRAY)
hest = np.zeros([256],dtype=np.int32)
hs = gray.shape[0]
ws = gray.shape[1]
for h in range(0,hs):
for w in range(0,ws):
pix = gray[h,w]
hest[pix] += 1
idx = np.where(hest == np.max(hest))
idxx = idx[0][0]
for h in range(0,hs):
for w in range(0,ws):
pix = gray[h,w]
if idxx == pix:
return (int(srcimg[h,w,0]),int(srcimg[h,w,1]),int(srcimg[h,w,2]))
def rotate_bound_white_bg(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
pix_border = get_background(image)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
# -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
# borderValue 缺失背景填充色彩,此处为白色,可自定义
return cv2.warpAffine(image, M, (nW, nH),
borderValue=pix_border) # return cv2.warpAffine(image, M, (nW, nH),borderValue=(0,255,255))
# borderValue 缺省,默认是黑色(0, 0 , 0)
# return cv2.warpAffine(image, M, (nW, nH))
root = "/media/data_1/2019_project_test/PSENet/data/CTW1500/train/mark/"
list_hz = os.listdir(root)
for img_name in list_hz:
img_path = root + img_name
img = cv2.imread(img_path)
imgRotation = rotate_bound_white_bg(img, 45)
cv2.imshow("img", img)
cv2.imshow("imgRotation", imgRotation)
cv2.waitKey(0)
# -*- coding: utf-8 -*-
import cv2
import os
from math import *
import numpy as np
def get_pix_background(img):
T = 15
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]
for h in range(height):
for w in range(width):
b = img[h,w,0]
g = img[h, w, 1]
r = img[h, w, 2]
if abs(b-g)<T and abs(b-r)<T and abs(g-r)<T:
return (int(b),int(g),int(r))
return (int(img[1,1,0]),int(img[1,1,1]),int(img[1,1,2]))
# 旋转angle角度,缺失背景白色(255, 255, 255)填充
def rotate_bound_white_bg(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
pix_border = get_pix_background(image)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
# -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
# borderValue 缺失背景填充色彩,此处为白色,可自定义
return cv2.warpAffine(image, M, (nW, nH), borderValue=pix_border) #return cv2.warpAffine(image, M, (nW, nH),borderValue=(0,255,255))
# borderValue 缺省,默认是黑色(0, 0 , 0)
# return cv2.warpAffine(image, M, (nW, nH))
root = "/media/data_2/everyday/0725/hz/"
list_hz = os.listdir(root)
for img_name in list_hz:
img_path = root + img_name
img = cv2.imread(img_path)
imgRotation = rotate_bound_white_bg(img, 45)
cv2.imshow("img", img)
cv2.imshow("imgRotation", imgRotation)
cv2.waitKey(0)
随机角度,批量操作
# -*- coding: utf-8 -*-
import cv2
import os
from math import *
import numpy as np
import random
def get_pix_background(img):
T = 15
height = img.shape[0]
width = img.shape[1]
channels = img.shape[2]
for h in range(height):
for w in range(width):
b = img[h,w,0]
g = img[h, w, 1]
r = img[h, w, 2]
if r<30 and b<30 and g<30:
continue
if abs(b-g)<T and abs(b-r)<T and abs(g-r)<T:
return (int(b),int(g),int(r))
return (int(img[1,1,0]),int(img[1,1,1]),int(img[1,1,2]))
# 旋转angle角度,缺失背景白色(255, 255, 255)填充
def rotate_bound_white_bg(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
pix_border = get_pix_background(image)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
# -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
# borderValue 缺失背景填充色彩,此处为白色,可自定义
return cv2.warpAffine(image, M, (nW, nH), borderValue=pix_border) #return cv2.warpAffine(image, M, (nW, nH),borderValue=(0,255,255))
# borderValue 缺省,默认是黑色(0, 0 , 0)
# return cv2.warpAffine(image, M, (nW, nH))
root = "/media/data_2/everyday/0725/hegezhang/hz_hege/"
root_save = "/media/data_2/everyday/0725/hegezhang/save/"
list_hz = os.listdir(root)
for img_name in list_hz:
img_path = root + img_name
img = cv2.imread(img_path)
for cnt in range(4):
ang = random.randint(3,350)
print("img=%s, ang=%d"%(img_name,ang))
imgRotation = rotate_bound_white_bg(img, ang)
new_name = img_name.replace('.jpg',"_"+str(ang) + '.jpg')
cv2.imwrite(root_save + new_name,imgRotation)
# cv2.imshow("img", img)
# cv2.imshow("imgRotation", imgRotation)
# cv2.waitKey(0)
好记性不如烂键盘---点滴、积累、进步!