基于(Python下的OpenCV)图像处理的喷墨墨滴形状规范检测
生活随笔
收集整理的這篇文章主要介紹了
基于(Python下的OpenCV)图像处理的喷墨墨滴形状规范检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過圖像處理,分析數碼印花的噴頭所噴出來的墨滴形狀,與標準墨滴形狀對比分析,來判斷墨水及其噴頭設備的狀態,由兩部分構成
PS:獲取墨滴形狀照片和標準墨滴形狀照片都是手繪的,將就的看吧,主要功能已經實現了,思路也就是這些。
主要思路:
1,首先通過高速攝像頭捕獲數碼印花噴頭噴出的墨滴照片
2,將照片進行灰度和二值處理
3,獲取照片中墨滴的的邊緣
4,獲取邊緣所圍成的墨滴面積
5,對墨滴邊緣進行外接圓繪制,并獲取外接圓的直徑
6,將圖片繪制最小外接圓求出面積及其直徑
7,將圖像和標準墨滴圖像進行加權融合,并獲取交集部分輪廓圖像
8,獲取交集部分輪廓面積的大小
9,由以上數據分析,反推出墨水的工藝和噴頭設備的狀態情況
OpenCV部分
一、將彩色的照片轉化為黑色背景、灰度圖、二值化
由第一步將拍攝的彩色照片繪制成黑色背景下的二值圖
效果如下:
二、獲取照片的輪廓(我這里使用的是內輪廓)
import cv2 import numpy as np from matplotlib import pyplot as plt from math import sqrtdef show_photo(name,picture):#圖像顯示函數cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()img = cv2.imread('E:\Jupyter_workspace\study\data/water.png') show_photo('img ',img )gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#轉換為灰度圖 ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)#對圖像進行二值處理,小于127為0,大于127為255 binary, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)cnt = contours[1]draw_img = img.copy() res =cv2.drawContours(draw_img,[cnt],-1,(0,0,255),2) show_photo('res',res)原圖:
捕獲內輪廓后的結果:
三、計算密閉輪廓的面積和周長
cv2.arcLength(cnt,True)#輪廓所對應的周長,True表示閉合的 cv2.contourArea(cnt)#第一個輪廓所對應的面積四、繪制墨滴內輪廓的最小外接圓
x, y, w, h = cv2.boundingRect(cnt) (x,y),radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) img_radius = cv2.circle(img,center,radius,(0,255,0),2)#(B,G,R),2為輪廓粗細程度 show_photo('img_radius',img_radius)墨滴內輪廓的最小外接圓:
五、填充最小外接圓,方便后續求輪廓
img_gray = cv2.cvtColor(img_radius, cv2.COLOR_BGR2GRAY) _,th = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)# 尋找輪廓,使用cv2.RETR_CCOMP尋找內外輪廓 img_radius_full, contours, hierarch = cv2.findContours(th, cv2.RETR_CCOMP, 2) # 找到內層輪廓并填充hierarchy = np.squeeze(hierarch)#使用np.squeeze壓縮hierarch的成為一維數據for i in range(len(contours)):# 存在父輪廓,說明是里層if (hierarchy[i][3] != -1):cv2.drawContours(img_radius_full, contours, i, (255, 255, 0), -1)#這里的(255,255,0)代表cyan,也可自定義show_photo('img_radius_full',img_radius_full) #cv2.imwrite('E:\Python-workspace\OpenCV\OpenCV/yanyu.jpg', img)將墨滴內輪廓的最小外接圓填充:
六、繪制最小外接圓輪廓
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#轉換為灰度圖 ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)#對圖像進行二值處理,小于127為0,大于127為255 binary, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)cnt = contours[0]draw_img = img.copy() res =cv2.drawContours(draw_img,[cnt],-1,(0,0,255),2) show_photo('res',res)墨滴內輪廓的最小外接圓輪廓:
七、獲取最小外接圓面積、周長、直徑和半徑
#最小外接圓的周長 cv2.arcLength(cnt,True)#輪廓所對應的周長,True表示閉合的#最小外接圓的面積 cv2.contourArea(cnt)#輪廓所對應的面積#最小外接圓直徑 cv2.arcLength(cnt,True)/2/3.1415926#最小外接圓直徑 sqrt(int(cv2.contourArea(cnt)/3.1415926))八、與標準墨滴樣式進行加權融合
import cv2 import matplotlib.pyplot as plt import numpy as npimg_1 = cv2.imread('E:\Jupyter_workspace\study\data/water.png') img_2 = cv2.imread('E:\Jupyter_workspace\study\data/water_img.png')res = cv2.addWeighted(img_1,0.6,img_2,0.4,0)#加權融合 #R = αx + βy + b #其中x為img_1,y為img_2,α為0.4權重,β為0.6權重,b為0偏移量 #plt.imshow(res) show_photo('res',res)gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)#轉換為灰度圖 ret, thresh = cv2.threshold(gray,50,255,cv2.THRESH_BINARY)#對圖像進行二值處理,小于127為0,大于127為255 binary, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)#cnt = contours[5] cnt = contours[0] cnt1 = contours[5]draw_img = res.copy() res =cv2.drawContours(draw_img,[cnt],-1,(0,0,255),2) res =cv2.drawContours(draw_img,[cnt1],-1,(0,255,0),2) show_photo('res',res)標準墨滴:
獲取墨滴與標準墨滴進行加權融合:
九、繪制獲取墨滴圖像和標準墨滴圖像交集部分輪廓
gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)#轉換為灰度圖 ret, thresh = cv2.threshold(gray,50,255,cv2.THRESH_BINARY)#對圖像進行二值處理,小于127為0,大于127為255 binary, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)#cnt = contours[5] cnt = contours[0] cnt1 = contours[5]draw_img = res.copy() res =cv2.drawContours(draw_img,[cnt],-1,(0,0,255),2) res =cv2.drawContours(draw_img,[cnt1],-1,(0,255,0),2) show_photo('res',res)獲取墨滴和標準墨滴交集輪廓:
十、計算獲取墨滴與標準墨滴交集部分面積
cv2.contourArea(cnt)#輪廓所對應的面積到此,通過opencv已經獲取了足夠多的數據,由這些數據進行工藝上的對墨水配料反推加工
整合代碼如下:
import cv2 import numpy as np from matplotlib import pyplot as plt from math import sqrtdef show_photo(name,picture):#圖像顯示函數cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()test_img = cv2.imread('E:\Jupyter_workspace\study\data/water.png')#測試圖 origin_img = cv2.imread('E:\Jupyter_workspace\study\data/water_img.png')#標準圖show_photo('test ',test_img )#測試圖像gray = cv2.cvtColor(test_img,cv2.COLOR_BGR2GRAY)#轉換為灰度圖 ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)#對圖像進行二值處理,小于127為0,大于127為255 binary, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)cnt = contours[0]#1為內輪廓,0為外輪廓draw_img = test_img.copy() outline =cv2.drawContours(draw_img,[cnt],-1,(0,0,255),2) show_photo('outline',outline)#內輪廓length = cv2.arcLength(cnt,True) area = cv2.contourArea(cnt) print('輪廓周長為:',length)#輪廓所對應的周長,True表示閉合的 print('輪廓面積為:',area)#輪廓所對應的面積''' 繪制輪廓的最小外接圓 ''' x, y, w, h = cv2.boundingRect(cnt) (x,y),radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) draw_radius_img = test_img.copy() img_radius = cv2.circle(draw_radius_img,center,radius,(0,255,0),2)#(B,G,R),2為輪廓粗細程度 show_photo('img_radius',img_radius)''' 填充最小外接圓 ''' img_gray = cv2.cvtColor(img_radius, cv2.COLOR_BGR2GRAY) _,th = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)# 尋找輪廓,使用cv2.RETR_CCOMP尋找內外輪廓 img_radius_full, contours, hierarch = cv2.findContours(th, cv2.RETR_CCOMP, 2) # 找到內層輪廓并填充hierarchy = np.squeeze(hierarch)#使用np.squeeze壓縮hierarch的成為一維數據for i in range(len(contours)):# 存在父輪廓,說明是里層if (hierarchy[i][3] != -1):cv2.drawContours(img_radius_full, contours, i, (255, 255, 0), -1)#這里的(255,255,0)代表cyan,也可自定義show_photo('img_radius_full',img_radius_full) r_c = cv2.arcLength(cnt,True)#輪廓所對應的周長,True表示閉合的 r_s = cv2.contourArea(cnt)#輪廓所對應的面積 # c = Π*d = 2*Π*r # s = Π*r*r r_d = r_c/3.1415926print('最小外接圓輪廓周長為:',r_c)#輪廓所對應的周長,True表示閉合的 print('最小外接圓輪廓面積為:',r_s)#輪廓所對應的面積 print('最小外接圓輪廓直徑為:',r_d)#輪廓所對應的面積''' 與標準墨滴圖像進行加權融合 ''' res = cv2.addWeighted(origin_img,0.6,test_img,0.4,0)#加權融合 #R = αx + βy + b #其中x為img_1,y為img_2,α為0.4權重,β為0.6權重,b為0偏移量 #plt.imshow(res) show_photo('res',res)gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)#轉換為灰度圖 ret, thresh = cv2.threshold(gray,50,255,cv2.THRESH_BINARY)#對圖像進行二值處理,小于127為0,大于127為255 binary, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)#cnt = contours[0] cnt1 = contours[5] #cnt1 = contours[5]draw_mix_img = res.copy() mix_img =cv2.drawContours(draw_mix_img,[cnt1],-1,(0,0,255),2) #res =cv2.drawContours(draw_img,[cnt1],-1,(0,255,0),2) show_photo('mix_img',mix_img)mixed_s = cv2.contourArea(cnt1)#輪廓所對應的面積 mixed_c = cv2.arcLength(cnt1,True)#輪廓所對應的周長,True表示閉合的print('交集輪廓周長為:',mixed_c)#輪廓所對應的周長 print('交集輪廓面積為:',mixed_s)#輪廓所對應的面積 print('\n')if area < 9000 :print('警報:噴頭堵塞!!!') elif mixed_s > 1000 :print("警報:墨水形狀不標準!!!") elif r_d > 110 :print('警報:墨水過于粘稠!!!') else :print("一切正常")測試墨滴圖像:
繪制墨滴外輪廓:
繪制墨滴最小外接圓:
填充最小外接圓:
與標準墨滴進行加權融合:
獲取交集部分:
控制臺結果顯示:
墨水工藝部分
總結
以上是生活随笔為你收集整理的基于(Python下的OpenCV)图像处理的喷墨墨滴形状规范检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 麦冬多少钱啊?
- 下一篇: 19-Harris角点检测