cv::mat inside

本文通过一个具体的C++代码示例介绍了如何使用OpenCV库创建并操作矩阵(Mat对象),包括初始化、元素赋值和读取等基本操作,并探讨了OpenCV中矩阵内存管理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
int main()
{
	int row = 4, col = 4;
	cv::Mat vertexmap(row, col, CV_32FC3);
	for(int i = 0; i < row; ++i)
	{
		float* matRow = (float*)vertexmap.ptr(i);
		for(int j = 0; j < col; ++j)
		{
			matRow[j*3] = j+i;
			matRow[j*3 + 1] = j+i;
			matRow[j*3 + 2] = j+i;
		}
	}

	for(int i = 0; i < row; ++i)
	{
		float* matRow = (float*)vertexmap.ptr(i);
		for(int j = 0; j < col; ++j)
		{
			cout<<matRow[j*3]<<" ";
			cout<<matRow[j*3 + 1]<<" ";
			cout<<matRow[j*3 + 2]<<" ";
		}
		cout<<"\n";
	}
	cout<<"OK\n";
	return 0;
}

***********************************************************
~$ g++ -o test test.cpp  `pkg-config --cflags --libs opencv`
~$ ./test
0 0 0 1 1 1 2 2 2 3 3 3 
1 1 1 2 2 2 3 3 3 4 4 4 
2 2 2 3 3 3 4 4 4 5 5 5 
3 3 3 4 4 4 5 5 5 6 6 6 
OK






#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
int main()
{
	int row = 4, col = 4;
	cv::Mat vertexmap(row, col, CV_32FC3);
	for(int i = 0; i < row; ++i)
	{
		float* matRow = (float*)vertexmap.ptr(i);
		for(int j = 0; j < col; ++j)
		{
			matRow[j*3] = j+i;
			matRow[j*3 + 1] = j+i;
			matRow[j*3 + 2] = j+i;
		}
	}

	for(int i = 0; i < row; ++i)
	{
		float* matRow = (float*)vertexmap.ptr(i);
		for(int j = 0; j < col; ++j)
		{
			cout<<matRow[j*3]<<" ";
			cout<<matRow[j*3 + 1]<<" ";
			cout<<matRow[j*3 + 2]<<" ";
		}
		cout<<"\n";
	}
	cout<<"\nsecond\n";
		for(int i = 0; i < vertexmap.rows; ++i)
	{
		float* matRow = (float*)vertexmap.ptr(i);
		
		for(int j = 0; j < vertexmap.cols; ++j)
		{
			cout<<vertexmap.at<cv::Vec3f>(i,j)[0]<<" ";
			cout<<vertexmap.at<cv::Vec3f>(i,j)[1]<<" ";
			cout<<vertexmap.at<cv::Vec3f>(i,j)[2]<<" ";
		}
		cout<<"\n";
	}
	cout<<"OK\n";

	int sz[]={3,2,2};

	cv::Mat dm(3,sz,CV_32FC1,cv::Scalar(0));
	cout<<dm.step[0]<<","<<dm.step[1]<<","<<dm.step[2]<<"\n";//similar to sizeof,get byte size of a dim?
	cout<<dm.elemSize()<<"\n";//similar to sizeof,get byte size of an element
	return 0;
}

~$ g++ -o test test.cpp  `pkg-config --cflags --libs opencv`
~$ ./test
0 0 0 1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3 4 4 4
2 2 2 3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5 6 6 6

second
0 0 0 1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3 4 4 4
2 2 2 3 3 3 4 4 4 5 5 5
3 3 3 4 4 4 5 5 5 6 6 6
OK
16,8,4
4



6.为了搞定这个问题,OpenCV使用引用计数机制。其思路是让每个Mat
 对象有自己的信息头,但共享同一个矩阵。这通过让矩阵指针指向同一地址而实现。而拷贝构造函数则只拷贝信息头和矩阵指针 ,而不拷贝矩阵。(都是引用,改变一个的话都会改变
bool isPolygonInside(const std::vector<cv::Point>& polygon1, const std::vector<cv::Point>& polygon2, double& outsideArea) { // Check if all vertices of polygon1 are inside polygon2 for (const auto& vertex : polygon1) { double distance = cv::pointPolygonTest(polygon2, vertex, true); if (distance < 0) { // Vertex is outside polygon2 // Calculate area of polygon1 outside polygon2 cv::Mat polygon1Mat = cv::Mat(polygon1).reshape(1); cv::Mat polygon2Mat = cv::Mat(polygon2).reshape(1); std::vector<cv::Point2f> intersectionPolygon; if (cv::isContourConvex(polygon1) && cv::isContourConvex(polygon2)) { cv::Mat intersectionMat; cv::intersectConvexConvex(polygon1Mat, polygon2Mat, intersectionMat); if (cv::countNonZero(intersectionMat) > 0) { intersectionMat.reshape(2).copyTo(intersectionPolygon); } } else { cv::Rect rect1 = cv::boundingRect(polygon1Mat); cv::Rect rect2 = cv::boundingRect(polygon2Mat); cv::Rect intersectionRect = rect1 & rect2; if (!intersectionRect.empty()) { cv::Mat intersectionMat = cv::Mat::zeros(intersectionRect.size(), CV_8UC1); cv::fillConvexPoly(intersectionMat, polygon1 - rect1.tl(), cv::Scalar(255)); cv::fillConvexPoly(intersectionMat, polygon2 - rect2.tl(), cv::Scalar(0), cv::LINE_AA); std::vector<std::vector<cv::Point>> contours; cv::findContours(intersectionMat, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); if (!contours.empty()) { intersectionPolygon = contours[0]; } } } double intersectionArea = std::abs(cv::contourArea(intersectionPolygon)); double polygon1Area = std::abs(cv::contourArea(polygon1)); outsideArea = polygon1Area - intersectionArea; return false; } } // All vertices of polygon1 are inside polygon2 return true; }
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值