欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python opencv實(shí)現(xiàn)圖片旋轉(zhuǎn)矩形分割

 更新時(shí)間:2018年07月26日 09:33:26   作者:Imcy  
這篇文章主要為大家詳細(xì)介紹了python opencv實(shí)現(xiàn)圖片旋轉(zhuǎn)矩形分割,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

有時(shí)候需要對有角度的矩形框內(nèi)圖像從原圖片中分割出來。這里的程序思想是,先將圖片進(jìn)行矩形角度的旋轉(zhuǎn),使有角度的矩形處于水平狀態(tài)后,根據(jù)原來坐標(biāo)分割圖片。
參考:python opencv實(shí)現(xiàn)旋轉(zhuǎn)矩形框裁減功能

修改原來的程序:

1.旋轉(zhuǎn)函數(shù)的輸入僅為矩形的四點(diǎn)坐標(biāo)
2.角度由公式計(jì)算出來
3.矩形四點(diǎn)pt1,pt2,pt3,pt4由txt文件讀入
4.在旋轉(zhuǎn)程序中還處理了順時(shí)針和逆時(shí)針及出現(xiàn)矩形框翻轉(zhuǎn)的問題。

代碼:

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

'''旋轉(zhuǎn)圖像并剪裁'''
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) # 矩形框旋轉(zhuǎn)角度
  print angle

  if pt4[1]>pt1[1]:
    print "順時(shí)針旋轉(zhuǎn)"
  else:
    print "逆時(shí)針旋轉(zhuǎn)"
    angle=-angle

  height = img.shape[0] # 原始圖像高度
  width = img.shape[1]  # 原始圖像寬度
  rotateMat = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1) # 按angle角度旋轉(zhuǎn)圖像
  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)

  # 旋轉(zhuǎn)后圖像的四點(diǎn)坐標(biāo)
  [[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]]))

  # 處理反轉(zhuǎn)的情況
  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) # 裁減得到的旋轉(zhuǎn)矩形框
  cv2.waitKey(0)
  return imgRotation # rotated image


?!「鶕?jù)四點(diǎn)畫原矩形
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)

# 讀出文件中的坐標(biāo)值
def ReadTxt(directory,imageName,last):
  fileTxt=directory+"http://rawLabel//"+imageName[:7]+last # txt文件名
  getTxt=open(fileTxt, 'r') # 打開txt文件
  lines = getTxt.readlines()
  length=len(lines)
  for i in range(0,length,4):
    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,re.split('\n| ',lines[i+3])[:2]))
    # float轉(zhuǎn)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 = "G://grasp//grapCode//trainImage//jpg//4"
  last = 'cneg.txt'
  imageName="pcd0247r.png"
  ReadTxt(directory,imageName,last)

原帶角度的矩形框:

旋轉(zhuǎn)矩形框:

分割:

這里寫圖片描述

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Python繪制3D立體愛心圖案的示例詳解

    基于Python繪制3D立體愛心圖案的示例詳解

    這篇文章主要為大家詳細(xì)介紹了利用Python實(shí)現(xiàn)繪制3D立體愛心圖案的四種不同方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下
    2022-11-11
  • python os.path模塊常用方法實(shí)例詳解

    python os.path模塊常用方法實(shí)例詳解

    os.path模塊主要用于文件的屬性獲取,在編程中經(jīng)常用到,以下是該模塊的幾種常用方法。感興趣的朋友跟隨小編一起看看吧
    2018-09-09
  • Python 將 CSV 分割成多個(gè)文件的示例代碼

    Python 將 CSV 分割成多個(gè)文件的示例代碼

    在本文中,我們討論了如何使用 Pandas 庫創(chuàng)建 CSV 文件, 此外,我們還討論了兩種常見的數(shù)據(jù)拆分技術(shù),行式數(shù)據(jù)拆分和列式數(shù)據(jù)拆分,需要的朋友可以參考下
    2023-06-06
  • 最實(shí)用的20個(gè)python小技巧

    最實(shí)用的20個(gè)python小技巧

    大家好,本篇文章主要講的是最實(shí)用的20個(gè)python小技巧,感興趣的同學(xué)快來看一看吧,希望對你有幫助
    2021-11-11
  • 通過Python爬蟲代理IP快速增加博客閱讀量

    通過Python爬蟲代理IP快速增加博客閱讀量

    本文主要對通過Python爬蟲代理IP快速增加博客閱讀量的方法進(jìn)行分析介紹。具有很好的參考價(jià)值,需要的朋友一起來看下吧
    2016-12-12
  • Python PyWebIO提升團(tuán)隊(duì)效率使用介紹

    Python PyWebIO提升團(tuán)隊(duì)效率使用介紹

    這篇文章主要為大家介紹了Python PyWebIO提升團(tuán)隊(duì)效率使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • 在PyCharm中找不到Conda創(chuàng)建的環(huán)境的解決方法

    在PyCharm中找不到Conda創(chuàng)建的環(huán)境的解決方法

    本文主要介紹了在PyCharm中找不到Conda創(chuàng)建的環(huán)境的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Python使用folium excel繪制point

    Python使用folium excel繪制point

    今天小編就為大家分享一篇關(guān)于Python使用folium excel繪制point,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能

    python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能

    手機(jī)上突然收到了某銀行的短信提示,看了一下手機(jī)的位數(shù),正好是11位,我一想,這不就是標(biāo)準(zhǔn)的手機(jī)號(hào)碼嗎?于是想用python的庫實(shí)現(xiàn)查詢手機(jī)號(hào)碼歸屬地查詢自由,所以本文給大家介紹了如何用python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能,需要的朋友可以參考下
    2024-03-03
  • 如何基于python實(shí)現(xiàn)畫不同品種的櫻花樹

    如何基于python實(shí)現(xiàn)畫不同品種的櫻花樹

    這篇文章主要介紹了如何基于python實(shí)現(xiàn)畫不同品種的櫻花樹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01

最新評論