Image Segmentation (Part 2). RG Chromaticity | by Ralph Caubalejo | Jan, 2021

0
52

In Part 1, we had been in a position to speak about 3 Different Techniques(Thresholding, Otsu’s, and HSV Color Space) to phase other items in a picture.

This time, we can display a a lot more outlined manner of segmenting the items the use of a a lot more sophisticated methodology this is used for sophisticated photographs.

Let us say we’ve other varieties of distinctive colours in our symbol, how are we able to leverage a picture processing option to display the distinctiveness of each and every pixel values?

The perfect manner to try this is by the use of Chromaticity Segmentation.

In a way, Chromaticity Segmentation seems to be on the aggregate of the pixel values inside the symbol and maps it to the pink and inexperienced channel to decide the share. Chromaticity Segmentation can also be additionally coined as RG Chromaticity.

To higher display the way it works, allow us to do a little experiments:

Let us load our previous pattern from section 1:

import numpy as np
from skimage.io import imshow, imread
from skimage.colour import rgb2gray
import matplotlib.pyplot as plt
pattern = imread('flowers2.png')
sample_g = rgb2gray(pattern)
fig, ax = plt.subplots(1,2,figsize=(10,5))
ax[0].imshow(pattern)
ax[1].imshow(sample_g,cmap='grey')
ax[0].set_title('Colored Image',fontsize=15)
ax[1].set_title('Grayscale Image',fontsize=15)
plt.display()
Figure 1: Sample Image (Image by Author)

RG Chromaticity

For this newsletter, we can nonetheless attempt to phase the white flower from the picture. This time, we can be the use of RG Chromaticity as the picture segmentation methodology.

To higher perceive the picture, we will have to first take a look at the scatterplot of the pixel values of the pictures. The code to turn that is as follows:

sample_R = pattern[:,:,0]*1.0/pattern.sum(axis=2)
sample_G = pattern[:,:,1]*1.0/pattern.sum(axis=2)
fig, ax = plt.subplots(1,2,figsize=(10,5))
ax[0].imshow(pattern)
ax[1].scatter(sample_R.flatten(),sample_G.flatten())
ax[0].set_title('Colored Image',fontsize=15)
ax[1].set_title('Pixel Values',fontsize=15)
ax[1].set_xlabel('Red Channel',fontsize=10)
ax[1].set_ylabel('Green Channel',fontsize=10)
plt.tight_layout()
plt.display()
Figure 2: Scatterplot of Pixel Values (Image by Author)

Figure 2 displays the scatter plot in regards to the pink channel because the x-axis and the fairway channel because the y-axis. Notice that the pixel values are extra obvious at the high-intensity inexperienced channel pixel values.

To be capable to phase the white flower, we will have to first outline a pattern patch within the symbol. The code for that’s as follows:

from matplotlib.patches import Rectanglefig, ax = plt.subplots(1,2,figsize=(10,10))
ax[0].imshow(pattern)
ax[0].add_patch(Rectangle((590, 570), 100, 100, edgecolor='r', facecolor='none'));
ax[0].set_title('Patch Location',fontsize=15)
#Showing Patch
patch = pattern[570:670, 590:680]
ax[1].imshow(patch)
ax[1].set_title('Patch',fontsize=15)
plt.display()
Figure 3: Patch Allocation (Image by Author)

Notice in Figure Three that we used the whitest a part of the flower as a result of we might simplest phase the pixel values which might be the similar as this patch pixel price. To visualize that, we will plot the scatter plot of the patch itself:

patch_R = patch[:,:,0]*1.0/patch.sum(axis=2)
patch_G = patch[:,:,1]*1.0/patch.sum(axis=2)
fig, ax = plt.subplots(1,2,figsize=(10,5))
ax[0].imshow(patch)
ax[1].scatter(patch_R.flatten(),patch_G.flatten())
ax[0].set_title('Patch',fontsize=15)
ax[1].set_title('Pixel Values',fontsize=15)
ax[1].set_xlabel('Red Channel',fontsize=10)
ax[1].set_ylabel('Green Channel',fontsize=10)
ax[1].set_xlim(0,1)
ax[1].set_ylim(0,1)
plt.tight_layout()
plt.display()
Figure 4: Patch Scatterplot (Image by Author)

Figure Four displays that the pixel values for the pink channel and inexperienced channel are positioned simplest in the course of the plot. To phase the white flower, we would need all of the pixel values inside of this area.

To phase it, we might take the usual deviation and the imply of the patch initialized.

std_patch_R = np.std(patch_R.flatten())
mean_patch_R = np.imply(patch_R.flatten())
std_patch_G = np.std(patch_G.flatten())
mean_patch_G = np.imply(patch_G.flatten())
def gaussian(p,imply,std):
go back np.exp(-(p-mean)**2/(2*std**2))*(1/(std*((2*np.pi)**0.5)))

After getting the imply and usual deviation, we might interpolate the unique symbol to the patch in order that it is going to observe the distribution. For this newsletter, we used the Gaussian distribution as our distribution curve.

prob_R = gaussian(sample_R,mean_patch_R,std_patch_R)
prob_G = gaussian(sample_G,mean_patch_G,std_patch_G)
fig, ax = plt.subplots(1,3,figsize=(15,10))
ax[0].imshow(sample_g,cmap='grey')
ax[1].imshow(prob_R,cmap='grey')
ax[2].imshow(prob_R,cmap='grey')
ax[1].set_title('Grayscale',fontsize=15)
ax[1].set_title('Red Channel',fontsize=15)
ax[2].set_title('Green Channel',fontsize=15)
plt.display()
Figure 5: Red and Green Channel Chromaticity (Image by Author)

Figure Five displays the result of getting the interpolated values for the pink and inexperienced channel, understand that within the grayscale mode, we had been in a position to phase the flower bouquets additionally.

To get the white flower particular section, we might want to multiply the effects.

prob = prob_R * prob_Gfig, ax = plt.subplots(1,2,figsize=(10,10))
ax[0].imshow(pattern)
ax[1].imshow(prob,cmap='grey')
ax[0].set_title('Colored Image',fontsize=15)
ax[1].set_title('Final Image',fontsize=15)
plt.display()
Figure 6: Final Image the use of Chromaticity (Image by Author)

Figure 6 displays our Final Image the use of the RG Chromaticity Technique. The effects display that RG Chromaticity is in fact wat higher when segmenting items that experience various colour areas.

For this newsletter, we had been in a position to turn how we will use the pink and inexperienced channels in an effort to phase items in a picture. We had been in a position additionally to turn how the pixel values can be utilized to phase the picture from its background the use of the chromaticity plot. In conclusion, Image Segmentation may be very useful in developing datasets for long term Data Science Projects.

Stay Tuned for extra articles about Image Processing!

LEAVE A REPLY

Please enter your comment!
Please enter your name here