You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

282 lines
8.5 KiB

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import sys
import re
def getImage(filename:str,rows,cols):
f = open(filename)
text = f.read()
# buffer = text.split("\t")
# buffer = re.split("\t|\n|\ ",text)
buffer = text.split()
image = np.zeros((rows,cols))
for i in range(rows):
for j in range(cols):
image[i,j] = float(buffer[cols*i+j])
return image
def getDualHist(image0:np.array,image1:np.array):
min = np.min([np.min(image0),np.min(image1)])
max = np.max([np.max(image0),np.max(image1)])
image0 = ((image0 -min)*255/(max-min)).astype(np.uint8)
image1 = ((image1 -min)*255/(max-min)).astype(np.uint8)
hist0 = np.zeros((256))
hist1 = np.zeros((256))
for i in range(image0.shape[0]):
for j in range(image0.shape[1]):
hist0[image0[i,j]] += 1
hist1[image1[i,j]] += 1
return min,max,hist0,hist1
def getHist(image:np.array,minmax = None):
if(minmax):
min = minmax[0]
max = minmax[1]
else:
min,max = np.min(image),np.max(image)
image = (image-min)*255/(max-min)
image = image.astype(np.int32)
hist = np.zeros(256)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
if(image[i,j]<256 and image[i,j] >=0):
hist[image[i,j]] += 1
return min, max,hist
def subImage(image0:np.array,image1:np.array)->np.array:
img = image0-image1
return img
def divImage(image0:np.array,image1:np.array)->np.array:
img = image0/image1
return img
def diedPoint(image:np.array, mask:np.array):
threshold = np.sum(image*(1-mask))/np.sum(1-mask)/2
# print("died point thre", threshold)
mask = np.zeros(image.shape)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
mask[i,j] = 0 if image[i,j]>=threshold else 1
return mask
def hotPoint(image:np.array,mask:np.array):
threshold = np.sum(image* (1-mask))*2/np.sum(1-mask)
# print("hot point thre ",threshold)
mask = np.zeros(image.shape)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
mask [i,j] = 0 if image[i,j]<= threshold else 1
return mask
def getValidPoint(image:np.array,noise:np.array):
maskd = []
maskd.append(np.zeros(image.shape))
maskh = []
maskh.append(np.zeros(image.shape))
maskd.append( diedPoint(image,np.sign(maskh[-1]+maskd[-1])))
maskh.append( hotPoint(noise, np.sign(maskh[-1]+maskd[-1])))
# print(np.sum(maskd[-1]),np.sum(maskh[-1]))
while True:
maskd.append( diedPoint(image,np.sign(maskh[-1]+maskd[-1])))
maskh.append( hotPoint(noise, np.sign(maskh[-1]+maskd[-1])))
h = np.sum(maskh[-2])
d = np.sum(maskd[-2])
dh = np.sum(maskh[-1])-np.sum(maskh[-2])
dd = np.sum(maskd[-1])-np.sum(maskd[-2])
# print(d,dd,h,dh)
if dd*1000<=d and dh*1000<=h:
break
return maskd[-1],maskh[-1]
# maskdt = diedPoint
def getAnotherPoint(image:np.array,pos:(int,int),index:int):
if image[pos] >= 0:
return index,image
image[pos] = index
for row in range(-5,6):
if pos[0]+row<0 or pos[0]+row>=image.shape[0]:
continue
for col in range(-5,6):
if pos[1]+col<0 or pos[1]+col>=image.shape[1]:
continue
getAnotherPoint(image,(pos[0]+row,pos[1]+col),index)
return index+1, image
# if pos[0]-1>=0:
# if pos[1]-1 >=0:
# getAnotherPoint(image,(pos[0]-1,pos[1]-1),index)
# if pos[1]+1<image.shape[1]:
# getAnotherPoint(image,(pos[0]-1,pos[1]+1),index)
# if pos[0]+1<image.shape[0]:
# if pos[1]-1 >=0:
# getAnotherPoint(image,(pos[0]+1,pos[1]-1),index)
# if pos[1]+1<image.shape[1]:
# getAnotherPoint(image,(pos[0]+1,pos[1]+1),index)
def getCluster(r:int,dirs:int,image:np.array,rs:int, cs:int):
kernel = np.ones((2*r+1,2*r+1))
for i in range(2*r+1):
for j in range(2*r+1):
if np.power(i-r,2)+np.power(j-r,2)>r*r:
kernel[i,j] = 0
mask = cv.dilate(image.astype(np.uint8),kernel.astype(np.uint8))
num,rst = cv.connectedComponents(mask.astype(np.uint8),connectivity=dirs)
cluster = np.zeros(num)
log = []
for i in range(num):
log.append(""+str(i)+"簇:")
for i in range(image.shape[0]):
for j in range(image.shape[1]):
if image[i,j]>0:
cluster[int(rst[i,j])] += 1
log[int(rst[i,j])]+="\n"+str(int(i+rs))+","+str(int(j+cs))
else:
rst[i,j] = 0
# mask = -image
# cluster = []
# idx = 1
# for i in range(image.shape[0]):
# for j in range(image.shape[1]):
# idx,mask = getAnotherPoint(mask,(i,j),idx)
plt.imsave(str(image.shape[1])+"x"+str(image.shape[0])+".png",rst,cmap="Blues")
f = open(str(image.shape[1])+"x"+str(image.shape[0])+".txt","w")
# cluster = np.zeros(idx)
# for i in range(image.shape[0]):
# for j in range(image.shape[1]):
# if mask[i,j] != 0:
# cluster[int(mask[i,j])] += 1
# log[int(mask[i,j])]+="\n"+str(int(i+rs))+","+str(int(j+cs))
for i in range(1,num):
log[i]+="\n"+str(int(cluster[i]))+"个坏点"
f.write(log[i]+"\n")
f.close()
return cluster
def calcCluster(r, dirs, image:np.array):
# sys.setrecursionlimit(1000000)
width = image.shape[0]
height = image.shape[1]
base = int(image.shape[1]/640)
ret = []
w = base*64
h = base*64
imtemp = image[int(width/2)-int(w/2):int(width/2)+int(w/2),int(height/2)-int(h/2):int(height/2)+int(h/2)]
clu = getCluster(r,dirs, imtemp, width/2-imtemp.shape[0]/2, int(image.shape[1]/2)-imtemp.shape[1]/2)
sum = len(clu)
p = np.zeros(4)
for i in range(1,len(clu)):
if clu[i]<=3:
p+=np.array([1,1,1,1])
elif clu[i] <= 6:
p+=np.array([0,1,1,1])
elif clu[i] <= 12:
p+= np.array([0,0,1,1])
else:
p+= np.array([0,0,0,1])
ret.append(sum-1)
ret.append(p[0])
ret.append(p[1]-p[0])
ret.append(p[3]-p[1])
ret.insert(0,str(imtemp.shape[0])+"x"+str(imtemp.shape[1]))
# ret.append(p[3]-p[2])
w = base*128
h = base*128
imtemp = image[int(width/2)-int(w/2):int(width/2)+int(w/2),int(height/2)-int(h/2):int(height/2)+int(h/2)]
clu = getCluster(r,dirs, imtemp, width/2-imtemp.shape[0]/2, int(image.shape[1]/2)-imtemp.shape[1]/2)
sum = len(clu)
p = np.zeros(4)
for i in range(1,len(clu)):
if clu[i]<=3:
p+=np.array([1,1,1,1])
elif clu[i] <= 6:
p+=np.array([0,1,1,1])
elif clu[i] <= 12:
p+= np.array([0,0,1,1])
else:
p+= np.array([0,0,0,1])
ret.append(sum-1)
ret.append(p[0])
ret.append(p[1]-p[0])
ret.append(p[3]-p[1])
ret.insert(1,str(imtemp.shape[0])+"x"+str(imtemp.shape[1]))
# ret.append(p[3]-p[2])
w = base*256
h = base*320
imtemp = image[int(width/2)-int(w/2):int(width/2)+int(w/2),int(height/2)-int(h/2):int(height/2)+int(h/2)]
clu = getCluster(r,dirs,imtemp, width/2-imtemp.shape[0]/2, int(image.shape[1]/2)-imtemp.shape[1]/2)
sum = len(clu)
p = np.zeros(4)
for i in range(1,len(clu)):
if clu[i]<=3:
p+=np.array([1,1,1,1])
elif clu[i] <= 6:
p+=np.array([0,1,1,1])
elif clu[i] <= 12:
p+= np.array([0,0,1,1])
else:
p+= np.array([0,0,0,1])
ret.append(sum-1)
ret.append(p[0])
ret.append(p[1]-p[0])
ret.append(p[3]-p[1])
ret.insert(2,str(imtemp.shape[0])+"x"+str(imtemp.shape[1]))
# ret.append(p[3]-p[2])
image[int(width/2)-int(w/2):int(width/2)+int(w/2),int(height/2)-int(h/2):int(height/2)+int(h/2)] = 0
clu = getCluster(r,dirs,image,0,0)
sum = len(clu)
p = np.zeros(4)
for i in range(1,len(clu)):
if clu[i]<=3:
p+=np.array([1,1,1,1])
elif clu[i] <= 6:
p+=np.array([0,1,1,1])
elif clu[i] <= 12:
p+= np.array([0,0,1,1])
else:
p+= np.array([0,0,0,1])
ret.append(sum-1)
# ret.append(p[1]-p[0])
ret.append(p[2]-p[1])
ret.append(p[3]-p[2])
ret.insert(3,str(imtemp.shape[0])+"x"+str(imtemp.shape[1])+"之外")
return ret
def getEF(ms2:np.array, ms15:np.array):
a = np.abs( np.average(ms15) - np.average(ms2))*1000
b = np.std(ms15-ms2)*1000
return [a,b]