dicom图像最小像素和位置

时间:2016-11-02 20:34:13

标签: image matlab crop dicom minmax

我是新来的。我有一些dicom图像。

  1. 我需要获得4个最大像素值及其所有图像的坐标
  2. 并自动裁剪每个图像的128 x 128个4个色块,使中心像素成为已找到的最大像素之一
  3. 保存补丁
  4. 通过这种方式,我需要从每个像素中提取四个补丁。请告诉我如何做到这一点。

    我为一张图片制作了这段代码,但它没有给我正确答案:

    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 个答案:

答案 0 :(得分: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以获取行/列编号。

答案 1 :(得分:0)

您可以bash vips ImageMagick

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

如果有必要,我可以删除对 @Autowired public MyBean( @Value("${some.prop}") String prop, @Value("${some.prop2}") String prop2) { } 的需求。