drawcontours区域的图像处理

Image processing on the drawcontours area

我想将 drawcontours区域转换为RGB图像,然后再次将其转换为HSV以便更新更低随着时间的推移,每帧的上限值。

注意:我想避免使用矩形区域的ROI,因为drawcontours是实际区域。

我尝试根据 drawContours 区域 roi2 = clone1[contour[[0]]] cv2.imshow("roi2", roi2) 而不是矩形区域 roi1 = clone1[y:y + h, x:x + h] cv2.imshow("roi1", roi1)

显示感兴趣区域 (ROI)

不知是否可行

或者如何使用原始图像(RGB图像)的副本为除drawContours区域之外的整个图像创建蒙版?类似于 roi1 = clone1[...]

填写我的代码:

import cv2
import numpy as np
import time

cap = cv2.VideoCapture(0)
width = cap.get(3)  # float
height = cap.get(4)  # float

time.sleep(2.0)
while (1):
    _, img = cap.read()
    clone1 = img.copy()
    if _ is True:
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        continue

    black_lower = np.array([0, 0, 0], np.uint8)
    black_upper = np.array([180, 255, 30], np.uint8)
    black = cv2.inRange(hsv, black_lower, black_upper)
    # Morphological Transform, Dilation

    kernal = np.ones((5, 5), "uint8")
    black = cv2.dilate(black, kernal)
    res_black = cv2.bitwise_and(img, img, mask=black)
    (_, contours, hierarchy) = cv2.findContours(black, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    cv2.imshow("ROI_", _)
    cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:1]  # get largest  contour area
    for pic, contour in enumerate(cnts):
        area = cv2.contourArea(contour)
        if (area > 300):
            x, y, w, h = cv2.boundingRect(contour)
         
            # segmented = max(cnts, key=cv2.contourArea)
          

            img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
            roi1 = clone1[y:y + h, x:x + h]
            cv2.imshow("roi1", roi1)
            cv2.putText(img, "Black Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))
            bbbbb = cv2.drawContours(img, [contour], -1, (0, 255, 0), 3)  # segmentation
            # roi2 = clone1[contour[[0]]]
            # cv2.imshow("roi2", roi2)

    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break
import cv2
import numpy as np
import time

cap = cv2.VideoCapture(0)
width = cap.get(3)  # float
height = cap.get(4)  # float
time.sleep(2.0)
while (1):
    _, img = cap.read()
    clone1 = img.copy()
    if _ is True:
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    else:
        continue

    black_lower = np.array([0,0,0], np.uint8)
    black_upper = np.array([180, 255, 30], np.uint8)
    black = cv2.inRange(hsv, black_lower, black_upper)
    # Morphological Transform, Dilation

    kernal = np.ones((5, 5), "uint8")
    black = cv2.dilate(black, kernal)
    res_black = cv2.bitwise_and(img, img, mask=black)

    (_, contours, hierarchy) = cv2.findContours(black, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    cnts = sorted(contours, key=cv2.contourArea, reverse=True)[:1]  # get largest  contour area
    for pic, contour in enumerate(cnts):
        # print type(contour), type(img)
        # print contour.dtype, img.dtype
        area = cv2.contourArea(contour)
        if (area > 300):
            x, y, w, h = cv2.boundingRect(contour)


            img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 2)
            roi1 = clone1[y:y + h, x:x + w]
            cv2.imshow("roi1", roi1)
            cv2.putText(img, "Black Colour", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0))
            bbbbb = cv2.drawContours(img, [contour], -1, (0, 255, 0), 3)  # segmentation
            # roi2 = clone1[contour[[0]]]
            # cv2.imshow("roi2", roi2)
            # imaf = np.uint8(contour)
            # imaf = cv2.polylines(img, [contour], True, (0, 255, 0), 5)
            # print 'imaf', imaf.dtype, img.dtype

            # roi2 = cv2.drawContours(mask, contours, -1, (0, 255, 0), 1)
            mask = np.zeros(img.shape, np.uint8)
            # roi_corners = np.array([[(2,2), (150,50), (100,100), (2,100)]], dtype=np.int32)
            # roi_corners = np.array([[(x,y),(x + w,y),(x + w, y + h),(x, y + h)]], dtype=np.int32)
            roi_corners = np.array([contour], dtype=np.int32)
            channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
            ignore_mask_color = (255,) * channel_count
            cv2.fillPoly(mask, roi_corners, ignore_mask_color)
            # cv2.fillConvexPoly(mask, roi_corners, ignore_mask_color)
            masked_image = cv2.bitwise_and(img, mask)

            hsvRoi1 = cv2.cvtColor(masked_image, cv2.COLOR_BGR2HSV)
            black_lower = np.array(
                [hsvRoi1[:, :, 0], hsvRoi1[:, :, 1], hsvRoi1[:, :, 2]])
            black_upper = np.array(
                [hsvRoi1[:, :, 0], hsvRoi1[:, :, 1], hsvRoi1[:, :, 2]])

            h_values = np.array([hsvRoi1[:, :, 0]])
            h_min = h_values[(h_values >= 1)].min()
            h_max = h_values[(h_values <= 178)].max()

            s_values = np.array([hsvRoi1[:, :, 1]])
            s_min = s_values[(s_values >= 1)].min()
            s_max = s_values[(s_values <= 254)].max()

            v_values = np.array([hsvRoi1[:, :, 2]])
            v_min = v_values[(v_values >= 1)].min()
            v_max = v_values[(v_values <= 254)].max()

            black_lower = np.array([h_min, s_min, v_min], np.uint8)
            black_upper = np.array([h_max, s_max, v_max], np.uint8)


            print 'black_lower, black_upper',black_lower, black_upper
            cv2.imshow("roihsv", masked_image)

    cv2.imshow("Color Tracking", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break