Home Crop the specific color region and remove the noisy regions (Python+OpenCV)

# Crop the specific color region and remove the noisy regions (Python+OpenCV)

user2802
1#
user2802 Published in May 26, 2018, 11:40 pm

I have a problem while getting a binary image from colored images. cv2.inRange() function is used to get mask of an image (simillar with thresholding) and I want to delete unnecessary parts, minimizing erosion of mask images. The biggest problem is that masks are not regularly extracted.

## Samples

Crack:

Typical one

Ideal one:

My first object is making second picture as third one. I guess getting contour that has biggest area and deleting other contours(also for the mask) would be work. But can't not find how.

Second probleme is that the idea I described above would not work for the first image(crack). This kind of images could be discarded. But anyway it should be labeled as crack. In so far, I don't have ideas for this.

## What I did

Here is input image and codes 42_1.jpg

class Real:
__ex_low=np.array([100,30,60])
__ex_high=np.array([140,80,214])

__ob_low=np.array([25,60,50]) #27,65,100])
__ob_high=np.array([50,255,255]) #[45,255,255])

kernel = np.ones((3,3), np.uint8)
return op

def __del_ext(self, img_got):
img = img_got[0:300,]
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

temp=array1.tolist()

xmin=min(array2[0])     #find the highest point covered blue
x,y,channel=img.shape
img=img[xmin:x,]
hsv=hsv[xmin:x,]

return img, hsv

def __init__(self, img_got):
img, hsv = self.__del_ext(img_got)

ymin=min(array2[1])
ymax=max(array2[1])
xmin=min(array2[0])
xmax=max(array2[0])

self.x = xmax-xmin
self.y = ymax-ymin
self.ratio = self.x/self.y

# xmargin = int(self.x*0.05)
#ymargin = int(self.y*0.05)

self.img = img[(xmin):(xmax),(ymin):(ymax)]

#models = glob.glob("D:/Python36/images/motor/*.PNG")

#last_size = get_last_size(models[-1])
#m2= Model(models[39],last_size)

r1 = Real(img)

cv2.imshow("2",r1.img)

It would be great if codes are written in python3, but anything will be okay.