OpenCV学习之基础操作(1116)

#包含头文件

#include <opencv2/opencv.hpp>
#include"highgui.h"
#include <opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <string> 
#include<fstream>
#include <opencv2/core/core.hpp>  
#include"opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;

#图像掩膜操作

//图像掩膜操作
//int main(int argc, char** argv)
//{
//	//读取本地的一张图片便显示出来
//	Mat img, dst;
//	img = imread("Ironman.jpg"); 
//	if (img.empty()) {
//		cout << "could not load image..." << endl;
//		return  -1;
//	}
//	namedWindow("input window", CV_WINDOW_AUTOSIZE);
//	imshow("input window", img);
//
//
//	int offsetx = img.channels();
//	int cols = (img.cols - 1)*img.channels(); //获取图像的列数、注意还有图像的通道数         
//	int rows = img.rows;                      //获取图像的行数
//	dst = Mat::zeros(img.size(), img.type()); //创建一个和源图像大小相同,类型相同的全0矩阵
//
//	for (int row = 1; row < (rows - 1); row++) {
//		const uchar* previous = img.ptr<uchar>(row - 1);//上
//		const uchar* current = img.ptr<uchar>(row);  //获取当前行的指针
//		const uchar* next = img.ptr<uchar>(row + 1);   //下
//		uchar* output = dst.ptr<uchar>(row);
//		for (int col = offsetx; col < cols; col++) {
//			output[col] = saturate_cast<uchar> (5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
//		}
//
//	}
//	namedWindow("contrast image demo", CV_WINDOW_AUTOSIZE);
//	imshow("contrast image demo", dst);
//	waitKey(0);
//}

#使用OpenCV自带函数filter2D来实现掩膜操作:

//使用OpenCV自带函数filter2D来实现掩膜操作:
//int main(int argc, char** argv)
//
//{
   
//	//读取本地的一张图片便显示出来
//	Mat img, dst;
//	img = imread("Ironman.jpg");
//	if (img.empty()) {
   
//		cout << "could not load image..." << endl;
//		return  -1;
//	}
//	namedWindow("input window", CV_WINDOW_AUTOSIZE);
//	imshow("input window", img);
//	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);//定义掩膜
//	//调用filter2D
//	filter2D(img, dst, img.depth(), kernel);
//	namedWindow("掩膜操作后", CV_WINDOW_AUTOSIZE);
//	imshow("掩膜操作后", dst);
//	//imwrite("D:\\learn_CV\\image\\contrast.jpg", dst);
//	waitKey(0);
//}

#图片复制操作

//图片复制操作
//int main(int argc, char** argv) {
   
//	Mat src;
//	src = imread("Ironman.jpg");
//	if (src.empty()) {
   
//		cout << "could not load images...";
//		return -1;
//	}
//	imshow("input", src);
//	Mat dst;
//	dst = Mat(src.size(), src.type());
//	dst = Scalar(127, 25, 45);
//	dst = src.clone();
//	imshow("clone", dst);
//	src.copyTo(dst);
//	imshow("clone", dst);
//
//	waitKey(0);
//	return 0;
//}

#Mat对象的使用

//Mat对象的使用
//int main(int argc, char* argv[])
//{
   
//
//	Mat src = imread("Ironman.jpg");
//	if (src.empty())
//	{
   
//		cout << "could not open image ..." << endl;
//		return -1;
//	}
//
//	Mat gray;
//	cvtColor(src, gray, COLOR_BGR2GRAY);
//	int rows = gray.rows;
//	int cols = gray.cols;
//	imshow("src", src);
//	imwrite("src.jpg", src);
//	imshow("gray", gray);
//	imwrite("gray.jpg", gray);
//
//	Mat ingray = gray.clone();
//	//单通道
//	for (int col = 0; col < cols; col++)
//	{
   
//		for (int row = 0; row < rows; row++)
//		{
   
//			int g = gray.at<uchar>(row, col);
//			ingray.at<uchar>(row, col) = 255 - g;
//		}
//	}
//	imshow("ingray", ingray);
//	imwrite("ingray_pix.jpg", ingray);
//
//	//三通道
//	Mat dst;
//	dst.create(src.size(), src.type());
//	int height = src.rows;
//	int width = src.cols;
//	int nc = src.channels();
//
//	Mat gray1(src.rows, src.cols, CV_8UC1);
//	Mat gray2(src.rows, src.cols, CV_8UC1);
//	gray1 = Scalar(0);
//	gray2 = Scalar(0);
//
//	for (int col = 0; col < cols; col++)
//	{
   
//		for (int row = 0; row < rows; row++)
//		{
   
//			if (nc == 1)
//			{
   
//				int t = gray.at<uchar>(row, col);
//				ingray.at<uchar>(row, col) = 255 - t;
//			}
//			else if (nc == 3)
//			{
   
//				int b = src.at<Vec3b>(row, col)[0];
//				int g = src.at<Vec3b>(row, col)[1];
//				int r = src.at<Vec3b>(row, col)[2];
//
//				dst.at<Vec3b>(row, col)[0] = 255 - b;
//				dst.at<Vec3b>(row, col)[1] = 255 - g;
//				dst.at<Vec3b>(row, col)[2] = 255 - r;
//
//				gray1.at<uchar>(row, col) = max(b, max(g, r));
//				gray2.at<uchar>(row, col) = min(b, min(g, r));
//
//			}
//		}
//	}
//
//	//取负片 同样的有bitwise_and,bitwise_nor,bitwise_or等对像素点的逻辑运算
//	Mat insrc1;
//	src.copyTo(insrc1);
//
//	bitwise_not(src, insrc1);
//
//	imshow("insrc_pix", dst);
//	imshow("insrc_bitwise", insrc1);
//	imshow("gray_min", gray1);
//	imshow("gray_max", gray2);
//	imwrite("insrc_pix.jpg", dst);
//	imwrite("insrc_bitwise.jpg", insrc1);
//	imwrite("gray_min.jpg", gray1);
//	imwrite("gray_max.jpg", gray2);
//
//	waitKey(0);
//	return 0;
//}

#绘制几何图形

void MyLines();
void MyRectangle();
void MyEllipse();
void MyCircle();
void MyPolygon();

Mat src;
int main(int argc, char* argv[])
{
   

	src = imread("Ironman.jpg");
	if (src.empty())
	{
   
		cout << "could not open image ..." << endl;
		return -1;
	}
	MyLines();
	MyRectangle();
	MyEllipse();
	MyCircle();
	MyPolygon();

	imshow("test", src);
	waitKey(0);

	return 0;
}

void MyLines()
{
   
	Point p1 = Point(50, 50);
	Point p2; p2.x = 300; p2.y = 300;
	Scalar colors = Scalar(0, 0, 255);
	line(src, p1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值