Пример #1
0
def getContours(im, approx_value=1):  #Return contours approximated
    storage = cv.CreateMemStorage(0)
    contours = cv.FindContours(cv.CloneImage(im), storage, cv.CV_RETR_CCOMP,
                               cv.CV_CHAIN_APPROX_SIMPLE)
    contourLow = cv.ApproxPoly(contours, storage, cv.CV_POLY_APPROX_DP,
                               approx_value, approx_value)
    return contourLow
def find_leds(thresh_img):
    """
    Given a binary image showing the brightest pixels in an image, 
    returns a result image, displaying found leds in from PIL import Image
a rectangle
    """
    contours = cv2.FindContours(thresh_img,
                                cv2.CreateMemStorage(),
                                mode=cv2.CV_RETR_EXTERNAL,
                                method=cv2.CV_CHAIN_APPROX_NONE,
                                offset=(0, 0))

    regions = []
    while contours:
        pts = [pt for pt in contours]
        x, y = zip(*pts)
        min_x, min_y = min(x), min(y)
        width, height = max(x) - min_x + 1, max(y) - min_y + 1
        regions.append((min_x, min_y, width, height))
        contours = contours.h_next()

        out_img = cv2.CreateImage(cv2.GetSize(grey_img), 8, 3)
    for x, y, width, height in regions:
        pt1 = x, y
        pt2 = x + width, y + height
        color = (0, 0, 255, 0)
        cv2.Rectangle(out_img, pt1, pt2, color, 2)

    return out_img, regions
Пример #3
0
 def somethingHasMoved(self):
     # Find contours
     storage = cv.CreateMemStorage(0)
     contours = cv.FindContours(self.gray_frame, storage,
                                cv.CV_RETR_EXTERNAL,
                                cv.CV_CHAIN_APPROX_SIMPLE)
     self.currentcontours = contours  # Save contours
     while contours:  # For all contours compute the area
         self.currentsurface += cv.ContourArea(contours)
         contours = contours.h_next()
     avg = (
         self.currentsurface * 100
     ) / self.surface  # Calculate the average of contour area on the total size
     self.currentsurface = 0  # Put back the current surface to 0
     if avg > self.threshold:
         return True
     else:
         return False
Пример #4
0
def getPupil(frame):
	pupilImg = cv.CreateImage(cv.GetSize(frame), 8, 1)
	cv.InRangeS(frame, (30,30,30), (80,80,80), pupilImg)
	contours = cv.FindContours(pupilImg, cv.CreateMemStorage(0), mode = cv.CV_RETR_EXTERNAL)
	del pupilImg
	pupilImg = cv.CloneImage(frame)
	while contours:
		moments = cv.Moments(contours)
		area = cv.GetCentralMoment(moments,0,0)
		if (area > 50):
			pupilArea = area
			x = cv.GetSpatialMoment(moments,1,0)/area
			y = cv.GetSpatialMoment(moments,0,1)/area
			pupil = contours
			global centroid
			centroid = (int(x),int(y))
			cv.DrawContours(pupilImg, pupil, (0,0,0), (0,0,0), 2, cv.CV_FILLED)
			break
		contours = contours.h_next()
	return (pupilImg)
Пример #5
0
orig = cv.imread('lena.png')

#im = cv.CreateImage(cv.GetSize(orig), 8, 1)
im = cv.cvtColor(orig,cv.COLOR_BGR2GRAY)
#cv.CvtColor(orig, im, cv.CV_BGR2GRAY)
#Keep the original in colour to draw contours in the end

cv.Threshold(im, im, 128, 255, cv.CV_THRESH_BINARY)
cv.ShowImage("Threshold 1", im)

element = cv.CreateStructuringElementEx(5*2+1, 5*2+1, 5, 5, cv.CV_SHAPE_RECT)

cv.MorphologyEx(im, im, None, element, cv.CV_MOP_OPEN) #Open and close to make appear contours
cv.MorphologyEx(im, im, None, element, cv.CV_MOP_CLOSE)
cv.Threshold(im, im, 128, 255, cv.CV_THRESH_BINARY_INV)
cv.ShowImage("After MorphologyEx", im)
# --------------------------------

vals = cv.CloneImage(im) #Make a clone because FindContours can modify the image
contours=cv.FindContours(vals, cv.CreateMemStorage(0), cv.CV_RETR_LIST, cv.CV_CHAIN_APPROX_SIMPLE, (0,0))

_red = (0, 0, 255); #Red for external contours
_green = (0, 255, 0);# Gren internal contours
levels=2 #1 contours drawn, 2 internal contours as well, 3 ...
cv.DrawContours (orig, contours, _red, _green, levels, 2, cv.CV_FILLED) #Draw contours on the colour image

cv.ShowImage("Image", orig)
cv.WaitKey(0)

Пример #6
0
    def run(self):
        # Capture first frame to get size
        frame = cv.QueryFrame(self.capture)
        frame_size = cv.GetSize(frame)

        width = frame.width
        height = frame.height
        surface = width * height  #Surface area of the image
        cursurface = 0  #Hold the current surface that have changed

        grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1)
        moving_average = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3)
        difference = None

        while True:
            color_image = cv.QueryFrame(self.capture)

            cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3,
                      0)  #Remove false positives

            if not difference:  #For the first time put values in difference, temp and moving_average
                difference = cv.CloneImage(color_image)
                temp = cv.CloneImage(color_image)
                cv.ConvertScale(color_image, moving_average, 1.0, 0.0)
            else:
                cv.RunningAvg(color_image, moving_average, 0.020,
                              None)  #Compute the average

            # Convert the scale of the moving average.
            cv.ConvertScale(moving_average, temp, 1.0, 0.0)

            # Minus the current frame from the moving average.
            cv.AbsDiff(color_image, temp, difference)

            #Convert the image so that it can be thresholded
            cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY)
            cv.Threshold(grey_image, grey_image, 70, 255, cv.CV_THRESH_BINARY)

            cv.Dilate(grey_image, grey_image, None, 18)  #to get object blobs
            cv.Erode(grey_image, grey_image, None, 10)

            # Find contours
            storage = cv.CreateMemStorage(0)
            contours = cv.FindContours(grey_image, storage,
                                       cv.CV_RETR_EXTERNAL,
                                       cv.CV_CHAIN_APPROX_SIMPLE)

            backcontours = contours  #Save contours

            while contours:  #For all contours compute the area
                cursurface += cv.ContourArea(contours)
                contours = contours.h_next()

            avg = (
                cursurface * 100
            ) / surface  #Calculate the average of contour area on the total size
            if avg > self.ceil:
                print("Something is moving !")
            #print avg,"%"
            cursurface = 0  #Put back the current surface to 0

            #Draw the contours on the image
            _red = (0, 0, 255)
            #Red for external contours
            _green = (0, 255, 0)
            # Gren internal contours
            levels = 1  #1 contours drawn, 2 internal contours as well, 3 ...
            cv.DrawContours(color_image, backcontours, _red, _green, levels, 2,
                            cv.CV_FILLED)

            cv.ShowImage("Target", color_image)

            # Listen for ESC or ENTER key
            c = cv.WaitKey(7) % 0x100
            if c == 27 or c == 10:
                break
Пример #7
0
def showNaoImage(IP, PORT, camID):  # 参数分别为IP、PORT、摄像头ID(区分上下摄像头)

    #链接nao的摄像头
    camProxy = ALProxy("ALVideoDevice", IP, PORT)

    resolution = 2  # VGA``
    colorSpace = 11  # RGB
    videoClient = camProxy.subscribe("python_client", resolution, colorSpace,
                                     5)  # 设置分辨率、帧速、颜色空间

    t0 = time.time()
    camProxy.setParam(18, camID)  # 设置摄像头

    naoImage = camProxy.getImageRemote(videoClient)  #  将获取的图像赋给naoImage
    t1 = time.time()

    camProxy.unsubscribe(videoClient)
    imageWidth = naoImage[0]
    imageHeight = naoImage[1]
    imagechannls = naoImage[2]  # naoImage[6]为imagedata

    frameArray = numpy.frombuffer(naoImage[6], dtype=numpy.uint8).reshape(
        imageHeight, imageWidth, imagechannls)
    cimg = cv2.cvtColor(frameArray, cv2.COLOR_BGR2HSV)
    gray = cv2.cvtColor(frameArray, cv2.COLOR_BGR2GRAY)  # 转换为BGR图像
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)  # 霍夫降噪 平滑处理
    thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]  # 取得二值图
    # im_cv = numpy.zeros((imageHeight, imageWidth, 3), numpy.uint8)#初始化图像im_cv
    #
    # im_cv.data = array  #将从摄像头获取的图像copy到im_cv,转为mat
    #
    # # 转化颜色空间由BGR到RGB
    # b, g, r = cv2.split(im_cv)
    # img1 = cv2.merge([r, g, b])
    # # 转mat到cvmat
    # img3 = cv2.fromarray(img1)
    # cv2.SaveImage("test22.bmp",img3)
    # #转换颜色空间到HSV
    # imgHSV = cv2.CreateImage(cv2.GetSize(img3), 8, 3)
    # cv2.CvtColor(img3, imgHSV, cv2.CV_RGB2HSV)
    #
    # cimg,cimg_c=hsvProceed(imgHSV,camID) # 调用hsvProceed处理图像,返回二值图
    #圈取最小矩形框
    #初始化
    storage = cv2.CreateMemStorage(0)
    cnts = cv2.FindContours(thresh, storage, cv2.RETR_LIST,
                            cv2.CHAIN_APPROX_SIMPLE)
    currtnt = cnts
    Area = 0
    left_right = 0
    up_down = 0
    #为不同摄像头设置不同筛选条件
    if camID == 0:
        areamax = 2500
        areamin = 40
        valuemin = 25
        value_w = 641
        valuemax = 481
    else:
        areamax = 5000
        areamin = 400
        valuemin = 0
        value_w = 500
        valuemax = 400

    while cnts:
        rect = cv2.BoundingRect(cnts, 0)  #获得单连通矩形框
        area = rect[2] * rect[3]  #获得矩形框面积
        #获得矩形框中心点坐标
        rect_center_x = rect[0] + rect[2] / 2
        rect_center_y = rect[1] + rect[3] / 2
        #调用choose0文件下的radio函数,筛选圆形部分
        # radio_c = choose0.radio(cimg_c,rect)

        radio = float(rect[2]) / rect[3]  #计算矩形框的长宽比
        #以下if语句均为筛选条件
        if rect[1] >= valuemin:
            if rect[1] <= valuemax:
                if rect[0] <= value_w:
                    if area > areamin:
                        if area < areamax:
                            if radio > 0.6:
                                if radio < 1.6:
                                    # if radio_c == 1:
                                    cv2.DrawContours(frameArray, cnts,
                                                     (255, 255, 0),
                                                     (255, 255, 0), 0,
                                                     1)  #画出单连通轮廓
                                    cv2.Rectangle(
                                        frameArray, (rect[0], rect[1]),
                                        (rect[0] + rect[2], rect[1] + rect[3]),
                                        (0, 0, 255), 1)  #画出矩形框

                                    rect_center_x = rect[0] + rect[2] / 2
                                    rect_center_y = rect[1] + rect[3] / 2
                                    #计算通过条件的矩形框的面积以及在图像中的位置
                                    Area = rect[2] * rect[3]
                                    left_right = rect_center_x - cimg.width / 2
                                    up_down = rect_center_y - cimg.height / 2

        cnts = cnts.h_next()

    return Area, left_right, up_down  #返回球的面积以及在图像中的位置
    def process_image(self, slider_pos):
        global cimg, source_image1, ellipse_size, maxf, maxs, eoc, lastcx,lastcy,lastr
        """
        This function finds contours, draws them and their approximation by ellipses.
        """
        stor = cv.CreateMemStorage()

        # Create the destination images
        cimg = cv.CloneImage(self.source_image)
        cv.Zero(cimg)
        image02 = cv.CloneImage(self.source_image)
        cv.Zero(image02)
        image04 = cv.CreateImage(cv.GetSize(self.source_image), cv.IPL_DEPTH_8U, 3)
        cv.Zero(image04)

        # Threshold the source image. This needful for cv.FindContours().
        cv.Threshold(self.source_image, image02, slider_pos, 255, cv.CV_THRESH_BINARY)

        # Find all contours.
        cont = cv.FindContours(image02,
            stor,
            cv.CV_RETR_LIST,
            cv.CV_CHAIN_APPROX_NONE,
            (0, 0))

        maxf = 0
        maxs = 0
        size1 = 0

        for c in contour_iterator(cont):
            if len(c) > ellipse_size:
                PointArray2D32f = cv.CreateMat(1, len(c), cv.CV_32FC2)
                for (i, (x, y)) in enumerate(c):
                    PointArray2D32f[0, i] = (x, y)


                # Draw the current contour in gray
                gray = cv.CV_RGB(100, 100, 100)
                cv.DrawContours(image04, c, gray, gray,0,1,8,(0,0))

                if iter == 0:
                    strng = segF + '/' + 'contour1.png'
                    cv.SaveImage(strng,image04)
                color = (255,255,255)

                (center, size, angle) = cv.FitEllipse2(PointArray2D32f)

                # Convert ellipse data from float to integer representation.
                center = (cv.Round(center[0]), cv.Round(center[1]))
                size = (cv.Round(size[0] * 0.5), cv.Round(size[1] * 0.5))

                if iter == 1:
                    if size[0] > size[1]:
                        size2 = size[0]
                    else:
                        size2 = size[1]

                    if size2 > size1:
                        size1 = size2
                        size3 = size

                # Fits ellipse to current contour.
                if eoc == 0 and iter == 2:
                    rand_val = abs((lastr - ((size[0]+size[1])/2)))
                    if rand_val > 20 and float(max(size[0],size[1]))/float(min(size[0],size[1])) < 1.5:
                        lastcx = center[0]
                        lastcy = center[1]
                        lastr = (size[0]+size[1])/2

                    if rand_val > 20 and float(max(size[0],size[1]))/float(min(size[0],size[1])) < 1.4:
                        cv.Ellipse(cimg, center, size,
                                  angle, 0, 360,
                                  color,2, cv.CV_AA, 0)
                        cv.Ellipse(source_image1, center, size,
                                  angle, 0, 360,
                                  color,2, cv.CV_AA, 0)

                elif eoc == 1 and iter == 2:
                    (int,cntr,rad) = cv.MinEnclosingCircle(PointArray2D32f)
                    cntr = (cv.Round(cntr[0]), cv.Round(cntr[1]))
                    rad = (cv.Round(rad))
                    if maxf == 0 and maxs == 0:
                        cv.Circle(cimg, cntr, rad, color, 1, cv.CV_AA, shift=0)
                        cv.Circle(source_image1, cntr, rad, color, 2, cv.CV_AA, shift=0)
                        maxf = rad
                    elif (maxf > 0 and maxs == 0) and abs(rad - maxf) > 30:
                        cv.Circle(cimg, cntr, rad, color, 2, cv.CV_AA, shift=0)
                        cv.Circle(source_image1, cntr, rad, color, 2, cv.CV_AA, shift=0)
                        maxs = len(c)
        if iter == 1:
            temp3 = 2*abs(size3[1] - size3[0])
            if (temp3 > 40):
                eoc = 1
Пример #9
0
def landmarkdetect(IP, PORT, camID):

    camProxy = ALProxy("ALVideoDevice", IP, PORT)

    resolution = 2  # VGA``
    colorSpace = 11  # RGB
    videoClient = camProxy.subscribe("python_client", resolution, colorSpace,
                                     5)

    camProxy.setParam(18, camID)

    naoImage = camProxy.getImageRemote(videoClient)
    camProxy.unsubscribe(videoClient)
    imageWidth = naoImage[0]
    imageHeight = naoImage[1]

    array = naoImage[6]

    im_cv = numpy.zeros((imageHeight, imageWidth, 3), numpy.uint8)

    im_cv.data = array

    #im_cv = cv2.imread("img3.jpg",1)
    b, g, r = cv2.split(im_cv)
    img1 = cv2.merge([r, g, b])
    img3 = cv2.fromarray(img1)
    #img3=cv.LoadImage("img3.jpg",1)
    cv.SaveImage("save1.jpg", img3)
    imgHSV = cv.CreateImage(cv.GetSize(img3), 8, 3)
    cv.CvtColor(img3, imgHSV, cv.CV_RGB2HSV)

    cimg, cimg_c = hsvProceed(imgHSV)
    # cv.ShowImage("imgHSV", imgHSV)
    # cv.ShowImage("cimg", cimg)
    # cv.WaitKey(5)

    #
    # img3 = cv.LoadImage("save1.jpg",1)
    # imgHSV = cv.CreateImage(cv.GetSize(img3), 8, 3)
    #
    # cv.CvtColor(img3, imgHSV, cv.CV_RGB2HSV)
    # cimg, cimg_c=hsvProceed(imgHSV)
    # cv.ShowImage("s",cimg_c)

    storage = cv2.CreateMemStorage(0)
    cnts = cv.FindContours(cimg, storage, cv2.RETR_LIST,
                           cv2.CHAIN_APPROX_SIMPLE)
    currtnt = cnts
    x = 0.0
    Area = 0
    left_right = 0
    up_down = 0
    if camID == 0:
        areamax = 6000
        areamin = 350
        value = img3.height / 7
    else:
        areamax = 5000
        areamin = 400
        value = 0

    while cnts:
        rect = cv2.BoundingRect(cnts, 0)
        area = rect[2] * rect[3]
        rect_center_x = rect[0] + rect[2] / 2
        rect_center_y = rect[1] + rect[3] / 2
        # if camID == 1:
        #  radio_c = choose0.radio(cimg_c,rect)
        # if camID == 0:
        #  radio_c = choose0.choose02(cimg_c,rect)

        radio = float(rect[2]) / rect[3]
        if rect[1] > 10:

            if area > areamin:
                if area < areamax:
                    if radio > 0.1:
                        if radio < 1.0:
                            #if radio_c ==   1:

                            # cv2.cv.DrawContours(img3, cnts, (255, 255, 0), (255, 255, 0), 0, 1)
                            # cv2.cv.Rectangle(img3,(rect[0],rect[1]),(rect[0]+rect[2],rect[1]+rect[3]),(0,0,255),1)

                            #choose0.radio(cimg_c,rect)
                            rect_center_x = rect[0] + rect[2] / 2
                            rect_center_y = rect[1] + rect[3] / 2

                            Area = rect[2] * rect[3]
                            left_right = rect_center_x - cimg.width / 2
                            up_down = rect_center_y - cimg.height / 2
                            x, y = getloacation(rect[2], left_right)

        cnts = cnts.h_next()

    # cv2.cv.ShowImage("fsfs", img3)
    # cv.WaitKey(1)
    return Area, left_right, x
Пример #10
0
def main():
    original = cv2.namedWindow("Original", cv2.WINDOW_KEEPRATIO)
    hsv = cv2.namedWindow("HSV", cv2.WINDOW_KEEPRATIO)
    mask = cv2.namedWindow("Mask", cv2.WINDOW_KEEPRATIO)
    res1 = cv2.namedWindow("Avg1", cv2.WINDOW_KEEPRATIO)
    res2 = cv2.namedWindow("Avg2", cv2.WINDOW_KEEPRATIO)

    #res = cv2.namedWindow("Res", cv2.WINDOW_KEEPRATIO)

    vc = cv2.VideoCapture(0)
    cv2.moveWindow("Original", 0, 0)
    cv2.moveWindow("HSV", 300, 0)
    cv2.moveWindow("Mask", 700, 0)
    cv2.moveWindow("Avg1", 300, 300)

    cv2.moveWindow("Avg2", 700, 300)

    # create trackbars for color change
    cv2.createTrackbar('RL', 'HSV', 10, 255, nothing)
    cv2.createTrackbar('RU', 'HSV', 10, 255, nothing)
    cv2.createTrackbar('GL', 'HSV', 10, 255, nothing)
    cv2.createTrackbar('GU', 'HSV', 10, 255, nothing)
    cv2.createTrackbar('BL', 'HSV', 10, 255, nothing)
    cv2.createTrackbar('BU', 'HSV', 10, 255, nothing)

    # create switch for ON/OFF functionality
    #switch = '0 : OFF \n1 : ON'
    #cv2.createTrackbar(switch, 'Original',0,1,nothing)

    if vc.isOpened():  # try to get the first frame
        rval, frame = vc.read()
    else:
        rval = False

    avg1 = np.float32(frame)
    avg2 = np.float32(frame)
    while rval:
        # Convert BGR to HSV
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        h = hsv[0]
        s = hsv[1]
        v = hsv[2]
        print "H:", h, " S: ", s, "V: ", v
        cv2.accumulateWeighted(frame, avg1, 0.1)
        cv2.accumulateWeighted(frame, avg2, 0.01)
        res1 = cv2.convertScaleAbs(avg1)
        res2 = cv2.convertScaleAbs(avg2)

        cv2.FindContours("Original", )
        #set mouse callback to print out color values
        cv2.setMouseCallback("Original", onMouse)
        # get current positions of four trackbars
        rl = cv2.getTrackbarPos('RL', 'HSV')
        gl = cv2.getTrackbarPos('GL', 'HSV')
        bl = cv2.getTrackbarPos('BL', 'HSV')
        ru = cv2.getTrackbarPos('RU', 'HSV')
        gu = cv2.getTrackbarPos('GU', 'HSV')
        bu = cv2.getTrackbarPos('BU', 'HSV')
        #s = cv2.getTrackbarPos(switch,'HSV')

        # define range of blue color in HSV
        lower_red = np.array([rl, gl, bl])
        upper_red = np.array([ru, gu, bu])
        lower_blue = np.array([rl, gl, bl])
        upper_blue = np.array([ru, gu, bu])
        lower_green = np.array([rl, gl, bl])
        upper_green = np.array([ru, gu, bu])
        # Threshold the HSV image to get only blue colors
        mask = cv2.inRange(hsv, lower_green, upper_green)

        # Bitwise-AND mask and original image
        #res = cv2.bitwise_and(frame, frame, mask=mask)

        cv2.imshow("Original", frame)
        cv2.imshow("HSV", hsv)
        cv2.imshow("Mask", mask)
        cv2.imshow("Avg1", res1)
        cv2.imshow('Avg2', res2)
        #cv2.imshow("Res", res)

        rval, frame = vc.read()
        key = cv2.waitKey(20)
        if key == 27:  # exit on ESC
            break
    cv2.destroyAllWindows()