Search code examples
pythonnumpyscikit-image

Python; Determining and adding noise to image segments


In order to get the grips of coding I decided to determine the standard deviation in a certain area of a x-ray picture using Python/Numpy/SKimage. To start with I decided to use thresholding in order to get a part of the image, this wasn't hard.

However, at this point everything above/below the threshold is zero at thus is included in the measurements I would like to do. Thus I need to exclude the data below/above the threshold.

I imagine it would be able to create a map or exclude certain value’s or possibly more exotic solutions. However, at this point I’m thinking I might be heading in the wrong direction.

My basics -

import numpy as np
import matplotlib.pyplot as plt
import imageio

image = imageio.imread('thx.jpg', as_gray=True)
image = image.astype('int32')

test1 = np.array(image, copy=True)
test1[image >= 100] = 0

I'm looking for a way to exclude the data above/below the threshold. Could someone provide me with a small push in the right direction?

Edit: It's nice to have an easy day at work once in a while; A sub-solution to my problem is adding all values greater/smaller then to a list and determining the standard deviation from there. This however leaves me with the problem of implementing the noise to the image segments.

im_row = image.shape[0]
im_col = image.shape[1]
grthen = []
smlthen = []

for i in range(0,im_col-1):
    for j in range(0,im_row-1):
        if (j > 100):
           grthen.append(j)
        else:
            smlthen.append(j)

print(np.std(smlthen))
print(np.std(grthen))

Solution

  • I think the problem is that you set all those pixels to zero, then try to get statistics from them. Instead, realize that test1[image < 100] only refers to those pixels below the threshold... so I think you can just get your statistics from that, e.g. with np.std(test1[image < 100]).

    You might like to take a look at scikit-image, which contains lots of tools for thresholding, dealing with binary images, using those as masks (which is essentially what you're doing), etc.