Python 清除OCR图像上的黑点

Python 清除OCR图像上的黑点,python,opencv,python-imaging-library,Python,Opencv,Python Imaging Library,我想用python清除图像上的黑点。因为我需要对图像文件应用ocr处理 我把图像转换成单色,所以我得到了这个图像; 所以,我想删除黑点 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]

我想用python清除图像上的黑点。因为我需要对图像文件应用ocr处理

我把图像转换成单色,所以我得到了这个图像;

所以,我想删除黑点

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()
def temizleHips2(x,y,w,h,listei):
koordina=list(nfind(x,y))
x=int(x)
y=int(y)
w=int(w)
h=int(h)
i=0
a=0
m=4
b=0
对于x范围内的i(8):
b=0
k=坐标[i]
x2,y2=k(k)
如果x2>=0且y2>=0且x2大于0:
对于每个输入数据:
如果每个=255:
每个=0
elif each==0:
每个=255
newData.append(每个)
img.putdata(newData)
x1,y1=0,0
tmp_isim=“a”+dosya
图像保存(“b”+tmp_isim,“TIFF”)
img=Image.open(tmp_isim)
imgmat=img.load()
x、 y=img.size
x1=0
y1=0
德格=0
temizlemes=[]
对于范围(0,x)内的x1:
对于范围(0,y)内的y1:
如果imgmat[x1,y1]==0:
deger=temizleHips(x1,y1,x,y,imgmat)
temizlemes.append(deger)
如果德格imgmat[x1,y1]:
打印“noktalar:”+str(x1)+“,”+str(y1)+“ilk:”+str(imgmat[x1,y1])+“son:”+str(deger)
其他:
附加(imgmat[x1,y1])
img.putdata(temizlemes)
img.show()
图像保存(tmp_isim,“TIFF”)
tem=img.load()
我得到这个图像; 但是,我想在第二张图片上清除黑色像素周围的“s”。
我找不到问题出在哪里

你应该试着找到大轮廓并删除另一个轮廓

为什么不使用中值滤波或具有合适内核的形态学操作来完成这些工作?