0

I want to detect the circlip in the fixture. if circlip is not present it should give a message "circlip not present".

  • 1
    lighting needs to be improved. avoid reflections. camera-object pose needs to be improved. picture appears to have been taken at an angle. a frontal view would be better. – Christoph Rackwitz Feb 28 '22 at 12:06

2 Answers2

1

Binarization applied to the saturation component gives interesting results.

enter image description here

vs.

enter image description here

But the circlip needs to remain tinted.

0

The solution provided by @YvesDaoust gives good insights into solving the problem.

Thresholding on Saturation channel as suggested by @YvesDaoust, followed by Morphological closing, and largest connected component extraction solves this specific problem.

Note that this solution is not general for all illumination conditions, resolutions, rotation, color, etc...
But it might work for similar conditions.

res

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np

img = cv2.imread("input2.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
s = hsv[...,1]

th = 100
s[s<th]=0

op = cv2.MORPH_CLOSE
morph_elem = cv2.MORPH_ELLIPSE
morph_size = 5
element = cv2.getStructuringElement(morph_elem, (2*morph_size + 1, 2*morph_size+1), (morph_size, morph_size))
mph = cv2.morphologyEx(s, op, element)

# Reference: https://stackoverflow.com/a/47057324
def lcc (image):
    image = image.astype('uint8')
    nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4)
    sizes = stats[:, -1]

    max_label = 1
    max_size = sizes[1]
    for i in range(2, nb_components):
        if sizes[i] > max_size:
            max_label = i
            max_size = sizes[i]

    img2 = np.zeros(output.shape)
    img2[output == max_label] = 255
    img2 = img2.astype(np.uint8)
    return img2

mask = lcc(mph)

thresh = 20000000

if np.sum(mask) < thresh:
    print("circlip not present")
    res = img
else: 
    res = cv2.bitwise_and(img,img,mask = mask)

cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img", res)
cv2.waitKey(0)
Bilal
  • 3,191
  • 4
  • 21
  • 49