目录
代码1
代码2:
#coding=utf-8
import numpy as np
import cv2
import os
import tensorflow as tf
class Point(object):
x =0
y= 0
# 定义构造方法
def __init__(self, x=0, y=0):
self.x = x
self.y = y
class tensor(object):
image_size = 32
batch_size = 256
def conv2d(self,name, l_input, w, b):
return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'), b), name=name)
def max_pool(self,name, l_input, k):
return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name)
def norm(self,name, l_input, lsize=4):
return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name)
def alex_net(self,_X, _weights, _biases):
_X = tf.reshape(_X, shape=[-1, 32, 32, 3])
conv1 = self.conv2d('conv1', _X, _weights['wc1'], _biases['bc1'])
pool1 = self.max_pool('pool1', conv1, k=2)
norm1 = self.norm('norm1', pool1, lsize=4)
# Dropout
# norm1 = tf.nn.dropout(norm1, _dropout)
conv2 = self.conv2d('conv2', norm1, _weights['wc2'], _biases['bc2'])
pool2 = self.max_pool('pool2', conv2, k=2)
norm2 = self.norm('norm2', pool2, lsize=4)
# Dropout
# norm2 = tf.nn.dropout(norm2, _dropout)
conv3 = self.conv2d('conv3', norm2, _weights['wc3'], _biases['bc3'])
pool3 = self.max_pool('pool3', conv3, k=2)
norm3 = self.norm('norm3', pool3, lsize=4)
# Dropout
# norm3 = tf.nn.dropout(norm3, _dropout)
dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]])
dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1')
dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') # Relu activation
out = tf.matmul(dense2, _weights['out']) + _biases['out']
activations = tf.identity(out, name='activations')
return out
# 0:right 1:left 2:down 3:up
def __init__(self):
self.sess = tf.InteractiveSession()
# train_image_batch, train_label_batch = utils.get_batch(trimage, trlabel, batch_size, image_size)
# test_image_batch, test_label_batch = utils.get_batch(tsimage, tslabel, batch_size, image_size)
tf.train.start_queue_runners(sess= self.sess)
self.images_ph = tf.placeholder(tf.float32, [None, self.image_size, self.image_size, 3])
# true_out = tf.placeholder(tf.float32, [None, 2])
npy_url = 'rotate_invariance_class_4_class_mini_55000.npy'
data_dict = np.load(npy_url, encoding='latin1').item()
weights = {
'wc1': tf.constant(data_dict['conv1_filters'][0], name="filters"),
'wc2': tf.constant(data_dict['conv2_filters'][0], name="filters"),
'wc3': tf.constant(data_dict['conv3_filters'][0], name="filters"),
'wd1': tf.constant(data_dict['fc1_weights'][0], name="weights"),
'wd2': tf.constant(data_dict['fc2_weights'][0], name="weights"),
'out': tf.constant(data_dict['out_weights'][0], name="weights")
}
biases = {
'bc1': tf.constant(data_dict['conv1_biases'][1], name="biases"),
'bc2': tf.constant(data_dict['conv2_biases'][1], name="biases"),
'bc3': tf.constant(data_dict['conv3_biases'][1], name="biases"),
'bd1': tf.constant(data_dict['fc1_biases'][1], name="biases"),
'bd2': tf.constant(data_dict['fc2_biases'][1], name="biases"),
'out': tf.constant(data_dict['out_biases'][1], name="biases")
}
self.pred = self.alex_net(self.images_ph, weights, biases)
prea = tf.argmax(self.pred)
self.pres = tf.nn.softmax( self.pred[0])
merge = tf.summary.merge_all()
self.sess.run(tf.global_variables_initializer())
def excute(self,target_image):
target_image = cv2.resize(target_image, (32, 32))
image_code = target_image.reshape((1, 32, 32, 3))
result = self.sess.run([ self.pres], feed_dict={ self.images_ph: image_code})
b = np.where(result[0] > 0)
print("result:",b[0][0])
return (b[0][0])
# return
class Line(object):
def __init__(self, x1,y1,x2,y2 ):
self.p1 = Point(x1,y1)
self.p2 = Point(x2,y2)
def GetLinePara(line):
line.a =line.p1.y - line.p2.y;
line.b = line.p2.x - line.p1.x;
line.c = line.p1.x *line.p2.y - line.p2.x * line.p1.y;
def GetCrossPoint(l1,l2):
GetLinePara(l1);
GetLinePara(l2);
d = l1.a * l2.b - l2.a * l1.b
p=Point()
if d==0:
return (-1,-1)
else:
p.x = int((l1.b * l2.c - l2.b * l1.c)*1.0 / d)
p.y = int((l1.c * l2.a - l2.c * l1.a)*1.0 / d)
return (p.x,p.y)
# coordinates of the number of intersections obtained
class Coordinates(object):
coord = []
def __init__(self):
self.centroidx = 0
self.centroidy = 0
self.corners = []
self.quad = 4
self.destination=[]
# find the Top right, Top left, Bottom right and Bottom left points
def calculateTRTLBRBL(self):
topoints = []
bottompoints = []
for tmp_cord in self.coord:
#tmp_cord has x and y ,len(tmp_cord)==2
if len(tmp_cord)==2:
if tmp_cord[1] < self.centroidy:
topoints.append(tmp_cord)
else:
bottompoints.append(tmp_cord)
if len(topoints)>1 and len(bottompoints)>1:
top_left = min(topoints)
top_right = max(topoints)
bottom_right = max(bottompoints)
bottom_left = min(bottompoints)
self.corners.append(top_left)
self.corners.append(top_right)
self.corners.append(bottom_right)
self.corners.append(bottom_left)
return self.corners
def rotate(img, angle):
height = img.shape[0]
width = img.shape[1]
if angle % 180 == 0:
scale = 1
elif angle % 90 == 0:
scale = float(max(height, width)) / min(height, width)
else:
scale = np.math.sqrt(pow(height, 2) + pow(width, 2)) / min(height, width)
rotateMat = cv2.getRotationMatrix2D((width / 2, height / 2), angle, scale)
rotateImg = cv2.warpAffine(img, rotateMat, (width, height))
return rotateImg # rotated image
# path=r"C:\Users\libanggeng\Pictures\yaozong/"
path=r"E:\data\0/"
# path=r"E:\data\error/"
right=0
wrong=[]
total=0
mtensor =tensor()
for filename in os.listdir(path):
if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".bmp"):
image_name=path+filename
surimg = cv2.imread(image_name)
img=surimg.copy()
pheight, pwidth = img.shape[:2]
hrate=pheight/200.0
height = int(pheight/hrate)
width=int(pwidth /hrate)
img = cv2.resize(img,(int(width) , int(height)))
hei=img.copy()
hei[:, :, :] = 0
# height, width = img.shape[:2]
mask = np.zeros(img.shape[:2],np.uint8)
# 背景模型
bgdModel = np.zeros((1,65),np.float64)
# 前景模型
fgdModel = np.zeros((1,65),np.float64)
rect = (1, 1, width - 3, height -3)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,10,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
#轮廓
# 必须先转化成灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thd = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)#这个效果最好
# 寻找轮廓
binary, contours, hierarchy = cv2.findContours(thd, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#横竖矫正
for cnt in contours:
# # 用绿色(0, 255, 0)来画出最小的矩形框架
x, y, w, h = cv2.boundingRect(cnt)
if w < width / 3 or h < height / 5:
continue
if w < h:
thd= rotate(thd,90)
surimg= rotate(surimg,90)
# cv2.imshow("sharpened", thd)
# cv2.imshow("surimg", surimg)
# cv2.waitKey()
break
# 寻找轮廓
binary, contours, hierarchy = cv2.findContours(thd, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# binary,contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 画出轮廓,-1,表示所有轮廓,画笔颜色为(0, 255, 0),即Green,粗细为3
# cv2.drawContours(img, contours, -1, (0, 255, 0), 3)
for cnt in contours:
hull = cv2.convexHull(cnt)
if len(hull)>3:
# # 用绿色(0, 255, 0)来画出最小的矩形框架
# x, y, w, h = cv2.boundingRect(cnt)
# if w3:
print("hull:", len(hull), "lines:", len(lines))
for point in lines:
for r, theta in point:
vcos = np.cos(theta)
vsin = np.sin(theta)
x0 = vcos * r
y0 = vsin * r
x1 = int(x0 + 1000 * (-vsin))
y1 = int(y0 + 1000 * (vcos))
x2 = int(x0 - 1000 * (-vsin))
y2 = int(y0 - 1000 * (vcos))
mlines.append((x0,y0,theta))
# The below for loop runs till r and theta values
# are in the range of the 2d array
values=[]
points=[]
for j in range(4):
aa=[]
values.append(aa)
points.append(aa)
topvalue=[]#顶点
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
ompactness, labels, centers = cv2.kmeans(np.array(mlines), 4,None,criteria, 20, cv2.KMEANS_RANDOM_CENTERS)
for point, allocation in zip(lines, labels):
for r, theta in point:
vcos = np.cos(theta)
vsin = np.sin(theta)
x0 = vcos * r
y0 = vsin * r
x1 = int(x0 + 1000 * (-vsin))
y1 = int(y0 + 1000 * (vcos))
x2 = int(x0 - 1000 * (-vsin))
y2 = int(y0 - 1000 * (vcos))
# cv2.line(hei, (x1, y1), (x2, y2), (255, 255, 255), 2)
print(allocation,x0,y0, point)
if allocation == 0:
cv2.line(hei, (x1, y1), (x2, y2), (0, 0, 255), 1)
values[0].append([x1,y1])
values[0].append([x2, y2])
elif allocation == 1:
cv2.line(hei, (x1, y1), (x2, y2), (0, 255, 0), 1)
values[1].append([x1, y1])
values[1].append([x2, y2])
elif allocation == 2:
cv2.line(hei, (x1, y1), (x2, y2), (255, 0, 0), 1)
values[2].append([x1, y1])
values[2].append([x2, y2])
elif allocation == 3:
cv2.line(hei, (x1, y1), (x2, y2), (255, 255, 0), 1)
values[3].append([x1, y1])
values[3].append([x2, y2])
else :
print( allocation)
# cv2.circle(blank_image_classified, (int(point[0]), int(point[1])), 1, color, -1)
print(len(labels))
for j in range(4):
[vx, vy, x, y] = cv2.fitLine(np.float32(values[j]), cv2.DIST_L2, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((width - x) * vy / vx) + y)
# cv2.line(hei, (width - 1, righty), (0, lefty), (0, 255, 255), 1)
topvalue.append([lefty, righty])
line0 = Line(0, topvalue[0][0], width - 1, topvalue[0][1])
line1 = Line(0, topvalue[1][0], width - 1, topvalue[1][1])
line2 = Line(0, topvalue[2][0], width - 1, topvalue[2][1])
line3 = Line(0, topvalue[3][0], width - 1, topvalue[3][1])
# line0= Line(topvalue[0][0],topvalue[0][1],topvalue[0][2],topvalue[0][3])
# line1 = Line(topvalue[1][0],topvalue[1][1],topvalue[1][2],topvalue[1][3])
# line2 = Line(topvalue[2][0],topvalue[2][1],topvalue[2][2],topvalue[2][3])
# line3 = Line(topvalue[3][0],topvalue[3][1],topvalue[3][2],topvalue[3][3])
pp=[]
p0= GetCrossPoint(line0, line1)
pp.append(p0)
p1 = GetCrossPoint(line0, line2)
pp.append(p1)
p2 = GetCrossPoint(line0, line3)
pp.append(p2)
p3 = GetCrossPoint(line1, line3)
pp.append(p3)
p4 = GetCrossPoint(line1, line2)
pp.append(p4)
p5 = GetCrossPoint(line2, line3)
pp.append(p5)
pp=np.array(pp)
b = pp[np.where((pp[:, 0] >= -width/3) & (pp[:, 0] <= width) & (pp[:, 1] >=-height/3)& (pp[:, 1] <=height))]
if len(b)!=4:
continue
for x,y in b:
if x>centroidx and y>centroidy:
cv2.circle(hei, (x, y), 5, (0, 0, 255))
points[3]=(x,y)
elif x>centroidx and y< centroidy:
cv2.circle(hei, (x, y), 5, (0, 255, 0))
points[0] = (x, y)
elif x < centroidx and y < centroidy:
points[1] = (x, y)
cv2.circle(hei, (x, y), 5, (255, 0, 0))
elif x < centroidx and y > centroidy:
points[2] = (x, y)
cv2.circle(hei, (x, y), 5, (0, 255, 255))
my_coordinate = Coordinates()
if len(points[0])>2 or len(points[0])<2 or len(points[0])<2 or len(points[2])<2 or len(points[3])<2:
cv2.imshow(filename,img)
continue
cenx = np.mean(points, axis=0)
my_coordinate.centroidx = cenx[0]
my_coordinate.centroidy = cenx[1]
# 模板大小
# coord.destination = np.float32([[0, 0], [pwidth, 0], [pwidth, pheight], [0, pheight]])
my_coordinate.destination = np.float32([[0, 0], [1000, 0], [1000, 630], [0, 630]])
# coord.size += 1
my_coordinate.coord = points
# Actual size
corners =np.array(my_coordinate.calculateTRTLBRBL())
if len(corners)==0:
continue
for aa in corners:
aa[0] =aa[0]* hrate
aa[1] =aa[1]* hrate
corners = np.float32((corners[0], corners[1], corners[2], corners[3]))
# print "transform", corners[0][0], corners[1][0], corners[2][0], corners[3][0]
# corners = np.float32(corners)
# 获取透视变换参数
transformationMatrix = cv2.getPerspectiveTransform(corners, my_coordinate.destination)
minVal = np.min(my_coordinate.destination[np.nonzero(my_coordinate.destination)])
# print "minVal", minVal, "width", self.shape[0]
maxVal = np.max(my_coordinate.destination[np.nonzero(my_coordinate.destination)])
# print "maxVal", maxVal, "height", self.shape[1]
# 透视变换
warpedImage = cv2.warpPerspective(surimg, transformationMatrix,
(1000, 630)) # (pwidth/2,pheight/2))#
# warpedImage, transformationMatrix
# cv2.imshow("image", warpedImage)
# cv2.waitKey(0)
# gray = cv2.cvtColor(warpedImage, cv2.cv.CV_BGR2GRAY)
blur = cv2.GaussianBlur(warpedImage, (5, 5), 2)
alpha = 1.5
beta = 1 - alpha # 1 - alpha
gamma = 0
sharpened = cv2.addWeighted(warpedImage, alpha, blur, beta, gamma)
res= mtensor.excute(sharpened)
if res==2:
sharpened=rotate(sharpened,180)
# cv2.namedWindow("sharpened"+filename, 0);
# cv2.resizeWindow("sharpened"+filename, int(width*2), int(height*2));
cv2.imshow("sharpened"+filename, sharpened)
#效果不好,有的直接画对角线了,内部逼近
# 显示图片
# cv2.namedWindow(filename, 0)# cv2.WINDOW_NORMAL)
# cv2.imshow(filename, img)
total=total+1
# cv2.imshow(x, img)
k = cv2.waitKey()
if k == ord("n"):
wrong.append(filename)
elif k==13:
right=right+1
else:
wrong.append(filename)
cv2.destroyWindow(filename)
cv2.destroyWindow("sharpened"+filename)
# cv2.destroyWindow("houghlines3.jpg")
print ("total:"+str(total)+" right:"+ str(right),"rate:",str(round(right*1.0/total,2)))
print (wrong)
# plt.imshow(img),plt.colorbar(),plt.show()
# coding=utf-8
import cv2
import numpy as np
# aaa=[[0,0,0],
# [0,0,0],
# [0,0,0],
# [0,0,0]]
# print aaa
imgpath=r"E:\data\shenfenzheng01\57.jpg"
# imgpath=r'C:\Users\libanggeng\Pictures\yaozong\3.jpg'
image = cv2.imread(imgpath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
lut = np.zeros(256, dtype=gray.dtype) # 创建空的查找表
hist, bins = np.histogram(gray.flatten(), 256, [0, 256])
cdf = hist.cumsum() # 计算累积直方图
cdf_m = np.ma.masked_equal(cdf, 0) # 除去直方图中的0值
cdf_m = (cdf_m - cdf_m.min()) * 255 / (cdf_m.max() - cdf_m.min()) # 等同于前面介绍的lut[i] = int(255.0 *p[i])公式
cdf = np.ma.filled(cdf_m, 0).astype('uint8') # 将掩模处理掉的元素补为0
# 计算
result2 = cdf[gray]
result = cv2.LUT(gray, cdf)
#
# cv2.imshow("OpenCVLUT", result)
# cv2.imshow("NumPyLUT", result2)
# gray = cv2.GaussianBlur(gray,(3,3),0)
edges = cv2.Canny(result2, 150, 150, apertureSize = 3)
# Apply edge detection method on the image
cv2.imshow('edges', edges)
# This returns an array of r and theta values
lines = cv2.HoughLines(edges,1,np.pi/180, 200)
# The below for loop runs till r and theta values
# are in the range of the 2d array
for a in lines:
for r,theta in a:
# Stores the value of cos(theta) in a
a = np.cos(theta)
# Stores the value of sin(theta) in b
b = np.sin(theta)
# x0 stores the value rcos(theta)
x0 = a*r
y0 = b*r
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
# cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
# (0,0,255) denotes the colour of the line to be
cv2.line(image,(x1,y1), (x2,y2), (0,0,255),2)
cv2.imshow('houghlines3.jpg', image)
# cv2.imwrite('houghlines3.jpg', img)
cv2.waitKey(0)