i have big number of screenshots need cropped. images similar - there rectangular window blue border, containing graphical elements inside. window contained inside 1 need crop inner window. across images dimensions of inner window different , content. content in cases includes elements rectangular form , - blue border, same border inner window. mentioning because thinking of following flow:
a script goes through images in target directory. each of them:
- find area cropped (inner window)
- crop area
- save file
how can done? python not compulsory, can other also.
it's not straightforward possible recipe:
import matplotlib.pyplot plt import numpy 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 = [],[] in range(im.shape[0]): 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 = [],[] 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()
, name of functions should self-explaining. synthetic data generated purpose of exercise. results (in order):
obviously each 1 of steps can changed depending on requirements functional solution majority of case-studies.
Comments
Post a Comment