题目1:
写一个算法函数对下面场景中的矩形框进行合并。要求对矩形框的中心点水平或竖直距离<=200 pixel 的所有的矩形框进行合并(小矩形框合并成最大外接矩形框),一直合并到不能再合并为止。
解题思路:
核心思路:正负200的那个框记为大框,小矩形框合并的叫外接框,这2个框一一对应
伪代码:
循环读出每个框的坐标:
如果是第一个框,计算对应的大框和外接框
如果是后续的框:
判断当前框是否在某个大框里面
如果在:
更新当前大框对应的外接框
如果不在:
计算当前框对应的大框,加入大框列表,并且计算外接框,加入外接框列表
完成
'''
题目:
写一个算法函数对下面场景中的矩形框进行合并。要求对矩形框的中心点水平或竖直距离<=200 pixel
的所有的矩形框进行合并(小矩形框合并成最大外接矩形框),一直合并到不能再合并为止。
解题思路:
核心思路:正负200的那个框记为大框,小矩形框合并的叫外接框,这2个框一一对应
伪代码:
循环读出每个框的坐标:
如果是第一个框,计算对应的大框和外接框
如果是后续的框:
判断当前框是否在某个大框里面
如果在:
更新当前大框对应的外接框
如果不在:
计算当前框对应的大框,加入大框列表,并且计算外接框,加入外接框列表
完成
'''
from __future__ import print_function
import json
import pprint
class Read():
def read_json(self):
# 读出所有的框的位置
return json.load(open('./P2/out.json','r',encoding="utf-8"))['shapes']
read = Read()
# print(read)
# pprint.pprint(read.read_json())
boxes = read.read_json()
# print(boxes)
# 判断一个矩形框是否包含在大矩形框中
def in_box(R1, R2):
if (R1[0][0]<=R2[0][0]) and (R1[0][1]>=R2[0][1]) and (R1[1][0]>=R2[1][0]) and (R1[1][1]<=R2[1][1]):
return True
else:
return False
# 根据2个矩形框输出外接矩形框的位置
def out_locaation(R1, R2):
return [[min(R1[0][0], R2[0][0]), max(R1[0][1], R2[0][1])],\
[max(R1[1][0], R2[1][0]), min(R1[1][1], R2[1][1])]]
flag = 1
# 定义大框和外接矩形框,这2个是一一对应的关系
big_boxes = []
out_boxes = [[]]
for box in boxes:
# print(box)
box_location = box['points']
# print(box_location)
# 取出其中的点的位置
x1, y1, x2, y2 = box_location[0][0], box_location[0][1], box_location[1][0],box_location[1][1]
if flag ==1: # 第一个框框
# 计算中心点
# print(box_location)
x_center, y_center = (x1+x2)/2, (y1+y2)/2
# print(x_center, y_center)
# 定义大矩形框,判断包含关系
big_boxes.append([[x_center-200, y_center+200],[x_center+200, y_center-200]])
# print(big_boxes)
# 定义第一个最大外接矩形框的位置
out_boxes[0] = box_location
flag = 0
continue
# 判断当前矩形框是否已经在大矩形框中了, 是就跳过, 不是就以此求新的大框
# 给出这个范围内的上下限, 判断其他矩形框的x、y的值是否在此范围内。
# 如果当前在大框内, 则更新外接矩形框, 完事进下一个
big_falg = 1
for i in range(len(big_boxes)):
if in_box(big_boxes[i], box_location): # 返回true表示包含关系
print("#####")
# 包含的话则更新对应外接矩形框的位置
# 外接矩形框由2个矩形框的左上和右下2个点来决定
print(box_location)
print(big_boxes[i])
print(out_boxes[i])
out_box = out_locaation(out_boxes[i], box_location)
print(out_box)
# 更新对应的外接框
out_boxes[i] = out_box
# print("@@@@")
big_falg = 0
# 因为只可能在一个大框内,所以break
break
# 如果不在, 则自己另外成一个大框
if big_falg:
print("$$$")
print(box_location)
x_center, y_center = (x1+x2)/2, (y1+y2)/2
# print(x_center, y_center)
# 定义大矩形框,判断包含关系
big_boxes.append([[x_center-200, y_center+200],[x_center+200, y_center-200]])
print(big_boxes)
# 不在的话以当前box为基础,将box添加进去 out_boxes 作为一个基础外接框
out_boxes.append(box_location)
# break
print("\n Result:\n")
# print(out_boxes)
for i in out_boxes:
print(i)
'''
通过图像拼接可以实现相机起始位置相同
https://www.freesion.com/article/7908226038/
两张图像中包含相同的部分(文字部分),可以实现拼接。
实现图像拼接有以下几步:
对每幅图进行特征点提取
对对特征点进行匹配
进行图像配准
把图像拷贝到另一幅图像的特定位置
对重叠边界进行特殊处理
'''
'''
https://www.freesion.com/article/7908226038/
通过图像拼接可以实现相机起始位置相同
两张图像中包含相同的部分(文字部分),可以实现拼接。
实现图像拼接有以下几步:
对每幅图进行特征点提取
对对特征点进行匹配
进行图像配准
把图像拷贝到另一幅图像的特定位置
对重叠边界进行特殊处理
'''
import cv2 as cv
import numpy as np
import sys
#读取两张图片
src = "./P1/Image_20220402111112545.jpg" # 以第三张图为原图,标准图,其他图按照这个来统一
img1 = "./P1/Image_20220402111106521.jpg"
img2 = "./P1/Image_20220402111109617.jpg"
img4 = "./P1/Image_20220402111117760.jpg"
img5 = "./P1/Image_20220402111120865.jpg"
img6 = "./P1/Image_20220402111123408.jpg"
img7 = "./P1/Image_20220402111136360.jpg"
img8 = "./P1/Image_20220402111151088.jpg"
img_list = [img1, img2, img4, img5, img6, img7, img8]
# 检查路径
for img in img_list:
if img is None:
print("路径问题")
sys.exit(0)
GOOD_POINTS_LIMITED = 0.99
orb = cv.ORB_create()
bf = cv.BFMatcher.create()
img1_3 = cv.imread(src,1)# 基准图像
kp1, des1 = orb.detectAndCompute(img1_3,None)
for dst in img_list:
img2_3 = cv.imread(dst,1)# 拼接图像
kp2, des2 = orb.detectAndCompute(img2_3,None)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)
goodPoints =[]
for i in range(len(matches)-1):
if matches[i].distance < GOOD_POINTS_LIMITED * matches[i+1].distance:
goodPoints.append(matches[i])
# goodPoints = matches[:20] if len(matches) > 20 else matches[:]
# print(goodPoints)
img3 = cv.drawMatches(img1_3,kp1,img2_3,kp2,goodPoints, flags=2,outImg=None )
src_pts = np.float32([kp1[m.queryIdx].pt for m in goodPoints]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in goodPoints]).reshape(-1, 1, 2)
M, mask = cv.findHomography( dst_pts,src_pts, cv.RHO)
# 获取原图像的高和宽
h1,w1,p1 = img2_3.shape
h2,w2,p2 = img1_3.shape
h = np.maximum(h1,h2)
w = np.maximum(w1,w2)
_movedis = int(np.maximum(dst_pts[0][0][0],src_pts[0][0][0]))
imageTransform = cv.warpPerspective(img2_3,M,(w1+w2-_movedis,h))
M1 = np.float32([[1, 0, 0], [0, 1, 0]])
h_1,w_1,p = img1_3.shape
dst1 = cv.warpAffine(img1_3,M1,(w1+w2-_movedis, h))
# dst_ = cv.add(dst1,imageTransform)
# dst_no = np.copy(dst)
dst_target = np.maximum(dst1,imageTransform)
# print(dst_target.shape)
dst_target = dst_target[:, :2048, :]
# print(dst_target.shape)
file = str(dst).split('/')[-1]
print(file)
cv.imwrite('./result_P1/'+ file + '.jpg', dst_target)