python opencv 裁剪图片_基本的Python OpenCV裁剪和调整大小

本文介绍了如何使用OpenCV库在Python中进行图像裁剪和缩放。通过提供图像的新尺寸,可以调整图像大小,而使用`crop=image[y:y1,x:x1]`可以实现指定区域的裁剪。对于保存缩放后的图像问题,确保使用`imwrite`正确保存。同时,`imshow`显示的是图像的实时视图,可能与独立图像查看器显示的不一致,因为窗口大小取决于图像像素。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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?

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?

解决方案

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 provides a function called resize to achieve image scaling. Two way to scale an image

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

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

Crop image in 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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值