在边缘检测方面需要帮助

时间:2019-05-12 15:33:15

标签: python image opencv3.0 convex-hull hsv

我正在尝试使用HSV方法检测从RGB相机获取实时照片的限制。

我找到了范围颜色,并尝试使用凸包,但它分别标记了每个部分。

首先,我将其转换为HSV照片:

import numpy as np

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from skimage import data
from skimage.color import rgb2hsv
from skimage.color import hsv2rgb

RGB= mpimg.imread('/home/komodo/Yasmin_new/123.jpg')
NIHSV= rgb2hsv(RGB)
RGBHSV= rgb2hsv(RGB)
h = RGBHSV[:,:,0]
s= RGBHSV[:,:,1]
v = RGBHSV[:,:,2]
m = len(h)
n = len(h[0])
for i in range(len(h)):
    for j in range(len(h[0])):
        if h[i,j]<=0.13 and s[i,j]> 0.18 and v[i,j]>0.8:
            h[i,j] = 1
            s[i, j] = 1
            v[i, j] = 1

#plt.figure()
#imgplot=plt.imshow(h)
#plt.figure()
#imgplot1=plt.imshow(s)
#plt.figure()
#imgplot2=plt.imshow(v)

NIHSV[:,:,0] = h
NIHSV[:,:,1] = s
NIHSV[:,:,2] = v

H = hsv2rgb(NIHSV)
plt.figure()
imgplot=plt.imshow(H)
plt.show(imgplot)

这是我从这段代码中得到的: image 1

然后,我尝试使用凸包:

# USAGE
# python detect_shapes.py --image shapes_and_colors.png
import sys
# import the necessary packages
# from pyimagesearch.shapedetector import ShapeDetector
# import argparse
# import imutils
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import numpy as np
import cv2
import time
import socket
import time
import struct

cap = cv2.VideoCapture(0)
while True:
    _, frame = cap.read()
    blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0)
    hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)
    lower_red = np.array([0, 46, 204])
    upper_red = np.array([23, 255, 255])
    mask = cv2.inRange(hsv, lower_red, upper_red)
    # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # convert to grayscale
    # blur = cv2.blur(mask, (3, 3))  # blur the image
    ret, thresh = cv2.threshold(mask, 50, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # create hull array for convex hull points
    hull = []

    # calculate points for each contour
    for i in range(len(contours)):
        # creating convex hull object for each contour
        hull.append(cv2.convexHull(contours[i], False))
    # create an empty black image
    drawing = np.zeros((thresh.shape[0], thresh.shape[1], 3), np.uint8)

    # draw contours and hull points
    for i in range(len(contours)):
        color_contours = (0, 255, 0)  # green - color for contours
        color = (255, 0, 0)  # blue - color for convex hull
        # draw ith contour
        cv2.drawContours(drawing, contours, i, color_contours, 1, 8, hierarchy)
        # draw ith convex hull object
        cv2.drawContours(drawing, hull, i, color, 1, 8)
    cv2.imshow("Frame", frame)
    cv2.imshow("Mask", drawing)
    key = cv2.waitKey(1)
    time.sleep(0.5)
    if key == 27:
        break
cap.release()
cv2.destroyAllWindows()

这是我从这段代码中得到的:

image 2

我想要得到什么:

image 3

0 个答案:

没有答案