# Coordinate conversion

**URL:** <https://discourse.itk.org/t/coordinate-conversion/6393>\
**Category:** Beginner Questions\
**Tags:** itk\
**Created:** [January 15, 2024, 11:05am UTC](https://discourse.itk.org/t/coordinate-conversion/6393 "2024-01-15T11:05:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellen-D216](https://discourse.itk.org/user_avatar/discourse.itk.org/ellen-d216/32/3083_2.png) [@Ellen-D216](https://discourse.itk.org/u/Ellen-D216)\
**Post date:** [January 15, 2024, 11:05am UTC](https://discourse.itk.org/t/coordinate-conversion/6393/1 "2024-01-15T11:05:22Z")

</div>

I want to convert the image to a polar coordinate system, process it, and then convert it back to the Cartesian coordinate system. How can I do it with itkCartesianToPolarTransform and itkPolarToCartesianTransform? With ResampleImageFilter? But there is a bug when I call SetTransform().

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [January 15, 2024, 2:39pm UTC](https://discourse.itk.org/t/coordinate-conversion/6393/2 "2024-01-15T14:39:29Z")

</div>

You approach sounds plausible. What is that bug?

Also take a look at:

> <https://github.com/KitwareMedical/SlicerITKUltrasound/blob/c7b719253d31b896b1f6970ddc87868905173f52/ScanConvertCurvilinearArray/ScanConvertCurvilinearArray.cxx#L39-L98>

---

<div class="post-metadata">

**Author:** ![Ellen-D216](https://discourse.itk.org/user_avatar/discourse.itk.org/ellen-d216/32/3083_2.png) [@Ellen-D216](https://discourse.itk.org/u/Ellen-D216)\
**Post date:** [January 16, 2024, 3:50am UTC](https://discourse.itk.org/t/coordinate-conversion/6393/3 "2024-01-16T03:50:39Z")

</div>

It seems like useful. I just want to convert a CT image from Cartesian coordinate system to polar coordinate system, and apply some filters.

---

<div class="post-metadata">

**Author:** ![Ellen-D216](https://discourse.itk.org/user_avatar/discourse.itk.org/ellen-d216/32/3083_2.png) [@Ellen-D216](https://discourse.itk.org/u/Ellen-D216)\
**Post date:** [January 22, 2024, 9:53am UTC](https://discourse.itk.org/t/coordinate-conversion/6393/4 "2024-01-22T09:53:35Z")

</div>

Can I use itk::CartesianToPolarTransform? It is simple to use.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [January 22, 2024, 2:00pm UTC](https://discourse.itk.org/t/coordinate-conversion/6393/5 "2024-01-22T14:00:15Z")

</div>

You should be able to do that. What problems did you encounter?

---

<div class="post-metadata">

**Author:** ![Ellen-D216](https://discourse.itk.org/user_avatar/discourse.itk.org/ellen-d216/32/3083_2.png) [@Ellen-D216](https://discourse.itk.org/u/Ellen-D216)\
**Post date:** [January 23, 2024, 3:26am UTC](https://discourse.itk.org/t/coordinate-conversion/6393/7 "2024-01-23T03:26:43Z")

</div>

I used `ResampleImageFilter`.

```auto
constexpr unsigned int Dimension = 3;

  using PixelType = float; using ScalarType = double;
  using ImageType = itk::Image<PixelType, Dimension>;
  using IndexValueType = typename itk::Index<Dimension>::IndexValueType;

  const typename ImageType::Pointer inputImage = cv::ReadMhdImage<float, 3>("recon.mhd");
  const typename ImageType::RegionType inputRegion = inputImage->GetLargestPossibleRegion();
  const typename ImageType::SizeType inputSize = inputRegion.GetSize();
  const typename ImageType::SpacingType inputSpacing = inputImage->GetSpacing();
  const typename ImageType::PointType inputOrigin = inputImage->GetOrigin();

  typename ImageType::SizeType outputSize;
  outputSize[0] = 720; outputSize[1] = 720; outputSize[2] = inputSize[2];
  typename ImageType::SpacingType outputSpacing = inputSpacing;
  typename ImageType::PointType outputOrigin = inputOrigin;

  using PolarTransformType = itk::CartesianToPolarTransform<ScalarType, Dimension>;
  auto polarTransform = PolarTransformType::New();

  itk::Point<ScalarType, Dimension> center;
  itk::ContinuousIndex<ScalarType, Dimension> centerSize;
  centerSize[0] = inputSize[0] / 2;
  centerSize[1] = inputSize[1] / 2;
  centerSize[2] = inputSize[2] / 2;

  center = inputImage->TransformContinuousIndexToPhysicalPoint<ScalarType, ScalarType>(centerSize);
  polarTransform->SetCenter(center);

  using LinearInterpolatorType = itk::LinearInterpolateImageFunction<ImageType, ScalarType>;
  auto interpolator = LinearInterpolatorType::New();

  using ResampleFilterType = itk::ResampleImageFilter<ImageType, ImageType>;
  auto resampleFilter = ResampleFilterType::New();

  resampleFilter->SetInput(inputImage);
  resampleFilter->SetTransform(polarTransform);
  resampleFilter->SetInterpolator(interpolator);
  resampleFilter->SetSize(outputSize);
  resampleFilter->SetOutputSpacing(outputSpacing);
  resampleFilter->SetOutputOrigin(outputOrigin);
  
  auto writer = itk::ImageFileWriter<ImageType>::New();
  writer->SetFileName("polar.mhd");
  writer->SetImageIO(itk::MetaImageIO::New());
  writer->SetInput(resampleFilter->GetOutput());
  writer->Update();

```

Resampled image and raw image look like this.

 ![1705980101744](https://discourse.itk.org/uploads/default/original/2X/b/b51abf40c96df3b25f651c7e7c6641c34876299b.png) ![1705980122047](https://discourse.itk.org/uploads/default/original/2X/c/ca74639df0ed6f9d6e2f8eac7b23b5c0e25a5431.png)  
The results are not satisfactory

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [January 23, 2024, 1:45pm UTC](https://discourse.itk.org/t/coordinate-conversion/6393/8 "2024-01-23T13:45:21Z")

</div>

You spherical object is not at the center of your image. If you want that, then you need to determine the center somehow. One option is manually picking the center. Another option is using [Hough circle transform](https://itk.org/Doxygen/html/classitk_1_1HoughTransform2DCirclesImageFilter.html).

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [January 23, 2024, 3:45pm UTC](https://discourse.itk.org/t/coordinate-conversion/6393/9 "2024-01-23T15:45:53Z")

</div>

If the goal is to remove the ring artifacts, [this filter may be helpful](https://github.com/InsightSoftwareConsortium/ITKRingArtifact).
