I wrote a simple Oct file to wrap an OpenCV function. This is my code:-
#include <octave/oct.h>
#include <opencv2/imgproc.hpp>
DEFUN_DLD (cornerHarris, args, , "Harris Corner Detector")
{
// Processing arguments
if(args.length()<4){
print_usage();
}
Matrix octInMat = args(0).matrix_value();
int blockSize = args(1).int_value();
int kSize = args(2).int_value();
double k = args(3).double_value();
int borderType = args(4).int_value();
// Dimentions
dim_vector dims = octInMat.dims();
int h = dims.elem(0);
int w = dims.elem(1);
// OpenCV Matrix
cv::Mat cvInMat = cv::Mat::zeros(h,w, CV_8U);
cv::Mat cvOutMat = cv::Mat::zeros(h,w, CV_32FC1);
// Converting Octave Matrix to OpenCV Matrix
for (int r=0;r<h;r++)
{
for(int s=0;s<w;s++)
{
cvInMat.at<int>(r,s) = octInMat(r,s);
}
}
cv::cornerHarris( cvInMat, cvOutMat, blockSize, kSize, k, borderType );
// Converting OpenCV Matrix to Octave Matrix
Matrix octOutMat = Matrix(dim_vector(h,w));
for (int r=0;r<h;r++)
{
for(int s=0;s<w;s++)
{
octOutMat(r,s) = cvOutMat.at<double>(r,s);
}
}
return octave_value(octOutMat);
}
But I am getting a segmentation error when the value of w
variable increased. Is there any short way to convert the matrices without looping? Or is there a way to resolve the segmentation error?
Documentations:-
I figured it out by commenting line by line in my code. The issue was occurred from this line because of a type casting issue.
cvInMat.at<int>(r,s) = octInMat(r,s);
I changed this as following.
cvInMat.at<uchar>(r,s) = (uchar)octInMat(r,s);
This answer helped me to fix it.