Skip to content

Latest commit

 

History

History
102 lines (73 loc) · 2.25 KB

Cable_Segmentation.md

File metadata and controls

102 lines (73 loc) · 2.25 KB

Cable Segmentaion in Python

This answer for this Question in StackOverflow.


How to segment coloured cable in Python

Theory: Thresholding in HSV Space. Reference Link

Theory

Morhpological Closing Theory Link

Input Image

Morpho closing Image source


Python Code

#========================
# Import Libraies
#========================
import numpy as np 
import matplotlib.pyplot as plt 
import cv2 as cv 

#------------------------
# Read Image
#========================
img = cv.imread("img.jpg")
imgHSV = cv.cvtColor(img, cv.COLOR_BGR2HSV)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

#------------------------
# Threshold Image
#========================
## mask of red
mask1 = cv.inRange(imgHSV, (0, 30, 0), (10, 255,255))
mask2 = cv.inRange(imgHSV, (170, 30, 0), (180, 255,255))

mask = cv.bitwise_or(mask1, mask2)

mask = np.tile(mask, (3,1,1))
mask = np.swapaxes(mask , 0, 1)
mask  = np.swapaxes(mask , 1, 2)

print(mask.shape)

th1 = cv.bitwise_and(img,mask)

#------------------------
# Morphology
#========================
kernel1 = np.ones((7,7),np.uint8)
kernel2 = np.zeros((70,70),np.uint8)
kernel2[10:60, 10:60] = 1

img_opn = cv.morphologyEx(th1 ,cv.MORPH_OPEN ,kernel1)
img_cls = cv.morphologyEx(img_opn, cv.MORPH_CLOSE, kernel2)

#------------------------
# Results Visualization
#========================
plt.figure(num = "Red Cable")

plt.subplot(221)
plt.imshow(img)
plt.title('Original')
plt.axis('off')

plt.subplot(222)
plt.imshow(th1)
plt.title('Thresholded')
plt.axis('off')

plt.subplot(223)
plt.imshow(img_opn)
plt.title('Opening')
plt.axis('off')

plt.subplot(224)
plt.imshow(img_cls)
plt.title('Result')
plt.axis('off')

plt.show()
#------------------------

Result

Input Image

Result Image