Linux使用openCV把拍摄的照片转换.jpg格式的照片

1、软件工具

ubuntu18.4
openCV3.4.13
准备工作在其他文档

2、代码

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include "opencv2/imgproc.hpp"
#include <opencv2/objdetect.hpp>

#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

int main(int, char**)
{
    Mat Colorimg;
    Mat Grayimg;
    Mat Face;
    vector<Rect> Allface;
	vector< uchar > Jpgface;

    //--- INITIALIZE VIDEOCAPTURE
    VideoCapture cap;
    // open the default camera using default API
    // cap.open(0);
    // OR advance usage: select any API backend
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
    // open selected camera using selected API
    cap.open(deviceID, apiID);  //打开相机
    // check if we succeeded
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return -1;
    }
    //--- GRAB AND WRITE LOOP
    cout << "Start grabbing" << endl
        << "Press any key to terminate" << endl;
        
    CascadeClassifier Classifier("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml");//分类器,人脸识别xml文件,训练的结果文件
	
	for (;;)
    {
        // wait for a new frame from camera and store it into 'frame'
        cap.read(Colorimg);
        // check if we succeeded
        if (Colorimg.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }

        cvtColor(Colorimg,Grayimg,CV_BGR2GRAY); //灰度处理

        equalizeHist(Grayimg,Grayimg); //权重处理

        Classifier.detectMultiScale(Grayimg,Allface);//从输入的对象中检测出不同尺寸的对象,把检测到的人脸都保存下来
        if(Allface.size())
        {
                rectangle(Colorimg,Allface[0],Scalar(255,255,0));//在人脸上画框
                Face = Grayimg(Allface[0]);  //把最大的那张人脸放入Face容器
                imencode(".jpg",Face,Jpgface); //把图片转成.jpg格式
                //到这儿可以结合智能云进行在线识别了
        }
        // show Camero and wait for a key with timeout long enough to show images
        imshow("Camero", Colorimg);
        if (waitKey(5) >= 0)
            break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

3、有些代码未解释看其他文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要实现银行卡或饭卡卡号检测功能,可以使用OpenCV中的数字图片模板匹配法。以下是一个简单的实现步骤: 1. 读取待检测的银行卡或饭卡图像,并将其转换为灰度图像。 2. 准备数字模板图像,可以手动制作或从已有的数字库中选择。将数字模板图像转换为灰度图像。 3. 使用OpenCV中的模板匹配函数,将数字模板与待检测图像逐一匹配,得到匹配得分和位置。 4. 根据匹配得分和位置,确定卡号所在的区域。 5. 对卡号区域进行OCR识别,得到卡号。 以下是一个简单的代码示例,用于实现上述功能: ``` #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { // 读取待检测图像 Mat src = imread("card.jpg", IMREAD_GRAYSCALE); if (src.empty()) { cout << "Could not open or find the image!\n" << endl; return -1; } // 准备数字模板图像 vector<Mat> templates; templates.push_back(imread("0.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("1.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("2.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("3.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("4.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("5.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("6.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("7.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("8.jpg", IMREAD_GRAYSCALE)); templates.push_back(imread("9.jpg", IMREAD_GRAYSCALE)); // 模板匹配 double minVal, maxVal; Point minLoc, maxLoc; for (int i = 0; i < templates.size(); i++) { Mat result; matchTemplate(src, templates[i], result, TM_CCORR_NORMED); minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc); rectangle(src, maxLoc, Point(maxLoc.x + templates[i].cols, maxLoc.y + templates[i].rows), Scalar(0, 255, 0), 2); } // 显示结果 namedWindow("Result", WINDOW_NORMAL); imshow("Result", src); waitKey(0); return 0; } ``` 在上述代码中,我们首先读取待检测的卡片图像和数字模板图像,然后使用`matchTemplate()`函数对每个数字模板进行匹配。最后,将匹配结果用矩形框标出,显示在窗口中。 请注意,以上示例仅演示了检测和定位数字的基本方法。要实现完整的卡号识别功能,还需要对卡号区域进行角度矫正和OCR识别等进一步处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漏洞百出

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值