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 <opencv2/imgproc.hpp>
#include <QDebug>
#include <QDir>
#include <QFile>
#include "iostream"
using namespace std;
using namespace cv;
/*!
* \brief myMaskFun
* \param srcImage
* \return
*/
Mat myMaskFun(Mat srcImage)
{
const int nChannels = srcImage.channels();
Mat resultImage(srcImage.size(), srcImage.type());
for (int j = 1; j < srcImage.rows - 1; j++)
{
const uchar* previous = srcImage.ptr<uchar>(j - 1);
const uchar* current = srcImage.ptr<uchar>(j);
const uchar* next = srcImage.ptr<uchar>(j + 1);
uchar * output = resultImage.ptr<uchar>(j);
for (int i = nChannels; i < nChannels*(srcImage.cols - 1); ++i)
{
*output++ = saturate_cast<uchar>(current[i - nChannels] + current[i + nChannels]
+ previous[i] + next[i]) / 4;
}
}
//! 进行边界处理
resultImage.row(0).setTo(Scalar(0));
resultImage.row(resultImage.rows - 1).setTo(Scalar(0));
resultImage.col(0).setTo(Scalar(0));
resultImage.col(resultImage.cols - 1).setTo(Scalar(0));
return resultImage;
}
/*!
* \brief systemMaskFun
* \param srcImage
* \return
*/
Mat systemMaskFun(Mat srcImage)
{
Mat resultImage(srcImage.size(), srcImage.type());
//! 构造核函数因子
Mat kern = (Mat_<float>(3, 3) << 0, 1, 0,
1, 0, 1,
0, 1, 0) / (float)(4);
filter2D(srcImage, resultImage, srcImage.depth(), kern);
return resultImage;
}
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 resGrayImage;
cvtColor(oriImage, resGrayImage,COLOR_BGR2GRAY);
imshow("grayImage", resGrayImage);
//! 基于像素邻域的掩码操作
Mat resImage = myMaskFun(resGrayImage);
imshow("resultImage1", resImage);
//! 系统函数方式
resImage = systemMaskFun(oriImage);
imshow("resultImage2", resImage);
waitKey(0);
return 0;
}