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

请帮我。

4

2 回答 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来获取行/列号。

于 2016-11-02T23:37:55.613 回答
0

你可以在bashImageMagickvips这样做

#!/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

于 2016-11-02T21:06:05.177 回答