DICOM 图像的最小值、最大值、像素和位置

3

我是一个新手。我有许多DICOM图像。

  1. 我需要获取所有图像的4个最大像素值及其坐标
  2. 并自动从每个图像中裁剪128 x 128的4个补丁,保持中心像素为已找到的最大像素之一
  3. 保存这些补丁

以此方式,我需要从每个像素中提取四个补丁。请告诉我如何做到这一点。

我已经为一个图像编写了此代码,但它没有给我正确的答案:

sortedValues = sort(grayImage, 'descend');
% Get the 4 max values and their coords
for k = 1 : 4
    thisValue = sortedValues(k);
    % Find where it occurs
    [rows, columns] = find(grayImage, thisValue);
    % Plot them over the image
    for k2 = 1 : length(rows)
        thisRow = rows(k2);
        thisColumn = columns(k2);
        plot(thisColumn, thisRow, 'r+');
        hold on;
        text(thisColumn, thisRow, num2str(k));
        % Crop into a new image
        row1 = thisRow - 64;
        row2 = row1 + 127;
        col1 = thisColumn - 64;
        col2 = col1 + 127;
        subImage = grayImage(row1:row2, col1:col2);
        % Now do something with subimage....
    end
end

请帮忙。


2
尝试添加一个示例图像,展示你得到的答案以及为什么它是错误的。 - Mark Setchell
2个回答

2

根据您的代码,以下是最直接的方法。请注意,我沿途进行了一些更正和改进:

imshow(grayImage);
hold on;   % only needed once

% corrected to sort the entire image grayImage(:), not just column-by-column
% sortedValues not used here, but left for future use
[sortedValues, sortedIndices] = sort(grayImage(:), 'descend');

for k = 1:4
   [m,n] = size(grayImage);
   [r,c] = ind2sub([m,n], sortedIndices(k));
   plot(c, r, 'r+');
   text(c, r, num2str(k));
   row1 = max(r - 64, 1);   % make sure rows/columns don't fall outside image
   row2 = min(r + 63, m);
   col1 = max(c - 64, 1);
   col2 = min(c + 63, n);
   subImage = grayImage(row1:row2, col1:col2);
   % Now do something with subimage...
end

hold off;

这里使用了sort的第二个输出来获取最大像素的索引,然后通过ind2sub将这些索引传递以获取行/列号。


0

你可以使用 vipsImageMagickbash 中这样做

#!/bin/bash
# Convert DICOM image to PNG for vips
convert image.dcm image.png

# Use vips to get x and y of 4 maximum pixel locations
{ read line; read -a x <<< "$line"
  read line; read -a y <<< "$line"
  read line; } < <(vips im_maxpos_vec image.png 4)

# Now crop them out as centres of 128x128 boxes
i=0
for index in "${!x[@]}"; do
   cx=${x[index]}
   cy=${y[index]}
   ((a=cx-64))
   ((b=cy-64))
   echo Cropping from $cx,$cy to sub-${i}.png
   convert image.png -crop 128x128+${cx}+${cy} +repage sub-${i}.png
   ((i=i+1))
done

如果需要的话,我可能可以删除对 vips 的需求。


我正在使用Matlab,请问Bash、VIPS和ImageMagick是什么? - user2102092
1
它们是一组极为强大、极为灵活、免费、开源的图像处理工具,但如果你不了解它们,也不用担心——只需在问题中标记Matlab,我相信其他人会帮助你解决问题。 - Mark Setchell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接