2013-10-13 31 views
0

我想用python清除圖像上的黑點。因爲我需要將ocr處理應用於圖像文件。清理圖像上的黑點OCR

我將圖像轉換爲monochorome顏色,所以我得到這個圖像; http://s23.postimg.org/bulq1dmt3/ba210.png

所以,我想刪除黑點;

def temizleHips2 (x,y,w,h,listei): 
koordinat=list(nfind(x,y)) 
x = int(x) 
y = int(y) 
w = int(w) 
h = int(h) 
i=0 
a=0 
m=4 
b=0 
for i in xrange(8): 
     b=0 
     k=koordinat[i] 
     x2,y2=koordinatparse(k) 
     if x2>=0 and y2>=0 and x2<w and y2<h: 
      if listei[x2,y2]==0: 
       a=a+1 
if a>2: 
    return 0 
else: 
    return 255 

def ultratemizle(dosya):  
# 290a.tif 
image_file = dosya 
img = Image.open(image_file) 
# img=img.convert('1') 
# img.save("209i.tif","TIFF") 
datas = list(img.getdata()) 
newData = list() 
temizlemes = list() 
temizlemeson = list() 
siyah =0 
beyaz =0 
for each in datas: 
    if each == 255: 
     beyaz = beyaz +1 
    else: 
     siyah = siyah+1 
if siyah > beyaz : 
    for each in datas: 
     if each == 255: 
      each=0 
     elif each==0: 
      each = 255 
     newData.append(each) 
    img.putdata(newData) 
x1,y1=0,0 

tmp_isim = "a"+dosya 
img.save("b"+tmp_isim, "TIFF") 
img = Image.open(tmp_isim) 
imgmat = img.load() 
x,y= img.size 
x1=0 
y1=0 
deger =0 
temizlemes =[] 
for x1 in range (0,x): 
    for y1 in range(0,y): 
     if imgmat[x1,y1] == 0: 
      deger = temizleHips(x1,y1,x,y,imgmat) 
      temizlemes.append(deger) 
      if deger != imgmat[x1,y1]: 
       print "noktalar : "+str(x1)+","+str(y1)+" ilk : "+str(imgmat[x1,y1])+" son: "+str(deger) 
     else: 
      temizlemes.append(imgmat[x1,y1]) 
img.putdata(temizlemes) 
img.show() 
img.save(tmp_isim,"TIFF") 

tem = img.load() 

我看到這個圖片; http://s16.postimg.org/wc97bdzdt/a356.png 但是,我想清除第二張圖片上黑色像素周圍的「s」。 我無法找到問題的所在

+1

你爲什麼不使用中值濾波或形態學操作與合適的內核做的工作? – Mzk

回答

0

你應該嘗試找到大的輪廓和刪除其它輪廓