1

After some processing of a image I, extracted some region of a image. Here is the .npy file.

segmented_image = np.load('data.npy')
plt.imshow(segmented_image)  

enter image description here

Now, I am trying to crop/segment the region of P. How can I do that ?

Thanks in advance.

2
  • 2
    I'd try to find min/max x/y values for the color pic, and then crop the image to this values Commented Mar 12, 2021 at 7:17
  • @kabooya yes, but I am trying to segment in a unsupervised way, like area calculating or shape. Commented Mar 12, 2021 at 8:04

1 Answer 1

1

You can try contour filtration.

import cv2
import numpy as np

image = np.load("data.npy")
cv2.imshow("image", image)

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

_, threshold_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY)
cv2.imshow("threshold_image", threshold_image)

contours, hierarchy = cv2.findContours(threshold_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

# here you can apply your conter filter logic
# In this image I can see biggest contur is "p"
selected_contour = max(contours, key=lambda x: cv2.contourArea(x))

mask_image = np.zeros_like(threshold_image)
cv2.drawContours(mask_image, [selected_contour], -1, 255, -1)
cv2.imshow("mask_image", mask_image)

segmented_image = cv2.bitwise_and(image, image, mask=mask_image)
cv2.imshow("segmented_image", segmented_image)

cv2.waitKey(0)


process images

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.