【opencv学习之四十六】OpenCV4.2 QR二维码识别及定位

首先系统得安装opencv4以上的库,代码实验了一下,相比zxing和zbar,更简洁一些,并且提供了二维码定位;

代码如下:

#include 
#include 
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main()
{	//读取当前工程内图片
	Mat img = imread("1.jpg");
	QRCodeDetector QRdetecter;
	std::vector list;
	Mat res;
	std::string str=QRdetecter.detectAndDecode(img, list, res);

	for (int i = 0; i < list.size(); i++)
	{
	    if(i==3)
		line(img, list[i], list[0], Scalar(255, 255, 0), 2);
		else
		line(img, list[i], list[i + 1], Scalar(255, 255, 0), 2);
	}
	putText(img,str, Point(10, 400),FONT_HERSHEY_COMPLEX,2,Scalar(0, 255, 255), 2, 8);
	imshow("QR二维码定位", img);
	waitKey(0);
	return 0;
}

我的系统是ubuntu,所以用cmake编译的,cmake文件如下:


# cmake needs this line
cmake_minimum_required(VERSION 3.1)

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

project(qr)
find_package(OpenCV REQUIRED)
FILE(GLOB SC_FILES "*.cpp" "*.h")
add_executable(${PROJECT_NAME} ${SC_FILES})
target_link_libraries(qr PRIVATE ${OpenCV_LIBS})

效果如下:

【opencv学习之四十六】OpenCV4.2 QR二维码识别及定位_第1张图片

发现手机扫不出上面的图,附一张原图:

【opencv学习之四十六】OpenCV4.2 QR二维码识别及定位_第2张图片

你可能感兴趣的:(opencv)