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