opencv reshape函数说明
转自http://blog.csdn.net/yang6464158/article/details/20129991
reshape有两个参数:
其中,参数:cn为新的通道数,如果cn = 0,表示通道数不会改变。
参数rows为新的行数,如果rows = 0,表示行数不会改变。
注意:新的行*列必须与原来的行*列相等。就是说,如果原来是5行3列,新的行和列可以是1行15列,3行5列,5行3列,15行1列。仅此几种,否则会报错。
具体调用也很简单,代码如下所示:
- #include <iostream>
- #include <opencv/cv.h>
- #include <opencv/highgui.h>
- int main()
- {
- cv::Mat testMat = cv::Mat::zeros ( 5, 3, CV_8UC3 );
- std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- std::cout<<"testMat = "<<testMat<<std::endl;
- cv::Mat result = testMat.reshape ( 0, 3 );
- std::cout << " size of original testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- std::cout << " size of reshaped testMat: " << result.rows << " x " << result.cols << std::endl;
- std::cout << "result = " << result << std::endl;
- cv::waitKey(0);
- system("pause");
- return 0;
- }
结果如下:
比如说:下面的情况就会报错:
- #include <iostream>
- #include <opencv/cv.h>
- #include <opencv/highgui.h>
- int main()
- {
- cv::Mat testMat = cv::Mat::ones ( 5, 3, CV_8UC3 );
- std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- std::cout<<"testMat = "<<testMat<<std::endl;
- cv::Mat result = testMat.reshape ( 0, 6 );
- std::cout << " size of original testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- std::cout << " size of reshaped testMat: " << result.rows << " x " << result.cols << std::endl;
- std::cout << "result = " << result << std::endl;
- cv::waitKey(0);
- system("pause");
- return 0;
- }
因为行和列的乘积不相等
结果如下:
我们在使用reshape的时候一定不能用定义的Mat类型赋给原来的类型,必须重新定义一个新类。
可以这样:
- unsigned char v11[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
- cv::Mat A = cv::Mat(3, 4, CV_8U, v11);
- cv::Mat B = A.reshape(1, 12);
但不能这样:
- cv::Mat testMat = cv::Mat::zeros ( 5, 3, CV_8UC3 );
- std::cout << "size of testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
- testMat.reshape ( 0, 1 );
- std::cout << " size of reshaped testMat: " << testMat.rows << " x " << testMat.cols << std::endl;
opencv reshape函数说明的更多相关文章
- opencv3学习:reshape函数
在opencv中,reshape函数比较有意思,它既可以改变矩阵的通道数,又可以对矩阵元素进行序列化,非常有用的一个函数. 函数原型: C++: Mat Mat::reshape() const 参数 ...
- Matlab 的reshape函数
看Matlab的help文档讲得不是清楚. 先给上一段代码: >> a=[1 2 3;4 5 6;7 8 9;10 11 12]; >> b=reshape(a,2,6); 这 ...
- Matlab 的reshape函数(转)
看Matlab的help文档讲得不是清楚. 先给上一段代码: >> a=[1 2 3;4 5 6;7 8 9;10 11 12]; >> b=reshape(a,2,6); 这 ...
- OpenCV reshape The Matrix is not continuous, thus its number of rows can not be changed
When using OpenCV reshape and gets this error: OpenCV Error: Image step is wrong (The matrix is not ...
- python numpy.shape 和 numpy.reshape函数
导入numpy模块 from numpy import * import numpy as np ############################################### ...
- opencv-6-图像绘制与opencv Line 函数剖析
opencv-6-图像绘制与opencv Line 函数剖析 opencvc++qt 开始之前 越到后面, 写的越慢, 之前还抽空去看了下 学堂在线那篇文章提供的方法, 博客第一个人评论的我, 想想还 ...
- 【记录一个问题】macos下lldb调试opencv的一个程序,出现“failed to load objfile for”错误,并且无法调试进入opencv的函数
opencv编译使用了Debug版本,打开了BUILD_WITH_DEBUG_INFO=ON选项. 发现问题后,我又在CMAKE_CXX_FLAGS_DEBUG中设置为 -g -ggdb3,在CMAK ...
- 学习OpenCV——Gabor函数的应用
原文:http://blog.csdn.net/yao_zhuang/article/details/2532279 下载cvgabor.cpp和cvgabor.h到你的C/C++工程目录下 注:在我 ...
- 【麦子学院】OpenCV教程函数总结
个自带样例. parter 1: No1. adaptiveskindetector.cpp 利用HSV空间的色调信息的皮肤检測,背景不能有太多与肤色相似的颜色.效果不是特别好. No2. bagof ...
随机推荐
- derby_学习_00_资源帖
一.精选资料 二.参考资料
- 剑指offer--22.反转链表
时间限制:1秒 空间限制:32768K 热度指数:440624 本题知识点: 链表 题目描述 输入一个链表,反转链表后,输出新链表的表头. /* struct ListNode { int val; ...
- 使用Python和OpenCV通过网址URL获取图片
在OpenCV中通过图片的URL地址获取图片: # -*- coding: utf-8 -*- import numpy as np import urllib import cv2 # URL到图片 ...
- CANopenNode drvTemplate/CO_driver.h hacking
/************************************************************************ * CANopenNode drvTemplate/ ...
- UVALive - 5031 Graph and Queries (并查集+平衡树/线段树)
给定一个图,支持三种操作: 1.删除一条边 2.查询与x结点相连的第k大的结点 3.修改x结点的权值 解法:离线倒序操作,平衡树or线段树维护连通块中的所有结点信息,加个合并操作就行了. 感觉线段树要 ...
- P1605 迷宫(洛谷)
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右 ...
- Eclipse jar包源码下载
在使用intelliJ的时候,可以在IDE中直接下载jar包: Eclipse也可以!就是插件Java Source Attacher 这个插件是放在eclipse安装目录的dropins目录下.重启 ...
- SVN 命令使用-***
1.检出到某一版本: svn checkout -r 974 svn://220.231.xx.xx/仓库名 svn checkout svn://220.231.xx.xx/仓库名 svn ex ...
- Keepalived+Nginx实现负载均衡高可用
一.负载均衡高可用 Nginx作为负载均衡器,所有请求都到了Nginx,可见Nginx处于非常重点的位置,如果Nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了避免负载均衡服务器的宕机 ...
- 蓝桥杯 历届试题 PREV-1 核桃的数量
历届试题 核桃的数量 时间限制:1.0s 内存限制:256.0MB 问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑 ...