C++ 结构中的函数不做任何事情
我有一个简单的结构来表示图像左、上、右、下的百分比,用于裁剪:C++ 结构中的函数不做任何事情,c++,opencv,pointers,struct,C++,Opencv,Pointers,Struct,我有一个简单的结构来表示图像左、上、右、下的百分比,用于裁剪: struct RoiRect { unsigned int left; unsigned int top; unsigned int right; unsigned int bottom; bool isAllZero() { return left == 0 && top == 0 && left == 0 && bottom
struct RoiRect {
unsigned int left;
unsigned int top;
unsigned int right;
unsigned int bottom;
bool isAllZero() {
return left == 0 && top == 0 && left == 0 && bottom == 0;
}
void getCvRect(cv::Size cvSize, cv::Rect &cvRect) {
int height = cvSize.height;
int width = cvSize.width;
int x = ((double)(left/height)*100);
int y = ((double)(top/height)*100);
int w = width - ((double)(right/width)*100);
int h = height - ((double)(bottom/width)*100);
cvRect.x = x;
cvRect.y = y;
cvRect.width = w;
cvRect.height = h;
}
};
我用10,15,20,25这样的值初始化这个结构。这意味着图像应从右侧裁剪10%,从顶部裁剪15%,依此类推
在另一个类中,我将调用struct的getCvRect
,并传入图像的大小以及原始cv::Rect
对象,因此上述函数计算百分比并返回要从图像裁剪的矩形:
//inside another function
cv::Rect rect; //rect to be calculated by getCvRect function of the struct
bool crop; //should crop or not? if all == zero then NOT!
if(!mRoiRect.isAllZero()) {
crop = true;
mRoiRect.getCvRect(mat.size(), rect);
}
但是所有的努力都是徒劳的!我传入一个大小作为第一个参数,我非常确定图像大小为640x480…函数调用后的rect
对象显示640x480
…因此我的函数完全不做任何事情
我做错了什么,我能做些什么来修复或改进这项任务的更聪明的方法是什么
正确的实施方式是(对于任何感兴趣的人)
问题出在四行:
int x = ((double)(left/height)*100);
int y = ((double)(top/height)*100);
int w = width - ((double)(right/width)*100);
int h = height - ((double)(bottom/width)*100);
此处左侧/高度
等。。。全部使用整数除法,然后将结果转换为双精度。当然,其效果是x
和y
以及零和w==宽度和h==高度。你想写的很可能是
int x = ((double) left)/height*100;
int y = ((double) top)/height*100;
int w = width - ((double) right)/width*100;
int h = height - ((double) bottom)/width*100;
问题出在四行:
int x = ((double)(left/height)*100);
int y = ((double)(top/height)*100);
int w = width - ((double)(right/width)*100);
int h = height - ((double)(bottom/width)*100);
此处左侧/高度
等。。。全部使用整数除法,然后将结果转换为双精度。当然,其效果是x
和y
以及零和w==宽度和h==高度。你想写的很可能是
int x = ((double) left)/height*100;
int y = ((double) top)/height*100;
int w = width - ((double) right)/width*100;
int h = height - ((double) bottom)/width*100;
我使用的一个示例函数可能对改进代码很有用
Rect shrinkRect(Rect rect, int width_percent, int height_percent)
{
if (width_percent > 100) width_percent = 100;
if (height_percent > 100) height_percent = 100;
Rect newrect;
newrect.width = (rect.width * width_percent) / 100;
newrect.height = (rect.height * height_percent) / 100;
newrect.x = rect.x + (rect.width - newrect.width) / 2;
newrect.y = rect.y + (rect.height - newrect.height) / 2;
return newrect;
}
用法:假设您有一个Rect r=Rect(0,0100100)
你想缩小你的直肠%20吗
Rect shrinkedRect = shrinkRect(r, 80, 80)
收缩率为(10,10,80,80)
您可以为width\u percent
和height\u percent
指定不同的值。我使用的示例函数可能对改进代码很有用
Rect shrinkRect(Rect rect, int width_percent, int height_percent)
{
if (width_percent > 100) width_percent = 100;
if (height_percent > 100) height_percent = 100;
Rect newrect;
newrect.width = (rect.width * width_percent) / 100;
newrect.height = (rect.height * height_percent) / 100;
newrect.x = rect.x + (rect.width - newrect.width) / 2;
newrect.y = rect.y + (rect.height - newrect.height) / 2;
return newrect;
}
用法:假设您有一个Rect r=Rect(0,0100100)
你想缩小你的直肠%20吗
Rect shrinkedRect = shrinkRect(r, 80, 80)
收缩率为(10,10,80,80)
您可以为width\u percent
和height\u percent
提供不同的值。提示:left/height
作为整数执行以关闭。您在这里已经活跃了足够长的时间,能够理解最少的完整示例。(它们对开发和测试的重要性不亚于发布问题的重要性。)@Beta是的,你是对的……这是一个简单的错误……我也投票反对!提示:left/height
作为整数执行以关闭。您在这里已经活跃了足够长的时间,能够理解最少的完整示例。(它们对开发和测试的重要性不亚于发布问题的重要性。)@Beta是的,你是对的……这是一个简单的错误……我也投票反对!当我看到它时,它跳到了我的眼睛