2024-10-01 02:25:27 发布
网友
我有大量的截图需要裁剪。所有的图像看起来都很相似-有一个蓝色边框的矩形窗口,里面包含一些图形元素。此窗口包含在另一个窗口中,但我只需要裁剪内部窗口。在所有图像中,内部窗口的尺寸和内容都是不同的。大多数情况下,内容包括矩形的元素,有时还有蓝色边框,与内部窗口的边框相同。我之所以提到这一点,是因为我想到了以下流程:
遍历目标目录中所有图像的脚本。对于每个人:
如何做到这一点?Python不是强制的,也可以是任何其他的。在
这并不简单,但这是一个可能的方法:
import matplotlib.pyplot as plt import numpy as np def synthimage(): w,h = 300,200 im = np.random.randint(0,255,(w,h,3))/255 xa = np.random.randint(50,w-60) xb = xa + np.random.randint(50,90) ya = np.random.randint(50,h-60) yb = ya + np.random.randint(20,50) im[xa:xb,ya] = np.array([1,0,0]) im[xa:xb,yb] = np.array([1,0,0]) im[xa,ya:yb] = np.array([1,0,0]) im[xb,ya:yb] = np.array([1,0,0]) return im def getRectPoints(im): x,y = [],[] for i in range(im.shape[0]): for j in range(im.shape[1]): if (im[i,j]-np.array([1,0,0])).sum()==0: x.append(i) y.append(j) return np.array(x),np.array(y) def denoise(x,y): nx,ny = [],[] for i in range(x.shape[0]): d = np.sqrt((x[i]-x)**2+(y[i]-y)**2) m = d<2 if len(m.nonzero()[0])>2: nx.append(x[i]) ny.append(y[i]) return np.array(nx),np.array(ny) im = synthimage() plt.imshow(np.swapaxes(im,0,1),origin='lower',interpolation='nearest') plt.show() x,y = getRectPoints(im) plt.scatter(x,y,c='red') plt.xlim(0,300) plt.ylim(0,200) plt.show() nx,ny = denoise(x,y) plt.scatter(nx,ny,c='red') plt.xlim(0,300) plt.ylim(0,200) plt.show() #Assuming rectangle has no rotation (otherwise check Scipy ConveHull) xmi = nx.min() xma = nx.max() ymi = ny.min() yma = ny.max() new = np.ones(im.shape) new[xmi:xma,ymi:yma] = im[xmi:xma,ymi:yma] plt.imshow(np.swapaxes(new,0,1),origin='lower',interpolation='nearest') plt.show()
,函数的名称应该是自解释的。为这项工作的目的生成了合成数据。结果如下(按顺序):
显然,每个步骤都可以根据需求进行更改,但对于大多数案例研究来说,这是一个功能性的解决方案。在
这并不简单,但这是一个可能的方法:
,函数的名称应该是自解释的。为这项工作的目的生成了合成数据。结果如下(按顺序):
显然,每个步骤都可以根据需求进行更改,但对于大多数案例研究来说,这是一个功能性的解决方案。在
相关问题 更多 >
编程相关推荐