调用静态内联函数

时间:2014-02-14 08:24:48

标签: c++ visual-c++ opencv

如何调用此static inline函数?

static inline int xGradient(uchar* image, int x, int y)
{
    return ((int)(image[y-1, x-1])) +
        2*image[y, x-1] +
        image[y+1, x-1] -
        image[y-1, x+1] -
        2*image[y, x+1] -
        image[y+1, x+1];
}

static inline int yGradient(uchar* image, int x, int y)
{
    return ((int)(image[y-1, x-1])) +
        2*image[y-1, x] +
        image[y-1, x+1] -
        image[y+1, x-1] -
        2*image[y+1, x] -
        image[y+1, x+1];

我打电话给这个功能时遇到了麻烦。我称之为:

gx = xGradient(&data[ii], x,y);
gy = yGradient(&data[ii], x,y);
sum = abs(gx) + abs(gy);
sum = sum > 255 ? 255:sum;
sum = sum < 0 ? 0 : sum;

我没有得到gx和gy的结果。帮助我计算上面程序中的gxgy

  

这是我的代码

#include<iostream>
#include<omp.h>
#include<ctime>
#include<cmath>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

static inline int xGradient(uchar* image, int x, int y)
{
return ((int)(image[y-1, x-1])) +
        2*image[y, x-1] +
        image[y+1, x-1] -
        image[y-1, x+1] -
        2*image[y, x+1] -
        image[y+1, x+1];
}

static inline int yGradient(uchar* image, int x, int y)
{
return ((int)(image[y-1, x-1])) +
        2*image[y-1, x] +
        image[y-1, x+1] -
        image[y+1, x-1] -
        2*image[y+1, x] -
        image[y+1, x+1];
}

int main()  
 {
 Mat src, grey, grey2, dst;
 clock_t start, finish;
 int gx, gy, sum;
 size_t total;
 int sizes[2];
 start = clock();

 src= imread("E:/sobel/Debug/view_sea.bmp");
 imwrite("E:/sobel/Debug/Serial/Citra Asli.bmp", src );

 cvtColor(src,grey,CV_BGR2GRAY);
 imwrite("E:/sobel/Debug/Serial/Grayscale.bmp", grey );

 dst = grey.clone();
 if( !grey.data )
 {
     return -1;
 }

 total=grey.total();
 cv::Size s = grey.size();
 sizes[0] = s.height;
 sizes[1] = s.width;
 cout << "citra terdiri dari " << total << " piksel dengan ukuran " << sizes[0] << " x " << sizes[1] << " piksel" << endl;

int starty=(grey.rows);
if(starty==0)
{starty=1;}
int stopy=(grey.rows);
if(stopy>grey.rows - 1)
{stopy=grey.rows - 1;}

int ii=grey.cols;
uchar* data=grey.data;

 for(int y = starty; y < stopy; y++)
{
    ii++;
    for(int x = 1; x < sizes[1] - 1; x++)
     {
         gx = xGradient(&data[ii], x,y);
         gy = yGradient(&data[ii], x,y);
         sum = abs(gx) + abs(gy);
         sum = sum > 255 ? 255:sum;
         sum = sum < 0 ? 0 : sum;
         data[ii] = sum;
        ii++;

    }
     ii++;
}

 finish = clock();

 imwrite( "E:/sobel/Debug/Serial/Output sobel dengan Serial.bmp", src);

 cout << "Waktu Eksekusi Deteksi Tepi Serial adalah : " << float(finish-  start)/CLOCKS_PER_SEC << " detik" << endl;
 return 0;
}
  

我在这段代码中出错了

int ii=grey.cols;
uchar* data=grey.data;

 for(int y = starty; y < stopy; y++)
{
    ii++;
    for(int x = 1; x < sizes[1] - 1; x++)
     {
         gx = xGradient(&data[ii], x,y);
         gy = yGradient(&data[ii], x,y);
         sum = abs(gx) + abs(gy);
         sum = sum > 255 ? 255:sum;
         sum = sum < 0 ? 0 : sum;
         data[ii] = sum;
        ii++;

    }
     ii++;
}

1 个答案:

答案 0 :(得分:4)

我想,你在这里混淆了python / numpy和c ++语法。

虽然image[y-1, x-1]会在python中做正确的工作(给定一个2d numpy数组),

在c ++中,你只有1d uchar数组,它归结为image[x-1]。可能不是你的预期。

做得对,你的函数还需要一个额外的参数,大小为1行(宽度):

static inline int xGradient(uchar* image, int x, int y, int W)
{
    return (
        image[ W*(y-1) + (x-1)] +
        2*image[ W*(y) + (x-1)] +
        image[ W*(y+1) + (x-1)] -
        image[ W*(y-1) + (x+1)] -
        2*image[ W*(y) + (x+1)] -
        image[ W*(y+1) + (x+1)] );
}

但是,再次,因为我们在opencv中,为什么不使用Mat对象本身而不是原始字节:

static inline int xGradient(const Mat & img, int x, int y)
{
    return (
        img.at<uchar>( (y-1) , (x-1) ) +
        2*img.at<uchar>( (y) , (x-1) ) +
        img.at<uchar>( (y+1) , (x-1) ) -
        img.at<uchar>( (y-1) , (x+1) ) -
        2*img.at<uchar>( (y) , (x+1) ) -
        img.at<uchar>( (y+1) , (x+1) ) );
}
// and call it : 
Mat img;
int xg = xGradient(img,x,y);

请记住,在应用此功能时,您必须在图像中留出1个像素的边框,否则就会超出界限...