def score_of(self,line,image):
     image_grad_x = cv.CreateImage(cv.GetSize(image),cv.IPL_DEPTH_16S,1)
     image_grad_y = cv.CreateImage(cv.GetSize(image),cv.IPL_DEPTH_16S,1)
     cv.Sobel(image,image_grad_x,0,1)
     cv.Sobel(image,image_grad_y,0,1)
     scores = [self.score_pt(pt,image_grad_y) for pt in sampled(line,50)]
     return norm(scores)
示例#2
0
def edge_threshold(image, roi=None, debug=0):
    thresholded = cv.CloneImage(image)
    horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1)
    magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)
    vertical = cv.CloneImage(horizontal)
    v_edge = cv.CloneImage(image)
    magnitude = cv.CloneImage(horizontal)

    storage = cv.CreateMemStorage(0)
    mag = cv.CloneImage(image)
    cv.Sobel(image, horizontal, 0, 1, 1)
    cv.Sobel(image, vertical, 1, 0, 1)
    cv.Pow(horizontal, horizontal, 2)
    cv.Pow(vertical, vertical, 2)

    cv.Add(vertical, horizontal, magnitude)
    cv.Convert(magnitude, magnitude32f)
    cv.Pow(magnitude32f, magnitude32f, 0.5)
    cv.Convert(magnitude32f, mag)
    if roi:
        cv.And(mag, roi, mag)
    cv.Normalize(mag, mag, 0, 255, cv.CV_MINMAX, None)
    cv.Threshold(mag, mag, 122, 255, cv.CV_THRESH_BINARY)
    draw_image = cv.CloneImage(image)
    and_image = cv.CloneImage(image)
    results = []

    threshold_start = 17
    for window_size in range(threshold_start, threshold_start + 1, 1):
        r = 20
        for threshold in range(0, r):
            cv.AdaptiveThreshold(image, thresholded, 255, \
                cv.CV_ADAPTIVE_THRESH_MEAN_C, cv.CV_THRESH_BINARY_INV, window_size, threshold)
            contour_image = cv.CloneImage(thresholded)
            contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST)
            cv.Zero(draw_image)
            cv.DrawContours(draw_image, contours, (255, 255, 255),
                            (255, 255, 255), 1, 1)
            if roi:
                cv.And(draw_image, roi, draw_image)
            cv.And(draw_image, mag, and_image)
            m1 = np.asarray(cv.GetMat(draw_image))
            m2 = np.asarray(cv.GetMat(mag))
            total = mag.width * mag.height  #cv.Sum(draw_image)[0]

            coverage = cv.Sum(and_image)[0] / (mag.width * mag.height)
            if debug:
                print threshold, coverage
                cv.ShowImage("main", draw_image)
                cv.ShowImage("main2", thresholded)
                cv.WaitKey(0)
            results.append((coverage, threshold, window_size))

    results.sort(lambda x, y: cmp(y, x))
    _, threshold, window_size = results[0]
    cv.AdaptiveThreshold(image, thresholded, 255, cv.CV_ADAPTIVE_THRESH_MEAN_C, \
        cv.CV_THRESH_BINARY, window_size, threshold)

    return thresholded
示例#3
0
def gradient(img):
	cols, rows = cv.GetSize(img)

	x_drv = cv.CreateMat(rows,cols,cv.CV_32FC1)
	y_drv = cv.CreateMat(rows,cols,cv.CV_32FC1)
	mag = cv.CreateMat(rows,cols,cv.CV_32FC1)
	ang = cv.CreateMat(rows,cols,cv.CV_32FC1)

	cv.Sobel(img, x_drv, 1, 0)
	cv.Sobel(img, y_drv, 0, 1)
	cv.CartToPolar(x_drv,y_drv,mag,ang)
	return (mag,ang)
示例#4
0
def calc_hog(im, numorient=9):
    """
    calculate integral HOG (Histogram of Orientation Gradient) image (w,h,numorient)
    
    calc_hog(im, numorient=9)
    
    returns 
        Integral HOG image

    params 
        im : color image
        numorient : number of orientation bins, default is 9 (-4..4)
    
    """
    sz = cv.GetSize(im)
    gr = cv.CreateImage(sz, 8, 1)
    gx = cv.CreateImage(sz, 32, 1)
    gy = cv.CreateImage(sz, 32, 1)

    #convert to grayscale
    cv.CvtColor(im, gr, cv.CV_BGR2GRAY)

    #calc gradient using sobel
    cv.Sobel(gr, gx, 1, 0, 3)
    cv.Sobel(gr, gy, 0, 1, 3)

    #calc initial result
    hog = np.zeros((sz[1], sz[0], numorient))
    mid = numorient / 2
    for y in xrange(0, sz[1] - 1):
        for x in xrange(0, sz[0] - 1):
            angle = int(round(
                mid * np.arctan2(gy[y, x], gx[y, x]) / np.pi)) + mid
            magnitude = np.sqrt(gx[y, x] * gx[y, x] + gy[y, x] * gy[y, x])
            hog[y, x, angle] += magnitude

    #build integral image
    for x in xrange(1, sz[0] - 1):
        for ang in xrange(numorient):
            hog[y, x, ang] += hog[y, x - 1, ang]
    for y in xrange(1, sz[1] - 1):
        for ang in xrange(numorient):
            hog[y, x, ang] += hog[y - 1, x, ang]
    for y in xrange(1, sz[1] - 1):
        for x in xrange(1, sz[0] - 1):
            for ang in xrange(numorient):
                #tambah kiri dan atas, kurangi dengan kiri-atas
                hog[y, x, ang] += hog[y - 1, x,
                                      ang] + hog[y, x - 1,
                                                 ang] - hog[y - 1, x - 1, ang]
    return hog
示例#5
0
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
    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
示例#7
0
def edge_magnitude(image):
    magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)
    horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1)
    vertical = cv.CloneImage(horizontal)
    magnitude = cv.CloneImage(horizontal)

    mag = cv.CloneImage(image)
    cv.Sobel(image, horizontal, 0, 1, 1)
    cv.Sobel(image, vertical, 1, 0, 1)
    cv.Pow(horizontal, horizontal, 2)
    cv.Pow(vertical, vertical, 2)

    cv.Add(vertical, horizontal, magnitude)
    cv.Convert(magnitude, magnitude32f)
    cv.Pow(magnitude32f, magnitude32f, 0.5)
    cv.Convert(magnitude32f, mag)
    return mag
def sobel(im, xorder=1, yorder=1):
    recommended_size = {
        1: 3, 2: 3, 3: 5,
        4: 5, 5:7, 6: 7, 7:9, 8:9
    }
    im = rgb2gray(im)
    new_im = cv.CreateImage(cv.GetSize(im), cv.IPL_DEPTH_16S, 1)
    cv.Sobel(im, new_im, xorder, yorder,
        apertureSize=recommended_size[max(xorder, yorder)])
    cv.ConvertScaleAbs(new_im, im)
    return im
示例#9
0
 def image_to_show( self ):
     if self.mode == 0:
         return self.image
     gray_image = cv.CreateImage( (self.image.width, self.image.height), self.image.depth, 1 )
     cv.CvtColor(self.image, gray_image, cv.CV_BGR2GRAY)
     smooth_image = cv.CreateImage( (gray_image.width, gray_image.height), gray_image.depth, 1 )
     cv.Smooth(gray_image, smooth_image, param1=self.smooth_param, param2=self.smooth_param)
     if self.mode == 1:
         return smooth_image
     dx = cv.CreateImage( (smooth_image.width, smooth_image.height), cv.IPL_DEPTH_32F, 1 )
     cv.Sobel(smooth_image,dx,1,0)
     dy = cv.CreateImage( (smooth_image.width, smooth_image.height), cv.IPL_DEPTH_32F, 1 )
     cv.Sobel(smooth_image,dy,0,1)
     if self.mode == 2:
         return dx
     if self.mode == 3:
         return dy
     else:
         sum = cv.CreateImage( (dx.width,dx.height),dx.depth,1 )
         cv.Add(dx,dy,sum)
         cv.ConvertScale(sum,sum,0.5)
         return sum
         
     return self.view_image
示例#10
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 __produce_gradient_image(self, i, scale):
        size = cv.GetSize(i)
        grey_image = cv.CreateImage(size, 8, 1)

        size = [s / scale for s in size]
        grey_image_small = cv.CreateImage(size, 8, 1)

        cv.CvtColor(i, grey_image, cv.CV_RGB2GRAY)

        df_dx = cv.CreateImage(cv.GetSize(i), cv.IPL_DEPTH_16S, 1)
        cv.Sobel(grey_image, df_dx, 1, 1)
        cv.Convert(df_dx, grey_image)
        cv.Resize(grey_image,
                  grey_image_small)  #, interpolation=cv.CV_INTER_NN)
        cv.Resize(grey_image_small,
                  grey_image)  #, interpolation=cv.CV_INTER_NN)
        return grey_image
 def edge_classify(self,cv_image,l_line,r_line):
     image_hsv = cv.CloneImage(cv_image)
     cv.CvtColor(cv_image,image_hsv,cv.CV_RGB2HSV)
     image_hue = cv.CreateImage(cv.GetSize(image_hsv),cv.IPL_DEPTH_8U,1)
     cv.Split(image_hsv,image_hue,None,None,None)
     image_grad = cv.CreateImage(cv.GetSize(image_hsv),cv.IPL_DEPTH_16S,1)
     cv.Sobel(image_hue,image_grad,0,1)
     scores = []
     for line in (l_line,r_line):
         score = self.score_of(line,image_hue)
         print "Score: "
         print score
         scores.append(score)
 
     if scores[0] > scores[1]:
         return RIGHT
     else:
         return LEFT
 def find_wrinkles(self):
     rect = self.rect
     sob_mat = cv.CreateImage((rect[2], rect[3]), cv.IPL_DEPTH_8U,
                              self.frame_copy.nChannels)
     if self.frame_copy.origin == cv.IPL_ORIGIN_TL:
         cv.Copy(self.frame_copy, sob_mat)
     else:
         cv.Flip(self.frame_copy, sob_mat, 0)
     #sob_mat = cv.CloneMat(self.frame_copy)
     cv.Sobel(sob_mat, sob_mat, 1, 1)
     y_max, y_badania1, y_badania2, x_max, i = rect[
         3], rect[3] / 5, rect[3] / 4, rect[2], 0
     twarz = sob_mat
     for x in range(x_max):
         if twarz[x, y_badania1][0] + twarz[x, y_badania1][1] + twarz[
                 x, y_badania1][2] > self.white_level:
             i += 1
         if twarz[x, y_badania2][0] + twarz[x, y_badania2][1] + twarz[
                 x, y_badania2][2] > self.white_level:
             i += 1
     self.wrinkles = i / 2
     return self.wrinkles
示例#14
0
 def find_wrinkles(self):
     #full face image wrinkles
     rect = self.rect
     sob_mat = cv.CreateImage((rect[2], rect[3]), cv.IPL_DEPTH_8U,
                              self.frame_copy.nChannels)
     if self.frame_copy.origin == cv.IPL_ORIGIN_TL:
         cv.Copy(self.frame_copy, sob_mat)
     else:
         cv.Flip(self.frame_copy, sob_mat, 0)
     cv.Sobel(sob_mat, sob_mat, 1, 1)
     y_max, y_badania1, y_badania2, x_max, i = rect[
         3], rect[3] / 5, rect[3] / 4, rect[2], 0
     twarz = sob_mat
     for x in range(x_max):
         if twarz[x, y_badania1][0] + twarz[x, y_badania1][1] + twarz[
                 x, y_badania1][2] > self.white_level:
             i += 1
         if twarz[x, y_badania2][0] + twarz[x, y_badania2][1] + twarz[
                 x, y_badania2][2] > self.white_level:
             i += 1
     wrinkles_full = i / 2
     #teraz badam tylko cere
     #crop ratio  - left:0.25 , right:0.25 , up:0.12 , down:0.12
     rect_skin = (int(rect[2] * 0.25), int(rect[3] * 0.12),
                  int(rect[2] * 0.5), int(rect[3] * 0.76))
     twarz = cv.GetSubRect(sob_mat, rect_skin)
     y_max, y_badania1, y_badania2, x_max, i = rect_skin[
         3], rect_skin[3] / 5, rect_skin[3] / 4, rect_skin[2], 0
     for x in range(x_max):
         if twarz[x, y_badania1][0] + twarz[x, y_badania1][1] + twarz[
                 x, y_badania1][2] > self.white_level:
             i += 1
         if twarz[x, y_badania2][0] + twarz[x, y_badania2][1] + twarz[
                 x, y_badania2][2] > self.white_level:
             i += 1
     wrinkles_small = i / 2
     #zwracam stosunek zmarszczek na cerze do zmarszczek na calym zdjeciu twarzy
     return wrinkles_small / float(wrinkles_full)
示例#15
0
from numpy import *
from pylab import *

import cv

image = cv.LoadImage('miches.jpg',cv.CV_LOAD_IMA­GE_GRAYSCALE)

# create the window
cv.NamedWindow('Miches', cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage('Miches', image) # show the image
cv.WaitKey() # close window


# Sobel operator
sobel = cv.CreateMat(image.height, image.width, cv.CV_32FC1)
cv.Sobel(image,sobel,1,1,3)
cv.ShowImage('Miches Sobel', sobel)
cv.SaveImage('miches_sobel.jpg', sobel)
cv.WaitKey()
________________________________________­___________________________

blur_smooth.py

from PIL import Image
from numpy import *
from pylab import *

import cv


# this will load and show an image in gray scale
示例#16
0
def detect(image, debug=False, display=None):
    work_image = cv.CreateImage((image.width, image.height), 8, 1)
    cv.CvtColor(image, work_image, cv.CV_BGR2GRAY)
    image = work_image
    edge = cv.CloneImage(image)
    thresholded = cv.CloneImage(image)
    v_edges = cv.CloneImage(image)
    h_edges = cv.CloneImage(image)
    vertical = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1)
    cv.Sobel(image, vertical, 1, 0, 1)
    cv.Abs(vertical, vertical)
    cv.Convert(vertical, v_edges)
    storage = cv.CreateMemStorage(0)
    result = np.asarray(cv.GetMat(v_edges), dtype=np.float)
    threshold = 6
    rects = []
    while len(rects) < 1 and threshold > 0:
        rects = []
        cv.Convert(vertical, v_edges)
        cv.AdaptiveThreshold(v_edges, v_edges, 255,
                             cv.CV_ADAPTIVE_THRESH_MEAN_C,
                             cv.CV_THRESH_BINARY_INV, 17, threshold)
        threshold -= 1
        storage = cv.CreateMemStorage(0)
        contour_image = cv.CloneImage(v_edges)
        contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST,
                                   cv.CV_CHAIN_APPROX_NONE, (0, 0))
        ext.filter_contours(contours, 30, ext.LESSTHAN)
        max_size = int(image.width * image.height * 0.1)
        # ext.filter_contours(contours, 200**2, ext.GREATERTHAN)
        ext.filter_contours(contours, max_size, ext.GREATERTHAN)

        if display:
            cv.Merge(v_edges, v_edges, v_edges, None, display)
        seeds = []
        if contours:
            seq = contours
            rects = []
            while seq:
                c = ext.as_contour(ext.wrapped(seq))
                r = (c.rect.x, c.rect.y, c.rect.width, c.rect.height)
                rects.append(r)
                if display:
                    cv.Rectangle(
                        display, (c.rect.x, c.rect.y),
                        (c.rect.x + c.rect.width, c.rect.y + c.rect.height),
                        (0, 0, 255), 1)
                seq = seq.h_next()
    rects.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2))
    seeds = rects[:]
    seeds.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3]))
    groups = []
    skip = False
    for seed in seeds:
        if seed not in rects:
            break
        found = False
        for group in groups:
            if seed in group:
                found = True
        if found:
            continue
        r = seed

        start = seed
        start_index = rects.index(seed)
        groups.append([seed])
        i = start_index - 1
        # delta = max(150, seed[2]/2)
        delta = seed[2] * 0.66
        if debug:
            print "left", seed, delta
            col = (randint(0, 255), randint(0, 255), randint(0, 255))
            cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]),
                         (255, 255, 255), 3)
            cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]),
                         col, -1)
            cv.ShowImage("main", display)
            if not skip:
                c = cv.WaitKey(0)
            if c == ord("a"):
                skip = True
        # scan left
        while 1:
            if i < 0:
                break
            rect = rects[i]
            if rect[0] + rect[2] < seed[0] - delta:
                if debug:
                    print "esc1", rect
                break
            if in_vertical(seed, start, rect):
                seed = rect
                groups[-1].append(rect)
                r = rect
                if debug:
                    print rect
                    cv.Rectangle(display, (r[0], r[1]),
                                 (r[0] + r[2], r[1] + r[3]), col, -1)
                    cv.ShowImage("main", display)
                    if not skip:
                        c = cv.WaitKey(0)
                    if c == ord("a"):
                        skip = True
            else:
                if debug:
                    print "rej1", rect
            i -= 1
        # scan right
        seed = start
        start_index = rects.index(seed)
        i = start_index + 1
        if debug:
            print
            print "right", seed
        while 1:
            if i >= len(rects):
                break
            rect = rects[i]
            if rect[0] > seed[0] + seed[2] + delta:
                if debug:
                    print "esc2", rect, rect[0] + rect[2] / 2, seed[
                        0] + seed[2] / 2 + delta
                break
            if in_vertical(seed, start, rect):
                seed = rect
                groups[-1].append(rect)
                r = rect
                if debug:
                    print rect
                    cv.Rectangle(display, (r[0], r[1]),
                                 (r[0] + r[2], r[1] + r[3]), col, -1)
                    cv.ShowImage("main", display)
                    if not skip:
                        c = cv.WaitKey(0)
                    if c == ord("a"):
                        skip = True
            else:
                if debug:
                    print "rej2", rect
            i += 1
        if debug:
            print

    # find min and max extent of group
    group_rects = []
    for group in groups:
        min_x, min_y = 1E6, 1E6
        max_x, max_y = -1, -1
        dev = []
        col = (randint(0, 255), randint(0, 255), randint(0, 255))
        for rect in group:
            r = rect
            if display:
                if r == group[0]:
                    cv.Rectangle(display, (r[0], r[1]),
                                 (r[0] + r[2], r[1] + r[3]), (255, 255, 255),
                                 3)
                cv.Rectangle(display, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]),
                             col, -1)
            min_x = min(min_x, r[0])
            min_y = min(min_y, r[1])
            max_x = max(max_x, r[0] + r[2])
            max_y = max(max_y, r[1] + r[3])
        if display:
            cv.Rectangle(display, (min_x, min_y), (max_x, max_y), (0, 255, 0),
                         1)
        width = max_x - min_x
        height = max_y - min_y
        rect = (min_x, min_y, width, height)
        group_rects.append(rect)
    return group_rects
示例#17
0
def segment_rect(image,
                 rect,
                 debug=False,
                 display=None,
                 target_size=None,
                 group_range=(3, 25)):
    global next
    skip = False
    best_chars = []
    best_threshold = None
    thresholded = cv.CloneImage(image)
    contour_image = cv.CloneImage(image)
    edges = cv.CloneImage(image)

    min_x, min_y, width, height = rect
    # cv.SetImageROI(thresholded, rect)
    cv.SetImageROI(contour_image, rect)
    cv.SetImageROI(image, rect)
    cv.SetImageROI(edges, rect)

    horizontal = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_16S, 1)
    magnitude32f = cv.CreateImage(cv.GetSize(image), cv.IPL_DEPTH_32F, 1)
    vertical = cv.CloneImage(horizontal)
    magnitude = cv.CloneImage(horizontal)
    cv.Sobel(image, horizontal, 0, 1, 3)
    cv.Sobel(image, vertical, 1, 0, 3)
    cv.Pow(horizontal, horizontal, 2)
    cv.Pow(vertical, vertical, 2)
    cv.Add(vertical, horizontal, magnitude)
    cv.Convert(magnitude, magnitude32f)
    cv.Pow(magnitude32f, magnitude32f, 0.5)
    cv.Convert(magnitude32f, edges)

    original_rect = rect
    if display:
        cv.SetImageROI(display, rect)
    for threshold in range(1, 20, 1):
        cv.SetImageROI(thresholded, original_rect)
        #for i in range(30, 60, 1):
        if display:
            cv.Merge(image, image, image, None, display)
        cv.Copy(image, thresholded)
        #cv.Threshold(thresholded, thresholded, i, 255, cv.CV_THRESH_BINARY_INV)
        cv.AdaptiveThreshold(thresholded, thresholded, 255,
                             cv.CV_ADAPTIVE_THRESH_MEAN_C,
                             cv.CV_THRESH_BINARY_INV, 17, threshold)
        #cv.AdaptiveThreshold(thresholded, thresholded, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 5, i)
        # skip rects greater than 50% thresholded
        summed = cv.Norm(thresholded, None, cv.CV_L1,
                         None) / 255 / thresholded.width / thresholded.height
        if summed > 0.5:
            continue
        if debug:
            cv.ShowImage("edge", thresholded)
        storage = cv.CreateMemStorage(0)
        cv.Copy(thresholded, contour_image)
        contours = cv.FindContours(contour_image, storage, cv.CV_RETR_LIST,
                                   cv.CV_CHAIN_APPROX_SIMPLE, (0, 0))
        ext.filter_contours(contours, 20, ext.LESSTHAN)
        groups = []
        rects = []
        edge_counts = []
        overlappings = {}
        if contours:
            seq = contours
            while seq:
                c = ext.as_contour(ext.wrapped(seq))
                r = (c.rect.x, c.rect.y, c.rect.width, c.rect.height)
                rects.append(r)
                seq = seq.h_next()
            similarity = 0.45  #0.3
            rects.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3]))
            for rect in rects:
                if debug:
                    print
                    print "R", rect, len(groups)
                cv.SetImageROI(edges,
                               (original_rect[0] + rect[0],
                                original_rect[1] + rect[1], rect[2], rect[3]))
                edge_count = cv.Sum(edges)[0] / 255 / (rect[2] * rect[3])
                edge_counts.append(edge_count)
                #                cv.ShowImage("edges", edges)
                #                cv.WaitKey(0)
                if debug and target_size:
                    print "X", target_size, rect
                    print(target_size[0] - rect[2]) / target_size[0]
                    print(target_size[1] - rect[3]) / target_size[1]
                if rect[2] > rect[3] or float(rect[3])/rect[2] < 3./3 or edge_count < 0.1\
                or (rect[2] == image.width and rect[3] == image.height) \
                or (target_size and not 0 < (target_size[0] - rect[2]) / target_size[0] < 0.3 \
                and not 0 < (target_size[1] - rect[3]) / target_size[1] < 0.05):
                    if debug:
                        print "rej", rect[2], ">", rect[3], "edge=", edge_count
                        cv.Rectangle(display, (rect[0], rect[1]),
                                     (rect[0] + rect[2], rect[1] + rect[3]),
                                     (0, 0, 255), 1)
                        cv.ShowImage("main", display)
                        if not skip and not next:
                            c = cv.WaitKey(0)
                            if c == ord("a"):
                                skip = True
                            if c == ord("z"):
                                next = True
                    continue
                added = False
                for group_id, group in enumerate(groups):
                    avg_width, avg_height, avg_y = 0, 0, 0
                    overlap = None
                    c = 0
                    for r in group:
                        avg_y += r[1] + r[3] / 2.0
                        avg_width += r[2]
                        avg_height += r[3]
                        irect = intersect(r, rect)
                        if irect[2] * irect[3] > 0.2 * r[2] * r[3]:
                            overlappings.setdefault(group_id,
                                                    []).append([r, rect])
                    avg_y /= float(len(group))
                    avg_width /= float(len(group))
                    avg_height /= float(len(group))
                    if debug:
                        print group
                    if (abs(avg_width - rect[2]) / avg_width < similarity or \
                     (rect[2] < avg_width)) and \
                    abs(avg_height - rect[3])/ avg_height < similarity and \
                    abs(avg_y - (rect[1] + rect[3]/2.0)) / avg_y < similarity:
                        group.append(rect)
                        added = True
                    else:
                        pass
                if not added:
                    # first char in group
                    groups.append([rect])
                if debug:
                    print "now:"
                    for g in groups:
                        print g
                    cv.Rectangle(display, (rect[0], rect[1]),
                                 (rect[0] + rect[2], rect[1] + rect[3]),
                                 (255, 0, 0), 1)
                    cv.ShowImage("main", display)
                    if not skip and not next:
                        c = cv.WaitKey(0)
                        if c == ord("a"):
                            skip = True
                        if c == ord("z"):
                            next = True
        if groups:
            #handle overlapping regions, default to average width match
            for group_id, over in overlappings.items():
                group = groups[group_id]
                avg_width = 0
                avg_height = 0
                for r in group:
                    avg_width += r[2]
                    avg_height += r[3]
                avg_width /= float(len(group))
                avg_height /= float(len(group))
                for r1, r2 in over:
                    if r2 not in group or r1 not in group:
                        continue
                    if debug:
                        print "over", r1, r2, r1[2] * r1[3], r2[2] * r2[
                            3], avg_width
                    d1 = abs(r1[2] - avg_width) + abs(r1[3] - avg_height)
                    d2 = abs(r2[2] - avg_width) + abs(r2[3] - avg_height)
                    if d1 < d2:
                        group.remove(r2)
                    else:
                        group.remove(r1)

            #group = max(groups, key=len)
            # from longest groups, find largest area
            groups.sort(key=len)
            groups.reverse()
            max_area = 0
            mad_index = -1
            for i, g in enumerate(groups[:5]):
                area = 0
                for r in g:
                    area += r[2] * r[3]
                if area > max_area:
                    max_area = area
                    max_index = i
            group = groups[max_index]
            # vertical splitting
            avg_width, avg_height, avg_y = 0, 0, 0
            if debug:
                print "G", group
            for r in group:
                avg_y += r[1] + r[3] / 2.0
                avg_width += r[2]
                avg_height += r[3]
            avg_y /= float(len(group))
            avg_width /= float(len(group))
            avg_height /= float(len(group))
            band_rects = []
            bound = bounding_rect(group)
            for i, rect in enumerate(rects):
                if edge_counts[i] < 0.1:
                    continue
                if (abs(avg_width - rect[2]) / avg_width < similarity or \
                 (rect[2] < avg_width)) and \
                 (abs(avg_height - rect[3]) / avg_height < similarity or  \
                 (rect[3] < avg_height)) and \
                abs(avg_y - (rect[1] + rect[3]/2.0)) < avg_height/2:
                    band_rects.append(rect)

            band_rects.sort(lambda x, y: cmp(y[2] * y[3], x[2] * x[3]))

            for i, rect_a in enumerate(band_rects[:-1]):
                if rect_a[2] * rect_a[3] < 0.2 * avg_width * avg_height:
                    continue
                merge_rects = []
                for rect_b in band_rects[i + 1:]:
                    w = avg_width
                    m1 = rect_a[0] + rect_a[2] / 2
                    m2 = rect_b[0] + rect_b[2] / 2
                    if abs(m1 - m2) < w:
                        merge_rects.append(rect_b)
                if debug:
                    print "M", merge_rects
                if merge_rects:
                    merge_rects.append(rect_a)
                    rect = bounding_rect(merge_rects)
                    area = 0
                    for r in merge_rects:
                        area += r[2] * r[3]
                    if (abs(avg_width - rect[2]) / avg_width < similarity or \
                    (rect[2] < avg_width)) and \
                    abs(avg_height - rect[3])/ avg_height < similarity and \
                    area > 0.5*(avg_width*avg_height) and \
                    abs(avg_y - (rect[1] + rect[3]/2.0)) / avg_y < similarity:
                        for r in merge_rects:
                            if r in group:
                                group.remove(r)
                        # merge into group
                        new_group = []
                        merged = False
                        for gr in group:
                            area2 = max(gr[2] * gr[3], rect[2] * rect[3])
                            isect = intersect(gr, rect)
                            if isect[2] * isect[3] > 0.4 * area2:
                                x = min(gr[0], rect[0])
                                y = min(gr[1], rect[1])
                                x2 = max(gr[0] + gr[2], rect[0] + rect[2])
                                y2 = max(gr[1] + gr[3], rect[1] + rect[3])
                                new_rect = (x, y, x2 - x, y2 - y)
                                new_group.append(new_rect)
                                merged = True
                            else:
                                new_group.append(gr)
                        if not merged:
                            new_group.append(rect)
                        group = new_group
                        cv.Rectangle(display, (rect[0], rect[1]),
                                     (rect[0] + rect[2], rect[1] + rect[3]),
                                     (255, 0, 255), 2)
            # avoid splitting
            split = False
            # select higher threshold if innovates significantly
            best_width = 0.0
            if best_chars:
                best_area = 0.0
                for rect in best_chars:
                    best_area += rect[2] * rect[3]
                    best_width += rect[2]
                best_width /= len(best_chars)
                area = 0.0
                overlapped = 0.0
                avg_width = 0.0
                avg_height = 0.0
                for rect in group:
                    area += rect[2] * rect[3]
                    avg_width += rect[2]
                    avg_height += rect[3]
                    for char in best_chars:
                        section = intersect(rect, char)
                        if section[2] * section[3] > 0:
                            overlapped += section[2] * section[3]
                avg_width /= len(group)
                avg_height /= len(group)
                quotient = overlapped / area
                quotient2 = (area - overlapped) / best_area
                if debug:
                    print area, overlapped, best_area
                    print group
                    print "QUO", quotient
                    print "QUO2", quotient2
            else:
                quotient = 0
                quotient2 = 1
                best_area = 0

            group.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2))
            best_chars.sort(lambda x, y: cmp(x[0] + x[2] / 2, y[0] + y[2] / 2))
            if group_range[0] <= len(group) <= group_range[1] and avg_width > 5 and avg_height > 10 and \
            ((quotient2 > 0.05 and (best_area == 0 or abs(area - best_area)/best_area < 0.4))
            or (quotient2 > 0.3 and area > best_area)):
                if debug:
                    print "ASSIGNED", group
                best_chars = group
                best_threshold = threshold  #get_patch(thresholded, original_rect)
            else:
                if debug:
                    print "not", quotient2, len(
                        group), avg_width, avg_height, area, best_area

        # best_chars = groups
        if debug:
            for rect in best_chars:
                cv.Rectangle(display, (rect[0], rect[1]),
                             (rect[0] + rect[2], rect[1] + rect[3]),
                             (0, 255, 0), 1)
            cv.ShowImage("main", display)
            if not skip and not next:
                c = cv.WaitKey(0)
                if c == ord("a"):
                    skip = True
                if c == ord("z"):
                    next = True
    best_chars.sort(lambda x, y: cmp(x[0], y[0]))
    cv.ResetImageROI(thresholded)
    cv.ResetImageROI(contour_image)
    cv.ResetImageROI(image)
    cv.ResetImageROI(edges)
    if display:
        cv.ResetImageROI(display)
    return best_chars, best_threshold
示例#18
0
                fo.write('\t')
        #fo.write(str(loopVar1+1))
        if loopVar1 != len(features) - 1:
            fo.write('\n')
    fo.close()


#---------------------------------------------------------------------------------------------#
# Load image 1 and get the corner points
feature_vectors = []
for loopVar1 in range(0, 26):
    orig_img_1 = cv.LoadImage(filename1 + str(loopVar1 + 1) + ".jpg", 1)
    input_img_1 = cv.LoadImage(filename1 + str(loopVar1 + 1) + ".jpg", 0)
    dy_img_1 = cv.CreateMat(orig_img_1.height, orig_img_1.width, cv.CV_32F)
    dx_img_1 = cv.CreateMat(orig_img_1.height, orig_img_1.width, cv.CV_32F)
    cv.Sobel(input_img_1, dy_img_1, 0, 1, apertureSize=3)
    cv.Sobel(input_img_1, dx_img_1, 1, 0, apertureSize=3)

    corner_img_1 = find_corner_strength(dx_img_1, dy_img_1, k,
                                        wh)  # Find the corner strengths
    th_r = 9e+8
    corner_strengths = non_max_supress(
        orig_img_1, corner_img_1, wr,
        th_r)  # Apply threshold and non-maximum supression
    #print len(corner_strengths), len(corner_strengths)-N
    #print corner_strengths
    corner_strengths = sorted(corner_strengths, key=itemgetter(0))
    best_corners = []
    for loopVar2 in range(
            len(corner_strengths) - 1,
            len(corner_strengths) - 1 - N, -1):
示例#19
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
示例#20
0
文件: sobel.py 项目: Nano1993/Lab3
#! /usr/bin/env python

from SimpleCV import Camera, Display, Image
import time
import matplotlib.pyplot as plt
import cv
import numpy as np

im = cv.LoadImage('/home/pi/Documents/Lab3/foto4.png',
                  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)
#!/usr/bin/env python
import cv
from cv2 import *
import math

# Set the camera
cam = VideoCapture(0)

# Read an image and save it
s, image = cam.read()
if s:
    # *** USER: change name of the file
    imwrite('cam_image.jpg', image)

# Open the image
image = cv.LoadImage('cam_image.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE)

# Code from glowing python (glowingpython.blogspot.co.uk/2011/10/beginning-with-opencv-in-python.html)
# Create a matrix of the same size than the original but with one channel.
dstSobel = cv.CreateMat(image.height, image.width, cv.CV_32FC1)

# Apply sobel algorithm
cv.Sobel(image, dstSobel, 1, 1, 3)
# End of code from glowing python

# Save the image
cv.SaveImage('sobel.jpg', dstSobel)
示例#22
0
#### Code for BARCODE detection  ######
import cv, sys
imgco = cv.LoadImage('/home/agnihotri/barcode/JPEG/counterservice_03.jpg')
img = cv.CreateImage(cv.GetSize(imgco), 8, 1)
imgx = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_16S, 1)
imgy = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_16S, 1)
thresh = cv.CreateImage(cv.GetSize(img), 8, 1)

### Convert image to grayscale ###
cv.CvtColor(imgco, img, cv.CV_BGR2GRAY)

### Finding horizontal and vertical gradient ###

cv.Sobel(img, imgx, 1, 0, 3)
cv.Abs(imgx, imgx)

cv.Sobel(img, imgy, 0, 1, 3)
cv.Abs(imgy, imgy)

cv.Sub(imgx, imgy, imgx)
cv.ConvertScale(imgx, img)

### Low pass filtering ###
cv.Smooth(img, img, cv.CV_GAUSSIAN, 7, 7, 0)

### Applying Threshold ###
cv.Threshold(img, thresh, 100, 255, cv.CV_THRESH_BINARY)

cv.Erode(thresh, thresh, None, 2)
cv.Dilate(thresh, thresh, None, 5)
def colorEdgeDetector():
    # Declare as globals since we are assigning to them now
    global capture
    global capture_index

    # Declare tesseract variables
    api = tesseract.TessBaseAPI()
    api.Init(".", "eng", tesseract.OEM_DEFAULT)
    api.SetPageSegMode(tesseract.PSM_SINGLE_LINE)

    # Variable to control size of peeking window
    xDiff = 120.0
    yDiff = 40.0
    rectArea = int(xDiff * 2) * int(yDiff * 2)

    # Capture current frame
    frame = cv.QueryFrame(capture)

    # Declare other variables used to manipulate the frame
    threshold_frame = cv.CreateImage(cv.GetSize(frame), 8, 1)
    hsv_frame = cv.CreateImage(cv.GetSize(frame), 8, 3)

    while True:
        # Copy the original frame to display later
        original_frame = cv.CloneImage(frame)

        # Use Sobel filter to detect edges
        cv.Smooth(frame, frame, cv.CV_BLUR, 3, 3)
        cv.Sobel(frame, frame, 2, 0, 5)
        cv.Smooth(frame, frame, cv.CV_BLUR, 3, 3)

        # Convert frame to HSV
        cv.CvtColor(frame, hsv_frame, cv.CV_BGR2HSV)

        # Remove all pixels that aren't a teal color: RGB(0, 180, 170) HSV(170, 75, 100)
        cv.InRangeS(hsv_frame, (70, 150, 150), (100, 255, 255),
                    threshold_frame)

        # Get moments to see if what was found is a license plate or not
        moments = cv.Moments(cv.GetMat(threshold_frame, 1), 0)
        area = cv.GetCentralMoment(moments, 0, 0)
        if (area > 60000):
            # Determine the x and y coordinates of the center of the object
            x = cv.GetSpatialMoment(moments, 1, 0) / area
            y = cv.GetSpatialMoment(moments, 0, 1) / area

            # Retrieve candidate license plate and test it's characters
            if int(x - xDiff) > 0 and int(y - yDiff) > 0 and (
                    int(x - xDiff) + int(xDiff * 2)) < 640 and (
                        int(y - yDiff) + int(yDiff * 2)) < 480:
                candidate = cv.GetSubRect(original_frame,
                                          (int(x - xDiff), int(y - yDiff),
                                           int(xDiff * 2), int(yDiff * 2)))
                candidateImg = cv.CreateImage(cv.GetSize(candidate), 8, 3)
                cv.Convert(candidate, candidateImg)
                candidateGrey = cv.CreateImage(cv.GetSize(candidate), 8, 1)
                cv.CvtColor(candidateImg, candidateGrey, cv.CV_RGB2GRAY)
                tesseract.SetCvImage(candidateImg, api)
                text = api.GetUTF8Text()
                print "License Plate Characters:", text
                """
                # Regex and this don't seem to work
                if text.isalnum(): 
                    print "License Plate Characters:",text
                """

            # Draw circle on center of object
            cv.Circle(original_frame, (int(x), int(y)), 2, (255, 255, 255), 10)

            # Rectangle
            cv.Rectangle(original_frame, (int(x - xDiff), int(y - yDiff)),
                         (int(x + xDiff), int(y + yDiff)),
                         cv.CV_RGB(0, 0, 255), 1)

        # Display image
        cv.ShowImage("CS201 - Tyler Boraski - Final Project", original_frame)

        # Capture new frame
        frame = cv.QueryFrame(capture)

        # If wrong camera index is initialized, press "n" to cycle through camera indexes.
        c = cv.WaitKey(10)
        if c == "n":
            camera_index += 1  # Try the next camera index
            capture = cv.CaptureFromCAM(camera_index)
            if not capture:  # If the next camera index didn't work, reset to 0.
                camera_index = 0
                capture = cv.CaptureFromCAM(camera_index)

        # If "esc" is pressed the program will end
        esc = cv.WaitKey(7) % 0x100
        if esc == 27:
            quit()
示例#24
0
def get_xposition(image):
    """
        Param: image
        Returns: x position of the center of barcode
    """
    imgco = cv.LoadImage(image)
    img = cv.CreateImage(cv.GetSize(imgco), 8, 1)
    imgx = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_16S, 1)
    imgy = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_16S, 1)
    thresh = cv.CreateImage(cv.GetSize(img), 8, 1)

    ### Convert image to grayscale ###
    cv.CvtColor(imgco, img, cv.CV_BGR2GRAY)

    ### Finding horizontal and vertical gradient ###

    cv.Sobel(img, imgx, 1, 0, 3)
    cv.Abs(imgx, imgx)

    cv.Sobel(img, imgy, 0, 1, 3)
    cv.Abs(imgy, imgy)

    cv.Sub(imgx, imgy, imgx)
    cv.ConvertScale(imgx, img)

    ### Low pass filtering ###
    cv.Smooth(img, img, cv.CV_GAUSSIAN, 7, 7, 0)

    ### Applying Threshold ###
    cv.Threshold(img, thresh, 100, 255, cv.CV_THRESH_BINARY)

    cv.Erode(thresh, thresh, None, 2)
    cv.Dilate(thresh, thresh, None, 5)

    ### Contour finding with max. area ###
    storage = cv.CreateMemStorage(0)
    barcode_found = False
    contour = cv.FindContours(thresh, storage, cv.CV_RETR_CCOMP,
                              cv.CV_CHAIN_APPROX_SIMPLE)
    area = 0
    while contour:
        max_area = cv.ContourArea(contour)
        if max_area > area:
            area = max_area
            bar = list(contour)
            barcode_found = True
        contour = contour.h_next()

    ### Draw bounding rectangles ###
    if barcode_found:
        bound_rect = cv.BoundingRect(bar)
        pt1 = (bound_rect[0], bound_rect[1])
        pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3])
        cv.Rectangle(imgco, pt1, pt2, cv.CV_RGB(0, 255, 255), 2)
        pt = ((pt2[0] - pt1[0]), (pt2[1] - pt1[1]))
        middle = ((pt[0] / 2 + pt1[0]), (pt[1] / 2 + pt1[1]))
        print middle[0]
        if DESKTOPMODE is True:
            cv.ShowImage('img', imgco)
            cv.WaitKey(0)
        return middle[0]
    elif barcode_found is False:
        print "No barcode was found."
        return 512