代码拉取完成,页面将自动刷新
import numpy as np
import cv2
from collections import Counter
"""
方案1, 实验版本,还未成功
主要思想是将线段提取出主要的斜率,并确定名片方向
张晋伟 - 21215158
"""
def CrossPoint(line1, line2):
"""找两条线的交点"""
x0, y0, x1, y1 = line1[0]
x2, y2, x3, y3 = line2[0]
dx1 = x1 - x0
dy1 = y1 - y0
dx2 = x3 - x2
dy2 = y3 - y2
D1 = x1 * y0 - x0 * y1
D2 = x3 * y2 - x2 * y3
y = float(dy1 * D2 - D1 * dy2) / (dy1 * dx2 - dx1 * dy2)
x = float(y * dx1 - D1) / dy1
return (int(x), int(y))
def SortPoint(points):
"""对四个交点进行排序"""
sp = sorted(points, key=lambda x: (int(x[1]), int(x[0])))
if sp[0][0] > sp[1][0]:
sp[0], sp[1] = sp[1], sp[0]
if sp[2][0] > sp[3][0]:
sp[2], sp[3] = sp[3], sp[2]
return sp
def drawlines(lines, original_pic):
"""将线们绘制在图形上"""
i = 1
for line in lines: # 3.将检测的线画出来(注意是极坐标噢)
# print("line[" + str(i - 1) + "]=", line)
x1, y1, x2, y2 = line[0] # 两点确定一条直线,这里就是通过遍历得到的两个点的数据 (x1,y1)(x2,y2)
cv2.line(original_pic, (x1, y1), (x2, y2), (0, 0, 255), 2) # 在原图上画线
i = i + 1
cv2.imshow("line_detect", original_pic) # 显示最后的成果图
def x_or_y_check(x0, y0, x1, y1):
if abs(x1 - x0) > abs(y1 - y0):
return 1 # 横线
else:
return 0 # 竖线
def get_function(x0, y0, x1, y1):
"""两点确定一条直线 y = ax + b(对于横线) x = cy + d(对于竖线)"""
length = ((x0 - x1)**2 + (y1 - y0)**2)**0.5
x_or_y = x_or_y_check(x0, y0, x1, y1)
if x_or_y: # 横线
a = (y1 - y0) / abs(x1 - x0)
b = min([y1, y0]) + a * min([x1, x0])
else: # 竖线
a = (x1 - x0) / abs(y1 - y0)
b = min([x1, x0]) + a * min([y1, y0])
return x_or_y, a, b, length, [x0, y0, x1, y1] # 返回: 竖线还是横线, 斜率, 偏移
def find_4_lines(lines):
"""从线段列表中找到最拟合的4条边框"""
lines_pool = [] # 构造一个横线方程池
# lines_pool_y = [] # 构造一个竖线方程池
for line in lines:
x0, y0, x1, y1 = line[0]
x_or_y, a, b, l, x = get_function(x0, y0, x1, y1) # 获得本条直线的表达式
print(x_or_y, a, b, round(a*5, 1), b//100, l, x)
#key = str(x_or_y) + '_' + str(round(a*5, 1)) + '_' + str(b//100)
if b//100 not in (2, 3, 4, 5, 6, 7) and abs(a) <= 0.35: # b为截距,过滤截距以过滤掉名片中间的字所产生的线。
lines_pool.append([x])
lines_pool.sort()
result = np.array(lines_pool)
return result
def imgcorr(src):
rgbsrc = src.copy()
gray = cv2.cvtColor(rgbsrc, cv2.COLOR_RGB2GRAY)
# blurimg = cv2.GaussianBlur(gray, (3, 3), 0)
ret, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_TRUNC)
Cannyimg = cv2.Canny(thresh, 50, 80)
# cv2.imshow('Cannyimg', Cannyimg)
lines = cv2.HoughLinesP(Cannyimg, 0.7, theta=np.pi / 180, threshold=100, minLineLength=100, maxLineGap=10)
result_line = find_4_lines(lines)
drawlines(result_line, rgbsrc) # 画出找到的线
# for i in range(int(np.size(lines) / 4)):
# for x1, y1, x2, y2 in lines[i]:
# cv2.line(rgbsrc, (x1, y1), (x2, y2), (255, 255, 0), 3)
# cv2.imshow('lines', rgbsrc)
# points = np.zeros((4, 2), dtype="float32")
# points[0] = CrossPoint(lines[0], lines[2])
# points[1] = CrossPoint(lines[0], lines[3])
# points[2] = CrossPoint(lines[1], lines[2])
# points[3] = CrossPoint(lines[1], lines[3])
# sp = SortPoint(points)
# width = int(np.sqrt(((sp[0][0] - sp[1][0]) ** 2) + (sp[0][1] - sp[1][1]) ** 2))
# height = int(np.sqrt(((sp[0][0] - sp[2][0]) ** 2) + (sp[0][1] - sp[2][1]) ** 2))
# dstrect = np.array([
# [0, 0],
# [width - 1, 0],
# [0, height - 1],
# [width - 1, height - 1]
# ], dtype="float32")
# transform = cv2.getPerspectiveTransform(np.array(sp), dstrect)
# warpedimg = cv2.warpPerspective(src, transform, (width, height))
# return warpedimg
if __name__ == '__main__':
src = cv2.imread(r"D:\Users\zhangjinwei\Desktop\books\master\first_semester\1_digital_pic\hw2\HW2\4.jpg")
dst = imgcorr(src)
# cv2.imshow("Image", dst)
cv2.waitKey(0)
# cv2.imwrite(r"D:\Users\zhangjinwei\Desktop\books\master\first_semester\1_digital_pic\hw2\mingpian_data\4_1.jpg", dst)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。