<jupyter_start><jupyter_text>disclaimer: To ensure that the notebook can be run from (more or less) any point, I try to load the relevant functions or modules whenever I use them in a cell. This is generally not good practice as it adds unneccesary overhead  0.  Image representation as numerical arrays We start by importing numpy and creating and printing a simple 9x9 checkerboard array<jupyter_code>%matplotlib inline
%load_ext autoreload
%autoreload
import numpy as np

#make a 9x9 checkerboard
checkBoard = #

print(checkBoard)<jupyter_output><empty_output><jupyter_text>Then, we import [pyplot](https://matplotlib.org/api/pyplot_api.html) and [image](https://matplotlib.org/api/image_api.html) modules from the ploting library [matplotlib](https://matplotlib.org/3.1.1/api/index.html). Using it, we can display our checkerboard array in image form:<jupyter_code>import matplotlib.pyplot as plt
import matplotlib.image as mpimg
plt.imshow(checkBoard, cmap='gray', interpolation='nearest') 
plt.show()<jupyter_output><empty_output><jupyter_text>As another simple example, we will import the [data](https://scikit-image.org/docs/dev/api/skimage.data.html) module image processing library [scikit-image](https://scikit-image.org/) and load a small image of a bush.  First, we want to print the pixel values:<jupyter_code>from skimage import data
image_of_a_bush = data.lfw_subset()
image_of_a_bush = image_of_a_bush[0,:,:]

#print the #of dimentions, the shape, and the pixel values of the image
print("The number of dimensions of the image is: ", image_of_a_bush.ndim)
print("The size of the image is: ", image_of_a_bush.shape)
print(image_of_a_bush)<jupyter_output><empty_output><jupyter_text>Can you see the bush?  Next, show the image:<jupyter_code>plt.figure(figsize=(1,1))

# display the image
plt.#<jupyter_output><empty_output><jupyter_text>1. Pixel-level operations  Now that we have a sense of what a digital image is, let's start manipulating it. We'll begin with simple pixel-level operations 1.1 Basic pixel-level operations Let's look at a more interesting image. From scikit-image data we'll open a example IHC image, and plot it using pyplot.<jupyter_code>from skimage import data
import matplotlib.pyplot as plt
import numpy as np

image_hist = data.immunohistochemistry()
#check the size of the image
print("The number of dimensions of the image is: ", image_hist.ndim)
print("The size of the image is: ", image_hist.shape)
plt.imshow(image_hist, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>Seems like we have an RGB image. Let's look at every channel independently.<jupyter_code>plt.figure(figsize=(15,5))
plt.subplot(131)
plt.gca().set_title('Red channel')
plt.imshow(, cmap='Reds', interpolation='nearest')
plt.subplot(132)
plt.gca().set_title('Green channel')
plt.imshow(, cmap='Greens', interpolation='nearest')
plt.subplot(133)
plt.gca().set_title('Blue channel')
plt.imshow(, cmap='Blues', interpolation='nearest')

plt.show()
#for the moment let's look at only the first color channel
image_hist = image_hist[:,:,0]
plt.gca().set_title('First channel')
plt.imshow(image_hist, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>We can invert the image using the *invert* function from [scikit-images utilities module](https://scikit-image.org/docs/dev/api/skimage.util.html):<jupyter_code>from skimage.util import invert

inverted_image = #

plt.figure(figsize=(15,5))
plt.subplot(121)
plt.gca().set_title('original image')
plt.imshow(image_hist, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('inverted image')
plt.imshow(inverted_image, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>Let's try some other pixel-level operations. We'll use the [Exposure module](https://scikit-image.org/docs/dev/api/skimage.exposure.htmlskimage.exposure.adjust_sigmoid) from scikit image.1. A gamma correction applies the nonlinear transform $V_{out} = V_{in}^\gamma$.2. A log transform applies $V_{out} = log(V_{in}+1)$.3. A sigmoid transform applies $V_{out} = \frac{1}{1+e^{gain\cdot(\text{cutoff}-V_{in})}}$.4. Equalization transforms the intensity histogram of an image to a uniform distribution. It often enhances the contrast of the image5. Contrast Limited Adaptive Histogram Equalization (CLAHE) works similarly to equalization thats applied separately to different regions of the image.Try to apply these by calling the relevant function from skimage.exposure, or by direct calculation. Play with the different parameters and see how they change the output.<jupyter_code>from skimage import exposure

# apply gamma scaling with gamma=2
gamma=
gamma_corrected = 

# apply logarithmic scaling
logarithmic_corrected = 

# apply sigmoidal scaling with cutoff=0.4
cutoff = 
sigmoid_corrected = 

# equalize
equalize_corrected = 

# apply Contrast Limited Adaptive Histogram Equalization (CLAHE)
CLHA_corrected = 

plt.figure(figsize=(15,10))

plt.subplot(231)
plt.gca().set_title('original')
plt.imshow(image_hist, cmap=plt.cm.gray)

plt.subplot(232)
plt.gca().set_title('gamma corrected')
plt.imshow(gamma_corrected, cmap=plt.cm.gray)

plt.subplot(233)
plt.gca().set_title('log corrected')
plt.imshow(logarithmic_corrected, cmap=plt.cm.gray)

plt.subplot(234)
plt.gca().set_title('sigmoid')
plt.imshow(sigmoid_corrected, cmap=plt.cm.gray)

plt.subplot(235)
plt.gca().set_title('equalized')
plt.imshow(equalize_corrected, cmap=plt.cm.gray)

plt.subplot(236)
plt.gca().set_title('CLHA corrected')
plt.imshow(CLHA_corrected, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>1.2 Image filtering Spatial filtering is an image processing technique for changing the intensities of a pixel according to the intensities of some neighborhood of pixels.  The *Kernel* of the filter defines the neighborhood and the weights asigned to each pixel in the neighborhood:This procedure is formally a convolution and is marked by an asterisk: $I_o = I_i\ast f$. *side note: since a convolution in the spatial domain is equivalent to multiplication in the frequency domain. Sometimes it is more computationally reasonable to calculate these in fourier space.**side side note: filtering can also be performed in the frequency domain by directly removing a set of frequencies from an image.* The kernel can be of any shape/size, it is applied to each pixel in the image, and the output is a new, filtered, image. The output image is often called the *response* to the given filter.Example, local average:  Filtering is an incredibly versatile tool with which you can emphasize certain features or remove other features.  Image processing operations implemented with filtering include smoothing, sharpening, and edge enhancement. To implement different image filters, we will use the [filters module from scikit-image](https://scikit-image.org/docs/dev/api/skimage.filters.html)  1.2.1 Smoothing Smoothing, aka low-pass filtering, is used for removing high-frequency noise from images. Most commonly, a gaussian kernel is used, but others (e.g. local mean/median) work too. We'll see the effect of gaussian filtering.Try to change the value of sigma (width of the gaussian) to see how the output changes.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np

from skimage import filters

image_hist = data.immunohistochemistry()

sigma = 2

gauss_filtered_img = 

plt.figure(figsize=(15,8))
plt.subplot(121)
plt.gca().set_title('original image')
plt.imshow(image_hist, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('response, gaussian smoothing')
plt.imshow(gauss_filtered_img, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>1.2.2 Sharpening sharpening is sometimes used to enhance a blurry (i.e. crappy) image. 1. Start with input image2. Apply gaussian filter with very narrow kernel3. Subtract filtered image from input image to get only high frequency components3. Amplify (alpha) and add high frequency components to original input image<jupyter_code>filter_blurred_f = filters.gaussian(gauss_filtered_img, sigma=0.5, multichannel=False)
alpha = 3
sharpened = gauss_filtered_img + alpha * (gauss_filtered_img - filter_blurred_f)


plt.figure(figsize=(15,8))
plt.subplot(121)
plt.gca().set_title('input - blury image')
plt.imshow(gauss_filtered_img, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('sharpened')
plt.imshow(sharpened, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>Direct application of edge detectors often results in somewhat noisy responses. A way to overcome this is by first smoothing the image with a gaussian filter and then applying the edge filter. The width of the gaussian kernel will determine the size of edges the filter detects.  1.2.3 Edge enhancement Edge detecting filters work by measuring the local spatial gradient of an image. Common types are the Sobel, Prewitt and Roberts.  The filters are usually applied to each direction individually and then the total magnitude of the gradient is calculated.$|\nabla| = \sqrt{\nabla_x^2+\nabla_y^2}$ Sobel:  Prewitt:  Roberts:<jupyter_code>from skimage import data
image_hist = data.immunohistochemistry()
image_hist = image_hist[:,:,2]


# sobel magnitude
filtered_img = 
# sobel horizontal
filtered_img_h = 
# sobel vertical
filtered_img_v = 

plt.figure(figsize=(15,16))
plt.subplot(221)
plt.gca().set_title('input image')
plt.imshow(image_hist[:,:,2], cmap=plt.cm.gray)
plt.subplot(222)
plt.gca().set_title('sobel filter response - magnitude')
plt.imshow(filtered_img, cmap=plt.cm.gray)
plt.subplot(223)
plt.gca().set_title('sobel filter response - horizontal edges')
plt.imshow(np.abs(filtered_img_h), cmap=plt.cm.gray)
plt.subplot(224)
plt.gca().set_title('sobel filter response - vertical edges')
plt.imshow(np.abs(filtered_img_v), cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>Direct application of edge detectors often results in somewhat noisy responses. A way to overcome this is by first smoothing the image with a gaussian filter and then applying the edge filter. The width of the gaussian kernel will determine the size of edges the filter detects.<jupyter_code>from skimage import data
from skimage import feature

image_hist = data.immunohistochemistry()
image_hist = image_hist[:,:,2]

# sobel magnitude
filtered_img = filters.sobel(image_hist)

#apply a gaussian filter, followed by a sobel filter with sigma=3
sigma=
DoG_img = 

plt.figure(figsize=(15,16))
plt.subplot(221)
plt.gca().set_title('input image')
plt.imshow(image_hist, cmap=plt.cm.gray)

plt.subplot(223)
plt.gca().set_title('sobel filter response - magnitude')
plt.imshow(filtered_img, cmap=plt.cm.gray)
plt.subplot(224)
plt.gca().set_title('DoG filter response - magnitude')
plt.imshow(DoG_img, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>1.2.4 Gaussian derivatives Now let's remember that the application of filters is an associative operation (because convolution is linear!). It's equivalent to apply the gaussian and then the gradient filter to the image and to apply the gradient filter to the gaussian, then apply the result to the image, i.e. $\nabla*(G*I) = (\nabla*G)*I$ where $\nabla$ is the gradient (derivative) filter, $G$ is the gaussian filter, and $I$ is the image.We can generalize this idea and apply the derivative multiple times, to get a family of interesting filters:$\nabla^n*G$:Notice that all of the even orders are edge detectors, while all the odd orders are ridge detectors! Let's use a second order derivative (aka a Laplacian) and make a ridge detector: We'll look at a retinal photo where vasculature is an interesting feature. We invert the image so that the vasculature appears as bright lines on a dark background<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage.color import rgb2gray
from skimage.util import invert

#this is how we load an image from the hard drive
image_retina = ((img_as_float(mpimg.imread("../Data/RetinalPhoto.png"))))
image_ridges = invert(rgb2gray(image_retina))

plt.figure(figsize=(15,5))
plt.subplot(121)
plt.gca().set_title('retinal photo')
plt.imshow(image_retina, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('inverted grayscale image')
plt.imshow(image_ridges, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>**Apply a gaussian filter, followed by a second order derivative (laplacian).**<jupyter_code>sigma=3
LoG_ridges = 

plt.imshow(LoG_ridges,cmap='gray')
plt.gca().set_title('retinal photo - response to LoG')<jupyter_output><empty_output><jupyter_text>**This commonly used ridge detector is called a Laplacian of Gaussian (LoG)!**(We'll get back to this image in a bit)  1.3 Masking A mask is a binary image (0s and 1s) that typically separates a given input image into Foreground (interesting) and Background (boring) regions, or for picking a region-of-interest (ROI). A mask is *applied* to an image by element-wise multiplication. The size of a mask must be *identical* to the size of the image it's applied to. Let's begin by creating a simple circular mask. We'll create an array where the value at each point is it's distance from the center of the image, and display it as an image:<jupyter_code>import matplotlib.pyplot as plt
import numpy as np

#dimensions in x and y
y = 512
x = 512

#position of center
centY = np.ceil(y/2)
centX = np.ceil(x/2)

#create the grid
yy,xx = np.indices((y,x))

#create radial distance map
radialDist = 

#display
plt.gca().set_title('Radial distance')
plt.imshow(radialDist, cmap='gray', interpolation='nearest')
plt.show()<jupyter_output><empty_output><jupyter_text>Of these points, we'll pick a circle of radius 100 and display it as an image:<jupyter_code>circ1 = 

plt.show()
plt.gca().set_title('Circle with radius 100')
plt.imshow(circ1, cmap='inferno', interpolation='nearest')<jupyter_output><empty_output><jupyter_text>This object is a **mask**. If you multiply this matrix of 0s and 1s with an image of the same size, only the parts that are ==1 will remain  Let's apply this mask to our histology image.<jupyter_code>plt.gca().set_title('Masked first channel')
plt.imshow(, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>What happens if we invert the mask?<jupyter_code>inverted_mask = 

plt.figure(figsize=(15,5))
plt.subplot(121)
plt.gca().set_title('inverted mask')
plt.imshow(inverted_mask, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('inverted masked image')
plt.imshow(image_hist[:,:,2]*, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>**Just for closure, let's see what happens when we look at the full RGB image and try to apply the mask**<jupyter_code>image = data.immunohistochemistry()

masked_image = image*circ1
plt.imshow(masked_image, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>**Whoops. Seems like something is wrong. Our problem is that numpy didn't know how to multiply a 512x512x3 with a 512x512 mask. Numpy makes solving this very easy by adding a singleton dimension (look up broadcasting in your spare time).**<jupyter_code>image = data.immunohistochemistry()
plt.gca().set_title('Masked image')
masked_image = mage*np.expand_dims(circ1,2)
plt.imshow(masked_image, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>1.4 Thresholding 1.4.1 Simple thresholding Thresholding an image is the process of setting an intensity (or intensities) for separating the different components of an image. In simplest case, the foreground and background have very different intensities. In that case thresholding is just clustering pixels by their intensity levels.<jupyter_code>#this function from skimage converts images of integer types into floats, which are easier to work with.
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
from skimage import data

# First, let's create a noisy image of blobs
image_blobs = img_as_float(data.binary_blobs(length=512, seed=1))
sigma = 0.22
image_blobs += np.random.normal(loc=0, scale=sigma, size=image_blobs.shape)

print("The number of dimensions of the image is: ", image_blobs.ndim)
print("The size of the image is: ", image_blobs.shape)
plt.imshow(image_blobs, cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>To find the right threshold, let's examine a histogram of pixel intensity values<jupyter_code>plt.hist(image_blobs.flatten(),bins=250)
plt.show()<jupyter_output><empty_output><jupyter_text>Pick an appropriate threshold, by eye, and see if you can remove the background. What happens when you increase or decrease the threshold?<jupyter_code>thresh = 

mask = 

masked_image = 

plt.figure(figsize=(15,5))
plt.subplot(131)
plt.gca().set_title('original')
plt.imshow(image_blobs, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(132)
plt.gca().set_title('mask')
plt.imshow(mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(133)
plt.gca().set_title('masked image')
plt.imshow(masked_image, interpolation='nearest', cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>Our mask looks ok, but it has a lot of salt & pepper speckle noise. Why is that?We can try and use what we learned before about filtering to clean up our results. What filter should we use?<jupyter_code>from skimage import filters

thresh = 

mask = 

masked_image = 

plt.figure(figsize=(15,5))
plt.subplot(131)
plt.gca().set_title('original')
plt.imshow(image_blobs, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(132)
plt.gca().set_title('mask')
plt.imshow(mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(133)
plt.gca().set_title('masked image')
plt.imshow(masked_image, interpolation='nearest', cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>It's usually a good idea before creating a mask to despeckle an image using a narrow gaussian filter!  1.4.2 Morphological operationsMorphology is a broad set of image processing operations that process images based on shapes. In a morphological operation, each pixel in the image is adjusted based on the value of other pixels in its neighborhood. By choosing the size and shape of the neighborhood, you can construct a morphological operation that is sensitive to specific shapes in the input image. (explanation from Mathworks)Morphological operations are based around a *structuring element*, which is a small binary image, often of a disk or a square. The structuring element is positioned at all possible locations in the image and it is compared with the corresponding neighbourhood of pixels. Some operations test whether the element "fits" within the neighbourhood, while others test whether it "hits" or intersects the neighbourhood.Common operations for image processingErosion - output image =1 wherever the structuring element **fits** (erodes the mask)Dilation - output image =1 wherever the structuring element **hits** (expands the mask)Opening - Erosion followed by dilation (opens gaps in spots where the mask is weakly connected)Closing - Dilation followed by erosion (closes holes in the mask)A very thorough explanation of morphological operationscould be found [here](https://www.cs.auckland.ac.nz/courses/compsci773s1c/lectures/ImageProcessing-html/topic4.htm)<jupyter_code>from skimage.morphology import erosion, dilation, opening, closing
from skimage.morphology import disk

#define a "disk" structuring element with radius 10
selem = 

#apply erosion, dilation, opening, and closing
erosion_mask = 

dilation_mask = 

opening_mask = 

closing_mask = 


plt.figure(figsize=(15,10))
plt.subplot(231)
plt.gca().set_title('mask')
plt.imshow(mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(232)
plt.gca().set_title('erosion')
plt.imshow(erosion_mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(233)
plt.gca().set_title('dilation')
plt.imshow(dilation_mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(235)
plt.gca().set_title('opening')
plt.imshow(opening_mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(236)
plt.gca().set_title('closing')
plt.imshow(closing_mask, interpolation='nearest', cmap=plt.cm.gray)<jupyter_output><empty_output><jupyter_text>1.4.3 Masking actual data We'll repeat the thresholding procedure using an actual microscopy image of fluorescent nuclei<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg

#this is how we load an image from the hard drive
image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))


fig = plt.figure(num=None, figsize=(7.1, 4.6), dpi=80, facecolor='w', edgecolor='k')
print("The number of dimensions of the image is: ", image_nuclei.ndim)
print("The size of the image is: ", image_nuclei.shape)
plt.imshow(image_nuclei, cmap=plt.cm.gray, vmin=0, vmax=0.01)<jupyter_output><empty_output><jupyter_text>Again, let's plot a histogram of intensity values<jupyter_code>plt.
plt.xlim((0, 0.02))
plt.show()<jupyter_output><empty_output><jupyter_text>And again we'll pick a value by eye:<jupyter_code>thresh = 

#remember to despeckle before creating a mask!
mask = 

plt.figure(figsize=(8,15))
plt.subplot(311)
plt.gca().set_title('original')
plt.imshow(image_nuclei, interpolation='nearest', cmap=plt.cm.gray, vmin=0, vmax=0.01)
plt.subplot(312)
plt.gca().set_title('mask')
plt.imshow(mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(313)
plt.gca().set_title('masked image')
plt.imshow(image_nuclei*mask, interpolation='nearest', cmap=plt.cm.gray, vmin=0, vmax=0.01)<jupyter_output><empty_output><jupyter_text>Not bad! But also not very scalable. If we have 100s of images we can't look at them one-by-one and find thresholds by eye. Next, we'll look at some methods for automatically finding the thresholds.  1.5 Automated threshold calculation There is a very large list of algorithms for threshold calculation out there that are optimized for different situations. We will briefly review a few of the most common ones. 1.5.1 Iterative mean thresholdingAlgorithm:1. Start with some threshold $T_i$2. Compute the means $m_0$ and $m_1$ of the BG and FG3. Update $T_{i+1} = \frac{m_0+m_1}{2}$4. Repeat until it converges 1.5.2 Otsu thresholdingThe algorithm exhaustively searches for the threshold that minimizes the intra-class variance, defined for a given threshold $T$ as a weighted sum of variances of the two classes:$\sigma^2_w(T)=\omega_0(T)\sigma^2_0(T)+\omega_1(T)\sigma^2_1(T)$For 2 classes, minimizing the intra-class variance is equivalent to maximizing inter-class variance, which is much easier to calculate:\begin{align}\sigma^2_b(T) & =\sigma^2-\sigma^2_w(T)=\omega_0(\mu_0-\mu_T)^2+\omega_1(\mu_1-\mu_T)^2 \\& =\omega_0(T) \omega_1(T) \left[\mu_0(T)-\mu_1(T)\right]^2\end{align} 1.5.3 Triangle thresholdingAlgorithm:1. Draw a straight line between the histogram peak and the brightest value.2. From every point on that line, draw the shortest connecting line to the histogram.3. Find longest of these connecting lines.4. Threshold is set at the intersection of that line and the curve.*note: Triangle thresholding is good for situations where the image is mostly background, and there is no clear "peak" of bright pixels.* [scikit-image's filters module](https://scikit-image.org/docs/dev/api/skimage.filters.html) implements a large variety of thresholding algorithms. Let's apply the ones we just learned about.<jupyter_code>from skimage import filters

#calculate iterative mean threshold
meanThresh = 
print(meanThresh)

#calculate otsu threshold
OtsuThresh = 
print(OtsuThresh)

#calculate triangle
TriThresh = 
print(TriThresh)<jupyter_output><empty_output><jupyter_text>Let's look at the resulting masks we get with each of these thresholds<jupyter_code>fig = plt.figure(num=None, figsize=(12, 8), dpi=80)
ax1 = fig.add_axes([0.1,0.6,0.4,0.4])

ax1.hist(image_nuclei.flatten(),bins=250)

ax1.axvline(meanThresh, color='g', linestyle='--')
ax1.axvline(OtsuThresh, color='r', linestyle='--')
ax1.axvline(TriThresh, color='k', linestyle='--')

ax1.legend(['mean' ,'otsu', 'triangle'])
ax1.set_title('histogram')

ax2 = fig.add_axes([0.6,0.6,0.4,0.4])
#get iterative mean mask (remember to despeckle)
mask_mean = 

ax2.imshow(mask_mean)
ax2.set_title('Iterative mean')
ax2.set_axis_off()

ax2 = fig.add_axes([0.1,0.1,0.4,0.4])
#get otsu mask
mask_otsu = 

ax2.imshow(mask_otsu)
ax2.set_title('Otsu')
ax2.set_axis_off()

ax2 = fig.add_axes([0.6,0.1,0.4,0.4])
#get triangle mask
mask_tri = 

ax2.imshow(mask_tri)
ax2.set_title('Triangle')
ax2.set_axis_off()<jupyter_output><empty_output><jupyter_text>Let's briefly look back at the retinal images and try to mask only the vasculature:**as before, load the image, invert, and apply LoG filter:**<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage.color import rgb2gray

#this is how we load an image from the hard drive
image_ridges = invert(rgb2gray(img_as_float(mpimg.imread("../Data/RetinalPhoto.png"))))

plt.imshow(image_ridges, cmap=plt.cm.gray)
plt.gca().set_title('retinal photo')
sigma = 3
LoG_ridges = filters.laplace(filters.gaussian(image_ridges, sigma=sigma))
ax2.set_axis_off()<jupyter_output><empty_output><jupyter_text>**Now, let's do the same procedure of automatically finding thresholds:**<jupyter_code>from skimage import filters

meanThresh = filters.threshold_mean(LoG_ridges)
print(meanThresh)

OtsuThresh = filters.threshold_otsu(LoG_ridges)
print(OtsuThresh)

TriThresh = filters.threshold_triangle(LoG_ridges)
print(TriThresh)

fig = plt.figure(num=None, figsize=(12, 8), dpi=80)
ax1 = fig.add_axes([0.1,0.6,0.4,0.4])

ax1.hist(image_nuclei.flatten(),bins=250)

ax1.axvline(meanThresh, color='g', linestyle='--')
ax1.axvline(OtsuThresh, color='r', linestyle='--')
ax1.axvline(TriThresh, color='k', linestyle='--')
ax1.legend(['mean' ,'otsu', 'triangle'])
ax1.set_title('histogram')

ax1 = fig.add_axes([0.2,0.65,0.3,0.3])

plt.imshow(image_ridges, cmap=plt.cm.gray)
ax1.set_axis_off()


ax2 = fig.add_axes([0.6,0.6,0.4,0.4])
mask_mean = 

ax2.imshow(mask_mean)
ax2.set_title('Iterative mean')
ax2.set_axis_off()

ax2 = fig.add_axes([0.1,0.1,0.4,0.4])
mask_otsu = 

ax2.imshow(mask_otsu)
ax2.set_title('Otsu')
ax2.set_axis_off()

ax2 = fig.add_axes([0.6,0.1,0.4,0.4])
mask_tri = 

ax2.imshow(mask_tri)
ax2.set_title('Triangle')
ax2.set_axis_off()<jupyter_output><empty_output><jupyter_text>**Play around with the width of the gaussian. Which thresholding algorithm works best in this case?**  1.5.4 Local thresholding All of the methods we saw so far are *global* in the sense that the same threshold is applied to the whole picture. Sometimes we can have an image with vastly different intensity distributions at different locations. Using local thresholding, we can overcome such cases.Let's compare the results from a global (Otsu) and a local threshold.<jupyter_code>from skimage import data

image = data.page()
fig = plt.figure(num=None, figsize=(12, 8), dpi=80)

#global thresholding
threshGlobal = filters.threshold_otsu(image)

ax1 = fig.add_axes([0.1,0.6,0.4,0.4])
ax1.set_title('mask - Otsu threshold')
plt.imshow(image ,cmap='gray')

ax2 = fig.add_axes([0.6,0.6,0.4,0.4])
ax2.set_title('mask - Otsu threshold')
plt.imshow(image>threshGlobal,cmap='gray')

#local thresholding

#Try and change this number and see what happens
block_size  = 81

#calculate local threshold map
threshLocal = 

ax1 = fig.add_axes([0.1,0.2,0.4,0.4])
ax1.imshow(threshLocal,cmap='gray')
ax1.set_title('local threshold map')


ax2 = fig.add_axes([0.6,0.2,0.4,0.4])
ax2.set_title('mask - Local threshold')
plt.imshow(image>threshLocal,cmap='gray')<jupyter_output><empty_output><jupyter_text>2. Image segmentation Image segmentation is the process of partitioning a digital image into multiple segments. The goal of segmentation is to simplify and/or change the representation of an image into something that is more meaningful and easier to analyze. 2.1 Connected components After we generate a mask, the simplest segmentation is achieved by taking regions in the mask that are connected and labeling each one as a separate object.  We begin by generating a simple mask using the triangle threshold method:<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters

image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))
TriThresh = filters.threshold_triangle(image_nuclei)
#despeckle
mask = filters.gaussian(image_nuclei, sigma=1)>TriThresh<jupyter_output><empty_output><jupyter_text>[scikit-image's measure module](https://scikit-image.org/docs/dev/api/skimage.measure.html) implements a  variety of useful methods for segmentation. the *label* function returns a *labeled image* of connected components (CCs). Each CC is uniquely numbered by an integer.$\begin{bmatrix} 1 & 1 & 0 & 0 & 2\\1 & 1 & 0 & 2 & 2\\0 & 0 & 0 & 0 & 0\\0 & 3 & 0 & 4 & 4\\0 & 0 & 0 & 4 & 4\\\end{bmatrix}$<jupyter_code>from skimage import measure
#generate a labeled matrix of connected components
labels = 

plt.figure(figsize=(12,5))

plt.subplot(121)
plt.imshow(mask, cmap='gray')
plt.subplot(122)
plt.imshow(labels, cmap='nipy_spectral')<jupyter_output><empty_output><jupyter_text>We can easily generate a *mask* for a **specific** CC using the binary operation *labels==i*<jupyter_code>i=43
mask_of_CC_i = 

plt.figure(figsize=(10,5))
plt.imshow(mask_of_CC_i, cmap='gray')<jupyter_output><empty_output><jupyter_text>Problem with simple CC segmentation : overlapping objects We often really care about having only a single object per label. Using CC, any overlapping objects will merge into one blob:<jupyter_code>i=87
mask_of_CC_i = labels==i
plt.imshow(mask_of_CC_i, cmap='gray')<jupyter_output><empty_output><jupyter_text>These problems can be partially resolved using morphological operations, but there's no silver bullet<jupyter_code>from skimage.morphology import erosion, dilation, opening, closing
from skimage.morphology import disk

#define a "disk" structuring element
selem1 = disk(10)
selem2 = disk(7)

plt.figure(figsize=(15,10))
plt.subplot(121)
plt.gca().set_title('original')
plt.imshow(mask, cmap='nipy_spectral')
plt.subplot(122)
plt.gca().set_title('opening')
plt.imshow(dilation(erosion(mask, selem1),selem2), interpolation='nearest', cmap='nipy_spectral')<jupyter_output><empty_output><jupyter_text>2.2 Watershed based segmentation 2.2.1 The watershed algorithm The watershed transformation treats the image it operates upon like a topographic map, with the brightness of each point representing its height, and finds the lines that run along the tops of ridges.More precisely, the algorithm goes as follows:1. *Label* local minima (i.e. $S_1$, $S_2$)2. Move to next higher intensity level3. Assign to each point the label of it's closest label set.**By passing the argument *watershed_line = 1* Points equidistant to multiple sets are labeled as boundaries and intensities set to 0**4. Repeat until all points are labeledLet's start with a very naive application. We will invert the image, and then simply apply the *watershed* function from the [scikit-image morphology module](https://scikit-image.org/docs/dev/api/skimage.morphology). The function returns a labeled image. We'll plot the edges of that image using a Sobel filter.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters

from skimage.util import invert
from skimage.morphology import watershed


image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))


#invert image
image_to_watershed = 

#Calculate watershed transform, remember to pass the  watershed_line = 1 argument
labels_naive = 
plt.figure(figsize=(15,10))

#let's look at all the boundaries
plt.figure(figsize=(12,20))
plt.subplot(211)
plt.gca().set_title('image fed to watershed')
plt.imshow(image_to_watershed, cmap='gray')
plt.subplot(212)
plt.gca().set_title('watershed result')
plt.imshow(filters.sobel(labels_naive), cmap='nipy_spectral')<jupyter_output><empty_output><jupyter_text>So this clearly didn't work. Why? How do we fix it?Noise generates a ton of local minima. Each gets its own basin. This leads to massive oversegmentation. Watershed segmentation is only a *part* of a segmentation pipeline. Preprocessing (denoising, smoothing, seeding minima) of the image is CRUCIAL for it to work well. The first thing we'll do is to apply the mask that we found before. This is simply done by adding a *mask* argument to the watershed function.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters

from skimage.util import invert
from skimage.morphology import watershed

image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))

#calculate mask
mask = filters.gaussian(image_nuclei, sigma=1)>TriThresh

#apply mask and invert image
masked image = 

inverted_masked_image = 

image_to_watershed = inverted_masked_image

#Calculate watershed transform
#Now, also pass the mask to the watershed function so it avoids segmenting the BG
labels_masked = 

#let's look at all the boundaries
plt.figure(figsize=(12,20))
plt.subplot(211)
plt.gca().set_title('image fed to watershed')
plt.imshow(image_to_watershed, cmap='gray')
plt.subplot(212)
plt.gca().set_title('watershed result')
plt.imshow(filters.sobel(labels_masked), cmap='nipy_spectral')<jupyter_output><empty_output><jupyter_text>So we got rid of all the BG regions, but we are still oversegmenting. Why?Let's try to smoothen the image and get rid of the many local minima. How wide should the gaussian kernel be?<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters

from skimage.util import invert
from skimage.morphology import watershed

image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))

#Calculate mask
mask = filters.gaussian(image_nuclei, sigma=1)>TriThresh

#mask, smooth, and invert the image
masked_image = image_nuclei*mask

sigma_for_smoothing = 
smoothed_masked_image = 

inverted_smoothed_masked_image = invert(smoothed_masked_image)

image_to_watershed = inverted_smoothed_masked_image
#Calculate watershed transform
#pass the mask to the watershed function so it avoids segmenting the BG
labels_masked_smooth = watershed(image_to_watershed, watershed_line = 1, mask=mask)

#let's look at all the boundaries
plt.figure(figsize=(12,20))
plt.subplot(211)
plt.gca().set_title('image fed to watershed')
plt.imshow(image_to_watershed, cmap='gray')
plt.subplot(212)
plt.gca().set_title('watershed result')
plt.imshow(filters.sobel(labels_masked_smooth), cmap='nipy_spectral')<jupyter_output><empty_output><jupyter_text>We're starting to get somewhere!! Can we do better? We can do more to help the algorithm by providing local markers (seeds) from which to start the process We will find seeds by calculating local maxima over areas that are larger than 30x30 pixels using the footprint argument for the function peak_local_max<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters
from skimage import measure

from skimage.util import invert
from skimage.morphology import watershed

from skimage.feature import peak_local_max

image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))


mask = filters.gaussian(image_nuclei, sigma=1)>TriThresh


#mask, smooth, and invert the image
masked_image = image_nuclei*mask
sigma_for_smoothing = 4
smoothed_masked_image = filters.gaussian(masked_image, sigma=sigma_for_smoothing)
inverted_smoothed_masked_image = invert(smoothed_masked_image)

image_to_watershed = inverted_smoothed_masked_image

#find local peaks to use as seeds
#focus on this function. Look at the effect of different arguments!
#Specifically. look at the footprint argument
MaskedImagePeaks = 


#This is for presentation of our markers
#create disk structuring element of radius 5
selem = disk(5)
#dilate local peaks so that close ones merge
peakMask = dilation(MaskedImagePeaks,selem)
# label local peak regions to find initial markers
markers = measure.label(peakMask)

#pass the *markers* argument to the watershed function
labels_localmax_markers = watershed(image_to_watershed,markers, watershed_line = 1, mask=mask)

#let's look at all the boundaries
plt.figure(figsize=(12,20))
plt.subplot(211)
plt.gca().set_title('image fed to watershed')
plt.imshow(image_to_watershed-peakMask, cmap='gray')
plt.clim((0.95, 1))
plt.subplot(212)
plt.gca().set_title('watershed result')
plt.imshow(filters.sobel(labels_localmax_markers), cmap='nipy_spectral')<jupyter_output><empty_output><jupyter_text>This is pretty good! We're still getting a few errors here and there, but there's no big systematic over- or under- segmentation. This is a typical good result when dealing with real data.   3. Feature extraction Feature extraction is a process of dimensionality reduction by which an initial raw image is reduced to a list of objects and attributes 3.1 Extracting region properties [scikit-image's measure module](https://scikit-image.org/docs/dev/api/skimage.measure.html)  implements a  method called *regionprops* that accepts a labeled mask of connected components, and, optionally, a corresponding image, and returns a list.  Each object on the list contains useful data about the size, shape, position, and intensity ([see the full list here](https://scikit-image.org/docs/dev/api/skimage.measure.htmlskimage.measure.regionprops)) of a specific component. The length of the list is equal to the total number of objects detected.  We'll start by extracting the number of CC we found and the area of each CC<jupyter_code>from skimage import measure

#We use regionprops to extract properties on all the CCs
props = measure.regionprops(labels_localmax_markers,image_nuclei)
#how many total connected components did we get?
print(len(props))
props[1].perimeter
#This is how we make a list of a specific property for each CC
areas = [r.area for r in props]

#Do the same for the "mean_intensity" property
intensities = [r.mean_intensity for r in props]

#let's look at all the boundaries
plt.figure(figsize=(12,6))
plt.subplot(121)
plt.gca().set_title('areas')
plt.hist(areas)
plt.subplot(122)
plt.gca().set_title('intensities')
plt.hist(intensities)<jupyter_output><empty_output><jupyter_text>3.2 Some options for data presentation**We can look at individual objects we found**<jupyter_code>i=2
plt.imshow(props[i].intensity_image)
plt.gca().set_title('Single cell')<jupyter_output><empty_output><jupyter_text>**Let's use a scatter plot to compare our results to the image**<jupyter_code>intensities = np.array([r.mean_intensity for r in props])
centroids = np.array([r.centroid for r in props])


fig = plt.figure(figsize=(12,15))
fig.add_axes([0.1,0.6,0.4,0.25])
plt.gca().set_title('original')
plt.imshow(image_nuclei, interpolation='nearest', cmap=plt.cm.gray, vmin=0, vmax=0.02)
fig.add_axes([0.6,0.6,0.4,0.25])
plt.scatter(centroids[:,1],centroids[:,0], c=intensities)
plt.axis('equal')
plt.gca().invert_yaxis()<jupyter_output><empty_output><jupyter_text>**Or even nicer scatter plots!**<jupyter_code>intensities = np.array([r.mean_intensity for r in props])
areas = np.array([r.area for r in props])

centroids = np.array([r.centroid for r in props])


fig = plt.figure(figsize=(12,15))
fig.add_axes([0.1,0.6,0.4,0.25])
plt.gca().set_title('original')
plt.imshow(image_nuclei, interpolation='nearest', cmap=plt.cm.gray, vmin=0, vmax=0.02)
fig.add_axes([0.6,0.6,0.4,0.25])
plt.scatter(centroids[:,1],centroids[:,0], c=intensities, s=areas/20)
plt.axis('equal')
plt.gca().invert_yaxis()

plt.text(centroids[10,1],centroids[10,0],props[10].label)<jupyter_output><empty_output><jupyter_text>**You can even draw your points directly on the image!**<jupyter_code>intensities = np.array([r.mean_intensity for r in props])
areas = np.array([r.area for r in props])

centroids = np.array([r.centroid for r in props])


fig = plt.figure(figsize=(12,15))
fig.add_axes([0.1,0.6,0.8,0.5])
plt.gca().set_title('original')
plt.imshow(image_nuclei, interpolation='nearest', cmap=plt.cm.gray, vmin=0, vmax=0.02)
plt.gca().patch.set_alpha(0.5)
plt.scatter(centroids[:,1],centroids[:,0], c=intensities, alpha=1)<jupyter_output><empty_output><jupyter_text>3.3 Converting regionprops to a table (Dataframe) Let's define some useful functions for converting a list of props into a pandas dataframe. These should become obsolete soon since the new version of scikit-image will have this functionality  These are some useful functions for converting a list of props into a pandas dataframe<jupyter_code>import pandas as pd

def scalar_attributes_list(im_props):
    """
    Makes list of all scalar, non-dunder, non-hidden
    attributes of skimage.measure.regionprops object
    """
    
    attributes_list = []
    
    for i, test_attribute in enumerate(dir(im_props[0])):
        
        #Attribute should not start with _ and cannot return an array
        #does not yet return tuples
        if test_attribute[:1] != '_' and not\
                isinstance(getattr(im_props[0], test_attribute), np.ndarray):                
            attributes_list += [test_attribute]
            
    return attributes_list


def regionprops_to_df(im_props):
    """
    Read content of all attributes for every item in a list
    output by skimage.measure.regionprops
    """

    attributes_list = scalar_attributes_list(im_props)

    # Initialise list of lists for parsed data
    parsed_data = []

    # Put data from im_props into list of lists
    for i, _ in enumerate(im_props):
        parsed_data += [[]]
        
        for j in range(len(attributes_list)):
            parsed_data[i] += [getattr(im_props[i], attributes_list[j])]

    # Return as a Pandas DataFrame
    return pd.DataFrame(parsed_data, columns=attributes_list)<jupyter_output><empty_output><jupyter_text>Now, to get all the properties in table form we simply run:<jupyter_code>props_df = regionprops_to_df(props)
props_df<jupyter_output><empty_output><jupyter_text>Finally, if we imaged our cells in multiple channels, we would want to use the same segmented nuclei and measure intensities of other channels.<jupyter_code>from skimage import measure
from skimage import img_as_float
import matplotlib.image as mpimg

image_2ndChannel = img_as_float(mpimg.imread("../Data/xy040-2.png"))

# extract regionprops using labels_localmax_markers mask from image_2ndChannel
props_other_channel = measure.regionprops(labels_localmax_markers,image_2ndChannel)


plt.figure(figsize=(12,6))
plt.subplot(121)
plt.gca().set_title('Nuclei')
plt.imshow(image_nuclei, cmap='gray')
plt.subplot(122)
plt.gca().set_title('other channel')
plt.imshow(image_2ndChannel, cmap='gray')<jupyter_output><empty_output><jupyter_text>Extract only the intensity related features<jupyter_code>mean_2nd_channel = [r.mean_intensity for r in props_other_channel]
max_2nd_channel = [r.max_intensity for r in props_other_channel]
min_2nd_channel = [r.min_intensity for r in props_other_channel]

plt.gca().set_title('intensities of 2nd channel')
plt.hist(mean_2nd_channel)<jupyter_output><empty_output><jupyter_text>Add these new features to the pandas dataframe<jupyter_code>props_df['mean_intensity_ch2'] = mean_2nd_channel
props_df['max_intensity_ch2'] = max_2nd_channel
props_df['min_intensity_ch2'] = min_2nd_channel

props_df<jupyter_output><empty_output><jupyter_text>**Sometimes it's easier to see a bimodal distribution in log scale**<jupyter_code>fig = plt.figure(figsize=(12,6))
fig.add_axes([0.1,0.1,0.4,0.4])
plt.gca().set_title('Histogram of intensities')
plt.hist(props_df.mean_intensity_ch2,20)
fig.add_axes([0.6,0.1,0.4,0.4])
plt.gca().set_title('Histogram of log of intensities')

plt.hist(np.log(props_df.mean_intensity_ch2),20)<jupyter_output><empty_output><jupyter_text>**We can compare distributions of different channels**<jupyter_code>plt.figure(figsize=(12,6))

plt.gca().set_title('scatter plot of intensities')
plt.scatter(np.log(props_df['max_intensity_ch2']), np.log(props_df['max_intensity']))
plt.xlabel('Ch2')
plt.ylabel('Ch1')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10574344877312364, 'fraction_numerical': 0.01969311191726189, 'mean_word_length': 6.080727504786216, 'pattern_counts': {'":': 0, '<': 217, '<?xml version=': 0, '>': 223, 'https://': 14, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '54', 'dir': 'jupyter-structured-clean-dedup', 'id': '394656', 'license': 'MIT', 'n_tokens_mistral': 15683, 'n_tokens_neox': 14403, 'n_words': 4353, 'path': '20201109/JupyterNotebooks/DIP_AOY_Student.ipynb', 'repo_name': 'alonyan/DIP', 'size': '44381'}