Taking a single cycle from an image

1 view (last 30 days)
I have a current waveform image of dimension of 750 * 750. In the current waveform, I have two cycles; before the fault and during the fault. I am only interested in getting the during fault cycle (spike in the current signal). I do not want the figure in gray scale as I need RGB figure. How can I get this cycle only from the images and save in a folder?
  11 Comments
Walter Roberson
Walter Roberson on 11 Apr 2023
If what you have is images rather than figures or data, then I suggest you start by searching the File Exchange for tag:digitize and try out a few of the programs that are created specifically to try to read out coordinates from images.
DGM
DGM on 11 Apr 2023
Do the waveform positions and axes extents vary between images? If not, it should be fairly simple. If they do, it'll be a huge fragile mess, given that this is a difficult JPG.

Sign in to comment.

Accepted Answer

DGM
DGM on 11 Apr 2023
Edited: DGM on 11 Apr 2023
I'm going to pretend that this won't just explode the moment it's fed a different crusty JPG.
Assuming the following:
  • the images all use the same trace colors
  • there is a ticklabel for y = 0
  • each image is centered on the transient event
  • the crop region begins at the last phase C ZC prior to the transient
  • the crop region encloses the rest of the trace
  • the plot box, ticks, and ticklabels don't need to be kept or reconstructed
inpict = imread('graph.jpg');
% try to get mask of the yellow trace
hsvpict = rgb2hsv(inpict);
lim = [0.103 0.306; 0.066 1; 0 0.985];
lim = permute(lim,[3 2 1]);
tracemask = all(hsvpict>=lim(1,1,:) & hsvpict<=lim(1,2,:),3);
% clean up the mask
tracemask = bwareaopen(tracemask,20);
tracemask = imclose(tracemask,strel('disk',3));
tracemask = bwskel(tracemask);
% blindly try to stitch together all the holes in the mask
% this is based only on blob proximity
% otherwise we have to find a way to distinguish endpoints from many spurs
S = regionprops(tracemask,'pixelidxlist');
sz = size(tracemask);
for k = 1:numel(S)-1
[y1 x1] = ind2sub(sz(1:2),S(k).PixelIdxList); % all points on the blobs
[y2 x2] = ind2sub(sz(1:2),S(k+1).PixelIdxList);
D = (x1.'-x2).^2 + (y1.'-y2).^2; % don't need sqrt
[minr minc] = find(D == min(D(:))); % find first minimizer
p1 = [x1(minc) y1(minc)]; % point on blob1
p2 = [x2(minr) y2(minr)]; % point on blob2
tracemask = tracemask | brline(sz,[p1; p2]); % draw a line
end
% show it
imshow(tracemask,'border','tight')
%% try to get mask of the plot box, ticks, labels
lim = [0 1; 0 0.5; 0 0.75];
lim = permute(lim,[3 2 1]);
boxmask = all(hsvpict>=lim(1,1,:) & hsvpict<=lim(1,2,:),3);
% try to clean it up
boxmask = imfill(boxmask,'holes'); % fill everything
boxmask = boxmask & ~bwareafilt(boxmask,1); % get rid of plot box
boxmask = imclose(boxmask,ones(21)); % connect each ticklabel as a blob
boxmask(end-100:end,:) = false; % chop off xticklabels
boxmask = bwareafilt(boxmask,1,'smallest'); % get smallest blob (the zero ticklabel)
% show it
%imshow(boxmask,'border','tight')
%% create a mask representing the zero line
S = regionprops(boxmask,'centroid');
yzmask = false(sz);
yzmask(S.Centroid(2),:) = true;
% show it
%imshow(yzmask,'border','tight')
%% find all zero crossings prior to the event
% i assume the event is centered in all plots
zcmask = tracemask & yzmask;
zcmask(:,round(sz(2)/2):end) = false;
% get the position of the last ZC in this window
S = regionprops(zcmask,'centroid');
startcol = round(S(end).Centroid(1));
% show it
%imshow(zcmask,'border','tight')
%hold on; plot(S(end).Centroid(1),S(end).Centroid(2),'o'); hold off
%% extract the ROI
alltracemask = hsvpict(:,:,2)>0.04; % can't always assume phase C defines extents
tracerows = any(alltracemask,2);
startrow = find(tracerows,1,'first');
endrow = find(tracerows,1,'last');
endcol = find(any(alltracemask,1),1,'last');
outpict = inpict(startrow:endrow,startcol:endcol,:);
% show it
imshow(outpict,'border','tight')
I disabled the display of the masks, since they are all single-pixel features and the figures are all downscaled with nearest-neighbor interpolation. Run the code yourself if you want to see anything.
Of course, nothing can be done about the fact that part of the plot is actually missing due to the legend box, which is now unreadable.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!