基本的Python OpenCV裁剪和调整大小 [英] Basic Python OpenCV cropping and resizing

查看:938
本文介绍了基本的Python OpenCV裁剪和调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我一些裁剪算法吗?它的openCV .. im试图弄清楚这一点.我知道方法是crop = image [y:y1,x:x1]. 如果我的图像具有new_dimensionXxnew_dimensionY像素,并且想要将其裁剪为相同的宽度,但高度要高于pointOfInterestX 121px以上.我该怎么办?

can someone help me with a little cropping algorithm? its openCV.. im trying to figure this out. I know the method is crop = image[y:y1, x:x1]. If I have an image with new_dimensionXxnew_dimensionY pixels and I want to crop it to the same width but the height just above 121px above pointOfInterestX. How can I do that?

另一个问题:

image = cv2.resize(image,(int(new_dimensionX), int(new_dimensionY)))
cv2.imwrite("test6.jpg", image)

文件test6.jpg不能反映其上方一行的调整大小.为什么?

The file test6.jpg does not reflect the resize done in the line just above it. Why?

推荐答案

当您使用 imshow()显示调整大小的图像时,它将在屏幕上显示该图像,并根据图像更改显示窗口的大小像素.当您使用图像查看器打开图像时,它将以固定的窗口大小打开图像,并且窗口大小不取决于图像像素

When you show the resized image with imshow() it shows the image on-screen and change showing window size according to an image pixel. when you open the image with image viewer it open image in fixed window size and window size don't depend on image pixel

OpenCV提供了一个称为调整大小"的功能来实现图像缩放.缩放图像的两种方法

OpenCV provides a function called resize to achieve image scaling. Two way to scale an image

  1. 通过提供所需的大小

  1. By providing required size

通过给出比例因子

如果您未指定大小(通过使用None),则它会期望X和Y缩放因子

If you don't specify a size (by using None), then it expects the X and Y scaling factors

同时提供缩放大小

import cv2
filename = "path_to_image"

oriimage = cv2.imread(filename)

print oriimage.shape
newx,newy = oriimage.shape[1]/4,oriimage.shape[0]/4 #new size (w,h)

newimage = cv2.resize(oriimage,(newx,newy))

print newimage.shape

cv2.imshow("original image",oriimage)
cv2.imshow("resize image",newimage)

cv2.waitKey(0)

具有缩放比例

import cv2
filename = "path_to_image"

image = cv2.imread(filename) 

small = cv2.resize(image, (0,0), fx=0.5, fy=0.5) 

large = cv2.resize(image, (0,0), fx=1.5, fy=1.5)

cv2.imshow("small image",small)
cv2.imshow("large image",large)

#To save rescale image
cv2.imwrite('s.jpg',small)
cv2.imwrite('l.jpg',large)

cv2.waitKey(0)

有关 resize()的详细参数 方法

For detail parameter of resize() method

opencv

import cv2
im_path = "path/to/image"
img = cv2.imread(im_path)

crop_img = img[0:400, 0:300] # Crop from {x, y, w, h } => {0, 0, 300, 400}

cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

Opencv imread方法读取图像并返回numpy数组,并且numpy数组的大小等于image数组.如果要裁剪图像,只需选择一个数组

Opencv imread method read image and return numpy array, and Size of numpy array equal to image array.If you want to crop image just select an array

img[0:400,0:300]

注意:其img [y:y + h,x:x + w] img首先为y,高度为x和宽度

Note : its img[y: y + h, x: x + w] img take first y and height second is x and width

这篇关于基本的Python OpenCV裁剪和调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆