问题描述
我正在尝试将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;
}
推荐答案
// 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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!