如何裁剪图像并仅保留其下半部分?
我试过了:
Mat cropped frame = frame(Rect(frame.cols/2, 0, frame.cols, frame.rows/2));
但它给了我一个错误。
我也尝试过:
double min, max;
Point min_loc, max_loc;
minMaxLoc(frame, &min, &max, &min_loc, &max_loc);
int x = min_loc.x + (max_loc.x - min_loc.x) / 2;
Mat croppedframe = = frame(Rect(x, min_loc.y, frame.size().width, frame.size().height / 2));
但它也不起作用。
答案 0 :(得分:5)
这里是任何初学者的python版本。
def crop_bottom_half(image):
cropped_img = image[image.shape[0]/2:image.shape[0]]
return cropped_img
答案 1 :(得分:2)
Rect
函数参数为Rect(x, y, width, height)
。在OpenCV中,数据的组织方式是第一个像素位于左上角,因此rect
应为:
Mat croppedFrame = frame(Rect(0, frame.rows/2, frame.cols, frame.rows/2));
答案 2 :(得分:0)
要快速复制粘贴:
image = YOURIMAGEHERE #note: image needs to be in the opencv format
height, width, channels = image.shape
croppedImage = image[int(height/2):height, 0:width] #this line crops
说明:
在OpenCV中选择图像的一部分,您可以简单地从图像中选择开始和结束像素。意思是:
image[yMin:yMax, xMin, xMax]
用人类的话来说:yMin = top | yMax =底部| xMin =左| xMax =右|
“:”表示从:左侧的值到右侧的值
要保留底部一半,我们只需做yMax/2:yMax
,这意味着从图像的一半到底部。 x为最大宽度的0。
请记住,OpenCV从图像的左上角开始,而增加Y值则意味着向下。
要获取图像的宽度和高度,可以执行image.shape,它提供3个值:
yMax,xMax, amount of channels
,您可能不会使用其中的频道。要获取高度和宽度,您还可以执行以下操作:
高度,宽度= image.shape [0:2]
这也称为获取感兴趣区域或ROI