opencv3+Zbar二维码矫正

形变和畸角变换

// Zbar_code.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<zbar.h>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\opencv.hpp>

using namespace std;
using namespace cv;
using namespace zbar;

#define GRAY_THRESH 150
#define HOUGH_VOTE 100

int main()
{
	Mat imageSource = imread("dajiang.jpg", 0);
	Mat srcImage = imageSource.clone();

	Point2f center = Point2f(imageSource.cols / 2, imageSource.rows / 2);
	double angle = 10;
	double scale = .5;
	Mat rotateMat;
	rotateMat = getRotationMatrix2D(center, angle, scale);
	warpAffine(imageSource, imageSource, rotateMat, imageSource.size());
	imshow("旋转", imageSource);
	
	Mat image;
	imageSource.copyTo(image);
	GaussianBlur(image, image, Size(3, 3), 0);  //滤波  
	threshold(image, image, 100, 255, CV_THRESH_BINARY);  //二值化  
	imshow("二值化", image);
	Mat element = getStructuringElement(2, Size(9, 9));  //膨胀腐蚀核  
														 //morphologyEx(image,image,MORPH_OPEN,element);   
	for (int i = 0; i<10; i++)
	{
		erode(image, image, element);
		i++;
	}
	imshow("腐蚀s", image);
	Mat image1;
	erode(image, image1, element);
	image1 = image - image1;
	imshow("边界", image1);
	//寻找直线 边界定位也可以用findContours实现  
	vector<Vec2f>lines;
	HoughLines(image1, lines, 1, CV_PI / 150, 250, 0, 0);
	Mat DrawLine = Mat::zeros(image1.size(), CV_8UC1);
	for (int i = 0; i<lines.size(); i++)
	{
		float rho = lines[i][0];
		float theta = lines[i][1];
		Point pt1, pt2;
		double a = cos(theta), b = sin(theta);
		double x0 = a*rho, y0 = b*rho;
		pt1.x = cvRound(x0 + 1000 * (-b));
		pt1.y = cvRound(y0 + 1000 * a);
		pt2.x = cvRound(x0 - 1000 * (-b));
		pt2.y = cvRound(y0 - 1000 * a);
		line(DrawLine, pt1, pt2, Scalar(255), 1, CV_AA);
	}
	imshow("直线", DrawLine);
	Point2f P1[4];
	Point2f P2[4];
	vector<Point2f>corners;
	goodFeaturesToTrack(DrawLine, corners, 4, 0.1, 10, Mat()); //角点检测  
	for (int i = 0; i<corners.size(); i++)
	{
		circle(DrawLine, corners[i], 3, Scalar(255), 3);
		P1[i] = corners[i];
	}
	imshow("交点", DrawLine);
	int width = P1[1].x - P1[0].x;
	int hight = P1[2].y - P1[0].y;
	P2[0] = P1[0];
	P2[1] = Point2f(P2[0].x + width, P2[0].y);
	P2[2] = Point2f(P2[0].x, P2[1].y + hight);
	P2[3] = Point2f(P2[1].x, P2[2].y);
	Mat elementTransf;
	elementTransf = getAffineTransform(P1, P2);
	warpAffine(imageSource, imageSource, elementTransf, imageSource.size(), 1, 0, Scalar(255));
	imshow("校正", imageSource);
	//Zbar二维码识别  
	ImageScanner scanner;
	scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
	int width1 = imageSource.cols;
	int height1 = imageSource.rows;
	uchar *raw = (uchar *)imageSource.data;
	Image imageZbar(width1, height1, "Y800", raw, width * height1);
	scanner.scan(imageZbar); //扫描条码      
	Image::SymbolIterator symbol = imageZbar.symbol_begin();
	if (imageZbar.symbol_begin() == imageZbar.symbol_end())
	{
		cout << "查询条码失败,请检查图片!" << endl;
	}
	for (; symbol != imageZbar.symbol_end(); ++symbol)
	{
		cout << "类型:" << endl << symbol->get_type_name() << endl << endl;
		cout << "条码:" << endl << symbol->get_data() << endl << endl;
	}
	namedWindow("Source Window", 0);
	imshow("Source Window", srcImage);

	waitKey(0);
	imageZbar.set_data(NULL, 0);

    return 0;
}



该方法还需要改进,后期会更新....

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值