OpenCV扫描图像三种方式练习

QT += core
QT -= gui

CONFIG += c++11

TARGET = myOpencvTest1
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += C:\OpenCv4.0\include  \
               C:\OpenCv4.0\include\opencv2

CONFIG(debug, debug|release): {
LIBS += C:\OpenCv4.0\x64\vc14\lib\opencv_world400d.lib
#LIBS += -LC:\OpenCv4.0\x64\vc14\lib \
#        -lopencv_world400d
} else:CONFIG(release, debug|release): {
LIBS += C:\OpenCv4.0\x64\vc14\lib\opencv_world400.lib
#LIBS += -LC:\OpenCv4.0\x64\vc14\lib \
#        -lopencv_world400
}


# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
#include <QCoreApplication>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <QDebug>
#include <QDir>
#include <QFile>
#include "iostream"

using namespace std;
using namespace cv;
/*!
 * \brief scanImagePtr
 * \param image
 * \return
 */
Mat& scanImagePtr(Mat &image){
    double start = static_cast<double>(getTickCount());
    //! 行
    int rowNumber = image.rows;
    //! 每一行元素个数 = 列数 x 通道数
    int colNumber = image.cols * image.channels();
    for (int i = 0; i < rowNumber; i++)
    {
        //! 获取第i行的首地址
        uchar* data = image.ptr<uchar>(i);
        for (int j = 0; j < colNumber; j++)
        {
            data[j] /= 2;
        }
    }
    double end = static_cast<double>(getTickCount());
    double time = (end - start) / getTickFrequency();
    cout << "type1:" << time << "s" << endl;
    return image;
}

/*!
 * \brief scanImageiterator
 * \param image
 * \return
 */
Mat& scanImageiterator(Mat &image){
    double start = static_cast<double>(getTickCount());
    //! 初始位置的迭代器
    Mat_<Vec3b>::iterator it = image.begin<Vec3b>();
    //! 终止位置的迭代器
    Mat_<Vec3b>::iterator itend = image.end<Vec3b>();
    for (; it != itend; it++)
    {
        //! 处理BGR三个通道
        (*it)[0] = 255;//B
        //(*it)[1] = 255;//G
        //(*it)[2] = 0;//R
    }
    double end = static_cast<double>(getTickCount());
    double time = (end - start) / getTickFrequency();//计算时间
    cout << "type2:" << time << "s" << endl;
    return image;
}

/*!
 * \brief scanImageAt
 * \param image
 * \return
 */
Mat& scanImageAt(Mat &image){
    double start = static_cast<double>(getTickCount());
    int rowNumber = image.rows;
    int colNumber = image.cols;
    for (int i = 0; i < rowNumber; i++)
        for (int j = 0; j < colNumber; j++)
        {
            //! 处理BGR三个通道
            //image.at<Vec3b>(i, j)[0] = 0;//B
            image.at<Vec3b>(i, j)[1] = 255;//G
            //image.at<Vec3b>(i, j)[2] = 0;//R
        }
    double end = static_cast<double>(getTickCount());
    double time = (end - start) / getTickFrequency();//计算时间
    cout << "type3:" << time << "s" << endl;
    return image;
}
int main()
{
    //! 只创建信息头部分
    Mat oriImage;

    //! 读入图片
    oriImage = imread("C:/1.png", IMREAD_COLOR);
    //! 判断图片是否成功
    if (oriImage.empty()){
        qDebug("image open erro");
        return 1;
    }

    if (!oriImage.data){
        qDebug("image open erro");
        return 1;
    }
    //! 显示原始图片
    imshow("ori", oriImage);

    //! ------------------指针操作-------------------------
    Mat targetImage;
    targetImage = scanImagePtr(oriImage);
    imshow("type1", targetImage);
    //! -----------------迭代器操作------------------------
    targetImage = scanImageiterator(oriImage);
    imshow("type2", oriImage);
    //! ----------------at方式-----------------------
    targetImage = scanImageAt(oriImage);
    imshow("type3", oriImage);
    waitKey(0);
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值