我的目录结构如下:
因为我在使用的是OpenCV ,所以项目里面的 头文件:#include <opencv2/opencv.hpp> 和命名空间 using namespace cv;
你直接删了就行,不然你直接把我代码复制过去会报错的,我这里只是告诉大家怎么调用其他类的函数
1. stdafx_h 常用的库文件
//
// stdafx.h
// FirstCTest
//
// Created by lishengfu on 2018/12/2.
// Copyright © 2018 lishengfu. All rights reserved.
//
#ifndef stdafx_h
#define stdafx_h
#include <iostream>
#include <opencv2/opencv.hpp>
#include "OpenCVFunction.hpp"
using namespace cv;
using namespace std;
#endif /* stdafx_h */
2. OpenCVFunction_hpp
//
// OpenCVFunction.hpp
// FirstCTest
//
// Created by lishengfu on 2018/12/2.
// Copyright © 2018 lishengfu. All rights reserved.
//
#ifndef OpenCVFunction_hpp
#define OpenCVFunction_hpp
#include <stdio.h>
#endif /* OpenCVFunction_hpp */
class OpenCVFunction{
public:
OpenCVFunction();
~OpenCVFunction();
public :
bool ROI_AddImage();
int num;
private:
};
3. OpenCVFunction.cpp
//
// OpenCVFunction.cpp
// FirstCTest
//
// Created by lishengfu on 2018/12/2.
// Copyright © 2018 lishengfu. All rights reserved.
//
#include "stdafx.h"
OpenCVFunction::OpenCVFunction(){
}
OpenCVFunction::~OpenCVFunction(){
}
//----------------------------------【ROI_AddImage( )函数】----------------------------------
// 函数名:ROI_AddImage()
// 描述:利用感兴趣区域ROI实现图像叠加
//----------------------------------------------------------------------------------------------
bool OpenCVFunction::ROI_AddImage(){
Mat srcImage = imread("/Users/administrator/Desktop/cool.jpg");
Mat logoImage = imread("/Users/administrator/Desktop/hackerLogo.jpg");
if ( srcImage.empty()) cout<< "srcImage null";
if ( logoImage.empty()) cout<< "logoImage null";
//【2】定义一个Mat类型并给其设定ROI区域
Mat srcImageROI = srcImage(Rect(50,50,logoImage.cols,logoImage.rows));
cout<<logoImage.cols<<endl;
cout<<logoImage.rows<<endl;
//【3】加载掩模(必须是灰度图)
Mat mask= imread("/Users/administrator/Desktop/hackerLogo.jpg",0);
logoImage.copyTo(srcImageROI,mask);
namedWindow("srcImage",CV_WINDOW_AUTOSIZE);
imshow("srcImage",srcImage);
imshow("mask",mask);
return true;
}
4. main.cpp
//
// main.cpp
// FirstCTest
//
// Created by lishengfu on 2018/11/29.
// Copyright © 2018 lishengfu. All rights reserved.
//
#include "stdafx.h"
int main() {
// insert code here...
OpenCVFunction *cv=new OpenCVFunction();
cv->ROI_AddImage();
waitKey(60000);
return 0;
}