Python: Image data is 90° rotated or inversed
Updated:Jul 19, 2022
Question
The output image data after RGB conversion appears 90° rotated or inversed.
Answer
Please check output data format before RGB conversion.
When entering data to adam_ai_run_net(): Planar format
When getting output data by numpy.reshape: Packed format
What is data format?
There are two formats used for AdamApp RGB data namely Planar format and Packed format.
Planar format is the data arranged respectively to each channel.
Packed format is the data where a set of the three, R, G and B data, are aligned.
AdamAppAPI_Specification_V1_08.pdf Excerpt from 6.8.10. ADAM_CV_RgbConvert
The RGB data converted by libAdamApiPython.adam_cv_yc2rgb
is Planar format.
Planar format must be used when entering RGB data to adam_ai_run_net
Convert RGB data to image data
numpy.reshape function is used to convert RGB data to image data.
The order is altered to n x m x l when executing numpy.reshape (l, m, n).
In addition, you can set up the order by setting order
in the fourth argument.
For example:
a = np.arange(24)
# a = [0,1,2・・・22,23]
print(a.reshape([2, 3, 4], order='C')) #C language style(Row major)(default)
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
print(a.reshape([2, 3, 4], order='F')) #FORTRAN(Column major)
# [[[ 0 6 12 18]
# [ 2 8 14 20]
# [ 4 10 16 22]]
#
# [[ 1 7 13 19]
# [ 3 9 15 21]
# [ 5 11 17 23]]]
Execute numpy.reshape() after changing RGB data from Planar to Packed format whenever converting RGB data to image data.
numpy.reshape from Packed format
numpy.reshape([height, width, 3], order='C')
The order will be adequate if you used Packed format when converting.
On the contrarily, the following occurs if you use Planar format when executing numpy.reshape ( resulting the image to appear 90° rotated or inversed).
numpy.reshape([width, height, 3], order='F')
Regardless of setting the order using order='F'
the image turns out to be 90° rotated or inversed if you used Planar format when converting.
Data format conversion for RGB data
Please use adam_cv_rgb_convert
when converting RGB data format.
Reference: