使用PIL修剪扫描的图像?

使用PIL修剪扫描的图像?,第1张

使用PIL修剪扫描的图像?

熵解似乎是有问题的,并且计算量过大。为什么不进行边缘检测?

我只是写了这个python代码来为自己解决这个问题。我的背景是白色的,所以我使用的标准是黑暗和彩色。我通过仅取每个像素的R,B或B值中的最小值来简化此标准,以便黑色或饱和红色都脱颖而出。我还使用了每一行或每一列的最暗像素的平均值。然后,我从每个边缘开始,一直努力直到超过阈值。

这是我的代码:

#these values set how sensitive the bounding box detection isthreshold = 200     #the average of the darkest values must be _below_ this to count (0 is darkest, 255 is lightest)obviousness = 50    #how many of the darkest pixels to include (1 would mean a single dark pixel triggers it)from PIL import Imagedef find_line(vals):    #implement edge detection once, use many times     for i,tmp in enumerate(vals):        tmp.sort()        average = float(sum(tmp[:obviousness]))/len(tmp[:obviousness])        if average <= threshold: return i    return i    #i is left over from failed threshold finding, it is the boundsdef getbox(img):    #get the bounding box of the interesting part of a PIL image object    #this is done by getting the darekest of the R, G or B value of each pixel    #and finding were the edge gest dark/colored enough    #returns a tuple of (left,upper,right,lower)    width, height = img.size    #for making a 2d array    retval = [0,0,width,height] #values will be disposed of, but this is a black image's box    pixels = list(img.getdata())    vals = []        #store the value of the darkest color    for pixel in pixels:        vals.append(min(pixel)) #the darkest of the R,G or B values    #make 2d array    vals = np.array([vals[i * width:(i + 1) * width] for i in xrange(height)])    #start with upper bounds    forupper = vals.copy()    retval[1] = find_line(forupper)    #next, do lower bounds    forlower = vals.copy()    forlower = np.flipud(forlower)    retval[3] = height - find_line(forlower)    #left edge, same as before but roatate the data so left edge is top edge    forleft = vals.copy()    forleft = np.swapaxes(forleft,0,1)    retval[0] = find_line(forleft)    #and right edge is bottom edge of rotated array    forright = vals.copy()    forright = np.swapaxes(forright,0,1)    forright = np.flipud(forright)    retval[2] = width - find_line(forright)    if retval[0] >= retval[2] or retval[1] >= retval[3]:        print "error, bounding box is not legit"        return None    return tuple(retval)if __name__ == '__main__':    image = Image.open('cat.jpg')    box = getbox(image)    print "result is: ",box    result = image.crop(box)    result.show()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5668361.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存