简体   繁体   中英

How to find coordinate of a rotated bounding box in Python?

I have a rotated rectangular as

在此处输入图片说明

It contains the values 255 for rectangular and 0 for background. I want to find a bounding box coordinate (x_min,x_max, y_min,y_max) (such as red box as bellow). Could you suggest to me the way to find it in python? Thanks

在此处输入图片说明

This is the way how can I obtained the rotated bounding box

import numpy as np
import skimage.transform
import Image
img = np.zeros([2088,1773], dtype=np.uint8)
img[386:575, 816:1000] = 255
img =skimage.transform.rotate(img, -20, mode='edge')
img=img*255
img = Image.fromarray(img)
if img.mode != 'RGB':
    img = img.convert('RGB')
img.save("rotated_bb.jpg")

This should work:

import numpy as np
import skimage.transform
img = np.zeros([2088,1773], dtype=np.uint8)
img[386:575, 816:1000] = 255
img = skimage.transform.rotate(img, -20, mode='edge')

rect = np.where(img==1)
xmin, xmax = rect[1].min(), rect[1].max()
ymin, ymax = rect[0].min(), rect[0].max()

just need to adapt it to RGB format if you really need to...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM