yuxiaomu1124 2019-06-03 09:20 采纳率: 0%
浏览 564

旋转矩形切割,有四个点坐标

想根据矩形的四个点坐标切割矩形

# -*- coding:utf-8 -*-
import cv2
from math import *
import numpy as np
import time,math
import os
import re

'''旋转图像并剪裁'''
def rotate(img,  pt1, pt2, pt3, pt4):
    print (pt1,pt2,pt3,pt4)
    withRect = math.sqrt((pt4[0] - pt1[0]) ** 2 + (pt4[1] - pt1[1]) ** 2)  # 矩形框的宽度
    heightRect = math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) **2)
    print (withRect,heightRect)
    angle = acos((pt4[0] - pt1[0]) / withRect) * (180 / math.pi)  # 矩形框旋转角度
    print (angle)

    if pt4[1]>pt1[1]:
        print ("顺时针旋转")
    else:
        print ("逆时针旋转")
        angle=-angle

    height = img.shape[0]  # 原始图像高度
    width = img.shape[1]   # 原始图像宽度
    rotateMat = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1)  # 按angle角度旋转图像
    heightNew = int(width * fabs(sin(radians(angle))) + height * fabs(cos(radians(angle))))
    widthNew = int(height * fabs(sin(radians(angle))) + width * fabs(cos(radians(angle))))

    rotateMat[0, 2] += (widthNew - width) / 2
    rotateMat[1, 2] += (heightNew - height) / 2
    imgRotation = cv2.warpAffine(img, rotateMat, (widthNew, heightNew), borderValue=(255, 255, 255))
    cv2.imshow('rotateImg2',  imgRotation)
    cv2.waitKey(0)

    # 旋转后图像的四点坐标
    [[pt1[0]], [pt1[1]]] = np.dot(rotateMat, np.array([[pt1[0]], [pt1[1]], [1]]))
    [[pt3[0]], [pt3[1]]] = np.dot(rotateMat, np.array([[pt3[0]], [pt3[1]], [1]]))
    [[pt2[0]], [pt2[1]]] = np.dot(rotateMat, np.array([[pt2[0]], [pt2[1]], [1]]))
    [[pt4[0]], [pt4[1]]] = np.dot(rotateMat, np.array([[pt4[0]], [pt4[1]], [1]]))

    # 处理反转的情况
    if pt2[1]>pt4[1]:
        pt2[1],pt4[1]=pt4[1],pt2[1]
    if pt1[0]>pt3[0]:
        pt1[0],pt3[0]=pt3[0],pt1[0]

    imgOut = imgRotation[int(pt2[1]):int(pt4[1]), int(pt1[0]):int(pt3[0])]
    cv2.imshow("imgOut", imgOut)  # 裁减得到的旋转矩形框
    cv2.waitKey(0)
    return imgRotation  # rotated image


#根据四点画原矩形
def drawRect(img,pt1,pt2,pt3,pt4,color,lineWidth):
    cv2.line(img, pt1, pt2, color, lineWidth)
    cv2.line(img, pt2, pt3, color, lineWidth)
    cv2.line(img, pt3, pt4, color, lineWidth)
    cv2.line(img, pt1, pt4, color, lineWidth)

# 读出文件中的坐标值
def ReadTxt(directory,imageName,last):
    fileTxt=directory+imageName[:4]+last  # txt文件名
    print(fileTxt)
    getTxt=open(fileTxt, 'r')  # 打开txt文件
    lines = getTxt.read()
    print(lines)
    length=len(lines)
    #print(length)
    for i in range(0,length,5):
        pt2=list(map(float,lines[i].split(' ')[:2]))
        pt1=list(map(float,lines[i+1].split(' ')[:2]))
        pt4=list(map(float,lines[i+2].split(' ')[:2]))
        pt3 = list(map(float,lines[i+3].split(' ')[:2]))
       # pt5 = list(map(float, lines[i + 3].split(' ')[:2]))
        #pt3=list(map(float,re.split(r'([;])',lines[i+3])[:2]))
        # float转int

        pt2=list(map(int,pt2))
        pt1=list(map(int,pt1))
        pt4=list(map(int,pt4))
        pt3=list(map(int,pt3))

        imgSrc = cv2.imread(imageName)
        drawRect(imgSrc, tuple(pt1),tuple(pt2),tuple(pt3),tuple(pt4), (0, 0, 255), 2)
        cv2.imshow("img", imgSrc)
        cv2.waitKey(0)
        rotate(imgSrc,pt1,pt2,pt3,pt4)


if __name__=="__main__":
    directory = "E:/yyk/output1/"
    last = '.txt'
    imageName="6671.jpg"
    ReadTxt(directory,imageName,last)

报错如下,C:\Users\yyk\Anaconda3\python.exe D:/untitled1/selflearning/1.py
E:/yyk/output1/6671.txt
Traceback (most recent call last):
426,337
786,299
File "D:/untitled1/selflearning/1.py", line 95, in
790,342
430,379
ReadTxt(directory,imageName,last)

File "D:/untitled1/selflearning/1.py", line 74, in ReadTxt
pt3 = list(map(float,lines[i+3].split(' ')[:2]))
ValueError: could not convert string to float: ','

Process finished with exit code 1
请问我该怎么改

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-08 21:59
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献