Search code examples
javaimage-processingopencv3.0cg

How can I segment a colored line?


I'm trying to segment the blue and black line of this image.

original image

but I was unable to segment them separately (using the OpenCV library in Java) and it resulted in this:

binarized image

I tried this function: mgproc.threshold(imgGray, imgThreshold, 0, 255, Imgproc.THRESH_BINARY + Imgproc.THRESH_OTSU);

How can I segment the blue and black lines separately using the OpenCV library?


Solution

  • If the colors of the lines are a given, you can define a color range and filter the image.

    Here is a sample code (not mine; taken from this website: https://pythonprogramming.net/color-filter-python-opencv-tutorial/)

    lower_red = np.array([30,150,50])
    upper_red = np.array([255,255,180])
    
    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(frame,frame, mask= mask)
    
    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)
    cv2.imshow('res',res)