示例#1
0
def gradient(grayscale, aperture_size=3):
    """ 
        Computes the gradient of an image using a Sobel filter.
    
        :param grayscale:  A field to derive.
        :type  grayscale:  HxW array float
        
        :param aperture_size: Aperture of the Sobel filter (odd).
        :type  aperture_size: int,odd,>=1
     
        :return: gx: Gradient in the *x* direction.
        :rtype: array(HxW,float)
        
        :return: gy: Gradient in the *y* direction.
        :rtype: array(HxW,float)
    """

    check_2d_array(grayscale, "grayscale")

    im = numpy_to_cv(grayscale)
    shape = (im.width, im.height)
    sobel = cv.CreateImage(shape, cv.IPL_DEPTH_32F, 1)
    cv.Sobel(im, sobel, 1, 0, aperture_size)
    gx = cv_to_numpy(sobel).squeeze()

    sobel = cv.CreateImage(shape, cv.IPL_DEPTH_32F, 1)
    cv.Sobel(im, sobel, 0, 1, aperture_size)
    gy = cv_to_numpy(sobel).squeeze()

    return gx.astype('float32'), gy.astype('float32')
示例#2
0
def integral_img (image,bins):
    #numorient=bins;
    size = cv.GetSize(image)
    gray_img = cv.CreateImage(size, 8, 1)
    xsobel = cv.CreateImage(size, 32, 1)
    ysobel = cv.CreateImage(size, 32, 1)
    
 #    "RGB to grayscale conversion"
    cv.CvtColor(image, gray_img, cv.CV_BGR2GRAY)	
    
  #  "Gradient computation using sobel operators"
    cv.Sobel(gray_img, xsobel, 1, 0, 3)
    cv.Sobel(gray_img, ysobel, 0, 1, 3)

    integral_hog= np.zeros((size[1], size[0], bins))
    for y in xrange(0, size[1]-1):
        for x in xrange(0, size[0]-1):
            angle1= (int(round(180*np.arctan2(ysobel[y,x], xsobel[y,x])/np.pi))+ 180)
            if angle1>=0 and angle1<40:
                angle=0
            elif angle1>=40 and angle1<80:
                angle=1
            elif angle1>=80 and angle1<120:
                angle=2
            elif angle1>=120 and angle1<160:
                angle=3
            elif angle1>=160 and angle1<200:
                angle=4
            elif angle1>=200 and angle1<240:
                angle=5
            elif angle1>=240 and angle1<280:
                angle=6
            elif angle1>=280 and angle1<320:
                angle=7
            elif angle1>=320 and angle1<=360:
                angle=8
            grad_m = np.sqrt(xsobel[y,x]*xsobel[y,x]+ysobel[y,x]*ysobel[y,x])
            integral_hog[y,x,angle] += grad_m
            
 #       "Now we build the integral image"
    for x in xrange(1, size[0]-1):
        for orient in xrange(bins):
            integral_hog[y,x,orient] += integral_hog[y,x-1,orient]
    for y in xrange(1, size[1]-1):
        for orient in xrange(bins):
            integral_hog[y,x,orient] += integral_hog[y-1,x,orient]
    for y in xrange(1, size[1]-1):
        for x in xrange(1, size[0]-1):
            for orient in xrange(bins):
                integral_hog[y,x,orient] += integral_hog[y-1,x,orient] + integral_hog[y,x-1,orient] - integral_hog[y-1,x-1,orient]
    return integral_hog
def sobel():
    cv.Smooth(src_image, dst_image, cv.CV_GAUSSIAN, 3, 3)
    src_gray = cv.CreateImage((src_image.width, src_image.height), 8, 1)
    dst_gray1 = cv.CreateImage((src_image.width, src_image.height), 8, 1)
    dst_gray = cv.CreateImage((src_image.width, src_image.height), 8, 1)
    cv.CvtColor(src_image, src_gray, cv.CV_BGR2GRAY)
    cv.Sobel(src_gray, dst_gray1, 0, 1, 3)
    cv.ConvertScaleAbs(dst_gray1, dst_gray1, 1, 0)
    cv.Sobel(src_gray, dst_gray, 1, 0, 3)
    cv.ConvertScaleAbs(dst_gray, dst_gray, 1, 0)
    cv.AddWeighted(dst_gray, 0.5, dst_gray1, 0.5, 0, dst_gray)
    cv.NamedWindow("Destination Image")
    cv.ShowImage("Destination Image", dst_gray)
    cv.WaitKey(0)
    def sobelGradient(self, image):
        '''Calculates the gradient in x and y direction using Sobel mask using default 3x3 filter'''
        if self.image_check(image) < 0:
            return -1

        gsimage = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1)
        if image.channels > 1:
            temp = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_8U, 1)
            cv.CvtColor(image, temp, cv.CV_BGR2GRAY)
            gsimage = temp
        else:
            gsimage = image
        #smoothing image

        #Creating empty images for dx,dy and temporary 16 bit dx16 and dy16
        dy16 = cv.CreateImage(cv.GetSize(gsimage), cv.IPL_DEPTH_16S, 1)
        dx16 = cv.CreateImage(cv.GetSize(gsimage), cv.IPL_DEPTH_16S, 1)
        dx = cv.CreateImage(cv.GetSize(gsimage), cv.IPL_DEPTH_8U, 1)
        dy = cv.CreateImage(cv.GetSize(gsimage), cv.IPL_DEPTH_8U, 1)

        #Convolving sobel mask to get gradient of dx and dy
        cv.Sobel(gsimage, dy16, 0, 1, apertureSize=3)
        cv.Sobel(gsimage, dx16, 1, 0, apertureSize=3)
        cv.ConvertScaleAbs(dx16, dx)
        cv.ConvertScaleAbs(dy16, dy)

        if self.visualize:
            while True:
                cv.NamedWindow("Original")
                cv.ShowImage("Original", gsimage)
                cv.NamedWindow("Sobelx")
                cv.ShowImage("Sobelx", dx)
                cv.NamedWindow("Sobely")
                cv.ShowImage("Sobely", dy)
                c = cv.WaitKey(5)
                if c > 0:

                    break
        cv.DestroyAllWindows()

        return (dx16, dy16)
示例#5
0
    def get_gradient_img(self, input_img):
        # Generate grad_x and grad_y to use Sobel
        grad_x=cv.CreateMat(input_img.height, input_img.width, input_img.type)
        grad_y=cv.CreateMat(input_img.height, input_img.width, input_img.type)
        abs_grad_x=cv.CreateMat(input_img.height, input_img.width, input_img.type)
        abs_grad_y=cv.CreateMat(input_img.height, input_img.width, input_img.type)
        grad =cv.CreateMat(input_img.height, input_img.width, input_img.type)

        #Gradient X
        #Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );#for small kernels use this instead of sobel
        cv.Sobel( input_img, grad_x, 1, 0)
        cv.Abs(grad_x, abs_grad_x )

        #Gradient Y
        #Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
        cv.Sobel( input_img, grad_y, 0, 1)
        cv.Abs( grad_y, abs_grad_y )

        #/// Total Gradient (approximate)
        cv.AddWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad )
        return grad
示例#6
0
def sobel_image(image, x, y):
    sobel_image = cv.CreateImage((image.width, image.height), image.depth,
                                 image.nChannels)
    cv.Sobel(image, sobel_image, x, y)
    return sobel_image
示例#7
0
    def find(self, img):
        started = time.time()
        gray = self.Cached('gray', img.height, img.width, cv.CV_8UC1)
        cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

        sobel = self.Cached('sobel', img.height, img.width, cv.CV_16SC1)
        sobely = self.Cached('sobely', img.height, img.width, cv.CV_16SC1)

        cv.Sobel(gray, sobel, 1, 0)
        cv.Sobel(gray, sobely, 0, 1)
        cv.Add(sobel, sobely, sobel)

        sobel8 = self.Cached('sobel8', sobel.height, sobel.width, cv.CV_8UC1)
        absnorm8(sobel, sobel8)
        cv.Threshold(sobel8, sobel8, 128.0, 255.0, cv.CV_THRESH_BINARY)

        sobel_integral = self.Cached('sobel_integral', img.height + 1,
                                     img.width + 1, cv.CV_32SC1)
        cv.Integral(sobel8, sobel_integral)

        d = 16
        _x1y1 = cv.GetSubRect(
            sobel_integral,
            (0, 0, sobel_integral.cols - d, sobel_integral.rows - d))
        _x1y2 = cv.GetSubRect(
            sobel_integral,
            (0, d, sobel_integral.cols - d, sobel_integral.rows - d))
        _x2y1 = cv.GetSubRect(
            sobel_integral,
            (d, 0, sobel_integral.cols - d, sobel_integral.rows - d))
        _x2y2 = cv.GetSubRect(
            sobel_integral,
            (d, d, sobel_integral.cols - d, sobel_integral.rows - d))

        summation = cv.CloneMat(_x2y2)
        cv.Sub(summation, _x1y2, summation)
        cv.Sub(summation, _x2y1, summation)
        cv.Add(summation, _x1y1, summation)
        sum8 = self.Cached('sum8', summation.height, summation.width,
                           cv.CV_8UC1)
        absnorm8(summation, sum8)
        cv.Threshold(sum8, sum8, 32.0, 255.0, cv.CV_THRESH_BINARY)

        cv.ShowImage("sum8", sum8)
        seq = cv.FindContours(sum8, cv.CreateMemStorage(), cv.CV_RETR_EXTERNAL)
        subimg = cv.GetSubRect(img, (d / 2, d / 2, sum8.cols, sum8.rows))
        t_cull = time.time() - started

        seqs = []
        while seq:
            seqs.append(seq)
            seq = seq.h_next()

        started = time.time()
        found = {}
        print 'seqs', len(seqs)
        for seq in seqs:
            area = cv.ContourArea(seq)
            if area > 1000:
                rect = cv.BoundingRect(seq)
                edge = int((14 / 14.) * math.sqrt(area) / 2 + 0.5)
                candidate = cv.GetSubRect(subimg, rect)
                sym = self.dm.decode(
                    candidate.width,
                    candidate.height,
                    buffer(candidate.tostring()),
                    max_count=1,
                    #min_edge = 6,
                    #max_edge = int(edge)      # Units of 2 pixels
                )
                if sym:
                    onscreen = [(d / 2 + rect[0] + x, d / 2 + rect[1] + y)
                                for (x, y) in self.dm.stats(1)[1]]
                    found[sym] = onscreen
                else:
                    print "FAILED"
        t_brute = time.time() - started
        print "cull took", t_cull, "brute", t_brute
        return found
示例#8
0
from pylab import *
import glob
import os 


i = 0
for files in glob.glob('D:/pic/car/*.jpg'):
    filepath,filename = os.path.split(files)
    image = cv2.imread(filepath + '/' + filename)
     # image = cv2.imread('D:/pic/car2.jpg')
    h,w = image.shape[:2]
    #灰度化
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    grayIPimage = cv.GetImage(cv.fromarray(gray))
    sobel  = cv.CreateImage((w, h),cv2.IPL_DEPTH_16S, 1)  #创建一张深度为16位有符号(-65536~65535)的的图像区域保持处理结果
    cv.Sobel(grayIPimage,sobel,2,0,7)         # 进行x方向的sobel检测
    temp  = cv.CreateImage(cv.GetSize(sobel),cv2.IPL_DEPTH_8U, 1)       #图像格式转换回8位深度已进行下一步处理
    cv.ConvertScale(sobel, temp,0.00390625, 0)
    cv.Threshold(temp, temp, 0, 255, cv2.THRESH_OTSU)
    kernal = cv.CreateStructuringElementEx(3,1, 1, 0, 0)
    cv.Dilate(temp, temp,kernal,2)
    cv.Erode(temp, temp,kernal,4)
    cv.Dilate(temp, temp,kernal,2)
#     cv.ShowImage('1', temp)
    kernal = cv.CreateStructuringElementEx(1,3, 0, 1, 0)
    cv.Erode(temp, temp,kernal,1)
    cv.Dilate(temp, temp,kernal,3)
#     cv.ShowImage('2', temp)
    temp = np.asarray(cv.GetMat(temp))
    contours, heirs  = cv2.findContours(temp,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    
示例#9
0
import cv2.cv as cv

im = cv.LoadImage('meinv.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE)

sobx = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
cv.Sobel(im, sobx, 1, 0, 3)  #Sobel with x-order=1

soby = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
cv.Sobel(im, soby, 0, 1, 3)  #Sobel withy-oder=1

cv.Abs(sobx, sobx)
cv.Abs(soby, soby)

result = cv.CloneImage(im)
cv.Add(sobx, soby, result)  #Add the two results together.

cv.Threshold(result, result, 100, 255, cv.CV_THRESH_BINARY_INV)

cv.ShowImage('Image', im)
cv.ShowImage('Result', result)

cv.WaitKey(0)
示例#10
0
dst = cv.CreateImage(cv.GetSize(r), 8, 1)
cv.Canny(r, dst, 100, 200)
cv.Threshold(dst, dst, 100, 255, cv.CV_THRESH_BINARY)
color_dst_standard = cv.CreateImage(cv.GetSize(r), 8, 3)
cv.CvtColor(r, color_dst_standard, cv.CV_GRAY2BGR)
lines = cv.HoughLines2(dst, cv.CreateMemStorage(0), cv.CV_HOUGH_STANDARD, 1,
                       pi / 180, 100, 0, 0)
cv.ShowImage("red edges canny", dst)
cv.SaveImage("red edges canny.png", dst)
cv.WaitKey(0)

enter = raw_input("Mostrar Sobel")
# sobel
#imagen en gris
sobx = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_16S, 1)
cv.Sobel(img, sobx, 1, 0, 3)  #Sobel with x-order=1

soby = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_16S, 1)
cv.Sobel(img, soby, 0, 1, 3)  #Sobel withy-oder=1

cv.Abs(sobx, sobx)
cv.Abs(soby, soby)

result = cv.CloneImage(img)
cv.Add(sobx, soby, result)  #Add the two results together.

cv.Threshold(result, result, 100, 255, cv.CV_THRESH_BINARY_INV)
cv.ShowImage('Grayscale edge Sobel', result)
cv.SaveImage("Grayscale edge Sobel.png", dst)
cv.WaitKey(0)
#azul