Extract sick label as a Numpy Array? - entropy

I have a single sitk label that applies to 4 different image volumes. I know that simple statistics on the label could be done through sitk. However, I need to calculate entropy and kurtosis within the label at each image volume.
Is there a way to extract the label data from each different image into a Numpy Array and then I can do this analysis?

Entropy and kurtosis for labels over an intensity image are already available in SimpleITK in the LabelIntensityStatisticsImageFilter
You would need to convert that label image and the intensity images into numpy arrays, then do masking in numpy to get the label's intensity. You should be able to use GetArrayViewFromImage to avoid data duplication.

Related

Image noise removal after hsv thresholding

I am currently trying to remove the noise on this image.
This image is obtained using cv2 hsv thresholding. Unfortunately there are a lot of random pixels and pieces that need to be filtered out. I've already tried open cv's fastNlMeansDenoisingColored function, this did not work. Is there anything else I could try?
You can try multiple things, I would try them in this order:
Blur the image before calculating the threshold
Change the threshold value
Erode&Dilate the image before calculating the threshold
Erode&Dilate afterwards is not so good:
Or going all in: use connectedComponentsWithStats and remove all components with a small area.

Extracting pixel values using Pillow - every coordinate I use gives the same value

I am attempting to use the Pillow library in Python 2.7 to extract pixel values at given coordinates on PNG and JPG images. I don't get an errors but I always get the same value irrespective of the coordinates I have used on an image where the values do vary.
This is an extract from my script where I print all the values (its a small test image):
from PIL import Image
box = Image.open("col_grad.jpg")
pixels = list(box.getdata())
print(pixels)
And from when I try to extract a single value:
from PIL import Image, ImageFilter
box = Image.open("col_grad.jpg")
value = box.load()
print(value[10,10])
I have been using these previous questions on this topic for guidance including:
Getting list of pixel values from PIL
How can I read the RGB value of a given pixel in Python?
Thanks for any help with this,
Alex
I'm not sure if you can access it the way you want, because of the complexity of images data.
Just get the pixel:
Image.getpixel(xy)
Returns the pixel value at a given position.
Parameters: xy – The coordinate, given as (x, y).
Returns: The pixel value. If the image is a multi-layer image, this method returns a tuple.:
You should consider doing one of the following:
FIRST convert your image to grayscale and then find the pixel values present.
img_grey= img.convert('1') # convert image to black and white
SECOND If you want the pixel values of RGB channels, you have to split your color image. Then find the pixel values in all the channels at a particular coordinate.
Image.split(img)

Get multiple image files as 2D tensor in tensorflow

For a softmax regression program using tensorflow in python, I want to get my 1000 jpeg image files as a 2D tensor x of: [image index, pixel index]. The "image index" is the image and the pixel index is a specific image pixel for that image.
The model equation is:
y = tf.nn.softmax(tf.matmul(x, W) + b)
where:
x = tf.placeholder(tf.float32, [None, image_size])
W = tf.Variable(tf.zeros([image_size, classes]))
b = tf.Variable(tf.zeros([classes]))
image size = height*width of image (constant for all images).
What is the best way in tensorflow to get my image files in that form?
When I do image processing I like to use either OpenCV (cv2.imread(...)) or Scipy (scipy.ndimage.imread(...)) to read the image files. I also think tensorflow might have its own image reader you can use. These two function return the image as a numpy array. You can specify in the arguments if you want grayscale or color. Now you need to preprocess the images. You may need to convert the datatype (OpenCV uses 8 bit integers rather than float32) and normalize the data. You can also resize at this point if the images are not all the same size.
You can then flatten these numpy arrays to get a flat representation of the images. Just call the flatten() function of the np.ndarray. After you have loaded and flattened the images you want for your batch, string them together in a numpy array np.array([img1, img2, ..., imgN]) and this array will be of shape [images, pixels]. You can then feed this to you x placeholder.
I would prefer to preprocess every images if it is for training, but for using Tensorflow on-line with a live image stream, I would try the following method that dynamically changes the data in the memory:
any_shape = [the most natural shape according to the data you already have...]
x_unshaped = tf.placeholder(tf.float32, any_shape)
x = tf.reshape(x_unshaped, [-1, image_size])
If your data is already properly ordered in memory, you could try tf.Tensor.set_shape():
The tf.Tensor.set_shape() method updates the static shape of a Tensor
object, and it is typically used to provide additional shape
information when this cannot be inferred directly. It does not change
the dynamic shape of the tensor.
Source: https://www.tensorflow.org/versions/r0.9/api_docs/python/framework.html

Image sort with matlab

I am trying to do some kind of image sorting.
I have 5 images and first one is my main image. I am trying to sort images according to their similarity.(Most similar image to less similar image).
Matlab had matchfeature method but I dont think I jave used it correctly because my results are wrong.I try to use:
[indexPairs,matchmetric] = matchFeatures(features1,features2,"MatchThreshold,10")
then i try to sort the matchmetric array.But it didnt work
Can anyone tell me some algorithm or any tips ?Thank you..
You could compute the correlation coefficient between every images and your main image and then sort them based on the coefficient.
doc corr2
For example, let's say you store all your images in a cell array (called ImageCellArray) in which the first image is your "main image":
for i = 2:size(ImageCellArray,2) % size(ImageCellArray,2) is the total # of images, i.e. the size of the cell array containing them.
CorrCoeff(i) = corr2(rgb2gray(ImageCellArray{1}),rgb2gray(ImageCellArray{i}));
end
[values indices] = sort(CorrCoeff); % sort the coefficients and get the number of the corresponging image.
Then you're good to go I guess.
You could compute the PSNR (peak signal-to-noise ratio) for each image compared to the main image. PSNR is a metric commonly used to measure the quality of a reconstructed compression against the original image.
It's implemented in Matlab in the Computer Vision System toolbox as a functional block, and there is also a psnr function in the Image Processing toolbox. The result will be a number in decibels you could use to rank the images. A higher PSNR value indicates greater similarity.
Take a look at this example of image retrieval. Instead of matching the features between pairs of images it uses the KDTreeSearcher from the Statistics Toolbox to find nearest neighbors of each feature from the query image across the whole set of database images.

entropyfilt in OpenCV

I am working on an image processing project and I have to use entopyfilt (from matlab).
I researched and found some information to do it but not enough. I can calculate the entropy value of an image, but I don't know how to write an entropy filter. There is a similar question in the site, but I also didn't understand it.
Can anybody help me to understand entropy filter?
From the MATLAB documentation:
J = entropyfilt(I) returns the array J, where each output pixel contains the entropy value of the 9-by-9 neighborhood around the corresponding pixel in the input image I. I can have any dimension. If I has more than two dimensions, entropyfilt treats it as a multidimensional grayscale image and not as a truecolor (RGB) image. The output image J is the same size as the input image I.
For each pixel, you look at the 9 by 9 area around the pixel and calculate the entropy. Since the entropy calculation is a nonlinear calculation, it is not something you can do with a simple kernel filter. You have to loop over each pixel and do the calculation on a per-pixel basis.

Resources