Skip to main content
 首页 » 编程设计

python之使用 Python 从星系照片中删除背景

2024年12月31日8cmt

你能给我一些关于如何使用 Python 从星系图像中删除背景的提示吗?我想以某种方式检测星系并移除它之外的所有东西。

有机会用 OpenCV 来做吗?

所有图片看起来像这样:

请您参考如下方法:

试试这个:

这个解决方案的基本思想是,在执行threshold()之后得到图像的轮廓,并检测轮廓中最大的轮廓。

import cv2 
image = cv2.imread("test.jpg", 1) 
img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 
cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU,img) 
im2, contours, hier = cv2.findContours(img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) 
 
if len(contours) != 0: 
    #find the biggest area 
    c = max(contours, key = cv2.contourArea) 
    x,y,w,h = cv2.boundingRect(c) 
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),1) 
 
cv2.imshow("Result", image) 
cv2.waitKey(0) 

输出: