OpenCV学习笔记(一)

显示图片

#include "pch.h"
#include<iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
using namespace cv;
int main()
{
	//打开文件
	Mat img = imread("D:\\test.jpg");
	//新建窗口
	namedWindow("测试opencv");
	//显示图片
	imshow("测试opencv", img);
	//等待时间
	cvWaitKey(6000000);
}

创建Mat类

#include "pch.h"
#include<iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
using namespace cv;
using namespace std;
int main()
{
	//创建无初始化矩阵
	Mat image1;
	//6行6列类型为8位单通道矩阵
	Mat image2(6, 6, CV_8UC1);
	//大小为7*7,类型为8位3通道矩阵
	Mat image3(Size(7,7),CV_8UC3);
	//8行8列,用1+3j填充的32位复矩阵
	Mat image4(8, 8, CV_32FC2, Scalar(1, 3));
	//大小为9*9,用1,2,3,填充的8位3通道矩阵
	Mat image5(Size(9,9),CV_8UC3,Scalar(1,2,3));
	//2-> 6
	Mat image6(image2);
	//
	cout << image1 << endl;
	cout << image2 << endl;
	cout << image3 << endl;
	cout << image4 << endl;
	cout << image5 << endl;
	cout << image6 << endl;
	return 0;
}

在这里插入图片描述

mat类常用函数

#include "pch.h"
#include<iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
using namespace cv;
using namespace std;
int main()
{
	Mat image1(10,8,CV_8UC1,Scalar(5));
	//获取行列数
	cout << "image1 row" << image1.rows<< endl;
	cout << "image1 col" << image1.cols << endl;
	//获取指定行列元素
	cout << image1.rowRange(1, 3) << endl;
	cout << image1.colRange(2, 4) << endl;
	//8*8  复数矩阵
	Mat image2(8, 8, CV_32FC2, Scalar(1, 5));
	//利用create方法重新创建10*10  8  无符号  3通道矩阵
	image2.create(10, 10, CV_8UC(3));
	cout << image2.channels() << endl;
	//转换矩阵类型
	image2.convertTo(image2, CV_32F);
	cout << image2.depth() << endl;
	//zeros创建矩阵
	Mat image3 = Mat::zeros(image2.rows, image2.cols, CV_8UC1);
	//*2
	image1.row(4) = image1.row(5) * 2;
	cout << image1 << endl;
	//赋值
	Mat image4 = image1.col(4);
	cout << image4 << endl;
	//复制
	image1.col(1).copyTo(image4);
	cout << image4 << endl;
	return 0;
}

在这里插入图片描述

灰度,滤波

    #include "pch.h"
    #include<iostream> 
    #include <opencv2/core/core.hpp> 
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/highgui/highgui.hpp> 
    using namespace cv;
    using namespace std;
    
    int main()
    {
    	Mat image = imread("d:\\test1.jpg");
    	if (image.empty())
    		return -1;
    	//转化为灰度图像
    	Mat gray;
    	cvtColor(image, gray, CV_RGB2GRAY);
    	imshow("gray",gray);
    	//均值平滑(均值滤波)
    	Mat blurdstimage;
    	blur(gray, blurdstimage, Size(5, 5),Point(-1,-1));
    	imshow("blurdstimage", blurdstimage);
    	//写入图像
    	imwrite("d:\\blurdstimage.png", blurdstimage);
    	waitKey(0);
    	return 0;
    }

在这里插入图片描述

图像翻转

#include "pch.h"
#include<iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp> 
using namespace cv;
using namespace std;

int main()
{
	Mat image = imread("d:\\test1.jpg");
	//输出定义
	Mat resultimage (image.size(),image.type());
	//x与y方向矩阵
	Mat xmapimage(image.size(), CV_32FC1);
	Mat ymapimage(image.size(), CV_32FC1);
	//取row,col
	int rows = image.rows;
	int cols = image.cols;
	//遍历x,y
	for (int j = 0; j < rows; j++)
		for (int i = 0; i < cols; i++)
		{
			xmapimage.at<float>(j, i) = cols - i;
			ymapimage.at<float>(j, i) = rows - j;
		}
	//重映射操作
	remap(image, resultimage, xmapimage, ymapimage, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
	//输出结果
	imshow("image", image);
	imshow("sultimage", resultimage);
	waitKey(0);

	return 0;
}

在这里插入图片在这里插入图片描述描述

平移

//author:@放码过来
#include "pch.h"
#include<iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp> 
using namespace cv;
using namespace std;

//平移,大小不变
Mat imagetranslations1(Mat & image, int xoffset, int yoffset)
{
	int nrows = image.rows;
	int ncols = image.cols;
	Mat resultimage(image.size(), image.type());
	for (int i = 0; i < nrows; i++)
	{
		for (int j = 0; j < ncols; j++)
		{
			//映射变换
			int x = j - xoffset;
			int y = i - yoffset;
			//边界判断
			if (x >= 0 && y >= 0 && x < ncols && y < nrows)
				resultimage.at<Vec3b>(i, j) = image.ptr<Vec3b>(y)[x];
		}
	}
	return resultimage;
}

//平移,大小改变
Mat imagetranslations2(Mat & image, int xoffset, int yoffset)
{
	//设置平移尺寸
	int nrows = image.rows + abs(yoffset);
	int ncols = image.cols + abs(xoffset);
	Mat resultimage(nrows, ncols, image.type());
	for (int i = 0; i < nrows; i++)
	{
		for (int j = 0; j < ncols; j++)
		{
			//映射变换
			int x = j - xoffset;
			int y = i - yoffset;
			//边界判断
			if (x >= 0 && y >= 0 && x < ncols && y < nrows)
				resultimage.at<Vec3b>(i, j) = image.ptr<Vec3b>(y)[x];
		}
	}
	return resultimage;
}
int main()
{
	Mat image = imread("d://test1.jpg");
	imshow("image", image);
	int xoffset = 50;
	int yoffset = 50;
	//左移 不改变大小
	Mat reimage1 = imagetranslations1(image, xoffset, yoffset);
	imshow("reimage1", reimage1);
	//左移 改变大小
	Mat reimage2 = imagetranslations2(image, xoffset, yoffset);
	imshow("reimage2", reimage2);
	xoffset = -50;
	yoffset = -50;
	//右移 不改变大小
	Mat reimage3 = imagetranslations1(image, xoffset, yoffset);
	imshow("reimage3", reimage3);
	waitKey(0);
	return 0;
}

在这里插入图片描述

缩放

//author:@放码过来
#include "pch.h"
#include<iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp> 
using namespace cv;
using namespace std;
//等间隔提取图像缩放
Mat imagereduction1(Mat &image, double kx, double ky)
{
	//获取输出图像分辨率
	int nrows = cvRound(image.rows*kx);
	int ncols = cvRound(image.cols*ky);
	Mat resultimage(nrows, ncols, image.type());
	for (int i = 0; i < nrows; ++i)
	{
		for (int j = 0; j < ncols; ++j)
		{
			//根据水平因子计算坐标
			int x = static_cast<int>((i + 1) / kx + 0.5) - 1;
			//根据垂直因子计算坐标
			int y = static_cast<int>((j + 1) / ky + 0.5) - 1;
			resultimage.at<Vec3b>(i, j) = image.at<Vec3b>(x, y);
		}
	}
	return resultimage;
}

Vec3b areaaverage(const Mat &image, Point_<int> leftpoint, Point_<int> rightpoint)
{
	int temp1 = 0, temp2 = 0, temp3 = 0;
	//计算区域内像素点个数
	int npix = (rightpoint.x - leftpoint.x + 1)*(rightpoint.y - leftpoint.y + 1);
	//对区域子块各个通道対像素值求和
	for (int i = leftpoint.x; i <= rightpoint.x; i++)
	{
		for (int j = leftpoint.y; j <= rightpoint.y; j++)
		{
			temp1 += image.at<Vec3b>(i, j)[0];
			temp2 += image.at<Vec3b>(i, j)[1];
			temp3 += image.at<Vec3b>(i, j)[2];
		}
	}
	//对每个通道求平均值
	Vec3b vectemp;
	vectemp[0] = temp1 / npix;
	vectemp[1] = temp2 / npix;
	vectemp[2] = temp3 / npix;
	return vectemp;
}

Mat imagereduction2(const cv::Mat &image, double kx, double ky)
{
	//获取输出图像分辨率
	int nrows = cvRound(image.rows*kx);
	int ncols = cvRound(image.cols*ky);
	Mat resultimage(nrows, ncols, image.type());
	//区域子块左上角行列坐标
	int leftrowcoordinate = 0;
	int leftcolcoordinate = 0;
	for (int i = 0; i < nrows; ++i)
	{
		//根据水平因子计算坐标
		int x = static_cast<int>((i + 1) / kx + 0.5) - 1;
		for (int j = 0; j < ncols; ++j)
		{
			//根据垂直因子计算坐标
			int y = static_cast<int>((j + 1)/ky + 0.5) - 1;
			//求解区域子块的均值
			resultimage.at<Vec3b>(i, j) = areaaverage(image, Point_<int>(leftrowcoordinate, leftcolcoordinate), Point_<int>(x, y));
			//更新下子块左上角的列坐标,行坐标不变
			leftcolcoordinate = y + 1;
		}
		leftcolcoordinate = 0;
		//跟新下子块左上角的行坐标
		leftrowcoordinate = x + 1;
	}
	return resultimage;
}
int main()
{
	Mat image = imread("d://test1.jpg");
	imshow("image", image);
	Mat resultimage1 = imagereduction1(image, 0.5, 0.5);
	imshow("res1", resultimage1);
	Mat resultimage2 = imagereduction2(image, 0.5, 0.5);
	imshow("res2", resultimage2);
	waitKey(0);
	return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值