好惨一女的,写了将近3周,还要推翻重来。立个碑纪念一下菜鸡本菜菜
主要是最后取样的思路不对。嗯。。就是算法不行。。太垃圾了。。。
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread(r'C:\Users\dell\Desktop\xianquan\1.bmp')
b, g, r = cv2.split(img)
b = np.mat(b)
g = np.mat(g)
r = np.mat(r)
k = (2 * r - g - b ) / (2*r + g + b + 0.00001)
k = k > 0.3
b = np.multiply(b, k)
r = np.multiply(r, k)
g = np.multiply(g, k)
result = cv2.merge((b, g, r ))
closed = cv2.erode(result, None, iterations=2)
closed = cv2.dilate(closed, None, iterations=3)
closed = cv2.dilate(closed, None, iterations=3)
closed = cv2.dilate(closed, None, iterations=3)
cv2.namedWindow("im", 0 )
cv2.imshow("im", closed)
cv2.waitKey(0)
cv2.destroyAllWindows()
gray = cv2.cvtColor(closed, cv2.COLOR_BGR2GRAY)
gray_mtx = np.mat(gray)
_, thresh = cv2.threshold(gray_mtx,1, 255, cv2.THRESH_BINARY)
gray_loc = np.nonzero(thresh)
#len(gray_loc) ==2
max_x = np.max(gray_loc[0])
min_x = np.min(gray_loc[0])
max_y = np.max(gray_loc[1])
min_y = np.min(gray_loc[1])
#???框选结果
img_rect = cv2.rectangle(gray,(min_x,min_y),(max_x,max_y),(255,255,255),3)
#np.shape(gray) == (2048,3027)
m0 = np.zeros((2048,10))
m1 = np.ones((2048,10))
m = np.mat(np.hstack((m1,m0)))
#m = np.concatenate((m2,m1),axis=1)
m_all = np.multiply(k,m)
m_final= np.multiply(gray,m_all)
img_res = np.nonzero(m_final)
print(type(k),type(m))
#min_val,max_val,min_indx,max_indx=cv2.minMaxLoc(gray_loc)
#print(max_x,min_x,max_y,min_y)
cv2.namedWindow('dst', 0)
cv2.imshow('dst', img_rect)
cv2.waitKey(0)
cv2.destroyAllWindows()
#cv2.imwrite(r"C:\Users\dell\Desktop\xianquan\exp" + str(i) + ".bmp", img)