使用python

时间:2018-07-19 10:02:19

标签: python python-3.x image-processing python-imaging-library straight-line-detection

这是我的代码,用于从单个图像中将多个对象裁剪为一个图像,但是我面临一个问题,也有点好奇地想知道是否要知道主图像中有多少个对象,然后我将如何为此,我需要调整行检测功能,我写了很多书,并增加了更多的逻辑。由于我是初学者,因此面临挑战。

我的代码如下:

import math, cv2
from scipy import misc
import numpy

def getFactors(num):

    sqt = int(math.sqrt(num))
    if (num % sqt) == 0:
        return (sqt,int(num/sqt))

    num1 = sqt
    num2 = sqt
    while True:
        num1 += 1
        num2 -= 1
        if (num1 >= num) or (num2 <= 0):
            return (num, 1)
        if (num % num1) == 0:
            return (num1, int(num/num1))
        if (num % num2) == 0:
            return (num2, int(num/num2))
    return

def splitImage(img, numsplits):

    # Get the factors for splitting. So if the number of splits is 9, then (3,3)
    # or if 6 then (2,3) etc.
    factors = getFactors(numsplits)
    # Height and width of each split
    h = int(img.shape[0] / factors[0])
    w = int(img.shape[1] / factors[1])
    # Handle both color and B&W images
    if img.ndim >= 3:
        size = (h,w,img.shape[2],numsplits)
    else:
        size = (h,w,numsplits)
    # Initialize the result array
    res = numpy.ndarray( size, dtype = img.dtype )
    # Iterate through the number of factors to split the source image horizontally
    # and vertically, and store the resultant chunks
    for i in range(factors[0]):
        for j in range(factors[1]):
            if img.ndim >= 3:
                res[:,:,:,((i*factors[1])+j)] = img[(i*h):((i+1)*h), (j*w):((j+1)*w),:]
            else:
                res[:,:,((i*factors[1])+j)] = img[(i*h):((i+1)*h), (j*w):((j+1)*w)]

    return res

def cropImage(img):

    # Convert image to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detect edges and lines
    edges = cv2.Canny(gray, 50, 150, apertureSize = 3)
    lines = cv2.HoughLines(edges, 1, numpy.pi/90, 200)

    min_x = img.shape[0]
    max_x = 0
    min_y = img.shape[1]
    max_y = 0
    # Find the extremal horizontal and vertical coordinates to crop
    for i in range(len(lines[:,0,0])):
        rho = lines[i,0,0]
        theta = lines[i,0,1]
        a = numpy.cos(theta)
        b = numpy.sin(theta)
        x = a*rho
        y = b*rho

        if abs(a) < 1e-06 :
            if min_y > int(y):
                min_y = int(y)
            if max_y < int(y):
                max_y = int(y)
        if abs(b) < 1e-06 :
            if min_x > int(x):
                min_x = int(x)
            if max_x < int(x):
                max_x = int(x)

    return img[min_y:max_y, min_x:max_x, :]

# Read image     
img = misc.imread('imaagi.jpg')
# Crop the image
img = cropImage(img)
# Call the splitter function
res = splitImage(img, 6)
# Save the results to files
for i in range(res.shape[-1]):
    if img.ndim >= 3:
        misc.imsave('res_{0:03d}.png'.format(i),res[:,:,:,i])
    else:
        misc.imsave('res_{0:03d}.png'.format(i),res[:,:,i])

我需要调整我编写的行检测功能并为其添加更多逻辑,以便它可以检测并打印主图像中存在多少个对象?

0 个答案:

没有答案