【问题标题】:crop image with opencv in c在c中使用opencv裁剪图像
【发布时间】:2015-05-06 23:58:44
【问题描述】:

我想从图像的所有侧面裁剪 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) 将您的代码转换为 C++ 和 OpenCV 2.0+。第二步)关注this post
  • 谢谢。是的,我的第一篇文章哭了。 3负至今。我应该使用 C 而不是 C++;
  • 为什么必须使用 C 而不是 C++? OpenCV 的 C API 在 5 年前被取代,不再受支持。
  • 这里通过前两篇教程了解cv::Matdocs.opencv.org/doc/tutorials/core/table_of_content_core/…的结构

标签: c opencv multidimensional-array resize-crop


【解决方案1】:

我找到了解决方案。可能对其他人有用。

  1. 根据此链接32bit boundary“如果列数 * 像素大小不是 4 的倍数,则每行如果图像将被填充”
  2. 正确的sweep方法是使用“widthStep”而不是“width”来考虑pad

widthStep_r = p_in_img->widthStep;

【讨论】:

    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2012-12-31
    • 1970-01-01
    相关资源
    最近更新 更多