灰度中心法
灰度重心法是根据每行光条纹横截面内的灰度分布特征逐行进行处理,通过在行坐标的方向上,逐行计算提取光条纹区域的灰度重心点,并将该点用来代表该截面的光条纹中心点位置,最后将所有中心点拟合形成光条纹中心线。具体操作步骤是,先对图像进行滤波除去噪声,然后再用阈值分割,保留大于阈值的部分,如图1-6左图是分割前的图像,图1-6右图是分割后的图像,其中区间(m, n)由阈值T决定。灰度重心法计算光条纹中心点的公式见式(1-7),光条纹第v列的灰度重心坐标为 ,在包含U行、V列的图像中坐标(u, v)处的像素灰度值为I(u,v),其中u=1,2,3,…,U; v=1,2,3…,V。
灰度重心法减小了光条纹灰度分布不均匀导致的误差,提高了光条纹中心线提取精度,而且由于算法简单,运用灰度重心法提取光条纹中心线时运算速度快,实时性好。但由于在每行的光条纹截面中参与计算的像素点的总数量不同,噪声干扰也会影响计算精度,所以中心点的位置坐标会出现沿行坐标方向的偏移误差。
python代码
import cv2
import numpy as np
def getLines(gray):
minLineLength = 15
maxLineGap = 5
lines = cv2.HoughLinesP(gray, 1.0, np.pi/180, 10, minLineLength=minLineLength, maxLineGap=maxLineGap)
return lines
def drawLine(lines,img):
for line in lines:
line = line[0]
cv2.line(img, (line[0], line[1]), (line[2], line[3]), 255)
def Gravity(img):
row,col,chanel = img.shape
lineimage = np.zeros((row,col),dtype=np.uint8)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
points= []
newimage = np.zeros((row,col),np.uint8)
for i in range(col):
Pmax = np.max(gray[:,i])
Prow = np.argmax(gray[:,i])
#print(Prow)
points.append([Prow,i])
for p in points:
#print(p)
newimage[p[0],p[1]] = 255
img[p[0],p[1],:] = [0,255,0]
cv2.namedWindow("origin",0)
cv2.namedWindow("centerLine",0)
lines = getLines(newimage)
drawLine(lines,lineimage)
cv2.imshow("origin",img)
cv2.imshow("lineimage",lineimage)
cv2.imshow("centerLine",newimage)
def GravityPlus(img, thresh):
row, col, chanel = img.shape
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
points = np.zeros((col,2))
newimage = np.zeros((row, col), np.uint8)
for i in range(col):
Pmax = np.max(gray[:, i])
#Pmin = np.min(gray[:, i])
if Pmax < thresh:
continue
pos = np.argwhere(gray[:,i]>=(Pmax-5))
#print("pos",pos)
length = len(pos)
sum_top,sum_down = 0.0, 0.0
if pos[-1]-pos[0] == length - 1:
#print("good cols",i)
for p in pos:
sum_top += p*gray[p,i]
sum_down += gray[p,i]
Prow = sum_top / sum_down
points[i]=[Prow[0],i]
for p in points:
#print(p)
pr,pc = map(int,p)
newimage[pr,pc] = 255
img[pr,pc,:] = [0,0,225]
# cv2.namedWindow("Plus_origin",0)
# cv2.namedWindow("Plus_centerLine",0)
# cv2.imshow("Plus_origin",img)
# cv2.imshow("Plus_centerLine",newimage)
return img, newimage
def GravityPlusK(img,thresh,k):
row, col, chanel = img.shape
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
points = np.zeros((col,2))
newimage = np.zeros((row, col), np.uint8)
for i in range(col):
posMax = np.argmax(gray[:, i])
Pmax = gray[posMax, i]
#Pmin = np.min(gray[:, i])
if Pmax<thresh:
continue
sumPix = 0
sumVal = 0
for index in range(-k,k):
sumPix+=gray[posMax+index,i]*(posMax+index)
sumVal+=gray[posMax+index,i]
valCenter =sumPix/sumVal
points[i]=[valCenter,i]
print(points)
for p in points:
#print(p)
pr,pc = map(int,p)
newimage[pr,pc] = 255
img[pr,pc,:] = [0,0,255]
cv2.namedWindow("Plus_origin",0)
cv2.namedWindow("Plus_centerLine",0)
cv2.imshow("Plus_origin",img)
cv2.imshow("Plus_centerLine",newimage)
return points
import time
start_time = time.time()
img = cv2.imread("./crop.png")
GravityPlus(img,100)
cv2.imshow("image", img)
cv2.waitKey(0)
print("One image need time: ", time.time() - start_time)
# if __name__ == "__main__":
# import time
# import os
# import tqdm
# image_path = "./image/"
# save_path = "./paper/ggm/"
# if not os.path.isdir(save_path): os.makedirs(save_path)
#
# sum_time = 0
# for img in tqdm.tqdm(os.listdir(image_path)):
# image = cv2.imread(os.path.join(image_path, img))
# start_time = time.time()
# _, image_c = GravityPlus(image, 50)
# end_time = time.time()
# sum_time += end_time - start_time
# cv2.imwrite(os.path.join(save_path, img), image_c)
# average_time = sum_time / len(os.listdir(image_path))
# print("Average one image time: ", average_time)
import cv2
import numpy as np
import time
def GravityCen(img):
row, col, chanel = img.shape
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
points = []
newimage = np.zeros((row, col), np.uint8)
for i in range(col):
pos = np.argmax(gray[:, i])
Pmax = gray[pos, i]
Pmin = np.min(gray[:, i])
if Pmax==Pmin:
continue
length = 2
sum =0.0
down = 0.0
for j in range(-2,3):
colp = pos+j
print(colp)
sum += colp*gray[colp,i]
down +=gray[colp,i]
Prow = sum/down
points.append([Prow,i])
for p in points:
#print(p)
pr,pc = map(int,p)
newimage[pr,pc] = 255
img[pr,pc,:] = [0,255,0]
cv2.namedWindow("Plus_origin",0)
cv2.namedWindow("Plus_centerLine",0)
cv2.imshow("Plus_origin",img)
cv2.imshow("Plus_centerLine",newimage)
return points
start_time = time.time()
img = cv2.imread("./3.png")
#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#np.savetxt("laser-v02.txt",gray,fmt="%.3d")
#rows,cols = gray.shape
blur = cv2.blur(img,(5,5))
points = GravityCen(blur)
#cv2.imshow("gray",gray)
cv2.imshow("blur",blur)
cv2.waitKey(0)
print("One image need time: ", time.time() - start_time)