适用于统计图像像素差距较明显,当像素值计算出来后可进行比较。
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
import cv2 as cv,cv2
import matplotlib.pyplot as plt
import numpy as np,time,os
# #不同算子的差异
def cv_show(img,name):
cv2.imshow(name,img)
cv2.waitKey()
cv2.destroyAllWindows()
def sobel(img):
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
sobelx = cv2.convertScaleAbs(sobelx)
sobely = cv2.convertScaleAbs(sobely)
sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0)
return sobelxy
def scharr(img):
scharrx = cv2.Scharr(img,cv2.CV_64F,1,0)
scharry = cv2.Scharr(img,cv2.CV_64F,0,1)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.convertScaleAbs(scharry)
scharrxy = cv2.addWeighted(scharrx,0.5,scharry,0.5,0)
return scharrxy
def laplacian(img):
laplacian = cv2.Laplacian(img,cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)
return laplacian
def statistics():
f= open(r'./id_pv.txt', "w")
img_path=r'/data2/enducation/answer_card/answer-card-recognition/id_img'
len_=len(os.listdir(img_path))
for j in range(len_):
img_path_=os.path.join(img_path,f"{j}.jpg")
# img_path_=r"/data2/enducation/answer_card/answer-card-recognition/id_img/21.jpg"
print(img_path_)
src = cv.imread(img_path_)
# cv.imshow("q",src)
h,w,ch = np.shape(src)
scharrx = cv.Scharr(src,cv.CV_64F,1,0)
scharry = cv.Scharr(src,cv.CV_64F,0,1)
scharrx = cv.convertScaleAbs(scharrx)
scharry = cv.convertScaleAbs(scharry)
scharrxy = cv.addWeighted(scharrx,0.5,scharry,0.5,0)
pw=int(w/9)
one_id_pv=[]
for i in range(9):
x1 = i * pw
y1 = 66
x2 = (i + 1) * pw
y2 = h
image = scharrxy[y1:y2, x1:x2,:]
h1,w1,c=image.shape
cv.rectangle(src, (x1, y1), (x2, y2), (100, 108, 255), 1)
#??????
gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
print(gray.shape)
# exit()
#??????????
# cv.imshow("gray",gray)
hest = np.zeros([256],dtype = np.int32)
#??????
pvs = []
for row in range(h1):
for col in range(w1):
pv = gray[row,col]
pvs.append(pv)
hest[pv] +=1
#????????????
x=np.mean(pvs)
one_id_pv.append(int(x))
print(x)
# plt.plot(hest,color = "r")
# plt.xlim([0,256])
# plt.show()
# #?????
# cv.waitKey(0)
# cv.destroyAllWindows()
# card_logger.info(f"{one_id_pv}\n")
f.write(f"{one_id_pv}+{j}\n")
f.write(f"{img_path_}\n\n")
if __name__ == '__main__':
path=r""
img=cv.imread(path)
# sobelxy=sobel(img)
# scharrxy=scharr(img)
# laplacian=laplacian(img)
# res = np.hstack((sobelxy,scharrxy,laplacian))
# cv_show(res, 'res')
statistics()