0

first of all, sorry for my bad english, im a spanish speaker so Im gonna try my best to explain my problem.

I have a normalized ([0-255]) dicom image, and I want to display 8 images, but changing the bit depth, I mean like: 1bit image, 2bit image, 3bit image, etc. my code is just to open and normalize the image, because I have no clue on what should I do to get what I want, maybe transform all pixel values to binary but I don't really know that much, Im starting to learn digital image processing. this is my code

import numpy as np
import matplotlib.pyplot as plt
import pydicom as dicom


IM = r"C:\Users\Nico\Desktop\tarea2\MR-cerebro-ANON\IM.dcm"
data = dicom.read_file(IM)
imagen = data.pixel_array[200]
img_norm = []
for pixel in imagen:
    img_norm.append((pixel/np.amax(imagen)*255))
plt.imshow(img_norm,cmap='bone'),plt.axis('off')
plt.title('Imagen normalizada')
plt.show()

UPDATE:

I finally get what I was looking for, so Im gonna put my code here in case anyone wants to improve it because I know it might not be so optimized, but I'm a starter so this is what I did, if anyone wants to optimize it, feel free to do it. Also, maybe with this result my question is clearer now, and It works with all 8-bits just modifing the int function on the second "for".Those are some images that I get normalized Image, 1 bit image,6 bits image

bin1 = []
bin11 = []
for i in c:
    for j in i:
        bin1.append('00000000'+bin(j)[2::])
for b in bin1:   
    bin11.append(int(b[-1:],2))    
plt.imshow(np.reshape(bin22,(480,480)),cmap='gray')
plt.title('Imagen de 1 bits')
plt.show()
Nicolas
  • 1
  • 4
  • What is your question? What is the result you get right now when running this code? Why the 200? That's not a lot of pixels, and it's one-dimensional. Usually, code that processes an image will have two loops, one inside the other, since an image is a two-dimensional data set. Is 200 maybe the width of your image, and you need to process each line of the image? I'm just hoping to give you some things to think about that will help you. – CryptoFool Nov 10 '20 at 05:19
  • Hi, my question is how can I display all 8 bit depth images, I mean, for example: a 1 bit depth image should be black and white only, but for 2 or more bit, it should be on a gray scale, so I want to display 8 differents images, one per bit depth. I'm sorry if i can not explain myself very well, hope you can understand me. 200 is the slice # of the dicom image, so im getting a sagital slice image (the images are 1024x1024) I really apreciate that you are trying to help me, thank you so much. I have no clue on what should I do to get all 8 bit depth images – Nicolas Nov 10 '20 at 05:46
  • You have code here to normalize across 0-255. You would just do the same thing for each bit depth. For 1 bit, you pick a cutoff value (128, for example), and anything below that is a 0, and anything above is a 1. Same goes for the other depths...you just map each pixel across the number of bits you're using. Then there's the question of the image format. Does the file format support these different depths, or are you really just creating grayscale (0-255) images but ones where you've quantized the pixel values appropriately for each "depth". – CryptoFool Nov 10 '20 at 06:06
  • Thanks for the help, Steve. Im gonna try that idea, I think It might work because Im just creating gray scale images so I'll give it a try, thank you so much for your time – Nicolas Nov 10 '20 at 17:20
  • Although you updated your question with working code I would suggest answering your own question with the code your found. While it may not be optimized if it answers this question it will undoubtedly assist others that may come across this in the future. – DCCoder Nov 12 '20 at 05:01
  • Sure, no problem, Im gonna do that right now, thanks for the suggestion – Nicolas Nov 12 '20 at 05:10

1 Answers1

0

UPDATE (Answer):

I finally get what I was looking for, so Im gonna put my code here in case anyone wants to improve it because I know it might not be so optimized, but I'm a starter so this is what I did, if anyone wants to optimize it, feel free to do it. Also, maybe with this result my question is clearer now, and It works with all 8-bits just modifing the int function on the second "for".Here are some images that I got 1 bit image, 5 bits image, 6 bits image, 8 bits image

bin1 = []
bin11 = []
for i in c:
    for j in i:
        bin1.append('00000000'+bin(j)[2::])
for b in bin1:   
    bin11.append(int(b[-1:],2))    
plt.imshow(np.reshape(bin22,(480,480)),cmap='gray')
plt.title('Imagen de 1 bits')
plt.show()
Nicolas
  • 1
  • 4