1 Star 0 Fork 0

繁华落尽 / Stitching-ID-Card-master

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
最小外接矩形.py 6.73 KB
一键复制 编辑 原始数据 按行查看 历史
繁华落尽 提交于 2021-02-04 18:05 . 1111
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
"""
@File :最小外接矩形.py
@Author :keyin
@Time :2021-02-01 21:30
"""
# !/usr/local/bin/python3
# -*- coding: utf-8 -*-
"""
@File :demo.py
@Author :keyin
@Time :2021-01-31 11:16
"""
import cv2
import os
from PyQt5.Qt import *
import numpy as np
import math
from PIL import Image
import matplotlib.pyplot as plt
from math import *
def mySize(img, size):
"""
:param imgshape: img.shape得到图片的宽和高
:param size: 在屏幕上最长边的尺寸
:return:
"""
y = img.shape[0]
x = img.shape[1]
multiples = 1
if y > size or x > size:
multiples_y = y // size
multiples_x = x // size
if multiples_x > multiples_y:
multiples = multiples_x + 1
else:
multiples = multiples_y + 1
y = y // multiples
x = x // multiples
img_show = cv2.resize(img, (x, y))
cv2.imshow('image', img_show)
return x, y
def minAr(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转化为灰度图
blur = cv2.GaussianBlur(gray, (3, 3), 0) # 用高斯滤波处理原图像降噪
blur = cv2.GaussianBlur(blur, (3, 3), 0) # 用高斯滤波处理原图像降噪
canny = cv2.Canny(blur, 20, 80) # 20是最小阈值,50是最大阈值 边缘检测
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
dilation = cv2.dilate(canny, kernel, iterations=1) # 膨胀一下,来连接边缘
dilation = cv2.dilate(dilation, kernel, iterations=1) # 膨胀一下,来连接边缘
# dilation = cv2.GaussianBlur(dilation, (3, 3), 0) # 用高斯滤波处理原图像降噪
im2, contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS) # 找边框
c_list = []
max_s = 0
cc = 0
for c in contours:
x, y, w, h = cv2.boundingRect(c)
c_list.append((w, h))
for i in range(len(c_list)):
s = c_list[i][0] + c_list[i][1]
if s > max_s:
max_s = s
cc = i
x, y, w, h = cv2.boundingRect(contours[cc])
minBox = cv2.rectangle(im2, (x, y), (x + w, y + h), (255, 255, 255), 5)
print("-----", x, y, w, h)
# rect = cv2.minAreaRect(c)
# box = cv2.boxPoints(rect)
# box = np.int0(box)
# minBox = cv2.drawContours(img, [box], 0, (0, 0, 255), 3)
# c = x
# d = x+w
# a = y
# b = y+h
#
# minBox = img[a:b, c:d]
return minBox
def findBorder(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转化为灰度图
blur = cv2.GaussianBlur(gray, (3, 3), 0) # 用高斯滤波处理原图像降噪
blur = cv2.GaussianBlur(blur, (3, 3), 0) # 用高斯滤波处理原图像降噪
canny = cv2.Canny(gray, 20, 80) # 20是最小阈值,50是最大阈值 边缘检测
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
dilation = cv2.dilate(canny, kernel, iterations=1) # 膨胀一下,来连接边缘
dilation = cv2.dilate(dilation, kernel, iterations=1) # 膨胀一下,来连接边缘
dilation = cv2.GaussianBlur(dilation, (3, 3), 0) # 用高斯滤波处理原图像降噪
im2, contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS) # 找边框
return contours
# return im2
def max_s(contours):
max_s = 0
box_max = object()
s_list = []
for c in range(len(contours)):
rect = cv2.minAreaRect(contours[c])
box = cv2.boxPoints(rect)
box = np.int0(box)
x = box[0][0]
y = box[0][1]
x1 = box[1][0]
y1 = box[1][1]
x = (x - x1) ** 2
y = (y - y1) ** 2
s = math.sqrt(x + y)
if s > max_s:
max_s = s
box_max = box
return box_max
def max_s2(contours):
s_list = []
for c in range(len(contours)):
rect = cv2.minAreaRect(contours[c])
box = cv2.boxPoints(rect)
box = np.int0(box)
x = box[0][0]
y = box[0][1]
x1 = box[1][0]
y1 = box[1][1]
x = (x - x1) ** 2
y = (y - y1) ** 2
s = math.sqrt(x + y)
s_list.append(s)
new_list = sorted(s_list)
# print('-------', new_list)
return new_list
def lineAngle(box_max):
new_box = caiqie(box_max)
x1 = new_box[0][0]
y1 = new_box[0][1]
x2 = new_box[1][0]
y2 = new_box[1][1]
k = (y2 - y1) / (x2 - x1)
# print(k)
a = math.atan(k)
b = math.degrees(a)
degree = b + 90
return degree
def min(img):
contours = findBorder(img)
box_max = max_s(contours)
for poit in box_max:
x = poit[0]
y = poit[1]
cv2.circle(img, (x, y), 10, (255, 0, 0), -1)
degree = lineAngle(box_max)
height, width = img.shape[:2]
minBox = cv2.drawContours(img, [box_max], 0, (0, 0, 255), 3)
# 旋转后的尺寸
heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))
matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)
matRotation[0, 2] += (widthNew - width) / 2 # 重点在这步,目前不懂为什么加这步
matRotation[1, 2] += (heightNew - height) / 2 # 重点在这步
imgRotation = cv2.warpAffine(minBox, matRotation, (widthNew, heightNew), borderValue=(0, 0, 0))
contours = findBorder(imgRotation)
box_max = max_s(contours)
# max_s2(contours)
# hull = cv2.convexHull(box_max)
# print(hull)
# length = len(hull)
# for i in range(len(hull)):
# cv2.line(img,tuple(hull[i][0]),tuple(hull[(i+1)%length][0]),(0,255,0),2)
# minBox = cv2.drawContours(minBox, [box_max], 0, (0, 255, 0), 3)
# print(minBox.shape)
return minBox
def caiqie(box):
contours = findBorder(img)
box_max = max_s(contours)
for poit in box_max:
x = poit[0]
y = poit[1]
cv2.circle(img, (x, y), 10, (255, 0, 0), -1)
def ROI_byMouse(img):
contours = findBorder(img)
box_max = max_s(contours)
# print(box_max)
for poit in box_max:
x = poit[0]
y = poit[1]
cv2.circle(img, (x, y), 10, (255, 0, 0), -1)
width, height = 856, 540
pts1 = np.float32(box_max)
print('pts1=',pts1)
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
# pts2 = pts1
print('pts2=',pts2)
matrix = cv2.getPerspectiveTransform(pts1, pts2)
imgOutPut = cv2.warpPerspective(img, matrix, (width, height))
wraped = imgOutPut
return imgOutPut
if __name__ == '__main__':
img_path = '5.jpg'
img = cv2.imread(img_path)
# minbox = min(img)
# minbox = findBorder(img)
minbox = ROI_byMouse(img)
mySize(minbox, 1000)
ROI_byMouse(img)
cv2.waitKey(0)
cv2.destroyAllWindows()
1
https://gitee.com/keyin/Stitching-ID-Card-master.git
git@gitee.com:keyin/Stitching-ID-Card-master.git
keyin
Stitching-ID-Card-master
Stitching-ID-Card-master
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891