How to Crop an Image in Opencv Using Python

How to crop an image in OpenCV using Python

It's very simple. Use numpy slicing.

import cv2
img = cv2.imread("lenna.png")
crop_img = img[y:y+h, x:x+w]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)

What is the easy way to crop a part of image using Python OpenCV

(x1, y1) are the coordinates of starting point, while (x2, y2) are the end points in the image. In a rectange you can think of them as top-left corner is (x1, y1) while (x2, y2) is bottom-right corner - or just like width and height.

But while cropping they have a little reverse format

cropImage = image[ y1: y2 , x1: x2] 
# or
cropImage = image[ Y: H, X: W ]

How to crop image to only text section with Python OpenCV?

Here's a simple approach:

  1. Obtain binary image. Load the image, grayscale, Gaussian blur, then Otsu's threshold to obtain a binary black/white image.

  2. Remove horizontal lines. Since we're trying to only extract text, we remove horizontal lines to aid us in our next step so incorrect contours will not merge together.

  3. Merge text into a single contour. The idea is that characters which are adjacent to each other are part of the wall of text. So we can dilate individual contours together to obtain a single contour to extract.

  4. Find contours and extract ROI. We find contours, sort contours by area, then extract the largest contour ROI using Numpy slicing.


Here's the visualization of each step:

Binary image -> Removed horizontal lines in green















12
threshhorizontal

How to crop image based on the object radius using OpenCV?

Here is the processing in Python/OpenCV for your new image.

Input:

Sample Image

import cv2
import numpy as np

# load image as grayscale
img = cv2.imread('Diabetic-Retinopathy_G_RM_151064169.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold input image
mask = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)[1]

# optional morphology to clean up small spots
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

# put mask into alpha channel of image
result = np.dstack((img, mask))

# save resulting masked image
cv2.imwrite('Diabetic-Retinopathy_G_RM_151064169_masked.png', result)

# display result, though it won't show transparency
cv2.imshow("mask", mask)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result

How to crop image using masked image and overlay on top of other image with same color?

import cv2
cat = cv2.imread('cat.jpg')
mask_cat = cv2.imread('mask_cat.jpg', 0)
result = cv2.bitwise_and(cat,cat,mask = mask_cat)

Sample Image

I also see you try to reshape the image.It can be done as follows.

width, height = 1742, 815
reshaped_result = cv2.resize(result, dsize=(width, height))

Sample Image

To place the cropped image on resized image

width, height = 1742, 815
result_final = np.zeros((height,width,3), np.uint8)
h, w = result.shape[:2]
hh, ww = result_final.shape[:2]
yoff = round((hh-h)/2)
xoff = round((ww-w)/2)
result_final[yoff:yoff+h, xoff:xoff+w] = result

Sample Image



Related Topics



Leave a reply



Submit