gpt4 book ai didi

在c中使用opencv裁剪图像

转载 作者:行者123 更新时间:2023-11-30 21:10:27 25 4
gpt4 key购买 nike

我想从图像的所有侧面裁剪 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. 根据此链接 32bit boundary “如果列数 * 像素大小不是 4 的倍数,则图像的每一行都会被填充”
  2. 正确的扫描方法是使用“widthStep”而不是“width”来考虑pad

widthStep_r = p_in_img->widthStep;

关于在c中使用opencv裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29681337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com