79635

How can I find new coordinates of boundary box of rotated image to modify its xml file for Tensorflo

Question:

I'm trying to make more dataset to train my model in Tensorflow for data augmantaion. I added the labels of boundary boxes to original image. I want to rotate image 45 degree and modify the xml file for the new exact boundary box(rectangle) to label new created image. It's resizing and fetching to window to not to loose anything on image.

Let me show you how I try:

def rotateImage(mat, angle): height, width = mat.shape[:2] image_center = (width / 2, height / 2) rotation_mat = cv2.getRotationMatrix2D(image_center, angle, 1) radians = math.radians(angle) sin = math.sin(radians) cos = math.cos(radians) bound_w = int((height * abs(sin)) + (width * abs(cos))) bound_h = int((height * abs(cos)) + (width * abs(sin))) rotation_mat[0, 2] += ((bound_w / 2) - image_center[0]) rotation_mat[1, 2] += ((bound_h / 2) - image_center[1]) rotated_mat = cv2.warpAffine(mat, rotation_mat, (bound_w, bound_h)) return rotated_mat image = cv2.imread("test.jpg") angle = 45 rotated_45_image = image.copy() rotated_45_image = rotateImage(rotated_45_image, angle=45) tree_for_45_rotated = ET.parse(file_name + ".xml") root = tree_for_xml.getroot() for object in root.iter("object"): xmin = object.find("bndbox").find("xmin") ymin = object.find("bndbox").find("ymin") xmax = object.find("bndbox").find("xmax") ymax = object.find("bndbox").find("ymax") print(xmin.text, ymin.text, xmax.text, ymax.text) print("new") new_xmin = math.cos(angle) * int(xmin.text) - math.sin(angle) * int(ymin.text) new_xmax = math.cos(angle) * int(xmax.text) - math.sin(angle) * int(ymin.text) new_ymin = math.sin(angle) * int(xmin.text) + math.cos(angle) * int(ymin.text) new_ymax = math.sin(angle) * int(xmin.text) + math.cos(angle) * int(ymax.text) print(new_xmin, new_ymin, new_xmax, new_ymax)

After rotation the image is like this: <a href="https://i.stack.imgur.com/v7ZZZ.jpg" rel="nofollow"><img alt="Rotated 45" class="b-lazy" data-src="https://i.stack.imgur.com/v7ZZZ.jpg" data-original="https://i.stack.imgur.com/v7ZZZ.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

By the way, I'm using Python and OpenCV. I can't calculate the exact new coordinates to label the image.

Thanks

Answer1:

I cannot add a comment in the post above, so sorry for the post. All you need is print after rotation values from <em>corners</em>

img = cv2.imread("test.jpg") rotated, corners = rotateImage(img, 30) print(corners)

and if you want a specific value just use

print(corners[0]) print(corners[1]) print(corners[2]) print(corners[3])

Recommend

  • Java2D Image Rotation Issue
  • Rotate MKAnnotationPinView according to the device heading
  • Rotate image without cropping OpenCV
  • How does drawRect and CGGraphicsContext work?
  • How to resize UIViewControllerWrapperView
  • Why In iOS 9 and Xcode 7 not show full view height and width in iPhone 6 and above?
  • Getting Coordinates of a Point in a circle
  • How to setCenter mapview with location in google maps sdk for iOS
  • Unable to add the file iOS application bundle
  • XSLT 1.0: copy everything except certain nodes according to value and variable
  • Creating BackgroundSubtractorMOG2 in Android by OpenCV 3.0 RC1
  • What to do if “git push heroku master” failed?
  • How read between delimiters in php DOM of a XML file?
  • Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke
  • How to transform Byte[](decoded as PNG or JPG) to Tensorflows Tensor
  • Runtime error in UVA Online Judge [closed]
  • Load Same ACF Map Twice on Same Page
  • Heroku push rejected - Hartl's Rails 3.2 tutorial
  • Can't delete or rename original file after resizing
  • D3 get axis values on zoom event
  • Grails calculated field in SQL
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Alert pop up with LWUIT
  • Disabling Alt-F4 on a Win Forms NotifyIcon
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • How to add date and time under each post in guestbook in google app engine
  • How to set/get protobuf's extension field in Go?
  • Check if a string to interpolate provides expected placeholders
  • javascript inside java/jsp code
  • Sending data from AppleScript to FileMaker records
  • JSON with duplicate key names losing information when parsed
  • How to show dropdown in excel using jrxml (jasper api)?
  • Return words with double consecutive letters
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Load html files in TinyMce
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • python draw pie shapes with colour filled