如何改进我的图像处理校准?

时间:2018-04-04 16:58:25

标签: python opencv image-processing

我想添加一个带轨迹栏的功能进行校准,以便检测特定(或希望的)形状。跟踪条应校准HSV和阈值。

这是我的代码,但它只检测完美照明中的完美形状,并且如果光线不完美则无法识别任何内容。这需要安装在无人机上,因此这将捕获近100英尺的视频,并且应该能够检测形状并捕获图像。

# import the necessary packages
import argparse
import cv2
import time
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
args = vars(ap.parse_args())

def nothing(x):
    print x
    pass

count=0                         #Initializing count to 0, which will be used for naming the captured frame.

# load the video
camera = cv2.VideoCapture(args["video"])
#camera=cv2.VideoCapture(0)                             #For live feed from External Camera
# keep looping
while True:
    # grab the current frame and initialize the status text
    (grabbed, frame) = camera.read()
    status = "No Targets/Waypoint"

    # check to see if we have reached the end of the
    # video
    if not grabbed:
        break

    # convert the frame to grayscale, blur it, and detect edges
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (7, 7), 0)
    edged = cv2.Canny(blurred, 50, 150)

    # find contours in the edge map
    _, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)

    # loop over the contours
    for c in contours:
        # approximate the contour
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.01 * peri, True)

        # ensure that the approximated contour is "roughly" rectangular
        if len(approx) >= 3 and len(approx) <= 6:
            # compute the bounding box of the approximated contour and
            # use the bounding box to compute the aspect ratio
            (x, y, w, h) = cv2.boundingRect(approx)
            aspectRatio = w / float(h)

            # compute the solidity of the original contour
            area = cv2.contourArea(c)
            hullArea = cv2.contourArea(cv2.convexHull(c))
            solidity = area / float(hullArea)

            # compute whether or not the width and height, solidity, and
            # aspect ratio of the contour falls within appropriate bounds
            keepDims = w > 25 and h > 25
            keepSolidity = solidity > 0.9
            keepAspectRatio = aspectRatio >= 0.8 and aspectRatio <= 1.2

            # ensure that the contour passes all our tests
            if keepDims and keepSolidity and keepAspectRatio:
                # draw an outline around the target and update the status
                # text
                cv2.drawContours(frame, [approx], -1, (0, 0, 255), 4)
                status = "Target(s)/Waypoint(s) Acquired"

                # compute the center of the contour region and draw the
                # crosshairs
                time.sleep(0.04)
                M = cv2.moments(approx)
                (cX, cY) = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
                (startX, endX) = (int(cX - (w * 0.15)), int(cX + (w * 0.15)))
                (startY, endY) = (int(cY - (h * 0.15)), int(cY + (h * 0.15)))
                cv2.line(frame, (startX, cY), (endX, cY), (0, 0, 255), 3)
                cv2.line(frame, (cX, startY), (cX, endY), (0, 0, 255), 3)
                success,image = camera.read()                   ########################################################
                print 'Read a new frame: ', success
                cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
                count += 1  
                time.sleep(1.5)                                             ###############################
    # draw the status text on the frame
    cv2.putText(frame, status, (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
        (0, 0, 255), 2)

    # show the frame and record if a key is pressed
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
        break

# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()

我可以使用哪些图像处理功能来校准HSV和阈值,并保存我们图像所需阈值的值?

例如,在这张图片中,由于光线良好,可以检测到pysquares(黑色),但是如果照明不像这样,这段代码找不到轮廓,我想要一个校准功能,我们在其中可以校准HSV值和阈值,使程序理解所需的数字。

Image

0 个答案:

没有答案