问题描述
我想裁剪图像,为此,我在 Python 中使用了 pyvips
包(如果您有另一个包可以让我比我的方法更快地完成工作,请共享它)。我的代码没有按预期工作:
在我所有的代码下面:
import sys
import pyvips
from skimage import io
from matplotlib import pyplot as plt
import numpy as np
def show_image(image,title='Image',cmap_type='gray'):
plt.imshow(image,cmap=cmap_type)
plt.title(title)
plt.axis('off')
plt.show()
def outline(file_name,new_file_name):
file = os.path.join(file_name)
im = pyvips.Image.new_from_file(file)
# find the value of the pixel at (0,0) ... we will search for all pixels
# significantly different from this
background = im(0,0)
# we need to smooth the image,subtract the background from every pixel,take
# the absolute value of the difference,then threshold
mask = (im.median(3) - background).abs() > 10
# sum mask rows and columns,then search for the first non-zero sum in each
# direction
columns,rows = mask.project()
# .profile() returns a pair (v-profile,h-profile)
left = columns.profile()[1].min()
right = columns.width - columns.fliphor().profile()[1].min()
top = rows.profile()[0].min()
bottom = rows.height - rows.flipver().profile()[0].min()
# and Now crop the original image
im = im.crop(left,top,right - left,bottom - top)
im.write_to_file(new_file_name)
return show_image(io.imread(os.path.join(new_file_name)))
对于这样的图像:
我的代码有效,但不适用于我想要裁剪的图像:
这里我只想检索红色矩形中的内容。
为此,我只更改了大纲函数中的一件事,即更改了背景值:
limit = [157.0,101.0,112.0]
background = limit
这里的限制是我要裁剪的图像中红色的RVB代码。而且我不知道为什么我的函数没有返回我想要的红色矩形。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)