import cv2 import json
img= cv2.imread(r"C:\Users\Uaena\Desktop\123.jpg") img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) sq = img.shape l=sq[0] #行 w=sq[1] #列 list=[] print(l,w) for i in range(l): for j in range(w): if img[i,j] < 180: continue else: list.append((i,j,)) f= open("text.xlsx","w")#打开一个写文件,json(将列表转换成字符串形式储存,要使用时也可恢复列表形式)用法 f.write(json.dumps(list) ) f.close()
import json f=open("text.xml","r" ) list=json.loads(f.read()) img= cv2.imread(r"C:\Users\Uaena\Desktop\11.jpg") sq=img.shape l=sq[0] w=sq[1] for i in range(l): for j in range(w): if [i,j]in list: # print([i,j]) continue else: img[i,j]=[255,0,0] cv2.imshow("1",img) cv2.imwrite(r"C:\Users\Uaena\Desktop\only.jpg",img) cv2.waitKey()