Home > Software design >  skimage regionprops orientation producing confusing results
skimage regionprops orientation producing confusing results

Time:02-20

I am trying to understand the orientation output from skimage.measure.regionprops_table but I am a bit confused. Can anyone help me a bit in understanding the output of the orientation?

According to documentation, orientation returns angle between the 0th axis (rows) and the major axis of the ellipse that has the same second moments as the region, ranging from -pi/2 to pi/2 counter-clockwise.

In the below code example, I created a rectangle along the x-axis so the major axis is along the x-axis. I was hoping to get 0 degree orientation angle but I get 90 degrees. For a 90 degree rotated rectangle, it returns 0 degree orientation angle. I am bit confused why is this the case. Shouldn't it be the other way around? For a rectangle placed at 30 degrees, orientation returns -60 degrees.

What do they mean with the 0th axis (rows)? May be someone can guide me according to the below example.

Here is a simple code that I tried:

from skimage.transform import rotate
import cv2
import numpy as np
from matplotlib import pyplot as plt
from skimage import measure
import pandas as pd
import math
from skimage.util import img_as_ubyte

testimg = np.zeros([100,150],dtype=np.uint8)
cv2.rectangle(testimg, (30, 40), (70, 50), (255,0,0), -1)
#testimg = img_as_ubyte(rotate(testimg, angle=90, order=0))
ret1, thresh = cv2.threshold(testimg, 0, 255, cv2.THRESH_BINARY cv2.THRESH_OTSU)
plt.imshow(thresh, cmap='gray')

ret3, markers = cv2.connectedComponents(thresh)
plt.imshow(markers)
testprops = measure.regionprops_table(markers, properties=['label', 'area', 'centroid', 'major_axis_length', 'minor_axis_length', 'orientation']) # v18.x
testdata = pd.DataFrame(testprops)
testdata['angle_degree'] = math.degrees(testdata['orientation'])

CodePudding user response:

In scikit-image, in 2D, the "0th" axis is the rows, which go from 0 at the top left corner to n at the bottom left corner. You can read more about this in the "coordinate conventions" page of the documentation:

https://scikit-image.org/docs/0.19.x/user_guide/numpy_images.html#coordinate-conventions

This is done to match the coordinates with the indexing of NumPy arrays. This means that indeed, orientation of 0 corresponds to the vertical, and 90 to the horizontal.

  • Related