子弹壳底火镶嵌完整性检测

毕设题目如题。

此为子弹底火,如何写出检测它的完整性的算法,求大佬告知,个人感觉用完整底火图片去匹配摄像头采集到的底火图片,但是时间太慢,不满足实际生产要求,或者用灰度值计算三瓣的开度?费解之,望大佬关注。

 

当然  毕设已经zuow做完 贴上代码供自己和大家观看。图形灰度化,二值化,边缘检测,开运算,之后还有贴标签的算法以及霍夫变换找到圆心和需要的圆,之后根据底火之间的角度关系来判断底火镶嵌是否完整。

 

#-*- coding: utf-8 -*-

import cv2
import cv2.cv as cv
import numpy as np
import scipy
from skimage import data,filters,segmentation,measure,morphology,color
from matplotlib import pyplot as plt
import scipy.misc as misc
import math
img=cv2.imread('/home/pi/Desktop/底火照片自制/biao1.png')
cv2.namedWindow("img_cv1",0)
cv2.namedWindow("img_cv2",0)
cv2.resizeWindow("img_cv1",340,240)
cv2.resizeWindow("img_cv2",340,240)
cv2.imshow('img_cv1',img)
cv2.waitKey(2)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(img.shape)
circles= cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT,1,100,param1=100,param2=30,minRadius=1,maxRadius=200)
print(circles)
print(len(circles[0]))
for circle in circles[0]:
    print(circle[2])
    x=int(circle[0])
    y=int(circle[1])
    r=int(circle[2])
    cv2.circle(img,(x,y),r,(0,0,255),-1)
cv2.imshow("img_cv2",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

im = cv2.imread(r"/home/pi/Desktop/底火照片自制/biao1.png")  
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)  
im_at_mean = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 7)  
im_at_mean2 = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 10)  
retval,im_at_fixed = cv2.threshold(im_gray, 100, 255, cv2.THRESH_BINARY)  
plt.subplot(2, 2, 1),plt.imshow(im_gray,'gray')  
plt.subplot(2, 2, 2),plt.imshow(im_at_mean,'gray')  
plt.subplot(2, 2, 3),plt.imshow(im_at_mean2,'gray')  
plt.subplot(2, 2, 4),plt.imshow(im_at_fixed,'gray')  
plt.show()
def openopration(img):  
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  
    iClose = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)  
    return iOpen
  
image = im_at_fixed
print(image.shape)  
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(8, 8))  
iOpen = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
x1=104
y1=55
cv2.namedWindow("image",0)
cv2.resizeWindow("image",340,240)
cv2.imshow('image', image)  
cv2.namedWindow("iOpen",0)
cv2.resizeWindow("iOpen",340,240)
cv2.imshow('iOpen', iOpen)  
cv2.waitKey(0)  
I=iOpen
image=color.rgb2gray(I)
thresh =filters.threshold_otsu(image)
bw =morphology.closing(image > thresh, morphology.square(3))
labels =measure.label(bw)
dst=color.label2rgb(labels)
cv2.namedWindow("dst",0)
cv2.resizeWindow("dst",340,240)
cv2.imshow('dst', dst)  
cv2.waitKey(0)
img = iOpen

ret,thresh = cv2.threshold(cv2.cvtColor(img,cv2.COLOR_BGR2GRAY),127,255,0)

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
print(type(contours))
print(type(contours[0]))
print(len(contours))
cnts=contours[0]
cnts1=contours[1]
cnts2=contours[2]
cnts3=contours[3]
cnts4=contours[4]
M = cv2.moments(cnts)
N=cv2.moments(cnts1)
O=cv2.moments(cnts2)
P=cv2.moments(cnts3)
Q=cv2.moments(cnts4)
imgnew = cv2.drawContours(img, contours, -1, (0,255,0), 3)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
#print(cx,cy)
cx1 = int(N['m10']/N['m00'])
cy1 = int(N['m01']/N['m00'])
#print(cx1,cy1)
cx2 = int(O['m10']/O['m00'])
cy2= int(O['m01']/O['m00'])
print(cx2,cy2)
cx3 = int(P['m10']/P['m00'])
cy3 = int(P['m01']/P['m00'])
print(cx3,cy3)
cx4 = int(Q['m10']/Q['m00'])
cy4 = int(Q['m01']/Q['m00'])
print(cx4,cy4)
AB=math.sqrt(np.square(cx3-cx4)+np.square(cy3-cy4))
print(AB)
BC=math.sqrt(np.square(cx2-cx3)+np.square(cy2-cy3))
print(BC)
AC=math.sqrt(np.square(cx2-cx4)+np.square(cy2-cy4))
print(AC)
d1=(cx3-cx4)*(y1-cy3)-(cy3-cy4)*(x1-cx3)
juli1=abs(d1/AB)
print(juli1)
d2=(cx2-cx3)*(y1-cy2)-(cy2-cy3)*(x1-cx2)
juli2=abs(d2/BC)
print(juli2)
d3=(cx2-cx4)*(y1-cy4)-(cy2-cy4)*(x1-cx4)
juli3=abs(d3/AC)
print(juli3)
#AC:(cy3-cy4)*x+(cx4-cx3)*y+(cy4-cy3)*cx+(cx3-cx4)*cy=0
ju1=abs((cy3-cy4)*x1+(cx4-cx3)*y1+(cy4-cy3)*cx4+(cx3-cx4)*cy4)
li1=math.sqrt(np.square(cy3-cy4)+np.square(cx4-cx3))
juli11=ju1/li1
print(juli11)
#AB: (cy2-cy3)x+(cx3-cx2)y+(cy3-cy2)cx3+(cx2-cx3)cy3=0
ju2=abs((cy2-cy3)*x1+(cx3-cx2)*y1+(cy3-cy2)*cx3+(cx2-cx3)*cy3)
li2=math.sqrt(np.square(cy2-cy3)+np.square(cx3-cx2))
juli22=ju2/li2
print(juli22)

#BC:(cy2-cy4)*x+(cx4-cx2)*y+(cy4-cy2)*cx2+(cx2-cx4)*cy2=0
ju3=abs((cy2-cy4)*x1+(cx4-cx2)*y1+(cy4-cy2)*cx2+(cx2-cx4)*cy2)
li3=math.sqrt(np.square(cy2-cy4)+np.square(cx4-cx2))
juli33=ju3/li3
print(juli33)
if (AB>3*juli1 and AB<4*juli1):
    if (BC>3*juli2 and BC<4*juli2):
        if (AC>3*juli3 and AC<4*juli3):
                print('hege')

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值