예제 #1
0
    def locateMarker(self, frame):
        self.frameReal = cv.CloneImage(frame)
        self.frameImag = cv.CloneImage(frame)
        self.frameRealThirdHarmonics = cv.CloneImage(frame)
        self.frameImagThirdHarmonics = cv.CloneImage(frame)

        # Calculate convolution and determine response strength.
        cv.Filter2D(self.frameReal, self.frameReal,
                    self.matReal)  # src, dst, kernel
        cv.Filter2D(self.frameImag, self.frameImag,
                    self.matImag)  # src, dst, kernel

        cv.Mul(self.frameReal, self.frameReal,
               self.frameRealSq)  # src, src, dst
        cv.Mul(self.frameImag, self.frameImag,
               self.frameImagSq)  # src, src, dst
        cv.Add(self.frameRealSq, self.frameImagSq, self.frameSumSq)

        # Calculate convolution of third harmonics for quality estimation.
        cv.Filter2D(self.frameRealThirdHarmonics, self.frameRealThirdHarmonics,
                    self.matRealThirdHarmonics)
        cv.Filter2D(self.frameImagThirdHarmonics, self.frameImagThirdHarmonics,
                    self.matImagThirdHarmonics)

        min_val, max_val, min_loc, max_loc = cv.MinMaxLoc(self.frameSumSq)
        self.lastMarkerLocation = max_loc
        (xm, ym) = max_loc
        self.determineMarkerOrientation(frame)
        #	self.determineMarkerQuality_naive(frame)
        self.determineMarkerQuality_Mathias(frame)
        #        self.determineMarkerQuality()
        return max_loc
예제 #2
0
    def hue_histogram_as_image(self, hist):
        """ Returns a nice representation of a hue histogram """

        histimg_hsv = cv.CreateImage((320, 200), 8, 3)

        mybins = cv.CloneMatND(hist.bins)  #Contain all values
        cv.Log(mybins, mybins
               )  #Calculate logarithm of all values (so there are all above 0)

        (_, hi, _, _) = cv.MinMaxLoc(mybins)
        cv.ConvertScale(mybins, mybins, 255. /
                        hi)  #Rescale all element to get the highest at 255

        w, h = cv.GetSize(histimg_hsv)
        hdims = cv.GetDims(mybins)[0]
        for x in range(w):
            xh = (180 * x) / (w - 1)  # hue sweeps from 0-180 across the image
            val = int(mybins[int(hdims * x / w)] * h / 255)
            cv.Rectangle(histimg_hsv, (x, 0), (x, h - val), (xh, 255, 64), -1)
            cv.Rectangle(histimg_hsv, (x, h - val), (x, h), (xh, 255, 255), -1)

        histimg = cv.CreateImage((320, 200), 8,
                                 3)  #Convert image from hsv to RGB
        cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR)
        return histimg
예제 #3
0
def binary_image(image, min_level, max_level):
    new_image = cv.CreateImage((image.width, image.height), image.depth,
                               image.nChannels)
    minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(image)
    threshold = minVal * min_level + maxVal * max_level
    cv.Threshold(image, new_image, threshold, 255, cv.CV_THRESH_BINARY)
    return new_image
예제 #4
0
def linear_translate(image):
    linear_image = cv.CreateImage((image.width, image.height), image.depth,
                                  image.nChannels)
    minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(image)
    scale = 255.0 / (maxVal - minVal)
    shift = -(scale * minVal)
    cv.Scale(image, linear_image, scale, shift)
    return linear_image
예제 #5
0
def gray_swap(image):
    new_image = cv.CreateImage((image.width, image.height), image.depth,
                               image.nChannels)
    minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(image)
    scale = -1
    shift = 255
    cv.Scale(image, new_image, scale, shift)
    return new_image
예제 #6
0
파일: opencv4.py 프로젝트: arshibassi/UAV
def drawGraph(ar, im, size):  #Draw the histogram on the image
    minV, maxV, minloc, maxloc = cv.MinMaxLoc(ar)  #Get the min and max value
    hpt = 0.9 * histsize
    for i in range(size):
        intensity = ar[
            i] * hpt / maxV  #Calculate the intensity to make enter in the image
        cv.Line(im, (i, size), (i, int(size - intensity)),
                cv.Scalar(255, 255, 255))  #Draw the line
        i += 1
예제 #7
0
    def compareImage(src, dst):
        W, H = cv.GetSize(src)
        w, h = cv.GetSize(dst)
        width = W - w + 1
        height = H - h + 1

        if (width > 0) and (height > 0):
            result = cv.CreateImage((width, height), 32, 1)
            cv.MatchTemplate(src, dst, result, cv.CV_TM_CCOEFF_NORMED)
            _, max_val, _, _ = cv.MinMaxLoc(result)
            return max_val
        else:  #TODO: raise exception (src size need to be >= dst)
            return -1
예제 #8
0
    def findImageEx(self, source, x, y, width, height):
        hdc = win32gui.GetWindowDC(self.hwnd)
        dc_obj = win32ui.CreateDCFromHandle(hdc)
        memorydc = dc_obj.CreateCompatibleDC()
        data_bitmap = win32ui.CreateBitmap()
        data_bitmap.CreateCompatibleBitmap(dc_obj, self.width, self.height)

        memorydc.SelectObject(data_bitmap)
        memorydc.BitBlt((0, 0), (self.width, self.height), dc_obj, (self.dx, self.dy), win32con.SRCCOPY)

        bmpheader = struct.pack("LHHHH", struct.calcsize("LHHHH"),
                                self.width, self.height, 1, 24)
        c_bmpheader = ctypes.create_string_buffer(bmpheader)

        # padded_length = (string_length + 3) & -3 for 4-byte aligned.
        c_bits = ctypes.create_string_buffer(" " * (self.width * ((self.height * 3 + 3) & -3)))

        res = ctypes.windll.gdi32.GetDIBits(memorydc.GetSafeHdc(), data_bitmap.GetHandle(),
                                            0, self.height, c_bits, c_bmpheader, win32con.DIB_RGB_COLORS)

        win32gui.DeleteDC(hdc)
        win32gui.ReleaseDC(self.hwnd, hdc)
        memorydc.DeleteDC()
        win32gui.DeleteObject(data_bitmap.GetHandle())

        cv_im = cv.CreateImageHeader((self.width, self.height), cv.IPL_DEPTH_8U, 3)
        cv.SetData(cv_im, c_bits.raw)
        # flip around x-axis
        cv.Flip(cv_im, None, 0)

        im_region = cv.GetSubRect(cv_im, (x, y, width, height))
        #cv.SaveImage('aaak.bmp', im_region)

        template_source = cv.LoadImage(source)

        # From the manual of MatchTemplate
        result_width = im_region.width - template_source.width + 1
        result_height = im_region.height - template_source.height + 1;
        result = cv.CreateImage((result_width, result_height), 32, 1)

        cv.MatchTemplate(im_region, template_source, result, cv2.TM_CCOEFF_NORMED)
        minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(result)
        #print minVal, maxVal, minLoc, maxLoc

        minLoc2 = minLoc[0] + x, minLoc[1] + y
        maxLoc2 = maxLoc[0] + x, maxLoc[1] + y

        return minVal, maxVal, minLoc2, maxLoc2
예제 #9
0
    def calibrate(self, img, mask_range = (50, 100)):
        # mask: ignore pixels that are really close or far away, like the
        # Nao (close, low values) and kinect noise (far far away, high values)
        _, _, width, height = cv.GetImageROI(img)
        mask = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1)
        cv.InRangeS(img, mask_range[0], mask_range[1], mask)

        # using the mask, create a new image containing only the pixels of
        # interest.
        self.base_img = cv.CreateImage((width, height), cv.IPL_DEPTH_8U, 1)
        cv.SetImageROI(self.base_img, cv.GetImageROI(img))

        # get the minimum value, and subtract that from all pixels so only
        # the difference in depth in the image remains.
        minimum, _, _, _ = cv.MinMaxLoc(img, mask)
        cv.SubS(img, minimum, self.base_img)
예제 #10
0
	def hue_histogram_as_image(self,hist):
		# Return a nice representation of a hue histogram
		histimg_hsv = cv.CreateImage((320,200),8,3)
		mybins = cv.CloneMatND(hist.bins)
		cv.Log(mybins,mybins)
		(_,hi,_,_) = cv.MinMaxLoc(mybins)
		cv.ConvertScale(mybins,mybins,255./hi)
		
		w,h = cv.GetSize(histing_hsv)
		hdims = cv.GetDims(mybins)[0]
		for x in range(w):
			xh = (180*x)/(w-1) # hue sweeps from 0-180 across the image
			val = int(mybins[int(hdims*x/w)]*h/255)
			cv2.rectangle(histimg_hsv,(x,0),(x,h-val),(xh,255,64),-1)
			cv2.rectangle(histimg_hsv,(x,h-val),(x,h),(xh,255,255),-1)
		histimg = cv2.cvtColor(histing_hsv,cv.CV_HSV2BGR)
		return histimg
예제 #11
0
파일: camshift.py 프로젝트: yulu/python_cv
    def hue_histogram_as_image(self, hist):
        """ Returns a nice representation of a hue histogram """

        histimg_hsv = cv.CreateImage((320, 200), cv.IPL_DEPTH_8U, 3)

        mybins = cv.CloneMatND(hist.bins)
        cv.Log(mybins, mybins)
        (_, hi, _, _) = cv.MinMaxLoc(mybins)
        cv.ConvertScale(mybins, mybins, 255. / hi)

        w, h = cv.GetSize(histimg_hsv)
        hdims = cv.GetDims(mybins)[0]
        for x in range(w):
            xh = (180 * x) / (w - 1)
            val = int(mybins[int(hdims * x) / w] * h / 255)
            cv.Rectangle(histimg_hsv, (x, 0), (x, h - val), (xh, 255, 64), -1)
            cv.Rectangle(histimg_hsv, (x, h - val), (x, h), (xh, 255, 255), -1)

        histimg = cv.CreateImage((320, 200), 8, 3)
        cv.CvtColor(histimg_hsv, histimg, cv.CV_HSV2BGR)
        return histimg
예제 #12
0
def Harris(image):
    gray = cv.CreateImage(cv.GetSize(image), 8, 1)
    harris = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)

    cv.CornerHarris(gray, harris, 5, 5, 0.1)

    #for y in range(0, 640):
    #    for x in range(0, 480):
    #      harris = cv.Get2D(harris_c, y, x) # get the x,y value
    #      # check the corner detector response
    #      if harris[0] > 10e-06:
    #       # draw a small circle on the original image
    #           cv.Circle(harris, (x, y), 2, cv.RGB(155, 0, 25))

    # I recommend using cvMinMaxLoc to find the min and max values and then use
    # cvConvertScale to shift the range [min..max] to [0..255] into an 8bit
    # image.

    print cv.MinMaxLoc(harris)
    gray2 = cv.CreateImage(cv.GetSize(image), 8, 1)
    cv.ConvertScale(harris, gray2)  # scale/shift?

    cv.NamedWindow('Harris')
    cv.ShowImage('Harris', gray)
예제 #13
0
im = cv.LoadImage('/home/pi/Documents/Lab3/foto4.png',
                  cv.CV_LOAD_IMAGE_GRAYSCALE)

dst_32f = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_32F, 1)

neighbourhood = 3
aperture = 3
k = 0.01
maxStrength = 0.0
threshold = 0.01
nonMaxSize = 3

cv.CornerHarris(im, dst_32f, neighbourhood, aperture, k)

minv, maxv, minl, maxl = cv.MinMaxLoc(dst_32f)

dilated = cv.CloneImage(dst_32f)
cv.Dilate(
    dst_32f, dilated
)  # By this way we are sure that pixel with local max value will not be changed, and all the others will

localMax = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U)
cv.Cmp(
    dst_32f, dilated, localMax, cv.CV_CMP_EQ
)  #compare allow to keep only non modified pixel which are local maximum values which are corners.

threshold = 0.01 * maxv
cv.Threshold(dst_32f, dst_32f, threshold, 255, cv.CV_THRESH_BINARY)

cornerMap = cv.CreateMat(dst_32f.height, dst_32f.width, cv.CV_8U)
예제 #14
0
    cv.DFT( dft_A, dft_A, cv.CV_DXT_FORWARD, complexInput.height )

    cv.NamedWindow("win", 0)
    cv.NamedWindow("magnitude", 0)
    cv.ShowImage("win", im)

    # Split Fourier in real and imaginary parts
    cv.Split( dft_A, image_Re, image_Im, None, None )

    # Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
    cv.Pow( image_Re, image_Re, 2.0)
    cv.Pow( image_Im, image_Im, 2.0)
    cv.Add( image_Re, image_Im, image_Re, None)
    cv.Pow( image_Re, image_Re, 0.5 )

    # Compute log(1 + Mag)
    cv.AddS( image_Re, cv.ScalarAll(1.0), image_Re, None ) # 1 + Mag
    cv.Log( image_Re, image_Re ) # log(1 + Mag)


    # Rearrange the quadrants of Fourier image so that the origin is at
    # the image center
    cvShiftDFT( image_Re, image_Re )

    min, max, pt1, pt2 = cv.MinMaxLoc(image_Re)
    cv.Scale(image_Re, image_Re, 1.0/(max-min), 1.0*(-min)/(max-min))
    cv.ShowImage("magnitude", image_Re)

    cv.WaitKey(0)
    cv.DestroyAllWindows()
예제 #15
0
파일: pymouse.py 프로젝트: shileen/PyMouse
        cv.DestroyWindow("Image")

    if flag == 0:
        cv.NamedWindow("Image", 1)
        cv.MoveWindow("Image", 300, 0)
        cv.ShowImage("Image", temp1)
        continue

    W, H = cv.GetSize(frame)
    w, h = cv.GetSize(template)

    width = W - w + 1
    height = H - h + 1
    result = cv.CreateImage((width, height), 32, 1)
    #matching the template by searching for the given region in the image
    cv.MatchTemplate(frame, template, result, cv.CV_TM_SQDIFF_NORMED)
    (min_x, max_y, minloc, maxloc) = cv.MinMaxLoc(result)
    (x, y) = minloc
    X1 = (x * 1366) / 640
    Y1 = (y * 768) / 480
    print X1, Y1
    #using pyautogui to move mouse
    pyautogui.moveTo(x + 300, y)
    cv.Rectangle(frame, (x, y), (x + w, y + h), color, 1, 0)
    cv.NamedWindow("Image", 1)
    cv.MoveWindow("Image", 300, 0)
    cv.ShowImage("Image", frame)

    if cv.WaitKey(10) == 27:
        break
예제 #16
0
def absnorm8(im, im8):
    """ im may be any single-channel image type.  Return an 8-bit version, absolute value, normalized so that max is 255 """
    (minVal, maxVal, _, _) = cv.MinMaxLoc(im)
    cv.ConvertScaleAbs(im, im8, 255 / max(abs(minVal), abs(maxVal)), 0)
    return im8
예제 #17
0
def detectObject(filename):
    img=cv.LoadImage(filename)
    '''
    #get color histogram
    '''
   
#     im32f=np.zeros((img.shape[:2]),np.uint32)
    hist_range=[[0,256],[0,256],[0,256]]
    im32f=cv.CreateImage(cv.GetSize(img), cv2.IPL_DEPTH_32F, 3)
    cv.ConvertScale(img, im32f)
    
    
    hist=cv.CreateHist([32,32,32],cv.CV_HIST_ARRAY,hist_range,3)
    '''
    #create three histogram'''
    b=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    g=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    r=cv.CreateImage(cv.GetSize(im32f), cv2.IPL_DEPTH_32F, 1)
    
   
    '''
    #create image backproject 32f, 8u
    '''
    backproject32f=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_32F,1)
    backproject8u=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_8U,1)
    '''
    #create binary
    '''
    bw=cv.CreateImage(cv.GetSize(img),cv2.IPL_DEPTH_8U,1)
    '''
    #create kernel image
    '''
    kernel=cv.CreateStructuringElementEx(3, 3, 1, 1, cv2.MORPH_ELLIPSE)
    cv.Split(im32f, b, g, r,None)

    planes=[b,g,r]
    cv.CalcHist(planes, hist)
    '''
    #find min and max histogram bin.
    '''
    minval=maxval=0.0
    min_idx=max_idx=0
    minval, maxval, min_idx, max_idx=cv.GetMinMaxHistValue(hist)
    '''
    # threshold histogram.  this sets the bin values that are below the threshold
    to zero
    '''
    cv.ThreshHist(hist, maxval/32.0)
    '''
    #backproject the thresholded histogram, backprojection should contian higher values for
    #background and lower values for the foreground
    '''
    cv.CalcBackProject(planes, backproject32f, hist)
    '''
    #convert to 8u type
    '''
    val_min=val_max=0.0
    idx_min=idx_max=0
    val_min,val_max,idx_min,idx_max=cv.MinMaxLoc(backproject32f)
    cv.ConvertScale(backproject32f, backproject8u,255.0/maxval)
    '''
    #threshold backprojected image. this gives us the background
    '''
    cv.Threshold(backproject8u, bw, 10, 255, cv2.THRESH_BINARY)
    '''
    #some morphology on background
    '''
    cv.Dilate(bw, bw,kernel,1)
    cv.MorphologyEx(bw, bw, None,kernel, cv2.MORPH_CLOSE, 2)
    '''
    #get the foreground
    '''
    cv.SubRS(bw,cv.Scalar(255,255,255),bw)
    cv.MorphologyEx(bw, bw, None, kernel,cv2.MORPH_OPEN,2)
    cv.Erode(bw, bw, kernel, 1)
    '''
    #find contours of foreground
    #Grabcut
    '''
    size=cv.GetSize(bw)
    color=np.asarray(img[:,:])
    fg=np.asarray(bw[:,:])
#     mask=cv.CreateMat(size[1], size[0], cv2.CV_8UC1)
    '''
    #Make anywhere black in the grey_image (output from MOG) as likely background
    #Make anywhere white in the grey_image (output from MOG) as definite foreground
    '''
    rect = (0,0,0,0)
   
    mat_mask=np.zeros((size[1],size[0]),dtype='uint8')
    mat_mask[:,:]=fg
    
    mat_mask[mat_mask[:,:] == 0] = 2
    mat_mask[mat_mask[:,:] == 255] = 1
    
    '''
    #Make containers 
    '''                               
    bgdModel = np.zeros((1, 13 * 5))
    fgdModel = np.zeros((1, 13 * 5))
    cv2.grabCut(color, mat_mask, rect, bgdModel, fgdModel,cv2.GC_INIT_WITH_MASK)
    '''
    #Multiple new mask by original image to get cut
    '''
    mask2 = np.where((mat_mask==0)|(mat_mask==2),0,1).astype('uint8')  
    gcfg=np.zeros((size[1],size[0]),np.uint8)
    gcfg=mask2
    
    img_cut = color*mask2[:,:,np.newaxis]

    contours, hierarchy=cv2.findContours(gcfg ,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    
    for cnt in contours:
        print cnt
        rect_box = cv2.minAreaRect(cnt)
        box = cv2.cv.BoxPoints(rect_box)
        box = np.int0(box)
        cv2.drawContours(color,[box], 0,(0,0,255),2)
    cv2.imshow('demo', color)
    cv2.waitKey(0)