1.首先導入工程所需要的三方包,,這里需要opencv,、numpy,、math
import cv2
import numpy as np
import math
2.讀取圖片
img = cv2.imread(file_path)
3.圖片去噪
img_c = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
4.處理成灰度圖
gray = cv2.cvtColor(img_c, cv2.COLOR_BGR2GRAY)
5.Sobel算子,,x方向求梯度,主要用于獲得數(shù)字圖像的一階梯度,常見的應用和物理意義是邊緣檢測,。(可以根據(jù)需求選擇算子)
sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
6.二值化
ret, binary = cv2.threshold(sobel, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
7.霍夫直線
hufu = binary.astype(np.uint8)
lines = cv2.HoughLinesP(hufu, 1, np.pi / 180, 30, minLineLength=40, maxLineGap=100)
# 在圖像上展示霍夫直線描出的直線
# for line in lines:
# cv2.line(img, (line[0][0], line[0][1]), (line[0][2], line[0][3]), (0, 0, 255), 2)
8.求出所有直線斜率,,求出眾數(shù)(考慮誤差),這一步會產(chǎn)生個bug,,因為這些直線是利用霍夫直線找到的,,所以說需要根據(jù)實際場景來設置霍夫直線的相關參數(shù),不然會對圖片中的線性非常敏感,,比如條形碼
k_dict = {}
k = 0
for line in lines:
if line[0][2] - line[0][0] == 0:
continue
print(line[0][3],line[0][1],line[0][2],line[0][0])
k = (line[0][3] - line[0][1]) / (line[0][2] - line[0][0])
# α = atan(k) * 180 / PI
k = math.atan(k) * 180 / np.pi
if len(k_dict.keys()) == 0 :
k_dict[k] = 1
else:
flag = False
for item in k_dict.keys():
if abs(item - k) < 2:
flag = True
k_dict[item] += 1
break
if not flag:
k_dict[k] = 1
must_k_num = 0
must_key = 0
for item in k_dict.keys():
if k_dict[item] > must_k_num:
must_k_num = k_dict[item]
must_key = item
print(must_key)
9.旋轉(zhuǎn)圖像,,在旋轉(zhuǎn)圖像之前需要對圖片進行填充防止旋轉(zhuǎn)后邊角溢出(這一步可以根據(jù)角度和勾股定理來計算精準的填充大小),,利用仿射變換來旋轉(zhuǎn)圖像
#旋轉(zhuǎn)圖像
h, w = img.shape[:2]
add_w = int((((w*w + h*h) ** 0.5) - w)/2)
add_h = int((((w*w + h*h) ** 0.5) - h)/2)
print(add_w,add_h)
img = cv2.copyMakeBorder(img,add_h,add_h,add_w,add_w, cv2.BORDER_CONSTANT,value=[0,0,0])
h, w = img.shape[:2]
center = (w//2, h//2)
M = cv2.getRotationMatrix2D(center, must_key, 1.0)
rotated = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC)
cv2.imwrite(file_path,rotated)
cv2.imshow("rotated", rotated)
cv2.waitKey(0)
全部代碼在這?。。,。,。?!
import cv2
import numpy as np
import math
def rotate(file_path):
img = cv2.imread(file_path)
#去噪
img_c = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
# 灰度圖
gray = cv2.cvtColor(img_c, cv2.COLOR_BGR2GRAY)
# cv2.imshow("gray", gray)
# Sobel算子,,x方向求梯度,主要用于獲得數(shù)字圖像的一階梯度,,常見的應用和物理意義是邊緣檢測,。
sobel = cv2.Sobel(gray, cv2.CV_8U, 1, 0, ksize=3)
# 二值化
ret, binary = cv2.threshold(sobel, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
# 霍夫直線
hufu = binary.astype(np.uint8)
lines = cv2.HoughLinesP(hufu, 1, np.pi / 180, 30, minLineLength=40, maxLineGap=100)
# for line in lines:
# cv2.line(img, (line[0][0], line[0][1]), (line[0][2], line[0][3]), (0, 0, 255), 2)
k_dict = {}
k = 0
#求出所有直線斜率,求出眾數(shù)(考慮誤差)
for line in lines:
if line[0][2] - line[0][0] == 0:
continue
print(line[0][3],line[0][1],line[0][2],line[0][0])
k = (line[0][3] - line[0][1]) / (line[0][2] - line[0][0])
# α = atan(k) * 180 / PI
k = math.atan(k) * 180 / np.pi
if len(k_dict.keys()) == 0 :
k_dict[k] = 1
else:
flag = False
for item in k_dict.keys():
if abs(item - k) < 2:
flag = True
k_dict[item] += 1
break
if not flag:
k_dict[k] = 1
must_k_num = 0
must_key = 0
for item in k_dict.keys():
if k_dict[item] > must_k_num:
must_k_num = k_dict[item]
must_key = item
print(must_key)
#旋轉(zhuǎn)圖像
h, w = img.shape[:2]
add_w = int((((w*w + h*h) ** 0.5) - w)/2)
add_h = int((((w*w + h*h) ** 0.5) - h)/2)
print(add_w,add_h)
img = cv2.copyMakeBorder(img,add_h,add_h,add_w,add_w, cv2.BORDER_CONSTANT,value=[0,0,0])
h, w = img.shape[:2]
center = (w//2, h//2)
M = cv2.getRotationMatrix2D(center, must_key, 1.0)
rotated = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC)
cv2.imwrite(file_path,rotated)
cv2.imshow("rotated", rotated)
cv2.waitKey(0)
謝謝大家觀看!
|