0

I have a 3D array. I want to change some pixel color of this 3D image. I want to change the pixel color for a given condition that is if my mask pixel for a fixed 3D co-ordinate is not equal 0 then I want to change my 3D array pixel to red color. Finally, I want to save this into a nifty format or .nii.gz format. I write some code this way, but this code gives me an error

  x, y, z=volume.shape
  for i in range (0, x):
     for j in range (0, y):
        for k in range (0, z):
           if(detections[i,j,k] !=0 ):
                volume[i ,j, k]=(255, 0, 0)
   ni_img = nib.Nifti1Image(output, volume.affine)
   nib.save(ni_img, '/content/SpineFinder-master/3d_path/output.nii.gz')



ValueError: setting an array element with a sequence volume[i, j, k]=(255, 0, 0)
Md. Abu Farhad
  • 373
  • 5
  • 21

2 Answers2

0

The reason you are getting an error is because you can't 'just' write RGB values to nifti files. Please look up the question attached: How to write a color 3D NIfTI with NiBabel?

code-lukas
  • 1,586
  • 9
  • 19
-1

I'd start with the missing comma (between i & j) in

volume[i j, k]=(255, 0, 0)
Orius
  • 1,093
  • 5
  • 11