3

I'm new here. I have a number of dicom images.

  1. I need to get 4 max pixel values and their co-ordinates of all the images
  2. and automatically crop the 128 by 128 4 patches from each image keeping the center pixel one of the max pixel that has been found
  3. save the patches

In this way I need to extract four patches from each pixel. Please tell me how I can do it.

I have made this code for one image but it's not giving me the right answer:

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

Please help me.

Mark Setchell
  • 146,975
  • 21
  • 182
  • 306

2 Answers2

2

Following your code, here's the most straightforward way to go about this. Note that I've made some corrections and improvements along the way:

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;

This uses the second output of sort to get the indices of the max pixels, then passes those indices through ind2sub to get the row/column number.

beaker
  • 14,885
  • 3
  • 26
  • 45
0

You can do it like this in bash with vips and 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

I can probably remove the need for vips if necessary.

Mark Setchell
  • 146,975
  • 21
  • 182
  • 306
  • i am using matlab, plz can you tell what is bash, vips, ImageMagic? – user2102092 Nov 02 '16 at 21:16
  • 1
    They are a set of extremely powerful, extremely flexible, free, open source image processing tools, but don't worry if you don't know them - just tag your question with `Matlab` and I am sure someone else will help you out. – Mark Setchell Nov 02 '16 at 22:25