第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在python numpy數(shù)組中,如何知道哪個(gè)對(duì)象更接近一張圖像中的一個(gè)點(diǎn)?

在python numpy數(shù)組中,如何知道哪個(gè)對(duì)象更接近一張圖像中的一個(gè)點(diǎn)?

浮云間 2023-10-31 14:01:55
我有一個(gè) numpy 數(shù)組,它代表一個(gè)圖像。該圖像有 3 種顏色:橙色(背景)、藍(lán)色(對(duì)象 1)和綠色(對(duì)象 2)。我使用 3 個(gè)值(0、1 和 2)來指示 numpy 數(shù)組中的 3 種顏色。兩個(gè)對(duì)象不重疊。我的問題是:如何知道哪個(gè)物體更接近圖像的中心(紅點(diǎn))?(這里,較近是指該物體到一個(gè)物體圖像中心的最近距離小于該物體到另一個(gè)物體圖像中心的最近距離)我的代碼是這樣的:import numpy as npfrom scipy import spatialimport timesub_image1 = np.ones((30, 30, 30))sub_image2 = np.ones((20, 10, 15))# pad the two sub_images to same shape (1200, 1200, 1200) to simulate my 3D medical dataimg_1 = np.pad(sub_image1, ((1100, 70), (1100, 70), (1100, 70)))img_2 = np.pad(sub_image1, ((1100, 80), (1130, 60), (1170, 15)))def nerest_dis_to_center(img):    position = np.where(img > 0)    coordinates = np.transpose(np.array(position))  # get the coordinates where the voxels is not 0    cposition = np.array(img.shape) / 2  # center point position/coordinate    distance, index = spatial.KDTree(coordinates).query(cposition)    return distancet1 = time.time()d1 = nerest_dis_to_center(img_1)d2 = nerest_dis_to_center(img_2)if d1 > d2:    print("img2 object is nearer")elif d2 > d1:    print("img1 object is nearer")else:    print("They are the same far")t2 = time.time()print("used time: ", t2-t1)# 30 seconds上面的代碼可以工作,但是速度很慢,并且需要很大的內(nèi)存(大約 30 GB)。如果你想在你的電腦上重現(xiàn)我的代碼,你可以使用更小的形狀而不是 (3200, 1200, 1200)。有沒有更有效的方法來實(shí)現(xiàn)我的目標(biāo)?
查看完整描述

2 回答

?
繁花不似錦

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊

我解決了這個(gè)問題。


因?yàn)閮蓚€(gè)3D數(shù)組太大了。所以首先我用最近鄰法將它們采樣到更小的尺寸。然后繼續(xù):


import numpy as np

from scipy import spatial

import time


sub_image1 = np.ones((30, 30, 30))

sub_image2 = np.ones((20, 10, 15))


# pad the two sub_images to same shape (1200, 1200, 1200) to simulate my 3D medical data

img_1 = np.pad(sub_image1, ((1100, 70), (1100, 70), (1100, 70)))

img_2 = np.pad(sub_image1, ((1100, 80), (1130, 60), (1170, 15)))


ori_sz = np.array(img_1.shape)

trgt_sz = ori_sz / 4

zoom_seq = np.array(trgt_sz, dtype='float') / np.array(ori_sz, dtype='float')

img_1 = ndimage.interpolation.zoom(img_1, zoom_seq, order=0, prefilter=0)

img_2 = ndimage.interpolation.zoom(img_2, zoom_seq, order=0, prefilter=0)

print("it cost this secons to downsample the nearer image" + str(time.time() - t0))  # 0.8 seconds



def nerest_dis_to_center(img):

    position = np.where(img > 0)

    coordinates = np.transpose(np.array(position))  # get the coordinates where the voxels is not 0

    cposition = np.array(img.shape) / 2  # center point position/coordinate

    distance, index = spatial.KDTree(coordinates).query(cposition)

    return distance


t1 = time.time()

d1 = nerest_dis_to_center(img_1)

d2 = nerest_dis_to_center(img_2)


if d1 > d2:

    print("img2 object is nearer")

elif d2 > d1:

    print("img1 object is nearer")

else:

    print("They are the same far")

t2 = time.time()

print("used time: ", t2-t1)

# 1.1 seconds


查看完整回答
反對(duì) 回復(fù) 2023-10-31
?
月關(guān)寶盒

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊

這可能不是最終的解決方案或最佳的運(yùn)行時(shí)間,必須用實(shí)際數(shù)據(jù)進(jìn)行測(cè)試。為了表達(dá)我的想法,我選擇了較小的矩陣大小并且僅使用 2D 情況


import numpy as np

import matplotlib.pyplot as plt



sub_image1 = np.ones((30, 30))  # 1st object

sub_image2 = np.ones((20, 10)) * 2  # 2nd object


# pad the two sub_images to same shape (120, 120)

img_1 = np.pad(sub_image1, ((110, 60), (60, 110)))

img_2 = np.pad(sub_image2, ((100, 80), (130, 60)))


final_image = img_1 + img_2  # creating final image with both objects in a background of zeros


image_center = (np.array([final_image.shape[0], final_image.shape[1]]) / 2).astype(np.int)


# mark the center

final_image[image_center[0], image_center[1]] = 10


# find the coordinates of where the objects are

first_obj_coords = np.argwhere(final_image == 1)  # could be the most time consuming operation

second_obj_coords = np.argwhere(final_image == 2) # could be the most time consuming 


# find their centers

first_obj_ctr = np.mean(first_obj_coords, axis=0)

second_obj_ctr = np.mean(second_obj_coords, axis=0)


# turn the centers to int for using them to index

first_obj_ctr = np.floor(first_obj_ctr).astype(int)

second_obj_ctr = np.floor(second_obj_ctr).astype(int)


# mark the centers of the objects

final_image[first_obj_ctr[0], first_obj_ctr[1]] = 10

final_image[second_obj_ctr[0], second_obj_ctr[1]] = 10


# calculate the distances from center to the object center

print('Distance to first object: ', np.linalg.norm(image_center - first_obj_ctr))

print('Distance to second object: ', np.linalg.norm(image_center - second_obj_ctr))


plt.imshow(final_image)

plt.show()


輸出

Distance to first object:  35.38361202590826

Distance to second object:  35.17101079013795

https://img1.sycdn.imooc.com/6540987c000120da04100404.jpg

查看完整回答
反對(duì) 回復(fù) 2023-10-31
  • 2 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)