The first thing is we are importing our image and now we are going to resize it. So to resize we have a function in opencv that helps us out, so we will write resize(),
resize | ( | src,//输入图像 | |
dst,//输出目标图像 | |||
dsize,//缩放后图像尺寸大小 | |||
double | fx = 0,//X方向上的缩放比例 | ||
double | fy = 0,//Y方向上的缩放比例 | ||
int | interpolation = INTER_LINEAR//插值法 | ||
) |
and then we have to write the input image which is image, and then we have to write the output image we will call this imgResize, and let’s declare it mats imageResize (创建一个矩阵接收目标图像). So let’s say we will write size and what is the size of our image. At this point, how can we find that okay let me show you how we can do that. We are going to write cout and we will print out the size. So what we can do is let’s say we want a very specific number to resize, so what we can resize it. So let’s run that. We need to define a new window. Run it, and you can see that the aspect ratio has changed, it is not the same aspect ratio it’s squeezed a little bit with on the width. So if you don’t want to do that, if you don’t have a particular number, you just want to scale it down, then instead of using this size, you can remove this and you can define how much you want to scale so here you have doublefX and doublefY, so you can define here for example 0.5 and 0.5. so this will scale instead of exact pixels. So let’s have a look. And there you go so now it has decreased it 50 percent. So I mostly use the scaling part. Because
It is more convenient. Unless I have a very specific number that I want the image to be.
Now we will learn how to crop our images, because this is very important a lot of the times when we find an object we want to crop it to add more processing on that specific region and a lot of the times this region is called region of interest roi. So to crop an image we simply have to write let’s say that our new image is imageCrop. So we can define it here imageCrop. Inside we are going to define the values for cropping so to do that we are going to use the rectangle data type. So we will write here rect and then we will define a name. so let’s say roi and inside that we have to define the x, y, width and height.
Eg: Rect roi(100:”X的起始坐标”,100:“Y的起始坐标”,600:“截取图像的宽”,600:“截取图像的高”)