总结1
创建空白图像,并在此基础上画圆、矩形、直线、椭圆、文本。利用这些工具画简单的图形。
参数的具体使用
代码
#include <iostream>
#include <string>
#include <sstream>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace std;
using namespace cv;
int main()
{
//create blank image
Mat img(512, 512, CV_8UC3, Scalar(255, 255, 255));
circle(img, Point(256, 256), 190, Scalar(255, 0, 255), FILLED);
ellipse(img, Point(175,195), Size(40,20), 0, 0, 360, Scalar(0, 0, 0), FILLED);
ellipse(img, Point(335,195), Size(40,20), 0, 0, 360, Scalar(0, 0, 0), FILLED);
line(img, Point(175,215), Point(175, 245), Scalar(0, 0, 255), 2);
line(img, Point(335,215), Point(335, 245), Scalar(0, 0, 255), 2);
circle(img, Point(256,306), 20, Scalar(0, 255, 255), FILLED);
rectangle(img, Point(235,375), Point(275, 380), Scalar(0, 0, 255),FILLED);
putText(img, String("OUCH!"), Point(215,40), FONT_HERSHEY_TRIPLEX, 1, Scalar(0, 255, 0), 3);
imshow("Img", img);
waitKey(0);
}
结果
总结2
利用透射变换将一个矩形区域提取出来
代码
#include <iostream>
#include <string>
#include <sstream>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace std;
using namespace cv;
float w = 92, h = 103;
Mat matrix, imgWarp;
int main()
{
Mat img;
img = imread("me.jpg");
resize(img, img, Size(), 0.4, 0.4);
//the coordinates of source points
Point2f src[4] = {{95,264}, {187,264}, {95,367}, {187,367}};
//the coordinates of destination points
Point2f dst[4] = {{0.0f,0.0f}, {w,0.0f}, {0.0f,h}, {w,h}};
//perspective transformation
matrix = getPerspectiveTransform(src, dst);
warpPerspective(img, imgWarp, matrix, Size(w,h));
//sign source points
for(int i = 0; i < 4; i++)
{
circle(img, src[i], 5, Scalar(0, 0, 255), FILLED);
}
//show image
imshow("Image", img);
imshow("Image Wrap", imgWarp);
waitKey(0);
}
结果
原图
处理后的图片