How to identify a specific region of an image?

조회 수: 20(최근 30일)
Dominik Mattioli
Dominik Mattioli 2018년 2월 15일
편집: Dominik Mattioli 2018년 2월 23일
I have some ultrasound movies that I have converted to sets of image frames. I'm trying to adjust the contrast of the region that shows the actual ultrasound (the output of the transducer). This is the direction in which I am heading:
I = imread('ultrasound_frame.png');
I2D = rgb2gray(I);
[e1,threshold1] = edge(I2D,'prewitt');
close all; figure(1);
imshow(e1,[]),title('prewitt');
% Find [x,y] of edge with the most common x-coordinates (this should be the top line?
[y,x] = find(e1); % [x,y] coords of edge.
[m,f] = mode(y); % Most common y-coord.
ix = find(ismember(y,m)); % Index m to x (and y).
hold on;
plot(x(ix),repmat(m,f),'r*'); % Prove it.
% Looks like I need to find the most-left and most-right x-coords of points
% that are all within the most-common-distance of each other.
xdist = diff(x(ix)); % Dist between x-coords.
changeDist = find(diff(xdist))+1;
[~,ilongestConsec] = max(diff(changeDist));
longestConsec = changeDist(ilongestConsec:ilongestConsec+1);
hold on;
plot(x(ix(longestConsec(1))),m,'g*'); % Plot left-most x-coord of line.
plot(x(ix(longestConsec(2))),m,'b*'); % Plot rght-most x-coord of line.
Since this window is a rectangle I will guess the y-coordinate offset to grab the coordinates of the rectangle's corners. Is there a more simple way of doing this?
Issues include:
- The location of the transducer's output in the ultrasound image may not always be the same.
- The shape of the transducer's output may not always be the same. It can be square or trapezoidal.
- The only constant landmark is the blue-circled "P", which is always located at/in or very near to the top left of the transducer's output. So my code above would not work if there is no distinguishable white line at the top of the transducer's output.
  댓글 수: 1
Dominik Mattioli
Dominik Mattioli 2018년 2월 15일
*The reason for needing the coordinates of the window/rectangle is so I can automatically imcrop that window/rectangle of the image.

댓글을 달려면 로그인하십시오.

답변(1개)

Dominik Mattioli
Dominik Mattioli 2018년 2월 23일
편집: Dominik Mattioli 2018년 2월 23일
I think this is an interesting question and I think I have a good solution so I will answer my own question.
Since this is a movie and I have many frames, I decided to loop through (most of) the frames and identify which pixel values differ between consecutive frames. This indicates movement and helps identify the "static" regions of the image, e.g. the top blue banner. By determining which pixels experience movement in the image throughout the analyzed frames, I can then compute a convex hull of those pixels. From there, a bounding box gives an approximate location of the region in question.
% imFolder = % The directory/folder holding all relevant im.
imFileName0 = [imFolder,'\050'];
N = dir([imFolder '/*.tif']); % # of images in folder.
nFrames = size(N,1);
interval = 5; % # of frames skipped.
viewFrames = 1:interval:nFrames-1;
if ~ismember(nFrames-1,viewFrames)
viewFrames = [viewFrames,nFrames-1];
end
% Read first image frame, convert to binary.
[I0,~] = imread([imFileName0,'.tif']);
% Initialize a binary image for identifying pixels that capture movement.
pix = zeros(size(I0));
% Loop through until the N-th frame, creating all-encompassing mask.
findSlash = strfind(imFileName0,'\'); % Find last forward-slash.
findFrameNum = findSlash(end)+1:length(imFileName0);
for idx = viewFrames(1:end)
% Get next frame.
nextFrameNum = num2str(str2double(imFileName0(findFrameNum)) + idx);
if length(nextFrameNum) == 2
nextFrameNum = ['0',nextFrameNum]; %#ok<AGROW>
end
imFileNameNextFrame = imFileName0; % Filename of next frame.
imFileNameNextFrame(findFrameNum) = nextFrameNum;
INext = imread([imFileNameNextFrame,'.tif']); % Read next image frame.
% Compare (nonbinary) the previous frame with nextFrameNum.
dynamicPixels = INext ~= I0;
pix(dynamicPixels) = 1;
% I0 becomes INext because we are comparing consecutive frames.
I0 = INext;
end
% Compute convex hull of pix to bound all changing-pixels.
CH = bwconvhull(imbinarize(rgb2gray(pix),'adaptive','Sensitivity',1),'union');
% Identify coordinates of a bounding box encasing all binary values in CH.
%%%alphaShape % Might be beneficial to use.
bb = regionprops(CH,'boundingbox');
figure;imshow(imread([imFileName0,'.tif']));hold on;
rectangle('position',bb.BoundingBox,'edgecolor','r','linewidth',2);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by