在 c 中使用 opencv 裁剪图像

crop image with opencv in c

我想从图像的所有边裁剪 1 个像素。 我的代码在某些页边距中运行良好,但在某些页边距中效果不佳(例如 widthleft=widthright=heightup=heightdown=1)。 我应该使用 C 而不是 C++。

IplImage* edgecuter_v3(unsigned int height, unsigned int width,
    IplImage* p_in_img_grey) {
unsigned int widthleft, widthright, heightup, heightdown, heighteff;
unsigned int widtheff;
widthleft = 1;
widthright = 1;
heightup = 1;
heightdown = 1;
widtheff = width - widthleft - widthright;
heighteff = height - heightup - heightdown;

IplImage *p_out_img;

unsigned char *p_in_img_data;
p_in_img_data = (unsigned char *) p_in_img_grey->imageData;
unsigned char (*p_char_array_in)[width];
p_char_array_in = (unsigned char (*)[width]) p_in_img_data;

p_out_img = cvCreateImage(cvSize(widtheff, heighteff), IPL_DEPTH_8U, 1);
unsigned char *p_out_img_data;
p_out_img_data = (unsigned char *) p_out_img->imageData;
unsigned char (*p_char_array_out)[widtheff];
p_char_array_out = (unsigned char (*)[widtheff]) p_out_img_data;

unsigned int row_indx;
unsigned int col_indx;
for (row_indx = 0; row_indx < heighteff ; row_indx++) {
    for (col_indx = 0; col_indx < widtheff; col_indx++) {
        p_char_array_out[row_indx ][col_indx ] =
                p_char_array_in[row_indx+heightup][col_indx+widthleft];
    }
}
cvNamedWindow("one", CV_WINDOW_AUTOSIZE);
cvShowImage("one", p_out_img);
cvWaitKey(0);
return p_out_img;}

我用其他方法和作业扫描索引,但不起作用。

            p_char_array_out[row_indx ][col_indx ] =
                p_char_array_in[row_indx+heightup][col_indx+widthleft];

非常感谢

我找到了解决方案。也许对其他人有用。

  1. 据此 link 32bit boundary "If the number of cols * pixel size isn't a multiple of 4 then each row if the image will be padded"
  2. sweep的正确方法是使用"widthStep"而不是"width"来考虑pad

widthStep_r = p_in_img->widthStep;