Вместо найденых контуров, я получаю пустое изображение

Как Вы видите я ищу общие контуры(контуры которые имеют общие точки), но при том что есть один положительный ответ, все равно я не получаю правильного изображения на выходе. Заранее спасибо за помощь!

import cv2
import numpy as np

image = cv2.imread("2.jpg")
blank = np.zeros(image.shape[0:2])

clahe = cv2.createCLAHE(clipLimit=3., tileGridSize=(8,8))
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)  #convert from BGR to LAB color space
l, a, b = cv2.split(lab)# split on 3 different channels
l2 = clahe.apply(l)#apply CLAHE to the L-channel
lab = cv2.merge((l2,a,b))#merge channels
img2 = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)#convert from LAB to BGR
blur = cv2.GaussianBlur(img2, (7,7), 0)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
original = hsv.copy()

red = np.zeros_like(image, np.uint8)

czerwony = cv2.inRange(hsv, (0, 70, 50), (10, 255, 255))
czerwony2 = cv2.inRange(hsv, (170, 70, 50), (180, 255, 255))
mask_czerwony = czerwony>0
mask_czerwony2 = czerwony2>0
red[mask_czerwony] = image[mask_czerwony]
red[mask_czerwony2] = image[mask_czerwony2]

im = image.copy()

gray = cv2.cvtColor(red, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)
contours1, hierarchy = cv2.findContours(image=thresh, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
contour_list1 = []
for c in contours1:

    approx = cv2.approxPolyDP(c, 0.04 * cv2.arcLength(c, True), True)
    area1 = cv2.contourArea(c)
    if 500 < area1 < 2000:
        contour_list1.append(c)
        cv2.drawContours(image=im, contours=contours1, contourIdx=-1, color=(0, 0, 255), thickness=-1, lineType=cv2.LINE_AA)
        cv2.imshow("im", im)
    image1 = cv2.drawContours(blank.copy(), contours1, 0, 1)

mask1 = cv2.inRange(hsv,(0,0,0),(179,255,70))#detecting of black(mixed filter)
image_copy1 = image.copy()

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
opening = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, kernel, iterations=1)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)#monchrome image

circles = cv2.HoughtCircles = (image, cv2.HOUGH_GRADIENT, 1, 300, np.array([]), 10, 30, 60, 300)

contours2,h = cv2.findContours(close,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

final = np.zeros(hsv.shape, np.uint8)
mask = np.zeros(hsv.shape[:2], np.uint8)
contour_list = []
for a in contours2:
    approx = cv2.approxPolyDP(a, 0.04 * cv2.arcLength(c, True), True)
    area2 = cv2.contourArea(a)

    if 1000 < area2 < 25000:
        contour_list.append(c)
        cv2.drawContours(image_copy1, [a], -1, (0,255,0), -1)
    cv2.imshow("blue", image_copy1)
    image2 = cv2.drawContours(blank.copy(), contours2, 1, 1)

for i in range(0,len(contours2)):
    cv2.drawContours(image_copy1, contours2, i, (0, 255, 0), -1)
    mask[...]=0
    cv2.drawContours(mask,contours2,i,255,-1)#contours
    averagecolor = cv2.mean(image, mask)
    draw_arerage_color = cv2.drawContours(final,contours2,i,averagecolor,-1)#final result
cv2.imshow('final', final)

contours = [contours1, contours2]
intersection = np.logical_and(image1, image2)
for g in contours:
    if intersection.any():
        cv2.drawContours(blank, contours, g, (0, 255, 0), 1)
    cv2.imshow("8",blank)

circles = cv2.HoughCircles(close, cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)

if circles is not None:
    print(len(circles))
circles = np.uint16(np.around(circles))

if cv2.waitKey(0):
    cv2.destroyAllWindows()

cv2.waitKey(0)

Ответы (0 шт):