
# `road` contains some unwanted spots/contours which are removed using the function "get_region" To get an approximate region of the road, I subtracted the mask and th.Ĭv2.subtract() performs arithmetic subtraction, where cv2 will take care of negative values.Mask # turning the region outside the green block white Th # function to obtain the largest contour in given image after filling itīlack = np.zeros((image.shape, image.shape), np.uint8) Isolating the green lego block img = cv2.imread(image_path) This answer is based on the usage of LAB color spaceġ. Here I am presenting a repeated segmentation approach using color. Morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)Ĭontours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)Ĭontours = contours if len(contours) = 2 else contoursĬv2.drawContours(result,, -1, (0, 0, 255), 2) Kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (29,29)) Morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) Kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) Thresh = cv2.inRange(median, lower, upper) Green_block = cv2.bitwise_and(img, img, mask = mask) # turning the region outside the green block white # function to obtain the largest contour in given image after filling itĬontours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) Th = cv2.threshold(a_component,127,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)īlack = np.zeros((img.shape,img.shape),np.uint8) Lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) Img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) Height = int(img.shape * scale_percent / 100) Width = int(img.shape * scale_percent / 100) Scale_percent = 50 # percent of original size Thanks, both and for your great solutions to this problem! I liked isolating / masking the green board, so I combined both: import cv2 The adaptive treshold lets you see edges quite well, but with HoughLinesP you don't get anything usable out of it # Median blurring to get rid of the noise invert imageīw = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ Out = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

Img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) I have already experimented a lot with HoughLinesP, converted the image to gray or b/w before, applied blur.

Now I am trying to detect the thick black lines (connecting the white squares) with OpenCV. I have the following image of a lego board with some bricks on it
