123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #coding=utf-8
- '''颜色串色检测'''
- import cv2 as cv
- import numpy as np
- #裁剪图片
- def cutImage(img_addr):
- img = cv.imread(img_addr)
- img_arr = np.array(img)
- hight = img_arr.shape[0]
- wight = img_arr.shape[1]
- dest_img = img_arr[hight/2+20:hight,0:wight,:]
- return dest_img
- #得到横坐标点
- def getSpot(dest_img):
- edge_img_rgb = cv.convertScaleAbs(cv.Sobel(dest_img,cv.CV_16S,1,0))
- edge_img_gray = cv.cvtColor(edge_img_rgb,cv.COLOR_BGR2GRAY)
- _,edge_img = cv.threshold(edge_img_gray,50,255,cv.THRESH_BINARY)
- #做一条水平线
- tumple1=[]
- tumple = []
- for y in range(0,edge_img.shape[1],1):
- if edge_img[70][y] == 255:
- tumple1.append(y)
- tumple.append(y)
- count = 0
- for i in range(0,len(tumple1)):
- if i != len(tumple1)-1:
- if tumple1[i+1] - tumple1[i]>15:
- if count == 0:
- tumple.insert(i+1,".")
- else :
- tumple.insert(count+i+1,".")
- count= count +1
- tumple.insert(len(tumple),".")
- lines =[]
- recode = 0
- for i in range(0,len(tumple),1):
- if tumple[i] == "." :
- if recode == 0:
- temp_arr = tumple[recode:i]
- recode = i
- else:
- temp_arr = tumple[recode+1:i]
- recode = i
- lines.append(sum(temp_arr)/len(temp_arr))
- return lines
- #输入图片地址,返回拆分图片
- def getPartImage(img_addr):
- dest_img = cutImage(img_addr)
- lines = getSpot(dest_img)
- images = []
- for i in range(0,len(lines)):
- if i ==0:
- images.append(dest_img[0:dest_img.shape[0],0:lines[i]-5,:])
- else:
- if i == len(lines)-1:
- images.append(dest_img[0:dest_img.shape[0],lines[i]+5:dest_img.shape[1]-5,:])
- images.append(dest_img[0:dest_img.shape[0],lines[i-1]+5:lines[i]-5,:])
- return images
- # 对外提供方法.函数准确度不够。
- # 废弃
- def cmpPicTotal(picpath1):
- images = getPartImage(picpath1)
- if len(images) != 8:
- print "彩色分离异常"
- else:
- for image in images:
- wight = image.shape[1]
- hight = image.shape[0]
- B, G, R = cv.split(image)
- sum_B = 0
- sum_G = 0
- sum_R = 0
- for x in range(0, hight, 1):
- for y in range(0, wight, 1):
- sum_B = sum_B + B[x][y]
- sum_G = sum_G + G[x][y]
- sum_R = sum_R + R[x][y]
- avg_B = sum_B / (wight * hight)
- avg_G = sum_G / (wight * hight)
- avg_R = sum_R / (wight * hight)
- for x in range(0, hight, 1):
- for y in range(0, wight, 1):
- if abs(image[x][y][0] - avg_B)>20 or abs(image[x][y][1] - avg_G) >20 or abs(image[x][y][2] -avg_R) >20:
- return False
- else:
- return True
|