Basic Python OpenCV cropping and resizing

2024/11/9 2:34:17

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?

Answer

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

  1. By providing required size

  2. 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.shapecv2.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

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

https://en.xdnf.cn/q/72219.html

Related Q&A

Why does Keras loss drop dramatically after the first epoch?

Im training a U-Net CNN in Keras/Tensorflow and find that loss massively decreases between the last batch of the first epoch, and the first batch of the second epoch: Epoch 00001: loss improved from in…

extract strings from a binary file in python

I have a project where I am given a file and i need to extract the strings from the file. Basically think of the "strings" command in linux but im doing this in python. The next condition is …

Installing numpy on Mac to work on AWS Lambda

Is there a way to install numpy on a Mac so that it will work when uploaded to AWS Lambda? I have tried a variety of different ways, including using different pip versions, using easy_install, and fol…

python- how to get the output of the function used in Timer

I want to run a function for 10s then do other stuff. This is my code using Timerfrom threading import Timer import timedef timeout():b=truereturn ba=false t = Timer(10,timeout) t.start()while(a==f…

Create automated tests for interactive shell based on Pythons cmd module

I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. Id like to c…

Matplotlib with multiprocessing freeze computer

I have an issue with matplotlib and multiprocessing. I launch a first process, where I display an image and select an area, and close the figure. Then I launch another process, where I call a graph fun…

Pull Tag Value using BeautifulSoup

Can someone direct me as how to pull the value of a tag using BeautifulSoup? I read the documentation but had a hard time navigating through it. For example, if I had:<span title="Funstuff&qu…

What is the practical difference between xml, json, rss and atom when interfacing with Twitter?

Im new to web services and as an introduction Im playing around with the Twitter API using the Twisted framework in python. Ive read up on the different formats they offer, but its still not clear to m…

how to grab from JSON in selenium python

My page returns JSON http response which contains id: 14Is there a way in selenium python to grab this? I searched the web and could not find any solutions. Now I am wondering maybe its just not poss…

Numpy: Array of `arange`s

Is there a way to take...>>> x = np.array([0, 8, 10, 15, 50]).reshape((-1, 1)); ncols = 5...and turn it into...array([[ 0, 1, 2, 3, 4],[ 8, 9, 10, 11, 12],[10, 11, 12, 13, 14],[15, 16, 17…