1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| #include </home/dl/InternetPlus/lib/zbarcode.h>
void Zbarcode::prProcess(Mat gray){ int threadvlaue = 255; Mat enhancedImage; equalizeHist(gray, enhancedImage); Mat binary; threshold( enhancedImage, binary,152, threadvlaue,THRESH_BINARY ); Mat morphedImage; Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); morphologyEx(binary, morphedImage, MORPH_OPEN, kernel); imshow("3",morphedImage); } void Zbarcode::QrcodeSearch(Mat src){ ImageScanner scanner; scanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1); Mat gray; cvtColor(src, gray, COLOR_BGR2GRAY); prProcess(gray); int width =gray.cols; int height = gray.rows; uchar *raw = gray.data; Image zbarImage(width, height, "Y800", raw, width * height); scanner.scan(zbarImage);
if (zbarImage.symbol_begin() != zbarImage.symbol_end()) { for (Image::SymbolIterator symbol = zbarImage.symbol_begin(); symbol != zbarImage.symbol_end(); ++symbol) { vector<Point> points; vector<Point2f> imagePoints; for (int i = 0; i < symbol->get_location_size(); ++i) { points.push_back(Point(symbol->get_location_x(i), symbol->get_location_y(i))); }
for (int i = 0; i < 4; ++i) { line(src, points[i], points[(i + 1) % 4], Scalar(0, 255, 0), 2); } for (; symbol != zbarImage.symbol_end(); ++symbol) { cout << "类型:" << endl << symbol->get_type_name() << endl << endl; cout << "条码:" << endl << symbol->get_data() << endl << endl; }
for (int i = 0; i < symbol->get_location_size(); ++i) { imagePoints.push_back(Point2f(symbol->get_location_x(i), symbol->get_location_y(i))); }
}} else { cout << "查询二维码失败!" << endl; imshow("4",src); } }
void Zbarcode::QrcodeSearch(Mat src){ ImageScanner scanner; scanner.set_config(ZBAR_QRCODE, ZBAR_CFG_ENABLE, 1); Mat gray; cvtColor(src, gray, COLOR_BGR2GRAY); prProcess(gray); int width =gray.cols; int height = gray.rows; uchar *raw = gray.data; Image zbarImage(width, height, "Y800", raw, width * height); scanner.scan(zbarImage);
if (zbarImage.symbol_begin() != zbarImage.symbol_end()) { for (Image::SymbolIterator symbol = zbarImage.symbol_begin(); symbol != zbarImage.symbol_end(); ++symbol) { vector<Point> points; vector<Point2f> imagePoints; for (int i = 0; i < symbol->get_location_size(); ++i) { points.push_back(Point(symbol->get_location_x(i), symbol->get_location_y(i))); }
for (int i = 0; i < 4; ++i) { line(src, points[i], points[(i + 1) % 4], Scalar(0, 255, 0), 2); }
vector<Point3f> objectPoints; objectPoints.push_back(Point3f(-1.5, 3, 0)); objectPoints.push_back(Point3f(1.5, 3, 0)); objectPoints.push_back(Point3f(1.5,0, 0)); objectPoints.push_back(Point3f(-1.5,0, 0));
for (int i = 0; i < symbol->get_location_size(); ++i) { imagePoints.push_back(Point2f(symbol->get_location_x(i), symbol->get_location_y(i))); }
cv::Mat cameraMatrix, distCoeffs; std::vector<double> camera = { 968.4681, 0, 988.8516, 0, 968.1535, 549.3471, 0, 0, 1 }; cameraMatrix = cv::Mat(camera); cameraMatrix = cameraMatrix.reshape(1, 3); std::vector<double> dist = { 0.1869, -0.2228, 0, 0, 0}; distCoeffs = cv::Mat(dist); distCoeffs = distCoeffs.reshape(1, 1);
Mat rvecs ; Mat tvecs ; solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs, rvecs, tvecs);
Mat rotM = Mat::eye(3, 3, CV_64F); Mat rotT = Mat::eye(3, 3, CV_64F); Rodrigues(rvecs, rotM); Rodrigues(tvecs, rotT); Mat adjustTvecs; Mat rot_camera2ptz = cv::Mat::eye(3, 3, CV_64FC1); Mat trans_camera2ptz = cv::Mat::zeros(3, 1, CV_64FC1); adjustTvecs = rot_camera2ptz * tvecs - trans_camera2ptz; adjustAngle(adjustTvecs,angle_x,angle_y,1,0);
Mat distance=Mat::zeros(Size(3,3), CV_64F); distance = rotM.t() * tvecs; imshow("4",src); printf("angle_x:%.4lf ,angle_y:%.4lf\n,distance: %.2lf\n", angle_x,angle_y,abs(distance.at<double>(2))); }} else { cout<<"error"<<endl; imshow("4",src); } }
#include <opencv2/opencv.hpp> #include <iostream> #include </home/dl/InternetPlus/lib/zbarcode.h> #include </home/dl/InternetPlus/lib/ArUcode.h>
using namespace cv;
int main() { Zbarcode cam; VideoCapture cap(0); while (1) { Mat src; cap >> src; cam.QrcodeSearch(src); waitKey(1); } return 0;
}
|