# Difference between opencv images and 3D slicer 

**URL:** <https://discourse.slicer.org/t/difference-between-opencv-images-and-3d-slicer/32671>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [November 8, 2023, 2:00pm UTC](https://discourse.slicer.org/t/difference-between-opencv-images-and-3d-slicer/32671 "2023-11-08T14:00:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saman\_Fouladi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/saman_fouladi/32/68212_2.png) [@Saman\_Fouladi](https://discourse.slicer.org/u/Saman_Fouladi)\
**Post date:** [November 8, 2023, 2:00pm UTC](https://discourse.slicer.org/t/difference-between-opencv-images-and-3d-slicer/32671/1 "2023-11-08T14:00:59Z")

</div>

Hello. I trained my 2D unet with images which were loaded using opencv in png format. The predicted results are promising for organ segmentation. To work with 3D nrrd image (to show in 3D Slicer), I convert a 3D image to 2D slides and then load them with SimpleITK.ReadImage (). After that I use from prediction model for organ segmentation . In this case the results are awful. Are loaded images using opencv very different in contrast with SimpleITK ? How can I deal with this problem?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 8, 2023, 2:42pm UTC](https://discourse.slicer.org/t/difference-between-opencv-images-and-3d-slicer/32671/2 "2023-11-08T14:42:40Z")

</div>

SimpleITK and OpenCV might use slightly different conventions (for example order of axes, or how you represent origin, spacing, and axis directions), but they can work on exactly the same images.

Note that OpenCV is developed by the computer vision community and you will find that their needs, assumptions, preferences, and conventions are quite different from the medical image computing community. It is easier to use libraries developed by the medical image computing community, such as ITK, VTK, MONAI, because they are designed to work well together and often used together.

> [@Saman\_Fouladi](#):
>
> To work with 3D nrrd image (to show in 3D Slicer), I convert a 3D image to 2D slides and then load them with SimpleITK.ReadImage ()

You do not need to split a 3D image to 2D slices to write to file. It is an unnecessary step that may slow down the processing and/or introduce errors. Instead, you can write the 3D image directly into a NRRD file using pynrrd; or if you are already using ITKPython or SimpleITK then you can convert your numpy array to an ITK image and use the ITK image writer.

---

<div class="post-metadata">

**Author:** ![Saman\_Fouladi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/saman_fouladi/32/68212_2.png) [@Saman\_Fouladi](https://discourse.slicer.org/u/Saman_Fouladi)\
**Post date:** [November 8, 2023, 5:52pm UTC](https://discourse.slicer.org/t/difference-between-opencv-images-and-3d-slicer/32671/3 "2023-11-08T17:52:14Z")

</div>

Thank you very much for your valuable information. I trained my network with opencv images, because I had to work on 2D images. So should I train my network again with SimpleITK.ReadImage () for 2D images?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 9, 2023, 2:55am UTC](https://discourse.slicer.org/t/difference-between-opencv-images-and-3d-slicer/32671/4 "2023-11-09T02:55:56Z")

</div>

What kind of images do you work with? Fluoroscopy, ultrasound, optical, …? Single image or a time sequence?

---

<div class="post-metadata">

**Author:** ![Saman\_Fouladi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/saman_fouladi/32/68212_2.png) [@Saman\_Fouladi](https://discourse.slicer.org/u/Saman_Fouladi)\
**Post date:** [November 9, 2023, 8:40am UTC](https://discourse.slicer.org/t/difference-between-opencv-images-and-3d-slicer/32671/5 "2023-11-09T08:40:19Z")

</div>

I am working on MRIs. I work on T2W images slide by slide.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 15, 2023, 12:49pm UTC](https://discourse.slicer.org/t/difference-between-opencv-images-and-3d-slicer/32671/6 "2023-11-15T12:49:49Z")

</div>

If you work with 3D medical images then it makes sense to use libraries that are developed fir this purpose, use ITKPython, SimpleITK, or Slicer readers/writers, work with 3D image objects and do the segmentation in 3D.

If you still want to do slice-by-slice segmentation in OpenCV (despite you can expect low-quality results from independently segmenting slices) then you can get the voxels of the input scalar volume and the output label volume as 3D numpy arrays, extract a slice, process it in OpenCV, and copy the processed voxels into a slice of the label volume array.
