• opencv-从图像旋转学习Mat数据訪问


    先看一个简单的样例


    代码:

    // ConsoleApplication3_6_23.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include<opencv2/opencv.hpp>
    #include<iostream>
    #include<vector>
    using namespace std;
    using namespace cv;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Mat src,dst,dst1,dst2;
    	src = imread("test.png");
    	if(!src.data)
    		return -1;
    	namedWindow("原图",CV_WINDOW_AUTOSIZE);
    	namedWindow("x--",CV_WINDOW_AUTOSIZE);
    	namedWindow("y--",CV_WINDOW_AUTOSIZE);
    	imshow("原图",src);
    	int nr = src.rows;
    	int nc = src.cols;
    	/************************************************************************/
    	/* x方向                                                                     */
    	/************************************************************************/
    	dst = src.clone();
    	for (int i = 0;i < nr;++i)
    	{
    		for (int j = 0;j < nc;++j)
    		{
    			dst.at<Vec3b>(i,j) = src.at<Vec3b>(i,nc-j-1);
    		}
    	}
    	imshow("x--",dst);
    	/************************************************************************/
    	/* y方向                                                                     */
    	/************************************************************************/
    	src.copyTo(dst1);
    	for (int i = 0;i < nr;++i)
    	{
    		for (int j = 0;j < nc;++j)
    		{
    			dst1.at<Vec3b>(i,j) = src.at<Vec3b>(nr-i-1,j);
    		}
    	}
    	imshow("y--",dst1);
    	waitKey(0);
    	return 0;
    }
    

    1、Mat的ptr和[]

    void image_rever(Mat& src,Mat& dst)
    {
    	int nr = src.rows;
    	int nc = src.cols;
    	int ch = src.channels();
    	for (int i = 0;i < nr;++i)
    	{
    		uchar* srcr = src.ptr<uchar>(i);
    		uchar* dstr = dst.ptr<uchar>(i);
    		for (int j = 0;j < nc;++j)
    		{
    			dstr[ch * j + 0] = srcr[ch * (nc - j - 1) + 0];
    			dstr[ch * j + 1] = srcr[ch * (nc - j - 1) + 1];
    			dstr[ch * j + 2] = srcr[ch * (nc - j - 1) + 2];
    		}
    	}
    }

    2、Mat的ptr和指针

    void image_rever(Mat& src,Mat& dst)
    {
    	int nr = src.rows;
    	int nc = src.cols;
    	int ch = src.channels();
    	for (int i = 0;i < nr;++i)
    	{
    		uchar* srcr = src.ptr<uchar>(i);
    		uchar* dstr = dst.ptr<uchar>(i);
    		for (int j = 0;j < nc;++j)
    		{
    			
    			*(dstr + ch * j + 0) = *(srcr + (ch * (nc - j - 1) + 0));
    			*(dstr + ch * j + 1) = *(srcr + (ch * (nc - j - 1) + 1));
    			*(dstr + ch * j + 2) = *(srcr + (ch * (nc - j - 1) + 2));
    		}
    	}
    }

    3、Mat的迭代器iterator

    void image_rever(Mat& src,Mat& dst)
    {
    	typedef cv::Mat_<cv::Vec3b>::iterator matIterator;
    	matIterator	db = dst.begin<cv::Vec3b>();
    	matIterator de = dst.end<cv::Vec3b>();
    
    	matIterator se = src.end<cv::Vec3b>() - 1;
    	for (; db != de; ++db,--se)
    	{
    		*db = *se;
    	}
    }

    void image_rever(Mat& src,Mat& dst)
    {
    	typedef cv::Mat_<cv::Vec3b>::iterator matIterator;
    	matIterator	db = dst.begin<cv::Vec3b>();
    	matIterator de = dst.end<cv::Vec3b>();
    
    	matIterator se = src.end<cv::Vec3b>() - 1;
    	for (; db != de; ++db,--se)
    	{
    		(*db)[0] = 255 - (*se)[0];
    		(*db)[1] = 255 - (*se)[1];
    		(*db)[2] = 255 - (*se)[2];
    	}
    }


  • 相关阅读:
    Linux下常用程序的代理服务器(proxy)配置
    如何下载google play免费应用的apk文件
    sublime text多文件夹查找关键字
    linux下查看文件夹的大小
    使用virtualenv搭建独立的Python环境
    python代码中使用settings
    /lib /usr/lib /usr/local/lib区别
    修改setup.py的源
    Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
    使用Restful风格中的post使用过遇到前端数据传送不到后端
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6755416.html
Copyright © 2020-2023  润新知