Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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');


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;
}
Posted
Updated 2-Jun-16 1:14am

1 solution

I don't know Matlab and the used Mat 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 the Mat type too). So you have to change the loops accordingly.

There is also an error in your code when assigning the elements to img1 (wrong index variable).

The code indicates that the dimensions of the input are swapped. So your output img1 must also use the swapped dimensions.

So your code should be like:
C++
// 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--;
}
 
Share this answer
 
v3
Comments
xpnazar 2-Jun-16 7:33am    
i've tried using your code in opencv but still error, what libraries are you use?
Jochen Arndt 2-Jun-16 7:37am    
I don't use any because (as I wrote):
I don't know Matlab and the used Mat type well.

But I forgot one index conversion (I will update my answer). k must be initialised with m - 1:
int k = m - 1;
Jochen Arndt 2-Jun-16 7:40am    
If there are still errors you should be specific about them (compiler or runtime error: post the error message; result is not as expected: explain).
xpnazar 2-Jun-16 8:04am    
the code is still error and this is the error message (14 errors):

Error 1 error C2780: 'const _Tp &cv::Mat::at(cv::Point) const' : expects 1 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 2 error C2780: '_Tp &cv::Mat::at(cv::Point)' : expects 1 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 3 error C2780: 'const _Tp &cv::Mat::at(const cv::Vec<int,n> &) const' : expects 1 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 4 error C2780: '_Tp &cv::Mat::at(const cv::Vec<int,n> &)' : expects 1 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 5 error C2780: 'const _Tp &cv::Mat::at(const int *) const' : expects 1 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 6 error C2780: '_Tp &cv::Mat::at(const int *)' : expects 1 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 7 error C2780: 'const _Tp &cv::Mat::at(int,int,int) const' : expects 3 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 8 error C2780: '_Tp &cv::Mat::at(int,int,int)' : expects 3 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 9 error C2783: 'const _Tp &cv::Mat::at(int,int) const' : could not deduce template argument for '_Tp' d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 10 error C2783: '_Tp &cv::Mat::at(int,int)' : could not deduce template argument for '_Tp' d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 11 error C2780: 'const _Tp &cv::Mat::at(int) const' : expects 1 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD

Error 12 error C2780: '_Tp &cv::Mat::at(int)' : expects 1 arguments - 2 provided d:\kuliah\pengolahan citra digital\visual studio 2013\pcd\pcd\rotasi.cpp 18 1 PCD
xpnazar 2-Jun-16 8:05am    
what should i do?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900