MATLAB到C ++或OpenCV的矩阵操作

时间:2016-07-13 15:23:59

标签: c++ matlab opencv matrix

我正在尝试用C ++进行此操作,但我无法绕过它。 我试着查看http://mathworks.com/help/matlab/ref/image.html,但我仍然没有得到它。

im 是Matlab中的矩阵。宽度为640

im(:,Width+(1:2),:) = im(:,1:2,:);

OpenCV矩阵或C++

中是否存在与此操作类似的内容

2 个答案:

答案 0 :(得分:1)

解决方案1 ​​

您可以使用colRange功能:

mat.colRange(0, 2).copyTo(mat.colRange(w, 2 + w)); 

示例:

//initilizes data
float data[2][4] = { { 1, 2, 3, 4}, { 5, 6, 7, 8 } };
Mat mat(2, 4, CV_32FC1, &data);
int w = 2; //w is equivelant to Width in your script, in this case I chose it to be 2

std::cout << "mat before: \n" << mat << std::endl;

mat.colRange(0, 2).copyTo(mat.colRange(w, 2 + w)); 

std::cout << "mat after: \n" << mat << std::endl;

结果:

mat before:
[1, 2, 3, 4;
 5, 6, 7, 8]
mat after:
[1, 2, 1, 2;
 5, 6, 5, 6]

解决方案2

或者,使用cv :: Rect对象,如下所示:

cv::Mat roi = mat(cv::Rect(w, 0, 2, mat.rows));
mat(cv::Rect(0, 0, 2, mat.rows)).copyTo(roi);

有几种初始化Rect的方法,在我的例子中我选择了以下的c-tor:

cv::Rect(int x, int y, int width, int height);

结果与解决方案1中的结果相同。

答案 1 :(得分:-3)

也许你也可以使用能满足需要的Eigen。它有Block operations

调整链接下提供的示例,您需要像:

Never Mind Folks.
[This link ][1] helps me to resolve the issue. Just added line to focus on next element **driver.findElement(By.id("ArrivalTime")).sendKeys(Keys.TAB);**

**The Update Code:**

driver.findElement(By.id("DeparturePoint")).sendKeys("New York");
driver.findElement(By.id("ArrivalPoint")).sendKeys("Paris");
driver.findElement(By.id("DepartureTime")).sendKeys("12:01 am");   
driver.findElement(By.id("ArrivalTime")).sendKeys("12:01 am");
Thread.sleep(1000); 
driver.findElement(By.id("ArrivalTime")).sendKeys(Keys.TAB);
driver.findElement(By.id("DepartureDate")).sendKeys("07/10/2016");  
driver.findElement(By.id("ArrivalDate")).sendKeys("07/16/2016");
Thread.sleep(1000); 
driver.findElement(By.id("ArrivalTime")).sendKeys(Keys.TAB);
  [1]: http://www.guru99.com/handling-date-time-picker-using-selenium.html

输出:

Eigen::MatrixXf m(4, 4);
m << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16;
cout <<"original matrix \n" << m << endl;
m.block<2, 2>(1, 1) = m.block<2, 2>(2, 2);
cout <<"modified matrix \n" << m << endl;
相关问题