예제 #1
0
def get_hand(img):
    global h_lower, h_upper, s_lower, s_upper
    storage = cv.CreateMemStorage(0)
    contours, hull, max_contours, max_hull = (0, 0, 0, 0)
    max_rect = (1, 1, 100, 100)
    dst = cv.CreateImage((img.width, img.height), 8, 3)
    hsv = cv.CreateImage((img.width, img.height), 8, 3)
    frame = cv.CreateImage((img.width, img.height), 8, 1)
    con = cv.CreateImage((img.width, img.height), 8, 1)
    cv.Zero(con)
    cv.Smooth(img, dst, cv.CV_GAUSSIAN, 5, 5)
    for i in range(3): cv.Smooth(dst, dst, cv.CV_GAUSSIAN, 5, 5)
    cv.CvtColor(dst, hsv, cv.CV_RGB2HSV)
    cv.InRangeS(hsv, (h_lower, s_lower, 0), (h_upper, s_upper, 256), frame) 
    kernel = cv.CreateStructuringElementEx(3, 3, 0, 0, cv.CV_SHAPE_RECT)
    #cv.MorphologyEx(frame, frame, None, kernel, cv.CV_MOP_CLOSE , 7)
#    cv.MorphologyEx(frame, frame, None, kernel, cv.CV_MOP_OPEN , 3)
    #contours = im.find_contours(frame)
    #hull = im.find_convex_hull(contours)
    print contours

    #max_hull_area, max_contour_area = (0, 0)
    #print "xxxxxxxx"
    #contour = contours.h_next()
    #print "........"
    #while (contour != 0):
    #    hull = cv.ConvexHull2(contour, storage, cv.CV_CLOCKWISE, 1);
    #    maxv = cv.ContourArea(hull)
    #    contour = contour.h_next()
    #cv.DrawContours(con, contours, red, blue, 1, 3, 8)
    

    cv.ShowImage("result", con)
예제 #2
0
    def detectSkin(self, bgrimg):
        img_temp = cv.CreateImage(im.size(bgrimg), 8, 3)
        cv.Smooth(bgrimg, img_temp, cv.CV_GAUSSIAN, 5, 5)
        for i in range(3):
            cv.Smooth(img_temp, img_temp, cv.CV_GAUSSIAN, 5, 5)
        #cv.ShowImage("Capture from camera", img_temp)

        skin = self._detectSkin(img_temp)
        return skin
예제 #3
0
def get_diff(old, new):
    """ Returns the difference between two BGR images.
    """
    size = cv.GetSize(old)
    diff = cv.CreateImage(size, 8, 1)
    old_grayscale = cv.CreateImage(size, 8, 1)
    new_grayscale = cv.CreateImage(size, 8, 1)
    cv.CvtColor(old, old_grayscale, cv.CV_BGR2GRAY)
    cv.CvtColor(new, new_grayscale, cv.CV_BGR2GRAY)
    cv.AbsDiff(old_grayscale, new_grayscale, diff)
    cv.Smooth(diff, diff, smoothtype=cv.CV_GAUSSIAN, param1=3, param2=3)
    cv.Threshold(diff, diff, 16, 255, cv.CV_THRESH_BINARY)
    cv.Smooth(diff, diff, smoothtype=cv.CV_GAUSSIAN, param1=13, param2=13)
    cv.Threshold(diff, diff, 200, 255, cv.CV_THRESH_BINARY)
    return diff
예제 #4
0
def read_image_canny(filename):
    img = cv.LoadImage(filename, 1)
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    edge = cv.CreateImage((img.width, img.height), 8, 1)
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
    cv.Smooth(gray, edge, cv.CV_BLUR, 3, 3, 0)
    #cv.Not(gray, edge)

    # run the edge dector on gray scale
    cv.Canny(gray, edge, 80, 160, 3)
    cv.Not(edge, edge)
    cv.Smooth(edge, edge, cv.CV_GAUSSIAN, 3, 3, 0)

    a_img = cv2array(edge)
    return a_img.flatten(1)
예제 #5
0
def run(im, headInfo):
    (cam, head) = headInfo
    # convert the image
    im = convertImage(im)
    #cv.SaveImage('a.jpg', im)
    # filter the image
    im = filterImage(im)
    #cv.SaveImage('b.jpg', im)
    # blur the image
    cv.Smooth(im, im, cv.CV_BLUR, 5, 5)
    #cv.SaveImage('c.jpg', im)
    # find the max value in the image
    (minVal, maxValue, minLoc, maxLocation) = cv.MinMaxLoc(im)
    #print maxValue/256.0

    # if the maxValue isn't hight enough return 'None'
    if maxValue / 256.0 < 0.4:
        return None

    # calculate the angles to the ball
    #(xAngle, yAngle) = calcAngles((xcoord, ycoord ), cam)
    # calculate the position of the ball
    position = calcPosition(maxLocation, cam)
    (xPos, yPos, xAngle, yAngle) = position

    #print position
    #track(position)
    return (xPos, yPos)
예제 #6
0
    def findRectPoints(self, oldRectPoints):
        hueRange = self.hueRange
        satRange = self.satRange
        valRange = self.valRange
        clone = cv.CloneImage(self.frame)
        hsv = cv.CloneImage(self.channels3)
        threshold = cv.CloneImage(self.channels1)
        threshold2 = cv.CloneImage(self.channels1)

        cv.Smooth(clone, clone, cv.CV_GAUSSIAN, 7, 7)

        cv.CvtColor(clone, hsv, cv.CV_BGR2HSV)
        cv.InRangeS(hsv, (hueRange[0], satRange[0], valRange[0]),
                    (hueRange[1], satRange[1], satRange[1]), threshold)
        cv.InRangeS(hsv, (hueRange[2], satRange[0], satRange[0]),
                    (hueRange[3], satRange[1], valRange[1]), threshold2)
        cv.Add(threshold, threshold2, threshold)
        cv.Erode(threshold, threshold, iterations=5)
        cv.Dilate(threshold, threshold, iterations=5)

        #       cv.ShowImage(self.color, threshold)

        memory = cv.CreateMemStorage(0)
        clone2 = cv.CloneImage(threshold)
        contours = cv.FindContours(clone2, memory, cv.CV_RETR_LIST,
                                   cv.CV_CHAIN_APPROX_SIMPLE, (0, 0))
        if not contours:
            rectPoints = oldRectPoints
        else:
            rectPoints = cv.BoundingRect(list(contours))
        return rectPoints
예제 #7
0
파일: sobel.py 프로젝트: wolfram2012/MOSSE
def sobel(im, xorder=1, yorder=0, aperture_size=3, sigma=None):
    '''
    void cv.Sobel(src, dst, xorder, yorder, apertureSize = 3) 
    @param im: Input image
    @param xorder: The order of the x derivative (see cv.Sobel openCV docs) 
    @param yorder: The order of the y derivative (see cv.Sobel openCV docs)
    @param aperture_size: How large a convolution window to use
    @param sigma: Optional smoothing parameter to be applied prior to detecting edges
    '''
    gray = im.asOpenCVBW()
    edges = cv.CreateImage(cv.GetSize(gray), 8, 1)

    if sigma != None:
        cv.Smooth(gray, gray, cv.CV_GAUSSIAN,
                  int(sigma) * 4 + 1,
                  int(sigma) * 4 + 1, sigma, sigma)

    #sobel requires a destination image with larger bit depth...
    #...so we have to convert it back to 8 bit for the pv Image...
    dst32f = cv.CreateImage(cv.GetSize(gray), cv.IPL_DEPTH_32F, 1)

    cv.Sobel(gray, dst32f, xorder, yorder, aperture_size)
    cv.Convert(dst32f, edges)
    edges = pv.Image(edges)

    return edges
예제 #8
0
	def CountPinkPixels(self,cv_img):
                cv.Smooth(cv_img, cv_img, cv.CV_BLUR, 3);

	        hsv_img = cv.CreateImage(cv.GetSize(cv_img), 8, 3)
	        cv.CvtColor(cv_img, hsv_img, cv.CV_BGR2HSV)

	        #limit all pixels that don't match our criteria, in this case we are  
        	#looking for purple but if you want you can adjust the first value in  
            	#both turples which is the hue range(120,140).  OpenCV uses 0-180 as  
            	#a hue range for the HSV color model 
            	thresholded_img =  cv.CreateImage(cv.GetSize(hsv_img), 8, 1)
            	cv.InRangeS(hsv_img, (120, 80, 80), (180, 255, 255), thresholded_img)
		#print type(thresholded_img)
		mat = cv.GetMat(thresholded_img)
		#mat = hsv_img.getNumpy()
		(width,height) = cv.GetSize(thresholded_img)
		aveW = 0
		aveH = 0
		count = 0
		for j in range(0,height):			
			for i in range(0,width):
				if (mat[j,i] == 255.0):
					count += 1
					aveH += j 
					aveW += i
					print i,j, mat[j,i]
		
		aveH = aveH/count
		aveW = aveW/count
		
		overlay = cv.CreateImage(cv.GetSize(cv_img),8,3)
		cv.Circle(overlay, (int(aveW), int(aveH)), 2, (255,255,255),20)
		cv.Add(cv_img,overlay,cv_img)
		return cv_img
    def getHsvRange(self):
        self.x_co = 0
        self.y_co = 0

        cv.NamedWindow('camera feed', cv.CV_WINDOW_AUTOSIZE)
        capture = cv.CaptureFromCAM(1)

        font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 0.5, 1, 0, 2, 8)

        while True:
            src = cv.QueryFrame(capture)
            #            src = cv.LoadImage('2012_automata.jpg')
            cv.Smooth(src, src, cv.CV_BLUR, 3)
            hsv = cv.CreateImage(cv.GetSize(src), src.depth, 3)
            cv.CvtColor(src, hsv, cv.CV_BGR2HSV)
            cv.SetMouseCallback("camera feed", self.on_mouse, 0)
            s = cv.Get2D(hsv, self.y_co, self.x_co)
            #        print "H:", s[0], "      S:", s[1], "       V:", s[2]
            cv.PutText(src,
                       str(s[0]) + "," + str(s[1]) + "," + str(s[2]),
                       (self.x_co, self.y_co), font, (55, 25, 255))
            cv.ShowImage("camera feed", src)
            if cv.WaitKey(10) == 27:
                return (s[0], s[1], s[2])
                break
def getIris(frame):
    iris = []
    copyImg = cv.CloneImage(frame)
    resImg = cv.CloneImage(frame)
    grayImg = cv.CreateImage(cv.GetSize(frame), 8, 1)
    mask = cv.CreateImage(cv.GetSize(frame), 8, 1)
    storage = cv.CreateMat(frame.width, 1, cv.CV_32FC3)
    cv.CvtColor(frame, grayImg, cv.CV_BGR2GRAY)
    cv.Canny(grayImg, grayImg, 5, 70, 3)
    cv.Smooth(grayImg, grayImg, cv.CV_GAUSSIAN, 7, 7)
    circles = getCircles(grayImg)
    iris.append(resImg)
    for circle in circles:
        rad = int(circle[0][2])
        global radius
        radius = rad
        cv.Circle(mask, centroid, rad, cv.CV_RGB(255, 255, 255), cv.CV_FILLED)
        cv.Not(mask, mask)
        cv.Sub(frame, copyImg, resImg, mask)
        x = int(centroid[0] - rad)
        y = int(centroid[1] - rad)
        w = int(rad * 2)
        h = w
        cv.SetImageROI(resImg, (x, y, w, h))
        cropImg = cv.CreateImage((w, h), 8, 3)
        cv.Copy(resImg, cropImg)
        cv.ResetImageROI(resImg)
        return (cropImg)
    return (resImg)
예제 #11
0
파일: main.py 프로젝트: micmax93/RpiTurret
 def captureReferenceImage(self):
     frame = self.getFrame()
     self.frameSize = cv.GetSize(frame)
     self.referencedImage = cv.CloneImage(frame)
     cv.Smooth(self.referencedImage, self.referencedImage, cv.CV_GAUSSIAN,
               9, 0)
     self.lastReferenceCaptureTime = TimeUtil.getCurrentTime()
예제 #12
0
    def process(self):

        def seq_to_iter(seq):
            while seq:
                yield seq
                seq = seq.h_next()

        def score_rect(r,p):
            x,y,w,h = r
            return w * h

        cv.Resize(self.frame, self.resized_frame)
        cv.CvtColor(self.resized_frame, self.hsv_frame, cv.CV_RGB2HSV)
        cv.Smooth(self.hsv_frame, self.smooth_frame, cv.CV_GAUSSIAN,
                31)

        for p in self.pompon:
            if p.calibration_done:

                self.in_range(p, self.smooth_frame, self.bin_frame)
                cv.Erode(self.bin_frame, self.mask, None, self.dilatation);

                if self.show_binary:
                    self.mask2 = cv.CloneImage(self.mask) # for miniature

                contour = seq_to_iter(cv.FindContours(self.mask,
                        cv.CreateMemStorage(), cv.CV_RETR_EXTERNAL,
                        cv.CV_CHAIN_APPROX_SIMPLE));

                rects = map((lambda c: cv.BoundingRect(c, 0)), contour)
                if rects:
                    x,y,w,h = max(rects, key=lambda r: score_rect(r,p))

                    p.pos = self.proc2sym(x+w/2,y+h/2)
예제 #13
0
파일: threshold.py 프로젝트: sdp-2011/sdp-9
    def threshold(self, frame, record, op=cv.And, magic=False):
        """Threshold a frame using a record of min/max thresholds

        Output is a new image.
        """
        colorspace, min, max = record

        tmp = cv.CloneImage(frame)
        if magic:
            cv.Smooth(tmp, tmp, cv.CV_GAUSSIAN, 11)
        # Work in the correct colorspace
        self.colorspaceConv[colorspace](tmp)

        num_chans = len(min)
        size = cv.GetSize(frame)
        chan = [
            cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) for _ in range(num_chans)
        ]
        cv.Split(tmp, chan[0], chan[1], chan[2], None)

        minS = map(cv.Scalar, min)
        maxS = map(cv.Scalar, max)
        for i in range(num_chans):
            cv.InRangeS(chan[i], minS[i], maxS[i], chan[i])

        out = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
        op(chan[0], chan[1], out)
        op(out, chan[2], out)

        return out
예제 #14
0
def hsv_orange_red_threshold(input_image):
    blur_image = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC3)
    cv.Smooth(input_image, blur_image, cv.CV_BLUR, 10, 10)
    proc_image = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC3)
    cv.CvtColor(blur_image, proc_image, cv.CV_BGR2HSV)
    split_image = [
        cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1),
        cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1),
        cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1)
    ]
    cv.Split(proc_image, split_image[0], split_image[1], split_image[2], None)

    thresh_0 = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1)
    thresh_1 = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1)
    thresh_2 = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1)
    red_orange = cv.CreateMat(input_image.rows, input_image.cols, cv.CV_8UC1)
    cv.Threshold(split_image[1], thresh_0, 128, 255,
                 cv.CV_THRESH_BINARY)  # > 50% saturation
    cv.Threshold(split_image[0], thresh_1, 220, 255,
                 cv.CV_THRESH_BINARY)  # > Purple
    cv.Threshold(split_image[0], thresh_2, 10, 255,
                 cv.CV_THRESH_BINARY_INV)  # < Yellow-Orange
    cv.Add(thresh_1, thresh_2, red_orange)
    cv.And(red_orange, thresh_0, red_orange)

    return red_orange
def find_matches(imgpaths, patch, C=0.8):
    """ Runs template matching to find PATCH in each IMGPATHS.
    Input:
        list imgpaths: [imgpath_i, ...]
        IplImage patch: 
        float C:
    Output:
        list matches, [[imgpath_i, (x,y), score_i], ...]
    """
    matches = {}  # maps {str imgpath: [(x,y,score_i), ...]}
    for imgpath in imgpaths:
        img = cv.LoadImage(imgpath, cv.CV_LOAD_IMAGE_GRAYSCALE)
        img_smooth = cv.CreateImage((img.width, img.height), img.depth,
                                    img.channels)
        cv.Smooth(img, img_smooth, cv.CV_GAUSSIAN, param1=17, param2=17)
        M = cv.CreateMat(img.height - patch.height + 1,
                         img.width - patch.width + 1, cv.CV_32F)
        cv.MatchTemplate(img_smooth, patch, M, cv.CV_TM_CCOEFF_NORMED)
        M_np = np.array(M)
        score = np.inf
        while score > C:
            M_idx = np.argmax(M_np)
            i = int(M_idx / M.cols)
            j = M_idx % M.cols
            score = M_np[i, j]
            if score < C:
                break
            matches.setdefault(imgpath, []).append((j, i, score))
            # Suppression
            M_np[i - (patch.height / 3):i + (patch.height / 3),
                 j - (patch.width / 3):j + (patch.width / 3)] = -1.0
    return matches
예제 #16
0
def findFirstColorPattern(img, pattern):
    """
        try to test if one pixel is in our pattern
    """
    channels = [None, None, None]
    channels[0] = cv.CreateImage(cv.GetSize(img), 8, 1)  #blue
    channels[1] = cv.CreateImage(cv.GetSize(img), 8, 1)  #green
    channels[2] = cv.CreateImage(cv.GetSize(img), 8, 1)  #red
    ch0 = cv.CreateImage(cv.GetSize(img), 8, 1)  #blue
    ch1 = cv.CreateImage(cv.GetSize(img), 8, 1)  #green
    ch2 = cv.CreateImage(cv.GetSize(img), 8, 1)  #red
    cv.Split(img, ch0, ch1, ch2, None)
    dest0 = cv.CreateImage(cv.GetSize(img), 8, 1)
    dest1 = cv.CreateImage(cv.GetSize(img), 8, 1)
    dest2 = cv.CreateImage(cv.GetSize(img), 8, 1)
    dest3 = cv.CreateImage(cv.GetSize(img), 8, 1)
    cv.Smooth(ch0, channels[0], cv.CV_GAUSSIAN, 3, 3, 0)
    cv.Smooth(ch1, channels[1], cv.CV_GAUSSIAN, 3, 3, 0)
    cv.Smooth(ch2, channels[2], cv.CV_GAUSSIAN, 3, 3, 0)
    result = []
    for i in range(3):
        lower = pattern[i][2] - 25
        upper = pattern[i][2] + 25
        cv.InRangeS(channels[0], lower, upper, dest0)
        lower = pattern[i][1] - 25
        upper = pattern[i][1] + 25
        cv.InRangeS(channels[1], lower, upper, dest1)
        lower = pattern[i][0] - 25
        upper = pattern[i][0] + 25
        cv.InRangeS(channels[2], lower, upper, dest2)
        cv.And(dest0, dest1, dest3)
        temp = cv.CreateImage(cv.GetSize(img), 8, 1)
        cv.And(dest2, dest3, temp)
        result.append(temp)

    cv.ShowImage("result0", result[0])
    cv.WaitKey(0)
    cv.ShowImage("result1", result[1])
    cv.WaitKey(0)
    cv.ShowImage("result2", result[2])
    cv.WaitKey(0)
    cv.Or(result[0], result[1], dest0)
    cv.Or(dest0, result[2], dest3)
    cv.NamedWindow("result", cv.CV_WINDOW_AUTOSIZE)
    cv.ShowImage("result", dest3)
    cv.WaitKey(0)
    return dest3
예제 #17
0
def glyphRec(image):

    storage = cv.CreateMemStorage(0)

    contrast = cv.CreateImage(cv.GetSize(image), 8, 3)
    grey = cv.CreateImage(cv.GetSize(image), 8, 1)
    canny = cv.CreateImage(cv.GetSize(grey), cv.IPL_DEPTH_8U, 1)

    #increase contrast
    avg = cv.Avg(image)
    cv.AddS(image, cv.Scalar(-.5 * avg[0], -.5 * avg[1], -.5 * avg[2]),
            contrast)
    cv.Scale(contrast, contrast, 3)

    #make grayscale
    cv.CvtColor(contrast, grey, cv.CV_BGR2GRAY)

    #smooth
    cv.Smooth(grey, grey, cv.CV_GAUSSIAN, 3, 3)

    #edge detect
    cv.Canny(grey, canny, 20, 200, 3)

    #smooth again
    cv.Smooth(canny, canny, cv.CV_GAUSSIAN, 3, 3)

    #find lines
    lines = cv.HoughLines2(canny, storage, cv.CV_HOUGH_PROBABILISTIC, 3,
                           math.pi / 180, 50, 150, 40)

    #find corners
    corners = getCorners(lines)

    #find quadrilaterals
    quad = findGlpyh(contrast, corners)

    if quad == None:
        return None

    drawQuad(image, quad)

    grid = readGlyph(image, quad)
    printGrid(grid)
    print ''
    toCoords(grid)

    return grid
예제 #18
0
    def process_image(self, image, texture_type):
        spare = image
        #return image
        # get the size of the current image
        size = (image.width, image.height)

        cv.Smooth(spare, spare, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE)

        #out = cv.CreateImage( size, 8, 1)
        cannyB = cv.CreateImage(size, 8, 1)
        cannyR = cv.CreateImage(size, 8, 1)
        sobel = cv.CreateImage(size, 8, 1)
        yuv = cv.CreateImage(size, 8, 3)
        dest_canny = cv.CreateImage(size, 8, 3)
        gray = cv.CreateImage(size, 8, 1)

        cv.CvtColor(spare, yuv, cv.CV_BGR2YCrCb)
        cv.Split(yuv, gray, None, None, None)

        cv.Canny(gray, cannyB, 5, 50, 3)
        cv.Canny(gray, cannyR, 5, 150, 3)
        cv.Sobel(gray, sobel, 1, 0, 3)

        #cv.ConvertScale(sobel, sobel, -1, 255 )
        #cv.ConvertScale(cannyR, cannyR, -1, 155 )
        #cv.ConvertScale(gray, gray, -1, 255 )
        #cv.ConvertScale(cannyB, cannyB, -1, 255 )

        cv.Smooth(cannyR, cannyR, cv.CV_GAUSSIAN, 3, 3)
        cv.Smooth(cannyB, cannyB, cv.CV_GAUSSIAN, 3, 3)
        #cv.CvtColor( canny, canny, cv.CV_YCrCb2BGR )
        #cv.Merge(sobel, gray, sobel, None, dest)
        cv.Merge(cannyR, cannyB, gray, None, dest_canny)

        #cv.Merge(sobel, sobel, sobel, None, dest_sobel)
        #cv.Smooth( dest, dest, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE )
        #cv.ShowImage( 'canny', dest)

        #cv.Merge
        #cv.Smooth( dest_canny, dest_canny, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE )

        #success = True
        self.prevImage = image

        options = {'normal': dest_canny, 'post': dest_canny}
        #return yuv
        return options[texture_type]
    def processFrames(self):
        self.vidcap = cv2.VideoCapture(self.path)

        count = 0

        success, image = self.vidcap.read()
        print success

        self.createWindows()

        while True:
            success, image = self.vidcap.read()

            if not success:
                return

            spare = cv.fromarray(image)

            size = (spare.width / 2, spare.height / 2)

            cv.Smooth(spare, spare, cv.CV_GAUSSIAN, BLUR_SIZE, BLUR_SIZE)

            out = cv.CreateImage(size, 8, 3)
            cv.PyrDown(spare, out)

            yuv = cv.CreateImage(size, 8, 3)
            gray = cv.CreateImage(size, 8, 1)
            canny = cv.CreateImage(size, 8, 1)
            sobel = cv.CreateImage(size, 8, 1)
            harris = cv.CreateImage(size, cv.IPL_DEPTH_32F, 1)

            cv.CvtColor(out, yuv, cv.CV_BGR2YCrCb)
            cv.Split(yuv, gray, None, None, None)

            cv.Canny(gray, canny, 50, 200, 3)
            cv.CornerHarris(gray, harris, 3)
            cv.Sobel(gray, sobel, 1, 0, 3)

            cv.ConvertScale(canny, canny, -1, 255)
            cv.ConvertScale(sobel, sobel, -1, 255)

            for y in range(0, out.height):
                for x in range(0, out.width):
                    harr = cv.Get2D(sobel, y, x)
                    if harr[0] < 10e-06:
                        cv.Circle(out, (x, y), 2, cv.RGB(155, 0, 25))

            #cv2.imwrite("frame%d.jpg" % count, np.asarray(canny[:,:]))

            cv.ShowImage('canny', canny)
            #cv.ShowImage(   'harris'   , harris  )
            cv.ShowImage('sobel', sobel)
            cv.ShowImage('output', out)

            if cv2.waitKey(1) == 27:
                break
            count += 1

        return
예제 #20
0
def detect_and_draw(img):
    t1 = time.time()

    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1)

    # blur the source image to reduce color noise
    cv.Smooth(img, img, cv.CV_BLUR, 3)
    hsv_img = cv.CreateImage(cv.GetSize(img), 8, 3)
    cv.CvtColor(img, hsv_img, cv.CV_BGR2HSV)
    thresholded_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1)
    #cv.InRangeS(hsv_img, (120, 80, 80), (140, 255, 255), thresholded_img)

    # White
    sensitivity = 15
    cv.InRangeS(hsv_img, (0, 0, 255 - sensitivity), (255, sensitivity, 255),
                thresholded_img)

    # Red
    #cv.InRangeS(hsv_img, (0, 150, 0), (5, 255, 255), thresholded_img)

    # Blue
    #cv.InRangeS(hsv_img, (100, 50, 50), (140, 255, 255), thresholded_img)

    # Green
    #cv.InRangeS(hsv_img, (40, 50, 50), (80, 255, 255), thresholded_img)

    mat = cv.GetMat(thresholded_img)
    moments = cv.Moments(mat, 0)
    area = cv.GetCentralMoment(moments, 0, 0)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    cv.EqualizeHist(small_img, small_img)

    if (area > 5000):
        #determine the x and y coordinates of the center of the object
        #we are tracking by dividing the 1, 0 and 0, 1 moments by the area
        x = cv.GetSpatialMoment(moments, 1, 0) / area
        y = cv.GetSpatialMoment(moments, 0, 1) / area
        x = int(round(x))
        y = int(round(y))

        #create an overlay to mark the center of the tracked object
        overlay = cv.CreateImage(cv.GetSize(img), 8, 3)

        cv.Circle(overlay, (x, y), 2, (0, 0, 0), 20)
        cv.Add(img, overlay, img)
        #add the thresholded image back to the img so we can see what was
        #left after it was applied
        #cv.Merge(thresholded_img, None, None, None, img)
        t2 = time.time()
        message = "Color tracked!"
        print "detection time = %gs x=%d,y=%d" % (round(t2 - t1, 3), x, y)

    cv.ShowImage("Color detection", img)
예제 #21
0
def main():
	"""Parse the command line and set off processing."""
	# parse command line
	opts,args = getopt(sys.argv[1:], "i")
	if len(args) != 1:
		syntax()
		return 1
	# grab options
	invert = False
	for n,v in opts:
		if n == '-i':
			invert = True
	# load image
	grey = prepare_image(args[0], invert)
	size = cv.GetSize(grey)
	# a bit of smoothing to reduce noise
	smoothed = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1)
	cv.Smooth(grey, smoothed, cv.CV_GAUSSIAN, 5, 5)
	# adaptive thresholding finds the letters against the numberplate
	# background
	thresholded = cv.CloneImage(grey)
	cv.AdaptiveThreshold(smoothed, thresholded, 255, 
		cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C, cv.CV_THRESH_BINARY_INV, 
		19, 9)
	# use a hough transform to find straight edges in the image and then 
	# remove them - removes number plate edges to ensure that characters 
	# don't join with the edges of the plate
	storage = cv.CreateMemStorage()
	#lines = cv.HoughLines2(thresholded, storage, cv.CV_HOUGH_PROBABILISTIC,
	#                       1, math.pi/180, 50, 50, 2)
	#for line in lines:
	#	cv.Line(thresholded, line[0], line[1], 0, 3, 4)
	# grab the contours from the image
	cont = cv.FindContours(thresholded,
		storage,
		cv.CV_RETR_CCOMP,
		cv.CV_CHAIN_APPROX_NONE)
	# grab 'good' contours
	col = 128
	validated = []
	while cont:
		v = validate_contour(cont,grey)
		if v is not None:
			validated.append(v)
		cont = cont.h_next()
	# overlay bounding boxes of 'good' contours on the original image
	result = cv.LoadImage(args[0])
	clusters = cluster_fuck(set(validated))
	for cluster in clusters:
		cv.Rectangle(result, 
			(int(min([c.x1 for c in cluster])), 
			 int(min([c.y1 for c in cluster]))),
			(int(max([c.x2 for c in cluster])), 
			 int(max([c.y2 for c in cluster]))), 
			(0,0,255))
		for bbox in cluster:
			cv.Rectangle(result, (int(bbox.x1),int(bbox.y1)), 
				(int(bbox.x2), int(bbox.y2)), (255,0,0))
	quick_show(result)
예제 #22
0
def yellow(img):
    global yx
    global yy
    global bx
    global by

    #blur the source image to reduce color noise
    cv.Smooth(img, img, cv.CV_BLUR, 3)

    #convert the image to hsv(Hue, Saturation, Value) so its
    #easier to determine the color to track(hue)
    hsv_img = cv.CreateImage(cv.GetSize(img), 8, 3)
    cv.CvtColor(img, hsv_img, cv.CV_BGR2HSV)

    #limit all pixels that don't match our criteria, in this case we are
    #looking for purple but if you want you can adjust the first value in
    #both turples which is the hue range(120,140).  OpenCV uses 0-180 as
    #a hue range for the HSV color model
    thresholded_img = cv.CreateImage(cv.GetSize(hsv_img), 8, 1)

    cv.InRangeS(hsv_img, (20, 100, 100), (30, 255, 255),
                thresholded_img)  #yellow
    #determine the objects moments and check that the area is large
    #enough to be our object
    mat = cv.GetMat(thresholded_img)
    moments = cv.Moments(mat, 0)
    area = cv.GetCentralMoment(moments, 0, 0)

    #there can be noise in the video so ignore objects with small areas
    if (area > 100000):
        #determine the x and y coordinates of the center of the object
        #we are tracking by dividing the 1, 0 and 0, 1 moments by the area

        x = cv.GetSpatialMoment(moments, 1, 0) / area
        y = cv.GetSpatialMoment(moments, 0, 1) / area
        x = int(x)
        y = int(y)
        yx = x
        yy = y
        cv.Circle(img, (x, y), 5, (0, 0, 0), -1)

    cv.InRangeS(hsv_img, (100, 80, 80), (120, 255, 255),
                thresholded_img)  #pink

    #determine the objects moments and check that the area is large
    #enough to be our object
    mat = cv.GetMat(thresholded_img)
    moments = cv.Moments(mat, 0)
    area = cv.GetCentralMoment(moments, 0, 0)

    #there can be noise in the video so ignore objects with small areas
    if (area > 100):
        x = cv.GetSpatialMoment(moments, 1, 0) / area
        y = cv.GetSpatialMoment(moments, 0, 1) / area
        x = int(x)
        y = int(y)
        bx = x
        by = y
        cv.Circle(img, (x, y), 5, (0, 0, 255), -1)
예제 #23
0
 def przygotuj_zdjecie(self, src, ROI, white_level, dark_level):
     img = cv.LoadImageM(src, cv.CV_LOAD_IMAGE_COLOR)
     size = cv.GetSize(img)
     print size
     gray = cv.CreateImage(size, 8, 1)
     cv.CvtColor(img, gray, cv.CV_RGB2GRAY)
     print ROI
     if ROI != [-1, -1, -1, -1]:
         cv.SetImageROI(gray, (ROI[0], ROI[1], ROI[2], ROI[3]))
     cv.Threshold(gray, gray, white_level, 255, cv.CV_THRESH_BINARY)
     cv.Smooth(gray, gray, cv.CV_BLUR, 9, 9)
     cv.Threshold(gray, gray, white_level * 0.8, 255, cv.CV_THRESH_BINARY)
     cv.Smooth(gray, gray, cv.CV_BLUR, 5, 5)
     #cv.Dilate(gray,gray)
     #cv.Canny( gray, gray, 1.0, 1.0, 3)
     size = cv.GetSize(gray)
     return gray, size
예제 #24
0
def gaussianFilter(im, sigma):
    '''
    Smooth an image using a Gaussian filter.
    '''
    cvim = cv.CreateImage(im.size, cv.IPL_DEPTH_8U, im.channels)

    cv.Smooth(im.asOpenCV(), cvim, cv.CV_GAUSSIAN, 0, 0, sigma)
    return pv.Image(cvim)
예제 #25
0
def template_match(img, refimg, confidence=0.6, xwin=19, ywin=19):
    """
    Return all matches of refimg inside img, using Template Matching.
    (Gratefully) borrowed from:
        http://stackoverflow.com/questions/7670112/finding-a-subimage-inside-a-numpy-image/9253805#9253805
    Input:
        obj img: A numpy array representing an image
        obj refimg: A numpy array representing the reference image
        float confidence: threshold value (from [0,1]) for template
                          matching
    Output:
        A tuple of (x,y) coodinates, w.r.t the coordinate system of
        img.
    """
    # OpenCV requires either uint8, or float, but with floats it got
    # buggy and failed badly (I think it had to do with it not
    # correctly handling when 'img' had no decimals, but 'refimg'
    # had decimal expansions, which I suppose means that internally
    # OpenCV.matchTemplate does exact integer comparisons.
    img = img.astype('uint8')
    refimg = refimg.astype('uint8')

    I = cv.fromarray(img)
    ref = cv.fromarray(refimg)
    #I = cv.fromarray(np.copy(img))
    #ref = cv.fromarray(np.copy(refimg))
    I_s = cv.CreateMat(I.rows, I.cols, I.type)
    cv.Smooth(I, I_s, cv.CV_GAUSSIAN, param1=xwin, param2=ywin)
    ref_s = cv.CreateMat(ref.rows, ref.cols, ref.type)
    cv.Smooth(ref, ref_s, cv.CV_GAUSSIAN, param1=xwin, param2=ywin)
    #img = np.array(img, dtype='uint8')
    #refimg = np.array(refimg, dtype='uint8')
    result = cv.CreateMat(I_s.rows - ref_s.rows + 1, I_s.cols - ref_s.cols + 1,
                          cv.CV_32F)
    cv.MatchTemplate(I_s, ref_s, result, cv.CV_TM_CCOEFF_NORMED)
    #result = cv2.matchTemplate(img, refimg, cv2.TM_CCOEFF_NORMED)
    # result is a 'similarity' matrix, with values from -1.0 (?) to 1.0,
    # where 1.0 is most similar to the template image.
    result_np = np.asarray(result)
    match_flatidxs = np.arange(
        result_np.size)[(result_np > confidence).flatten()]
    return [
        flatidx_to_pixelidx(flatidx, result_np.shape)
        for flatidx in match_flatidxs
    ]
예제 #26
0
def main():
    s = scratch.Scratch()
    capture = cv.CaptureFromCAM(0)
    cv.NamedWindow("Track", 1)

    while True:
        #capture frame
        frame_o = cv.QueryFrame(capture)
        frame = cv.CreateImage((frame_o.width * 3 / 8, frame_o.height * 3 / 8),
                               frame_o.depth, frame_o.nChannels)
        cv.Resize(frame_o, frame)
        cv.Smooth(frame, frame, cv.CV_GAUSSIAN, 3, 3)

        #Convert to HSV
        imgHSV = cv.CreateImage(cv.GetSize(frame), 8, 3)
        cv.CvtColor(frame, imgHSV, cv.CV_BGR2HSV)

        #Thresh
        imgThreshed = cv.CreateImage(cv.GetSize(frame), 8, 1)
        cv.InRangeS(imgHSV, cv.Scalar(0, 124, 221), cv.Scalar(10, 255, 256),
                    imgThreshed)
        cv.Smooth(imgThreshed, imgThreshed, cv.CV_GAUSSIAN, 3, 3)

        mat = cv.GetMat(imgThreshed)
        moments = cv.Moments(mat)

        moment10 = cv.GetSpatialMoment(moments, 1, 0)
        moment01 = cv.GetSpatialMoment(moments, 0, 1)
        area = cv.GetCentralMoment(moments, 0, 0)

        if area > 1000:
            posX = int(moment10 / area)
            posY = int(moment01 / area)

            if posX >= 0 and posY >= 0:
                print("X: " + str(posX) + ", Y: " + str(posY))
                cv.Rectangle(frame, (posX - 10, posY - 10),
                             (posX + 10, posY + 10), cv.RGB(0, 255, 0))
                s.sensorupdate({'X': posX})
                s.sensorupdate({'Y': posY})

        cv.ShowImage("Track", frame)
        k = cv.WaitKey(70)
        if k % 0x100 == 27:
            break
예제 #27
0
def detect_and_draw(img, cascade):
    # allocate temporary images
    gray = cv.CreateImage((img.width, img.height), 8, 1)
    small_img = cv.CreateImage((cv.Round(
        img.width / image_scale), cv.Round(img.height / image_scale)), 8, 1)

    # convert color input image to grayscale
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

    # scale input image for faster processing
    cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)

    cv.EqualizeHist(small_img, small_img)

    window = cv.CreateImage((cv.Round(img.width), cv.Round(img.height)), 8, 3)
    if (cascade):
        t = cv.GetTickCount()
        faces = local_haar_detect(small_img, cascade, cv.CreateMemStorage(0),
                                  haar_scale, min_neighbors, haar_flags,
                                  min_size)
        t = cv.GetTickCount() - t
        print "detection time = %gms" % (t / (cv.GetTickFrequency() * 1000.))
        channels = None
        if faces:
            for ((x, y, w, h), n) in faces:
                # the input to cv.HaarDetectObjects was resized, so scale the
                # bounding box of each face and convert it to two CvPoints
                pt1 = (cv.Round(
                    (x + w * .2) * image_scale), cv.Round(y * image_scale))
                pt2 = (cv.Round(
                    (x + w * .8) * image_scale), cv.Round(
                        (y + h) * image_scale))

                window = cv.CreateImage((cv.Round(w * .6) * image_scale,
                                         cv.Round(h) * image_scale), 8, 3)
                cv.Smooth(window, window, cv.CV_GAUSSIAN)
                channels = [
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1),
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1),
                    cv.CreateImage((cv.Round(w * .6) * image_scale,
                                    cv.Round(h) * image_scale), 8, 1)
                ]
                cv.GetRectSubPix(img, window, (cv.Round(
                    (pt1[0] + pt2[0]) / 2.0), cv.Round(
                        (pt1[1] + pt2[1]) / 2.0)))
                cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
                cv.Split(window, channels[0], channels[1], channels[2], None)
                result.append([
                    cv.Avg(channels[0])[0],
                    cv.Avg(channels[1])[0],
                    cv.Avg(channels[2])[0]
                ])

    cv.ShowImage("result", img)
예제 #28
0
def edge_trackbar(position):
    #cv.Smooth(threshold_img,edge_img,cv.CV_BLUR,3,3,0)
    cv.Smooth(src, edge_img, cv.CV_BLUR, 3, 3, 0)
    #cv.Not(src,edge_img)

    #run the edge detector on threshold scale
    cv.Canny(src, edge_img, position, position * 3, 3)

    #show the image
    cv.ShowImage(window_edge, edge_img)
예제 #29
0
def main():
    pr_window = "imagen"
    capture = cv.CaptureFromCAM(-1)
    cv.NamedWindow(pr_window, 1)

    #    seteo tamanio de la ventana |-| comentar cuando no se necesite mostrar ventana
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, config.ancho)
    cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, config.alto)
    delay = 0
    while True:
        if (not (delay == 20)):
            delay += 1
            img = cv.QueryFrame(capture)
            #cv.ReleaseCapture( img )
        else:
            delay = 0
            frame = cv.QueryFrame(capture)
            maskN = cv.CreateImage(cv.GetSize(frame), 8, 1)
            hsvN = cv.CloneImage(frame)

            cv.Smooth(frame, frame, cv.CV_BLUR, 3)

            cv.CvtColor(frame, hsvN, cv.CV_BGR2HSV)
            cv.InRangeS(hsvN, config.min_range, config.max_range, maskN)

            moment = cv.Moments(cv.GetMat(maskN), 0)
            a = cv.GetCentralMoment(moment, 0, 0)

            if a > config.min_area:
                X = int(cv.GetSpatialMoment(moment, 1, 0) / a)
                print "X: " + str(X)
                print "min: " + str(config.min_x)
                print "max: " + str(config.max_x)
                #Y = int(cv.GetSpatialMoment (moment, 0, 1) / a)
                if X > config.max_x:
                    print "derecha"
                elif X < config.min_x:
                    print "izquierda"
                else:
                    print "centrado"
            else:
                print "objeto no detectado o muy pequeno"

            cv.ShowImage(pr_window, maskN)


#        descomentar para debug
#        X = int(cv.GetSpatialMoment (moment, 1, 0) / a)
#        print 'x: ' + str (X)  + ' area: ' + str (a)

# Con esto corto y salgo
#        if cv.WaitKey (100) != -1:
#            break

    return
예제 #30
0
def getCannyMask(image, threshold):
    """convert image to gray scale run the edge dector returns mask
    """
    res = cv.GetSize(image) 
    gray = cv.CreateImage (res, 8, 1)
    mask = cv.CreateImage (res, 8, 1)
    cv.CvtColor(image, gray, cv.CV_BGR2GRAY)
    cv.Smooth (gray, mask, cv.CV_BLUR, 3, 3, 0)
    cv.Not(gray, mask)
    cv.Canny(gray, mask, threshold, threshold * 3, 3)
    return mask