如何在opencv中将源代码从matlab转换为C ++ [英] How to convert the source code from matlab to C++ in opencv
问题描述
我正在尝试将MatLab中的代码转换为OpenCV,但我对以下几行感到困惑,因为我不太了解编程,有人可以帮助我吗?
I am trying to convert a code in MatLab to OpenCV but I am stuck about the following lines as I don't know much programming, can someone here help me?
[n,m]=size(citra);
for x=1:n
k=m;
for y=1:m
citra_baru(k,x)=citra(x,y);
k=k-1;
end;
end;
,完整代码为:
and the full code is:
clc;
clear;
disp('Rotate Operation');
disp('--------------------');
disp('input file name with the extension');
image= input('input image file = ');
citra=imread(image);
[n,m]=size(citra);
for x=1:n
k=m;
for y=1:m
new_citra(k,x)=citra(x,y);
k=k-1;
end;
end;
subplot(1,2,1),imshow(citra),title('Citra Gray');
subplot(1,2,2),imshow(new_citra),title('Citra Rotate 90');
我尝试了什么:
我试图像这样转换,但仍然是错误
#include< opencv\cv.h>
#include< opencv \highgui.h>
#include< iostream> ;
使用命名空间cv;
使用命名空间std;
int main()
{
Mat img = imread(D:\\Kuliah \\ Pengolahan Citra Digital \\PCD \\ images \\图3.35(a).jpg,CV_LOAD_IMAGE_UNCHANGED);
Mat img1(img.size(),img.type());
int n = img.size()。width;
int m = img.size()。height;
cout<< n<< << m;
for(int x = 1; x< = n; x ++){
int k = m;
for(int y = 1; y< = m; y ++){
img1.at< uchar>(n,m)= img.at< uchar>(x,y);
(k,x)=(x,y);
}
k = k-1;
}
namedWindow(Original Image,CV_WINDOW_AUTOSIZE);
imshow(Original Image,img);
namedWindow(Cropped Image,CV_WINDOW_AUTOSIZE);
imshow(裁剪图像,img1);
waitKey(0);
返回0;
}
What I have tried:
i've tried to convert like this, but still error
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("D:\\Kuliah\\Pengolahan Citra Digital\\PCD\\images\\Fig3.35(a).jpg", CV_LOAD_IMAGE_UNCHANGED);
Mat img1(img.size(), img.type());
int n = img.size().width;
int m = img.size().height;
cout << n << " " << m;
for (int x = 1; x <= n; x++){
int k = m;
for (int y =1; y <= m; y++){
img1.at<uchar>(n, m) = img.at<uchar>(x, y);
(k,x) = (x,y);
}
k=k-1;
}
namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
imshow("Original Image", img);
namedWindow("Cropped Image", CV_WINDOW_AUTOSIZE);
imshow("Cropped Image", img1);
waitKey(0);
return 0;
}
推荐答案
我不知道Matlab和使用过的Mat
类型。所以我的答案可能不是最终解决方案,但我希望它会有所帮助。
随着C / C ++索引从零开始(快速网络研究表明这一点也适用于Mat
类型。所以你必须相应地更改循环。
将元素分配给img1 $ c $时,代码中也有错误c>(错误的索引变量)。
代码表示交换输入的尺寸。所以你的输出img1
也必须使用交换的尺寸。
所以你的代码应该是这样的:
I don't know Matlab and the usedMat
type well. So my answer may be not the final solution but I hope it will help.
With C/C++ indexes are starting at zero (and a quick web research indicates that this applies to theMat
type too). So you have to change the loops accordingly.
There is also an error in your code when assigning the elements toimg1
(wrong index variable).
The code indicates that the dimensions of the input are swapped. So your outputimg1
must also use the swapped dimensions.
So your code should be like:
// See below
//Mat img1(img.size(), img.type());
int n = img.size().width;
int m = img.size().height;
cout << n << " " << m;
// Use swapped dimensions here.
// EDIT: Don't know if this works.
// The uncommented line should according to the documentation.
//Mat img1(m, n, img.type());
Mat img1(Size(m, n), img.type());
for (int x = 0; x < n; x++){
// EDIT: Was k = m; fixed
int k = m - 1;
for (int y = 0; y < m; y++){
// This should be (k,x) = (x,y)
// img1.at(n, m) = img.at(x, y);
img1.at(k, x) = img.at(x, y);
}
k--;
}
这篇关于如何在opencv中将源代码从matlab转换为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!