Polar coordinates
Polar coordinates are coordinates made up of poles, axes, and diameters ina plane. A fixed point O on the plane is called a pole. A ray Ox, called the polar axis, is introduced from O. Take another unit length, usually specifying that the Angle is positive anticlockwise. In this way, the position of any point P on the plane can be determined by the length ρ of line segment OP and the Angle θ from Ox to OP. The pair of ordered numbers (ρ, θ) is called the polar coordinates of point P, denoted as P (ρ, θ); ρ is called the polar diameter of P and θ is called the polar Angle of P.
Transformation relation between polar coordinates and Cartesian coordinates:
X = rho cosine theta
Y = rho sine theta
Conversely, ρ and θ can be calculated using x and y
API
public static void warpPolar(Mat src, Mat dst, Size dsize, Point center, double maxRadius, int flags)
Copy the code
-
Parameter 1: SRC, the original image, can be gray image or color image.
-
Parameter 2: DST. The output image after polar coordinate transformation has the same data type and channel number as the original image.
-
Parameter 3: Dsize, the target image size.
-
Parameter 4: Center, the origin coordinate of polar coordinates in polar coordinate transformation.
-
Parameter 5: maxRadius, the radius of the boundary circle during transformation, which also determines the proportional parameter during inverse transformation.
-
Parameters: six flags, interpolation method and the polar coordinates sign mapping method, interpolation method and the polar coordinate mapping method is as follows, between the two methods through the “+” or “|” connection.
// C++: enum InterpolationFlags
public static final int
INTER_NEAREST = 0.
INTER_LINEAR = 1.
INTER_CUBIC = 2.
INTER_AREA = 3.
INTER_LANCZOS4 = 4.
INTER_LINEAR_EXACT = 5.
INTER_MAX = 7.
WARP_FILL_OUTLIERS = 8.
WARP_INVERSE_MAP = 16;/ / inverse transformation
Copy the code// C++: enum WarpPolarMode
public static final int
WARP_POLAR_LINEAR = 0.// Polar transformation
WARP_POLAR_LOG = 256;// Semilog polar transformation
Copy the code
operation
class PolarActivity : AppCompatActivity() {
private lateinit var mBinding: ActivityPolarBinding
private lateinit var mRgb: Mat
private lateinit var mPolar: Mat
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_polar)
val bgr = Utils.loadResource(this, R.drawable.circle)
mRgb = Mat()
Imgproc.cvtColor(bgr, mRgb, Imgproc.COLOR_BGR2RGB)
showMat(mBinding.ivCircle, mRgb)
bgr.release()
}
override fun onCreateOptionsMenu(menu: Menu?).: Boolean {
menuInflater.inflate(R.menu.menu_polar, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.polar -> {
polarTransform(mRgb)
}
R.id.reverse_polar -> {
if (!this::mPolar.isInitialized) {
Toast.makeText(this."Please do the polar transformation first.", Toast.LENGTH_SHORT).show()
} else {
reversePolarTransform(mPolar)
}
}
}
return true
}
private fun polarTransform(source: Mat) {
val center = Point(source.width() / 2.0, source.height() / 2.0)
mPolar = Mat()
val size = mRgb.size()
Imgproc.warpPolar(
source,
mPolar,
size,
center,
center.x,
Imgproc.INTER_LINEAR + Imgproc.WARP_POLAR_LINEAR
)
showMat(mBinding.ivResult, mPolar)
}
private fun reversePolarTransform(source: Mat) {
val center = Point(source.width() / 2.0, source.height() / 2.0)
val dst = Mat()
val size = mRgb.size()
Imgproc.warpPolar(
source,
dst,
size,
center,
center.x,
Imgproc.INTER_LINEAR + Imgproc.WARP_INVERSE_MAP
)
showMat(mBinding.ivResult, dst)
}
private fun showMat(view: ImageView, source: Mat) {
val bitmap = Bitmap.createBitmap(source.width(), source.height(), Bitmap.Config.ARGB_8888)
Utils.matToBitmap(source, bitmap)
view.setImageBitmap(bitmap)
}
override fun onDestroy(a) {
mRgb.release()
mPolar.release()
super.onDestroy()
}
}
Copy the code
The effect
The source code
Github.com/onlyloveyd/…
This article is formatted using MDNICE