예제 #1
0
    def run(self):
        started = time.time()
        while True:

            currentframe = cv.QueryFrame(self.capture)
            instant = time.time()  #Get timestamp o the frame

            self.processImage(currentframe)  #Process the image

            if not self.isRecording:
                if self.somethingHasMoved():
                    self.trigger_time = instant  #Update the trigger_time
                    if instant > started + 10:  #Wait 5 second after the webcam start for luminosity adjusting etc..
                        print "Something is moving !"
                        if self.doRecord:  #set isRecording=True only if we record a video
                            self.isRecording = True
                cv.DrawContours(currentframe, self.currentcontours,
                                (0, 0, 255), (0, 255, 0), 1, 2, cv.CV_FILLED)
            else:
                if instant >= self.trigger_time + 10:  #Record during 10 seconds
                    print "Stop recording"
                    self.isRecording = False
                else:
                    cv.PutText(currentframe,
                               datetime.now().strftime("%b %d, %H:%M:%S"),
                               (25, 30), self.font, 0)  #Put date on the frame
                    cv.WriteFrame(self.writer, currentframe)  #Write the frame

            if self.show:
                cv.ShowImage("Image", currentframe)

            c = cv.WaitKey(1) % 0x100
            if c == 27 or c == 10:  #Break if user enters 'Esc'.
                break
예제 #2
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)
예제 #3
0
def function7():    
    # 7a  ##################################################################
    # 7b  ##################################################################
    # 7c  ##################################################################
                                #Draw contours around the blobs in the image    
    file1 = '/home/pi/seed/exercise2/part6/CombinednewBlurred.jpg'
    file1Contours = '/home/pi/seed/exercise2/part6/CombinednewBlurredCountours.jpg'
    im = cv2.imread(file1, 0)
    ########################################################################
    
    # Setup SimpleBlobDetector parameters.
    
    params = cv2.SimpleBlobDetector_Params()
    
    # Change thresholds
    params.minThreshold = 10;
    params.maxThreshold = 200;

    # Filter by Color
    params.filterByColor = True
    params.blobColor = 150
     
    # Filter by Area.
    params.filterByArea = True
    params.minArea = 50
    params.maxArea = 2000
     
    # Filter by Circularity
    params.filterByCircularity = False
    params.minCircularity = 0.1
     
    # Filter by Convexity
    params.filterByConvexity = False
    params.minConvexity = 0.17
     
    # Filter by Inertia
    params.filterByInertia = False
    params.minInertiaRatio = 0.01
     
    # Create a detector with the parameters
    ver = (cv2.__version__).split('.')
    if int(ver[0]) < 3 :
        detector = cv2.SimpleBlobDetector(params)
    else : 
        detector = cv2.SimpleBlobDetector_create(params)
    

    #detector = cv2.SimpleBlobDetector_create()

    external_color = [22, 255, 200]
    hole_color = 200
    max_level = 0
    keypoints = detector.detect(im)
    #im_with_contours = cv2.drawContours(im, file1Contours, external_color, hole_color,  max_level=0, thickness=1, lineType=8, offset=(0, 0))
    contours = cv.FindContours (color_mask, storage, method = cv.CV_CHAIN_APPROX_SIMPLE)
    cv2.DrawContours(img=im, contour=contours, external_color=cv.RGB(255, 0, 0), hole_color=cv.RGB(0, 255, 0), max_level=1 )
    
    #im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    cv2.imshow("Contours", im_with_contours)
    #cv2.imshow("Keypoints", im_with_keypoints)
    
    ###################################################################
    '''
    hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    # define range of purple color in HSV
    purpleMin = (115,50,10)
    purpleMax = (160, 255, 255)
    # Sets pixels to white if in purple range, else will be set to black
    mask = cv2.inRange(hsv, purpleMin, purpleMax)
        
    # Bitwise-AND of mask and purple only image - only used for display
    res = cv2.bitwise_and(frame, frame, mask= mask)

    # Set up the SimpleBlobdetector with default parameters.
    params = cv2.SimpleBlobDetector_Params()
     
    # Change thresholds
    params.minThreshold = 0;
    params.maxThreshold = 256;
     
    # Filter by Area.
    params.filterByArea = True
    params.minArea = 30
     
    # Filter by Circularity
    params.filterByCircularity = True
    params.minCircularity = 0.1
     
    # Filter by Convexity
    params.filterByConvexity = True
    params.minConvexity = 0.5
     
    # Filter by Inertia
    params.filterByInertia =True
    params.minInertiaRatio = 0.5
     
    detector = cv2.SimpleBlobDetector_create(params)
 
    # Detect blobs.
    reversemask=255-mask
    keypoints = detector.detect(reversemask)

    cv2.imshow("Blob", keypoints)
    '''
    
    cv2.waitKey(0)
예제 #4
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)

예제 #5
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
예제 #6
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
예제 #8
0
if __name__ == "__main__":
    orig = cv.LoadImage(
        r"C:\git\Python-Snippets\Image Recognition\images\D2C-Logins - RunDate 2019-10-03 - Part (22).image"
    )
    #Convert in black and white
    res = cv.CreateImage(cv.GetSize(orig), 8, 1)
    cv.CvtColor(orig, res, cv.CV_BGR2GRAY)

    #Operations on the image
    openCloseImage(res)
    dilateImage(res, 2)
    erodeImage(res, 2)
    smoothImage(res, 5)
    thresholdImage(res, 150, cv.CV_THRESH_BINARY_INV)

    #Get contours approximated
    contourLow = getContours(res, 3)

    #Draw them on an empty image
    final = cv.CreateImage(cv.GetSize(res), 8, 1)
    cv.Zero(final)
    cv.DrawContours(final, contourLow, cv.Scalar(255), cv.Scalar(255), 2,
                    cv.CV_FILLED)

    cv.ShowImage("orig", orig)
    cv.ShowImage("image", res)
    cv.SaveImage("modified.png", res)
    cv.ShowImage("contour", final)
    cv.SaveImage("contour.png", final)

    cv.WaitKey(0)