예제 #1
0
def readBmp(filename):
    image = [common.init_space(480, 640) for x in range(3)]
    with open(filename, 'rb') as fd:
        header = fd.read(54)
        for y in range(479, -1, -1):
            row = list(bytearray(fd.read(640 * 3)))
            for x in range(640):
                index = x * 3
                image[2][y][x] = row[index + 0]
                image[1][y][x] = row[index + 1]
                image[0][y][x] = row[index + 2]
    return image
예제 #2
0
def detect_circles(image):
    # PUT YOUR CODE HERE
    # access the image using "image[chanel][y][x]"
    # where 0 <= y < common.constants.WIDTH and 0 <= x < common.constants.HEIGHT
    # to create an auxiliar bidimentional structure
    # you can use "space=common.init_space(heigh, width)"
    numCircles = 0
    radius = 30.0
    radiusSquared = 30.0 * 30.0

    newImage = common.init_space(480, 640)
    hough = common.init_space(480, 640)

    # make edge detection greyscale image
    newImage, maxGradient = sobel(image, newImage)

    # normalize the image
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):
            newImage[y][x] = (newImage[y][x] / maxGradient) * 255

    # find the circles in hough space
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):
            if newImage[y][x] == 255:
                for a in range(640):
                    if (x - a)**2 < radiusSquared:
                        b = float(y) - math.sqrt(radiusSquared -
                                                 float((x - a)**2))
                        if 0.0 < b and b < 480.0:
                            hough[int(b)][a] += 1

    # find number of circles
    for y in range(480):
        for x in range(640):
            if hough[y][x] > 32:
                numCircles += 1

    return numCircles
예제 #3
0
def detect_circles(image):
    # PUT YOUR CODE HERE
    # access the image using "image[chanel][y][x]"
    # where 0 <= y < common.constants.WIDTH and 0 <= x < common.constants.HEIGHT
    # to create an auxiliar bidimentional structure
    # you can use "space=common.init_space(heigh, width)"

    #Sobel edge detection...
    edge_img = common.init_space(common.constants.HEIGHT,
                                 common.constants.WIDTH)
    for y in range(1, common.constants.HEIGHT - 1):
        for x in range(1, common.constants.WIDTH - 1):
            edge_img[y][x] = abs(sobel_x(image, y, x)) + abs(
                sobel_y(image, y, x))
    space = common.init_space(common.constants.HEIGHT, common.constants.WIDTH)
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):
            val = edge_img[y][x]
            if val != 0:
                for yc in range(y - 30, y + 31):
                    for xc in range(x - 30, x + 31):
                        if xc < common.constants.WIDTH and xc >= 0 and yc < common.constants.HEIGHT and yc >= 0:
                            rad = (y - yc)**2 + (x - xc)**2
                            if rad <= 908 and rad >= 892:
                                space[yc][xc] += 1
    maxval = 0
    count = 0
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):
            val = space[y][x]
            if val > maxval:
                count = 1
                maxval = val
            elif val == maxval:
                count += 1

    return count
def detect_circles(image):
    r = 30
    votes = common.init_space(common.constants.HEIGHT, common.constants.WIDTH)
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):
            pixel = (image[0][y][x], image[1][y][x], image[2][y][x])
            if background(y, x, image) == False:
                neighbouring_pixels = [
                    (image[0][y][x - 1], image[1][y][x - 1],
                     image[2][y][x - 1]),
                    (image[0][y][x + 1], image[1][y][x + 1],
                     image[2][y][x + 1]),
                    (image[0][y - 1][x], image[1][y - 1][x],
                     image[2][y - 1][x]),
                    (image[0][y + 1][x], image[1][y + 1][x],
                     image[2][y + 1][x]),
                    (image[0][y - 1][x - 1], image[1][y - 1][x - 1],
                     image[2][y - 1][x - 1]),
                    (image[0][y + 1][x + 1], image[1][y + 1][x + 1],
                     image[2][y + 1][x + 1]),
                    (image[0][y - 1][x - 1], image[1][y - 1][x - 1],
                     image[2][y - 1][x - 1]),
                    (image[0][y + 1][x + 1], image[1][y + 1][x + 1],
                     image[2][y + 1][x + 1])
                ]
                if (255, 255, 255) in neighbouring_pixels:
                    for i in range(y - r, y + r):
                        for j in range(x - r, x + r):
                            if background(i, j, image) == False and (
                                (y - i) * (y - i) + (x - j) * (x - j) == 900):
                                votes[i][j] = votes[i][j] + 1
                                # print("test")
    max_votes = votes[0][0]
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):
            if votes[y][x] > max_votes:
                max_votes = votes[y][x]
    count = 0
    for rows in votes:
        for value in rows:
            if value == max_votes:
                count = count + 1
    return count
예제 #5
0
def detect_slope_intercept(image):
    # PUT YOUR CODE HERE
    # access the image using "image[chanel][y][x]"
    # where 0 <= y < common.constants.WIDTH and 0 <= x < common.constants.HEIGHT
    # set line.m and line.b
    # to create an auxiliar bidimentional structure
    # you can use "space=common.init_space(heigh, width)"
    line = common.Line()

    bmin = -1000
    bmax = 1000
    mmin = -10
    mmax = 10
    #line_space = {}
    space = common.init_space(2000, 2000)
    bestm = 0
    bestb = 0
    maxval = 0
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):
            r = image[0][y][x]
            g = image[1][y][x]
            b = image[2][y][x]
            if r == 0 and g == 0 and b == 0:  # black pixel
                # find feasible lines
                #b = y - m*x
                for m in range(2000):
                    mval = (m - 1000) * 0.01
                    b = y - mval * x
                    if b >= bmin and b <= bmax:
                        b = int(b)
                        space[m][b] += 1
                        if space[m][b] >= maxval:
                            bestb = b
                            bestm = mval
                            maxval = space[m][b]
    # find the highest voted line
    line.m = bestm
    line.b = bestb
    return line
예제 #6
0
def detect_slope_intercept(image):
    # PUT YOUR CODE HERE
    # access the image using "image[chanel][y][x]"
    # where 0 <= y < common.constants.WIDTH and 0 <= x < common.constants.HEIGHT
    # set line.m and line.b
    # to create an auxiliar bidimentional structure
    # you can use "space=common.init_space(heigh, width)"
    line = common.Line()
    hough = common.init_space(2000, 2000)

    # go over image
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):

            if ((image[0][y][x] == 0) and (image[1][y][x] == 0)
                    and (image[2][y][x] == 0)):

                for m_graph in range(0, 2000):
                    m = float(m_graph) / float(100) - 10
                    b = -(m * x) + y
                    if (b > -1000 and b < 1000):
                        hough[m_graph][int(b) + 1000] += 1

    maximum = -1

    # find max in hough space
    for y in range(2000):
        for x in range(2000):
            if hough[y][x] > maximum:
                maximum = hough[y][x]
                m = float(y) / 100 - 10
                b = x - 1000.4

    line.m = m
    line.b = b

    return line
예제 #7
0
def detect_normal(image):
    # PUT YOUR CODE HERE
    # access the image using "image[chanel][y][x]"
    # where 0 <= y < common.constants.WIDTH and 0 <= x < common.constants.HEIGHT
    # set line.theta and line.r
    # to create an auxiliar bidimentional structure
    # you can use "space=common.init_space(heigh, width)"
    line = common.Line()
    hough = common.init_space(1800, 1800)

    # go over image
    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):

            if ((image[0][y][x] == 0) and (image[1][y][x] == 0)
                    and (image[2][y][x] == 0)):

                for theta_graph in range(0, 1800):
                    theta = math.radians(float(theta_graph) / float(10))
                    r = x * math.cos(theta) - y * math.sin(theta)
                    if (r > -900 and r < 900):
                        hough[theta_graph][int(r) + 900] += 1

    maximum = -1

    # find max in hough space
    for y in range(1800):
        for x in range(1800):
            if hough[y][x] > maximum:
                maximum = hough[y][x]
                theta = math.pi - math.radians(float(y) / float(10))
                r = 900 - x
    line.r = r
    line.theta = theta

    return line
예제 #8
0
def detect_normal(image):
    # PUT YOUR CODE HERE
    # access the image using "image[chanel][y][x]"
    # where 0 <= y < common.constants.WIDTH and 0 <= x < common.constants.HEIGHT
    # set line.theta and line.r
    # to create an auxiliar bidimentional structure
    # you can use "space=common.init_space(heigh, width)"
    line = common.Line()

    space = common.init_space(1800, 1800)

    for y in range(common.constants.HEIGHT):
        for x in range(common.constants.WIDTH):
            r = image[0][y][x]
            g = image[1][y][x]
            b = image[2][y][x]
            if r == 0 and g == 0 and b == 0:  # black pixel
                for deg in range(1800):
                    R = x * math.cos(deg * math.pi / 1800) + y * math.sin(
                        deg * math.pi / 1800)
                    if R <= 900 and R >= -900:
                        R = int(R) + 900
                        space[R][deg] += 1
    bestR = 0
    bestdeg = 0
    maxval = 0
    for deg in range(1800):
        for R in range(1800):
            val = space[R][deg]
            if val >= maxval:
                maxval = val
                bestdeg = deg
                bestR = R
    line.r = bestR - 900
    line.theta = (bestdeg * math.pi / 1800)
    return line