我使用在線合并工具合并了兩張肺部影像以顯示完整影像。

問題是由于每個影像的背景都是黑色的,所以我在顯示的影像中的肺部之間出現了不需要的間隙。
我想知道是否有辦法使用演算法或在線工具從影像中洗掉一個區域并減少肺部之間的間隙。
我檢查的另一種方法是使用 OpenCV 和 Python 進行全景影像拼接,我將嘗試將其作為連接影像的最后手段。
我想要的結果:

uj5u.com熱心網友回復:
這是我在 Python/OpenCV 的評論中提到的一種方式。
- 讀取輸入并轉換為灰度
- 二進制的閾值
- 獲取外部輪廓
- 過濾輪廓以僅保留大輪廓并將邊界框值放入串列中。還計算邊界框的最大寬度和最大高度。
- 設定所需的填充量
- 創建一個最大寬度和最大高度的黑色影像
- 按 x 值對邊界框串列進行排序
- 從串列中獲取第一項并裁剪和填充它
- 創建一個最大高度和所需焊盤大小的黑色影像
- 與之前的填充作物水平連接
- 從串列中獲取第二項并裁剪和填充它
- 與之前的填充結果水平連接
- 根據需要在周圍墊
- 保存輸出
輸入:

import cv2
import numpy as np
# load image
img = cv2.imread("lungs_mask.png", cv2.IMREAD_GRAYSCALE)
# threshold
thresh = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)[1]
# get the largest contour
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
# get bounding boxes of each contour if area large and put into list
cntr_list=[]
max_ht = 0
max_wd = 0
for cntr in contours:
area = cv2.contourArea(cntr)
if area > 10000:
x,y,w,h = cv2.boundingRect(cntr)
cntr_list.append([x,y,w,h])
if h > max_ht:
max_ht = h
if w > max_wd:
max_wd = w
# set padded
padding = 25
# create black image of size max_wd,max_ht
black = np.zeros((max_ht,max_wd), dtype=np.uint8)
# sort contours by x value
def takeFirst(elem):
return elem[0]
cntr_list.sort(key=takeFirst)
# Take first entry in sorted list and crop and pad
item = cntr_list[0]
x = item[0]
y = item[1]
w = item[2]
h = item[3]
crop = thresh[y:y h, x:x w]
result = black[0:max_ht, 0:w]
result[0:h, 0:w] = crop
# create center padding and concatenate
pad_center_img = np.zeros((max_ht,padding), dtype=np.uint8)
result = cv2.hconcat((result, pad_center_img))
# Take second entry in sorted list and crop, pad and concatenate
item = cntr_list[1]
x = item[0]
y = item[1]
w = item[2]
h = item[3]
crop = thresh[y:y h, x:x w]
temp = black[0:max_ht, 0:w]
temp[0:h, 0:w] = crop
result = cv2.hconcat((result, temp))
# Pad all around as desired
result = cv2.copyMakeBorder(result, 25, 25, 25, 25, borderType=cv2.BORDER_CONSTANT, value=(0))
# write result to disk
cv2.imwrite("lungs_mask_cropped.jpg", result)
# display it
cv2.imshow("thresh", thresh)
cv2.imshow("result", result)
cv2.waitKey(0)
結果:

uj5u.com熱心網友回復:
理念:
使用該

注意事項:
- 在線:
lung_1 = cv2.copyMakeBorder(img[y1: y1 h2, x1: x1 w1], 20, 20, 20, 20, cv2.BORDER_CONSTANT)請注意,我們使用了
h2而不是h1,就像我們要使用h1我們定義的一樣,np.hstack()由于陣列的高度不同,該方法會拋出錯誤。- 將
sorted()在
(x1, y1, w1, h1), (x2, y2, w2, h2) = sorted(map(cv2.boundingRect, contours))是
x, y, w, h按它們的x屬性排序,使肺從左到右串聯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/392423.html
