opencv 修改图片的尺寸
时间: 2024-11-25 17:15:39 浏览: 48
在OpenCV中,你可以使用`cv2.resize()`函数来修改图片的尺寸。这个函数用于改变图像的大小,支持两种缩放模式:保持宽高比(`interpolation=cv2.INTER_AREA`或`cv2.INTER_CUBIC`)和不保持比例(`interpolation=cv2.INTER_LINEAR`或`cv2.INTER_NEAREST`)。以下是一个简单的例子:
```python
import cv2
# 加载图片
image = cv2.imread('input.jpg')
# 指定新的宽度和高度,这里假设我们想把图片缩放到800x600像素
new_dim = (800, 600)
# 使用cv2.resize()函数调整尺寸
resized_image = cv2.resize(image, new_dim, interpolation=cv2.INTER_LINEAR)
# 保存处理后的图片
cv2.imwrite('output.jpg', resized_image)
```
在这个例子中,`interpolation=cv2.INTER_LINEAR`表示使用线性插值方法进行缩放,这是一种常用的高质量缩放方法。
相关问题
opencv 修改图片尺寸代码
当使用OpenCV来修改图片尺寸时,可以使用`resize()`函数。以下是一个示例代码:
```python
import cv2
# 读取图片
image = cv2.imread('input.jpg')
# 设置新的尺寸
new_width = 800
new_height = 600
# 修改尺寸
resized_image = cv2.resize(image, (new_width, new_height))
# 显示修改后的图片
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个示例中,我们首先使用`imread()`函数读取输入图片。然后,我们将新的宽度和高度设置为800和600。最后,使用`resize()`函数将图片修改为新的尺寸。修改后的图片可以使用`imshow()`函数显示出来。`waitKey(0)`用于等待用户按下任意键来关闭窗口,最后使用`destroyAllWindows()`关闭所有窗口。
请注意,示例中的`input.jpg`是输入图片的文件名,你需要根据实际情况修改为你要处理的图片文件名。
C++ opencv修改图片尺寸
使用OpenCV库可以方便地修改图片尺寸。下面是一个C++实例代码:
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("test.jpg"); // 读取图片
int width = 640; // 新的宽度
int height = 480; // 新的高度
Mat img_resized;
resize(img, img_resized, Size(width, height)); // 修改图片尺寸
imwrite("test_resized.jpg", img_resized); // 保存图片
return 0;
}
```
上述代码中,我们通过`resize()`函数来修改图片的尺寸。其中,第一个参数是原始图片,第二个参数是修改后的图片,第三个参数是修改后的图片尺寸。最后,我们通过`imwrite()`函数将修改后的图片保存到本地。
阅读全文
相关推荐














