基于图像处理的数码印花喷墨墨滴形状规范的研究(Python+OpenCV+Mysql)

大体思路:由于墨滴的不同参数会对墨滴的形态产生一定的影响,故如果通过研究墨滴的形态则通过海量的数据就可以大概确定墨滴的各项参数指标的范围。通过OpenCV对墨滴的喷出的形状进行图像处理,对墨滴图像进行一系列的分析,通过一系列参数存入Mysql数据库中最终找到参数与墨滴形态的关系。本篇博客是通过拿标准墨滴、参数墨滴的图像和Mysql数据库中的数据进行对比从而得出相应的结论。(当然这里的标准墨滴只不过是本人的一个例子而已,为了简化后续操作而自我设定的,故再次声明一下)

代码已经完全实现,有点繁琐,由于时间原因,本篇博客我会持续更新补充直到完全补充完毕...

实现步骤:

1、捕获标准墨滴图像,对其进行一系列处理,获取其周长circum_origin、面积area_origin
2、捕获测试墨滴图像,对其进行一系列处理,获取其周长circum_test、面积area_test、最小外接圆面积area_test_radius、最小外接圆周长circum_test_radius、最小外接圆直径diameter_test_radius
3、将标准墨滴图像与测试墨滴图像进行加权融合,并获取其交集部分周长circum_intersection、面积area_intersection
4、将这些参数存入到drop数据库下的shape表中,并与墨滴的工艺参数进行主外键关联,墨滴工艺参数主要为:黏度、表面张力
5、通过海量的数据,进行范围查找,即可由墨滴的形状参数确定出墨滴的工艺参数,从而达到我的课题目的。

一、墨滴的图像

标准墨滴图像:
请添加图片描述
测试墨滴图像:
请添加图片描述

二、墨滴通过灰度和二值化处理后的图像

(后续需要对测试图像和原图像进行加权融合操作,故这里需要通过ROI区域获取,指定图像的长宽,我这里使用的长宽为:250*250像素)
标准墨滴预处理后的图像:
请添加图片描述
测试墨滴预处理后的图像:
请添加图片描述

三、Mysql数据库的设计

我这里使用的是Mysql数据库,数据库名称为drop,里面存放shape表,表中属性分别为:id、circum、area、circum_radius、area_radius、diameter_radius

列名类型即精度数据说明描述
idintprimary key、NOT NULL 、AUTO_INCREMENT主键,递增,唯一,不为空
circumdecimal(12,4)共12位小数数据,其中小数位数4位存放图像的周长数据
areadecimal(12,4)共12位小数数据,其中小数位数4位存放图像的面积数据
area_radiusdecimal(12,4)共12位小数数据,其中小数位数4位存放图像最小外接圆的面积数据
circum_radiusdecimal(12,4)共12位小数数据,其中小数位数4位存放图像最小外接圆的周长数据
diameter_radiusdecimal(12,4)共12位小数数据,其中小数位数4位存放图像最小外接圆的直径数据

后续表还会进行补充完善...

在drop数据库下创建shape表
import pymysql
DBHOST = 'localhost'
DBUSER = 'root'
DBPASS = 'beyond'
DBNAME = 'drop'
DBSET = 'utf8'try:conn = pymysql.connect(host = DBHOST,user = DBUSER,password= DBPASS,database= DBNAME,charset= DBSET)#连接drop这个数据库print('seccessfull!!!')cur = conn.cursor()cur.execute("DROP TABLE IF EXISTS shape")#创建water表之前先检查是否存在这个表,若存在则删除sql = "CREATE TABLE shape(id int primary key NOT NULL AUTO_INCREMENT, circum decimal(12,4), area decimal(12,4), area_radius decimal(12,4), circum_radius decimal(12,4), diameter_radius decimal(12,4))"#创建表cur.execute(sql)print('create table seccess!!!')except pymysql.Error as e:print('table create is defeated!' + str(e))

在这里插入图片描述
在这里插入图片描述

四、获取测试墨滴的轮廓周长、轮廓面积、最小外接圆周长、最小外接圆面积、最小外接圆直径,为了方便后续处理和与数据库一致,这里的数据只保留四位小数

import cv2
import numpy as np
import pymysql
from matplotlib import pyplot as plt
from math import sqrtDBHOST = 'localhost'
DBUSER = 'root'
DBPASS = 'beyond'
DBNAME = 'drop'
DBSET = 'utf8'def show_photo(name,picture):#图像显示函数cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()#显示测试墨滴图像
img_test = cv2.imread('E:\Jupyter_workspace\study\data/test.png')
img_origin = cv2.imread('E:\Jupyter_workspace\study\data/origin_1.png')
#img_test = cv2.imread('E:\Jupyter_workspace\study\mysql\image/4.png')
#img_test = cv2.imread('E:\Jupyter_workspace\study\data/a1.png')
show_photo('img_test ',img_test )
show_photo('img_origin ',img_origin )#将照片转换为灰度图、二值化,获取其轮廓并将轮廓绘制
gray = cv2.cvtColor(img_test,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)
#show_photo('gray',gray)
cnt = contours[0]draw_img = img_test.copy()
test_outline =cv2.drawContours(draw_img,[cnt],-1,(0,0,255),2)
show_photo('test_outline',test_outline)#轮廓周长
circum_test = format(cv2.arcLength(cnt,True), '.4f')
#circum = int(cv2.arcLength(cnt,True))#轮廓所对应的周长,True表示闭合的
print("轮廓周长为:" + circum_test)
#轮廓面积
area_test = format(cv2.contourArea(cnt),'.4f')#轮廓所对应的面积
print("轮廓面积为:" + area_test)#轮廓最小外接圆
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_test,center,radius,(255,255,255),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寻找内外轮廓
image, 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, contours, i, (255, 255, 255), -1)#这里的(255,255,0)代表cyan,也可自定义show_photo('radius',img_radius)img_gray = cv2.cvtColor(img_radius, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 寻找二值化图中的轮廓
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[1]
cv2.drawContours(img_radius, [cnt], 0, (0, 0, 255), 2)area_test_radius = format(cv2.contourArea(cnt),'.4f')
circum_test_radius = format(cv2.arcLength(cnt,True), '.4f')
diameter_test_radius = format(cv2.arcLength(cnt,True)/3.1416, '.4f')print("最小外接圆的周长为:");print(circum_test_radius)
print("最小外接圆的面积为:");print(area_test_radius)
print("最小外接圆的直径为:");print(diameter_test_radius)img_test = cv2.imread('E:\Jupyter_workspace\study\data/test.png')
img_origin = cv2.imread('E:\Jupyter_workspace\study\data/origin_1.png')
res = cv2.addWeighted(img_test,0.5,img_origin,0.5,0)#加权融合
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]draw_img = res.copy()
res =cv2.drawContours(draw_img,[cnt],-1,(0,0,255),2)show_photo('res',res)img_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
_,th = cv2.threshold(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(hierarchy)#使用np.squeeze压缩hierarch的成为一维数据
#print(hierarchy.shape)for i in range(len(contours)):# 存在父轮廓,说明是里层if (hierarchy[i]!= -1):cv2.drawContours(res, contours, i, (255, 255,0), -1)#这里的(255,255,0)代表cyan,也可自定义show_photo('img_radius_full',img_radius_full)  #轮廓周长
circum_intersection = format(cv2.arcLength(cnt,True), '.4f')#轮廓所对应的周长,True表示闭合的
print("并集轮廓周长为:" + circum_intersection)
#轮廓面积
area_intersection = format(cv2.contourArea(cnt),'.4f')#轮廓所对应的面积
print("并集轮廓面积为:" + area_intersection)try:conn = pymysql.connect(host = DBHOST,user = DBUSER,password= DBPASS,database= DBNAME,charset= DBSET)#连接drop这个数据库print('seccessfull!!!')cur = conn.cursor()sql = "INSERT INTO shape(circum,area,area_radius,circum_radius,diameter_radius) VALUE (%s,%s,%s,%s,%s)"#向表中插入数据value = (circum_test,area_test,area_test_radius,circum_test_radius,diameter_test_radius)cur.execute(sql,value)conn.commit()print('insert seccess!!!')
except pymysql.Error as e:print('insert is defeated!' + str(e))conn.rollback()conn.close()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/378527.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASP.NET 主题(Themes)FAQ

1、主题是什么 主题由一组元素组成:外观、级联样式表 (CSS)、图像和其他资源。主题将至少包含外观。主题是在网站或 Web 服务器上的特殊目录中定义的。主题是一组Web Control的属性设置的集合,提供一种简单的方法设置控件的样式属性。 主题只在Web Contr…

Head First HTML与CSS、XHTML++笔记(第四章 WEB镇之旅 第五章 认识媒体)

第四章 链接&#xff08;详解<a>元素&#xff09; 目标锚 在目标位置 <h2><a id"chai">contentTest</a></h2> 在需要链接位置 <a href"index.html#chai">See</a> 链接到自身的目标锚 <a href"#top"…

Opencv实战【4】——图片动漫化处理

博主联系方式&#xff1a; QQ:1540984562 微信&#xff1a;wxid_nz49532kbh9u22 QQ交流群&#xff1a;750313950 目录动漫化风格的特点处理手段代码实现效果总结动漫化风格的特点 &#xff08;1&#xff09;动漫中的细节相对少&#xff1b; &#xff08;2&#xff09;动漫中的边…

nextshort_Java扫描仪的nextShort()方法与示例

nextshort扫描器类的nextShort()方法 (Scanner Class nextShort() method) Syntax: 句法&#xff1a; public short nextShort();public short nextShort(int rad);nextShort() method is available in java.util package. nextShort()方法在java.util包中可用。 nextShort() …

php 生成css文件怎么打开,php生成html文件的多种步骤介绍

//定义日期函数functiongetdatetime(){$datetimegetdate();$strReturn$datetime["year"]."-";$strReturn$strReturn.$datetime["mon"]."-";$strReturn$strReturn.$datetime["mday"];return$strReturn;}//定义时间函数(文件名…

08-KNN手写数字识别

标签下载地址 文件内容备注train-images-idx3-ubyte.gz训练集图片&#xff1a;55000张训练图片&#xff0c;5000张验证图片train-labels-idx1-ubyte.gz训练集图片对应的数字标签t10k-images-idx3-ubyte.gz测试集图片&#xff1a;10000张图片t表示test&#xff0c;测试图片&…

MFC odbc访问远程数据库

首先&#xff0c;MFC通过ODBC访问数据库&#xff0c;主要使用两个类&#xff0c;一个是CDataBase&#xff0c;一个是CRecordset。第一个是用于建立数据库连接的&#xff0c;第二个是数据集&#xff0c;用来查询的。步骤如下&#xff1a;1.实例化一个CDataBase对象&#xff0c;并…

微机原理——扩展存储器设计

目录【1】存储器的层次结构【2】存储器的分类【3】SRAM1、基本原理&#xff1a;2、结构&#xff1a;3、芯片参数与引脚解读&#xff1a;4、CPU与SRAM的连接方式【4】DRAM1、基本原理&#xff1a;2、结构3、芯片引脚解读&#xff1a;【5】存储器系统设计【6】存储器扩展设计&…

floatvalue 重写_Java Number floatValue()方法与示例

floatvalue 重写Number类floatValue()方法 (Number Class floatValue() method) floatValue() method is available in java.lang package. floatValue()方法在java.lang包中可用。 floatValue() method is used to return the value denoted by this Number object converted …

array_column php什么版本可以用,array_column兼容php5.5以下版本

gistfile1.txt// ----------------------------------------------------------------------// |获取二维数组中指定的一列&#xff0c;PHP5.5以后有专用函数array_column()// ----------------------------------------------------------------------// |param array $arr// …

。net学习之控件的使用注意点

jQuery使用 1、自定义属性的使用<script>$(#xwjj_i_main br[brinfoPd_KangQiao_Subject_Xwjj_br_1]).hide();</script> 2、ready代码块$(document).ready(function(){ //你的代码}); 3、简单的特效hide&#xff08;&#xff09;$("a").click(function()…

09-CNN手写数字识别

CNN卷积神经网络的本质就是卷积运算 维度的调整&#xff1a; tf.reshape(imageInput,[-1,28,28,1]) imageInput为[None,784]&#xff0c;N行* 784维 调整为 M28行28列*1通道 即&#xff1a;二维转化为四维数据 参数一&#xff1a;等价于运算结果M 参数二&#xff1a;28 28 表示…

【转】左值与右值

出处&#xff1a;http://www.embedded.com/electronics-blogs/programming-pointers/4023341/Lvalues-and-Rvalues C and C enforce subtle differences on the expressions to the left and right of the assignment operator If youve been programming in either C or C for…

Opencv将处理后的视频保存出现的问题

问题描述&#xff1a; 代码运行过程中&#xff0c;imshow出来的每帧的效果图是正确的&#xff0c;但是按照网上的方法保存下来却是0kb&#xff0c;打开不了。 参考的网上的一些方法&#xff0c;均是失败的&#xff0c;具体原因我也不清楚&#xff1a; 1、例如我这样设置&#x…

Java Number shortValue()方法与示例

Number类shortValue()方法 (Number Class shortValue() method) shortValue() method is available in java.lang package. shortValue()方法在java.lang包中可用。 shortValue() method is used to return the value denoted by this Number object converted to type short (…

MATLAB可以打开gms文件吗,gms文件扩展名,gms文件怎么打开?

.gms文件类型&#xff1a;Gesture and Motion Signal File扩展名为.gms的文件是一个数据文件。文件说明&#xff1a;Low-level, binary, minimal but generic format used to organize and store Gesture and Motion Signals in a flexible and optimized way; gesture-related…

黑白图片颜色反转并保存

将图像的黑白颜色反转并保存 import cv2 # opencv读取图像 img cv2.imread(rE:\Python-workspace\OpenCV\OpenCV/YY.png, 1) cv2.imshow(img, img) img_shape img.shape # 图像大小(565, 650, 3) print(img_shape) h img_shape[0] w img_shape[1] # 彩色图像转换为灰度图…

家猫WEB系统

现在只放源码在些.为它写应用很简单有空整理文档演示地址:jiamaocode.com/os/ 源码&#xff1a;http://jiamaocode.com/ProCts/2011/04/14/1918/1918.html转载于:https://www.cnblogs.com/jiamao/archive/2011/04/16/2018339.html

C# DataRow数组转换为DataTable

public DataTable ToDataTable(DataRow[] rows) { if (rows null || rows.Length 0) return null; DataTable tmp rows[0].Table.Clone(); // 复制DataRow的表结构 foreach (DataRow row in rows) tmp.Rows.Add(row); // 将DataRow添加…

plesk 运行不了php,如何在Plesk中使用composer(使用其他版本的PHP运行Composer)

对于基于Plesk的服务器, composer的默认安装将使用系统安装的PHP版本, 而不使用Plesk所安装的任何版本。尽管Composer至少需要PHP 5.3.2, 但是当你尝试在需要特定版本PHP的项目中安装依赖项时, 就会出现问题。例如, 如果你有一个至少需要PHP 7.2的项目, 并且系统的默认PHP安装是…