opencv识别多条形码数字_opencv测试代码

opencv识别多条形码数字_opencv测试代码这其实是一个小工程完成的功能: 使用摄像头采集图像进行预处理(检测部分) 提取出预处理的条形码图像(识别部分) 将条形码进行存入数据库(存储部分) 首先接到这个图像识别的小工程需要先确定这个工程的最初输入,和最后输出,输入就是普通的RGB图像,输出是数据库文件。其中需要完成的过程,就是我需要做得功能,检测部分、识别部分和存储部分,话不多说,上部分代码:/…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

这其实是一个小工程

完成的功能:

  • 使用摄像头采集图像进行预处理(检测部分)

  • 提取出预处理的条形码图像(识别部分)

  • 将条形码进行存入数据库(存储部分)

首先接到这个图像识别的小工程需要先确定这个工程的最初输入,和最后输出,输入就是普通的RGB图像,输出是数据库文件。

其中需要完成的过程,就是我需要做得功能,检测部分、识别部分和存储部分,话不多说,上部分代码:

//检测部分  需要用到opencv开源计算机视觉库
//输入是RGB  输出是保存的检测部分


Mat Check(Mat image)
{

    vector<vector<Point>> contours;
	vector<Vec4i> hiera;
	imshow("原图", image);

	//原图像大小调整,提高运算效率  
	//resize(image, image, Size(500, 300));
	//imshow("原图像", image); waitKey(15);		system("pause");



	//转化为灰度图  
	cvtColor(image, imageGray, CV_RGB2GRAY);
	//imshow("灰度图", imageGray); waitKey(15);		system("pause");

	//高斯平滑滤波  
	GaussianBlur(imageGray, imageGuussian, Size(3, 3), 0);
	//imshow("高斯平衡滤波", imageGuussian); waitKey(15);		system("pause");

	//求得水平和垂直方向灰度图像的梯度差,使用Sobel算子  
	Mat imageX16S, imageY16S;
	Sobel(imageGuussian, imageX16S, CV_16S, 1, 0, 3, 1, 0, 4);
	Sobel(imageGuussian, imageY16S, CV_16S, 0, 1, 3, 1, 0, 4);
	convertScaleAbs(imageX16S, imageSobelX, 1, 0);
	convertScaleAbs(imageY16S, imageSobelY, 1, 0);
	imageSobelOut = imageSobelX - imageSobelY;
	//imshow("X方向梯度", imageSobelX); waitKey(15);		system("pause");
	//imshow("Y方向梯度", imageSobelY); waitKey(15);		system("pause");
	//imshow("XY方向梯度差", imageSobelOut); waitKey(15);		system("pause");

	//均值滤波,消除高频噪声  
	blur(imageSobelOut, imageSobelOut, Size(3, 3));
	//imshow("均值滤波", imageSobelOut); waitKey(15);		system("pause");

	//二值化  
	Mat imageSobleOutThreshold;
	threshold(imageSobelOut, imageSobleOutThreshold, 100, 255, CV_THRESH_BINARY);
	//imshow("二值化", imageSobleOutThreshold); waitKey(15);		system("pause");

	//闭运算,填充条形码间隙  
	Mat  element = getStructuringElement(1, Size(9, 9));
	morphologyEx(imageSobleOutThreshold, imageSobleOutThreshold, MORPH_CLOSE, element);
	//imshow("闭运算", imageSobleOutThreshold); waitKey(15);		system("pause");

	//腐蚀,去除孤立的点  
	erode(imageSobleOutThreshold, imageSobleOutThreshold, element);
	//imshow("腐蚀", imageSobleOutThreshold); waitKey(15);		system("pause");

	//膨胀,填充条形码间空隙,根据核的大小,有可能需要2~3次膨胀操作  
	dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
	dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
	dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
	//dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
	//dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
	//dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
	//imshow("膨胀", imageSobleOutThreshold); waitKey(30);		system("pause");

	return imageSobleOutThreshold;
}


int main(int argc, char *argv[])
{
    ...;
    //测试用
	//image = imread("1.jpg");
	//定义两个容器去存放矩形区域
	vector<vector<Point>> contours;
	vector<Vec4i> hiera;
	imshow("原图", image);
	findContours(Check(image), contours, hiera, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
    for (int i = 0; i < contours.size(); i++)
	{
		Rect rect = boundingRect((Mat)contours[i]);
		rectangle(image, rect, Scalar(255), 2);
		Mat ROI = image(rect);
		sprintf(filenameIfZbar, "G:\\using\\text\\vs\\work\\zbar\\if1weima\\%d.jpg", i);
		//imshow(filename, rect);
		imwrite(filenameIfZbar, ROI);
		//imshow("ROI",image); waitKey(30);		system("pause");
		waitKey(1000); // 等待按下esc键,若需要延时1s则改用waitKey(1000);  

        ...;
	}
    ...;
}

Jetbrains全家桶1年46,售后保障稳定

检测效果图,已经存入图像图: 

opencv识别多条形码数字_opencv测试代码

opencv识别多条形码数字_opencv测试代码

识别部分输入是保存的检测为条形码区域图像,输出是一维码图像,部分代码:

int main(int argc, char *argv[])
{
    ...;
    for (int i = 0; i < contours.size(); i++)
	{ 
        ...;
		temp = image;


		cvtColor(temp, imageGray, CV_RGB2GRAY);
		//imshow("灰度图", imageGray);
		// 获取所摄取图像的长和宽  
		int width = imageGray.cols;
		int height = imageGray.rows;
		// 在Zbar中进行扫描时候,需要将OpenCV中的Mat类型转换为(uchar *)类型,raw中存放的是图像的地址;对应的图像需要转成Zbar中对应的图像zbar::Image  
		uchar *raw = (uchar *)imageGray.data;
		Image imageZbar(width, height, "Y800", raw, width * height);
		// 扫描相应的图像imageZbar(imageZbar是zbar::Image类型,存储着读入的图像)  
		scanner.scan(imageZbar); //扫描条码      
		Image::SymbolIterator symbol = imageZbar.symbol_begin();
		if (imageZbar.symbol_begin() == imageZbar.symbol_end())
		{
			cout << "查询条码失败,请检查图片!" << endl;
			//system(delFile);
			continue;
		}
		for (; symbol != imageZbar.symbol_end(); ++symbol)
		{
			string type, data;
			type = symbol->get_type_name();
			data = symbol->get_data();
			cout << "类型:" << endl << type << endl << endl;
			cout << "条码:" << endl << data << endl << endl;
			sprintf(filenameIsZbar, "G:\\using\\text\\vs\\work\\zbar\\is1weima\\_%s_%s.jpg", type.c_str(), data.c_str());
			//imshow(filename, rect);
			imwrite(filenameIsZbar, ROI);
            ...;
		}
		waitKey(1000); // 等待按下esc键,若需要延时1s则改用waitKey(1000);  
	}
    ...;
}

效果图:

 opencv识别多条形码数字_opencv测试代码

识别完成的图像进行存储,部分代码+效果图:

int main(int argc, char *argv[])
{
    ...;
    for (int i = 0; i < contours.size(); i++)
	{ 
        ...;
		for (; symbol != imageZbar.symbol_end(); ++symbol)
		{
            ...;
            //表data写入数据
			sprintf(filedir, "..\\is1weima\\_%s_%s.jpg", type.c_str(), data.c_str());
			sprintf(csql_table1, "INSERT INTO [zbar].[dbo].[data]([type],[data],[imag]) VALUES('%s','%s','%s')", type.c_str(), data.c_str(), filedir);
			Sql = csql_table1;
			try{
				_RecordsetPtr pRst(__uuidof(Recordset)); //实例化一个Recordset对象pRst
				_CommandPtr pCmd(__uuidof(Command)); //实例化一个Command对象pCmd
				pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConnection));
				pCmd->CommandText = (_bstr_t)Sql;
				pRst = pCmd->Execute(NULL, NULL, adCmdText);
				cout << "添加成功!" << endl;
				pRst.Release();
				pCmd.Release();
			}
			catch (_com_error e) {
				cout << e.ErrorMessage() << endl;
				cout << "添加失败!" << endl;
			}
			//表num写入数据
			try {
				sprintf(csql_table2, "select count(%s) [data] from [zbar].[dbo].[data]group by [data]", data.c_str());
				Sql = csql_table2;
				//_variant_t value;
				_RecordsetPtr pRst(__uuidof(Recordset)); //实例化一个Recordset对象pRst
				_CommandPtr pCmd(__uuidof(Command)); //实例化一个Command对象pCmd
				pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConnection));
				pCmd->CommandText = (_bstr_t)Sql;
				pRst = pCmd->Execute(NULL, NULL, adCmdText);
				int valueline = pRst->GetCollect("data");
				//update [zbar].[dbo].[data] set [data]='12456'where [imag]='..\is1weima\_EAN-13_4589732812540.jpg  
				cout << "查询成功!" << endl;
				sprintf(csql_table3, "update [zbar].[dbo].[num] set [num]='%d'where [data]='%s'", valueline, data.c_str());
				Sql = csql_table3;
				pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConnection));
				pCmd->CommandText = (_bstr_t)Sql;
				pRst = pCmd->Execute(NULL, NULL, adCmdText);
				cout << "修改成功!" << endl;
				pRst.Release();
				pCmd.Release();
			}
			catch (_com_error e) {
				//cout << e.ErrorMessage() << endl;
				//cout << "修改失败!" << endl;
				sprintf(csql_table1, "INSERT INTO [zbar].[dbo].[num]([type],[data],[num]) VALUES('%s','%s','%d')", type.c_str(), data.c_str(), 1);
				Sql = csql_table1;
				_RecordsetPtr pRstR(__uuidof(Recordset)); //实例化一个Recordset对象pRst
				_CommandPtr pCmdR(__uuidof(Command)); //实例化一个Command对象pCmd
				pCmdR->put_ActiveConnection(_variant_t((IDispatch*)pConnection));
				pCmdR->CommandText = (_bstr_t)Sql;
				pRstR = pCmdR->Execute(NULL, NULL, adCmdText);
				cout << "添加成功!" << endl;
				pRstR.Release();
				pCmdR.Release();
			}
		}
		waitKey(1000); // 等待按下esc键,若需要延时1s则改用waitKey(1000);  
	}
    ...;
}

opencv识别多条形码数字_opencv测试代码 opencv识别多条形码数字_opencv测试代码

opencv识别多条形码数字_opencv测试代码

工程已上传,审核中… 

审核通过OpenCV识别多条形码链接

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223795.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 硬件SPI控制ST7789V「建议收藏」

    文章目录前言一、能用的方法有那些?1.利用硬件UART模拟SPI,实现9位数据发送2.搭建一个数字逻辑电路,人为实现9位spi发送3.发送两组8位SPI二、具体操作1.具体实现总结前言这段时间闲着没事,决定自己做个手表玩玩,参考了一些开源项目和数据手册,决定主控使用STM32F411CEU6,屏幕选用了淘宝一家商家的ST7789V主控的240*240分辨率的圆形tft屏幕。利用商家给的软件spi例程成功将屏幕点亮之后,尝试使用改为硬件spi的方式,加快屏幕的刷新速度但是尝试了多种方案后始终无法点亮,

    2022年4月9日
    605
  • 如何构建NTP时间服务器「建议收藏」

    NTP服务器是用于局域网服务器时间同步使用的,可以保证局域网所有的服务器与时间服务器的时间保持一致,某些应用对时间实时性要求高的必须统一时间。互联网的时间服务器也有很多,例如ntpdatentp.fudan.edu.cn复旦大学的NTP免费提供互联网时间同步。NTP服务器监听端口为UDP的123,那就需要在本地防火墙开启运行客户端访问123端口,vi/etc/sysconfig/iptables添加如下规则:-AINPUT-mstate–stateNEW-mudp-pudp

    2022年4月12日
    32
  • Maven项目报错:“ SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder ”解决办法「建议收藏」

    Maven项目报错:“ SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder ”解决办法「建议收藏」运行Maven项目时,控制台出现如下图所示的报错信息:问题分析:根据报错提示,我们可以知道出错的原因是“加载类文件org.slf4j.impl.StaticLoggerBinder时失败”,而出错的地方主要是在于slf4j的jar包。官网给出的解决思路如下:Thiserrorisreportedwhentheorg.slf4j.impl.StaticLoggerBinder…

    2025年6月22日
    4
  • 什么叫买单报关_代理报关和买单报关费用是一样的吗

    什么叫买单报关_代理报关和买单报关费用是一样的吗报关是指货物、行李和邮递物品、运输工具等在进出关境或国境时由所有人或其代理人向海关申报,交验规定的单据、证件,请求海关办理进出口的有关手续。我国海关规定报关时应交纳的单据、证件。有:进出口货物报关单、进出口货物许可证、商品检验证书、动植物检疫证书、食品卫生检验证书以及提货单、装货单、运单、发票、装箱单等。买单出口,其实就是没有出口权的工厂或SOHO通过买别的进出口公司的核销单,以该公司的名义进行外贸出口。买单出口所买的“单”主要是指核销单,但是卖单出口服务的公司除了提供核销单之外还需要提供与核销单抬头一

    2022年9月21日
    3
  • 在vue中使用tinymce富文本编辑器+tinymce富文本编辑器插入图片

    在vue中使用tinymce富文本编辑器+tinymce富文本编辑器插入图片1.安装#npminstalltinymce-S2.把node_modules\tinymce里面的文件包括tinymce文件夹全部复制到static文件夹下面,如下图3.在mian.js中引入tinymce(也可以在组件中引入)importTinymcefrom’tinymce’Vue.prototype.$tinymce=Tinymce…

    2022年5月1日
    82
  • android Gradle 教程

    android Gradle 教程发现一个官方翻译后的gradle教程:http://avatarqing.github.io/Gradle-Plugin-User-Guide-Chinese-Verision/index.html

    2022年6月28日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号