2017-02-14 65 views
0

我正在尋找一種方法,我可以使用自動裁剪幾個地塊..沒有我手動必須設置裁剪框的大小。智能裁剪圖像

我需要裁剪頻譜曲線像這樣的列表,

enter image description here

在這我只需要在實際的情節,而不是其他。只是情節。

目前我正在像這樣修剪它。

print "Hstacked Image" 
images1 = Image.open(spectogram_path_train+"/"+name+"_plot_static_conv.png") 
images2 = Image.open(spectogram_path_train+"/"+name+"_plot_delta_conv.png") 
images3 =  Image.open(spectogram_path_train+"/"+name+"_plot_delta_delta_conv.png") 

box = (100,55,592,496) 
cropped1 = images1.crop(box) 
cropped2 = images2.crop(box) 
cropped3 = images3.crop(box) 

width1, height1 = cropped1.size 
width2, height2 = cropped2.size 
width3, height3 = cropped3.size 

sum_width = width1 + width2 + width3 
max_height = max(height1,height2,height3) 

new_im = Image.new('RGB',(sum_width,max_height)) 
x_offset = 0 

for im in [cropped1,cropped2,cropped3]: 
    new_im.paste(im,(x_offset,0)) 
    x_offset+=im.size[0] 

new_im.save(spectogram_path_train+"/"+name+"_plot_hstacked.png") 

這些框中的值設置爲裁剪這張圖片..框的左下參數始終是每個情節相同,但權可能會有所不同,它具有爲每個情節來自動確定。

我正在尋找一種智能作物,除了彩色陰謀外,還能去除所有的東西。

+1

除非您可以找到一些專門的第三方模塊來做這種事情,否則您可以通過查看像素值來確定上邊緣和右邊緣的位置。如果繪圖圖像與白色背景完全相似,則應該能夠通過從左側位置搜索右側來找到邊界,直到多個背景像素開始爲止,並且同樣向上朝向上邊緣。 – martineau

+1

哦..對不起。我以@馬蒂諾的方式解決了這個問題。 該解決方案是相當... – Loser

回答

0

所以..我決定跟隨@martineau建議的提出,利用了一個解決方案。

images1 = Image.open(static) 
images2 = Image.open(delta) 
images3 = Image.open(delta_delta) 

data_numpy = np.array(images1) 
number = 0 
right = 0 

for i in data_numpy[55,:]: 
# print i 
    number+=1 
    if i[0] == 234 and i[1] == 234 and i[2] == 242 and i[3] == 255 and number > 100: 
#  print "Found it!" 
     right = number 
     break 
    if i[0] == 255 and i[1] == 255 and i[2] == 255 and i[3] == 255 and number > 100: 
     right = number 
     break 
#print right 

box = (100,55,right,496) 

cropped1 = images1.crop(box) 
cropped2 = images2.crop(box) 
cropped3 = images3.crop(box) 

我希望的代碼不言自明,如果不是..

的for循環遍歷(一行只需要進行檢查,由於地塊的大小)的影像,並找到像素位置與灰色相同。當找到時將for循環中斷,並創建一個符合所需大小的框。

1

我不知道Python,但是您可以在終端上使用ImageMagick而不使用任何高級語言,它安裝在大多數Linux發行版上,可用於macOS和Windows。

首先,請注意,由於某種原因您的圖像有一個多餘的alpha通道,所以我將其關閉。

然後,我注意到所有你感興趣的東西都是飽和的顏色,所有無關的文本都是黑色/灰色和不飽和的,所以我會轉向飽和度作爲判別式。輸入終端的這個命令加載你的圖像,並將所有像素設置爲黑色,即零,它們是不飽和的,並保留其他地方的當前值。然後修剪邊界並保存結果。

convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim z.png 

enter image description here

如果我現在再次運行該命令,但只提取像素的頂級單排,並尋找第一個黑色一個,我會知道在哪裏可以裁剪:

convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim +repage -crop x1! txt: | awk -F, '/black/{print $1;exit}' 

496 

所以,我需要在列496,我與做裁剪:

convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim +repage -crop 496x+0+0 z.png 

enter image description here

如果我想自動完成整個過程中,我可以這樣做:

x=$(convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim +repage -crop x1! txt: | awk -F, '/black/{print $1;exit}') 
convert spectrum.png -alpha off -fx "saturation<0.2?0:u" -trim +repage -crop ${x}x+0+0 y.png 
+0

我沒有得到嘗試你的解決方案..我需要裁剪它在代碼中,因爲我需要裁剪和堆疊在一起..而且我猜測會需要更多的代碼.. – Loser