#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);return0;}
图像翻转
#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,yfor(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);return0;}
平移
//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);return0;}
缩放
//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);return0;}