import cv2 import numpy as np image=cv2.imread('4.jpg') # image=cv2.resize(image1,(200,200)) image_gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) w,h=image_gray.shape px_count=np.zeros(256) #照片长、宽分别是w,h for w_w in range(w): for h_h in range(h): px_count[image_gray[w_w,h_h]]+=1 # print(px_count) #找最大次数(最大峰) max_index=np.argmax(px_count,axis=0) #找次大峰 px_count[max_index]*=-1 next_max_index=np.argmax(px_count,axis=0) px_count[next_max_index]*=-1 #找谷值 if max_index>next_max_index: min_index=np.agrmin(px_count[next_max_index:max_index+1],axis=0) min_index+=next_max_index else: min_index=np.argmin(px_count[max_index:next_max_index+1],axis=0) min_index+=max_index #TODO.1 # image_gray 上面转换为黑白的照片; # min_index 谷值 #0~255 代表不同的颜色,min_index为阈值,大于阈值的全部置为255(白色) _,image_binary=cv2.threshold(image_gray,min_index,255,cv2.THRESH_BINARY) cv2.imshow('the original image',image) cv2.imshow('black and white figure',image_gray) cv2.imshow('the threshold figure',image_binary) if cv2.waitKey(0)&0xFF==27: cv2.destroyAllWindows()
4.jpg
照片来源于网络,侵权请联系我删除