-1

I have detected two lines in an image using cv2. now I want to get the RGB values of both lines in separate variables like left_line_veriable = ['rgb values'], right_line_rgb_values = ['rgb values']

Here is my code:

import cv2
import numpy as np

image = cv2.imread('tape.png')
image = cv2.cvtCOLOR(image, cv2.COLOR_BGR2GRAY)

# Apply adaptive threshold
image_thr = cv2.adaptiveThreshold(image, 255, cv2.THRESH_BINARY_INV, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 81, 2)

# Apply morphological opening with vertical line kernel
kernel = np.ones((image.shape[0], 1), dtype=np.uint8) * 255
image_mop = cv2.morphologyEx(image_thr, cv2.MORPH_OPEN, kernel)

color_detected_img = cv2.bitwise_and(image, image, mask=image_mop)

cv2.imshow('image', color_detected_img)

cv2.waitKey(0)
cv2.destroyAllWindows()

This is the image from which I want to get both line's RGB values in two variables as described above:

enter image description here

rizwan
  • 51
  • 1
  • 11
  • 1
    you have 593 different RGB values in this image.Maybe you should do the average of each line... If it is something like this, I would first segment them (e.g. with connectedComponents after a binary threshold to get 2 labeled structures here), then avg the RGB values of the pixels in each segment and identify which one is left and which one is right – api55 Dec 12 '19 at 12:31
  • @api55 I did that already using for loops. Is there any OpenCV method that can do this? – rizwan Dec 12 '19 at 12:40
  • please use websearch for such trivial questions. "average rgb opencv" "color contour area"... https://stackoverflow.com/questions/34969505/opencv-how-can-i-find-the-color-inside-a-contour-polygon – Piglet Dec 12 '19 at 13:07

1 Answers1

0

Maybe is not the most optimal way, but it is not hard to do. As I said in my comments, you can label the image to kind of segment the lines, then get the mean of the rgb values in it and the average position to get to know which one is left and right. Here is a small script to demonstrate what I am saying. The last part is just to show the results.

import cv2
import numpy as np

# load img and get the greyscale
img = cv2.imread("x.png")
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# label the image
ret, thres = cv2.threshold(grey, 1, 255, cv2.THRESH_BINARY)
labelAmount, labels = cv2.connectedComponents(thres)

# get the mean of the color and position
values = []
# first label (0) is background
for i in range(1, labelAmount):
  mask = np.zeros(labels.shape, dtype=np.uint8)
  mask[labels == i] = 255
  mean = cv2.mean(img, mask)[:-1]
  meanPos = np.mean(cv2.findNonZero(mask), axis=0)[0]
  values.append((mean, meanPos))

# sort them by x value (left to right)
values = sorted(values, key = lambda v : v[1][0])

left_line_color = values[0][0]
right_line_color = values[1][0]

# just to show the results
left_only = np.zeros(img.shape, dtype=np.uint8)
right_only = np.zeros(img.shape, dtype=np.uint8)
left_only = cv2.line (left_only, (int(values[0][1][0]), 0), (int(values[0][1][0]), img.shape[0]), left_line_color,5 )
right_only = cv2.line (right_only, (int(values[1][1][0]), 0), (int(values[1][1][0]), img.shape[0]), right_line_color,5 )
cv2.imshow("left_line", left_only)
cv2.imshow("right_line", right_only)
cv2.imshow("original", img)
cv2.waitKey(0)
api55
  • 11,070
  • 4
  • 41
  • 57