基本的 Python OpenCV 裁剪和调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45726646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basic Python OpenCV cropping and resizing
提问by David Kachlon
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?
有人可以帮我一些裁剪算法吗?它的 openCV .. 我试图弄清楚这一点。我知道方法是crop = image[y:y1, x:x1]。如果我有一张带有 new_dimensionXxnew_dimensionY 像素的图像,并且我想将其裁剪为相同的宽度,但高度高于 pointOfInterestX 的 121px。我怎样才能做到这一点?
One more question:
还有一个问题:
image = cv2.resize(image,(int(new_dimensionX), int(new_dimensionY)))
cv2.imwrite("test6.jpg", image)
The file test6.jpg does not reflect the resize done in the line just above it. Why?
文件 test6.jpg 不反映在它上面的行中完成的调整大小。为什么?
回答by Kallz
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
当您使用imshow()显示调整大小的图像时,它会在屏幕上显示图像并根据图像像素更改显示窗口大小。当您使用图像查看器打开图像时,它会以固定的窗口大小打开图像,并且窗口大小不依赖于图像像素
OpenCV provides a function called resize to achieve image scaling. Two way to scale an image
OpenCV 提供了一个名为 resize 的函数来实现图像缩放。缩放图像的两种方法
By providing required size
By giving scaling factor
通过提供所需的尺寸
通过给出比例因子
If you don't specify a size (by using None), then it expects the X and Y scaling factors
如果您不指定大小(通过使用 None),则它需要 X 和 Y 缩放因子
while providing scaling size
同时提供缩放大小
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)
with scaling ratio
与缩放比例
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)
For detail parameter of resize()method
resize()方法的详细参数
Crop image inopencv
在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 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
Opencv imread方法读取图像并返回numpy数组,numpy数组的大小等于图像数组。如果要裁剪图像,只需选择一个数组
img[0:400,0:300]
Note: its img[y: y + h, x: x + w] img take first y and height second is x and width
注意:它的 img[y: y + h, x: x + w] img 取第一个 y 和第二个高度是 x 和宽度