一、图像腐蚀
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main()
{
Mat srcImage = imread("C:\\Users\\挽笙\\Desktop\\1.jpg");
imshow("[原图]腐蚀操作", srcImage);
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
Mat dstImage;
erode(srcImage, dstImage, element);
imshow("[效果图]腐蚀操作",dstImage);
waitKey(0);
return(0);
}
二、图像旋转
#include<iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int test1()
{
Mat img1, img2,img3;
img2 = cv::Mat(640, 480,CV_8UC3);
img1 = imread("D://images//opencv测试图片//1.jpg");
if (img1.empty())
{
return -1;
}
int width1 = img1.rows;
int height1 = img1.cols;
std::cout << "w:" << width1 << " h1:" << height1<<std::endl;
double angle = 90.0; //角度
double scale = 1.0; //缩放系数
cv::transpose(img1, img2);
cv::flip(img2, img2, 1); //1:90 0:-90 -1:180
int width2 = img2.rows;
int height2 = img2.cols;
std::cout << "w:" << width2 << " h1:" << height2 << std::endl;
imshow("win1", img1);
imshow("win2", img2);
imwrite("D://images//opencv测试图片//img20.jpg", img2);
return 0;
}
int main()
{
test1();
waitKey(0);
return 0;
}
三、图像颜色识别
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>
using namespace cv;
int alpha_slider1, alpha_slider2;
void on_trackbar(int, void*) {}
int main()
{
VideoCapture capture(0); // 从摄像头读入视频
namedWindow("[总]");
createTrackbar("Hmin", "[总]", &alpha_slider1, 180, on_trackbar);
createTrackbar("Hmax", "[总]", &alpha_slider2, 180, on_trackbar);
Mat frame; // 定义一个Mat变量,用于存储每一帧的图像
Mat HSV;
Mat mask; // 保存inRange后的值
while (1)
{
capture >> frame; // 读取当前帧
cvtColor(frame, HSV, COLOR_BGR2HSV); // 转化为HSV
inRange(HSV, Scalar(alpha_slider1, 43, 46), Scalar(alpha_slider2, 255, 255), mask);
imshow("保存", mask);
imshow("[总]", frame);
waitKey(30); // 延时30ms
}
return 0;
}
|