MATLAB Answers

how to fix particular connected component in multiple frames

14 views (last 30 days)
QuestionsAccount
QuestionsAccount on 20 Aug 2020
Edited: Image Analyst on 26 Sep 2020
hi everyone.
i have binary image consisting of 8 to 10 connected components. actually i want to check weather the center of these 8 to 10 connnected component is changing postion in next frame or not but for that first i have to fix/label the connected components in all frames for making sure that the connected component in first frame was the same as in the next frame (like tracking but not propere tracking because i just want to find change in position of multiple connected compnents in multiiple frames).
How i can fix the particular connected components in multiple frames. eiather by assinging ID or label them but dont know how to code that in matlab. so because of begginer in matlab i need someone's help in codding.
thanx in advance

  14 Comments

QuestionsAccount
QuestionsAccount on 23 Aug 2020
no above attached screenshots are not consective frames, i just upload as an example for showing that some CC are changing position and some are not.
actually intially i didn't want change detection but here my first goal and question is how can i fix/lock the connected components in multiple video frames (that is the conneccted component marked as 1 in 1st frame will remain 1 in 2nd and upto last frame and similartly connected component marked as 2 in 1st frame will remain 2 in 2nd frame and upto last frame and so on for all other connected components).
extremly sorry to say but i did'nt understand Rik idea (may be because of begginer in matlab codding).plz explain it in detail for better understanding.
please help me in codding the above mention work.thanx
attched are some consective frames for example
jonas
jonas on 23 Aug 2020
If you post actual images and not screenshot then people are more likely to write code for you... I dont want to crop your desktop and binarize before even getting started.
QuestionsAccount
QuestionsAccount on 25 Aug 2020
These are the actual images. I preprocessed original images and after than i got binary image and i want to fix/ lock these CC that i showed in screenshots (means these screenshots are results of preprossed images) if your are talking about original images then i will attach that as well..

Sign in to comment.

Accepted Answer

jonas
jonas on 25 Aug 2020
Here's something I stitched together that works OK for the images you provided. I had to start by cropping them to the right size.. Note that this method will fail as soon as two blobs merge, disappears or move too much in a frame. All in all, not very robust.
files = dir('C:\Users\pics\*.png');
rect = ([360 130 700 520]);
for i = 1:numel(files)
BW{i} = imcrop(imbinarize(rgb2gray(imread(fullfile(files(i).folder,files(i).name)))),rect); %very long line
end
%% real action starts here
%build a colormap
cmap = repmat([perms([1 0 0]);perms([1 1 0]);[0,0,0]],2,1);
%start with a black image
im_old = zeros(size(BW{1}(:,:,1)));
k = 1
for j = 1:numel(files)
%empty new image
im_new = zeros(size(BW{1}(:,:,1)));
%load next frame
im_j = BW{j};
%remove small components
im_j = bwareaopen(im_j,20);
%find all blobs
S = bwconncomp(im_j,4);
%check if empty and continue to next frame if so
if S.NumObjects < 1
continue
end
%loop over objects
for i = 1:S.NumObjects
%apply mask of object 1 on previous image
overlapping = im_old(S.PixelIdxList{i});
%remove any non-overlapping pixels
overlapping(overlapping==0) = [];
if isempty(overlapping) %if no overlapping, assign new label
im_new(S.PixelIdxList{i}) = k;
k = k+1;
else
%if overlapping, assign most common label
im_new(S.PixelIdxList{i}) = mode(overlapping);
end
end
%replace old with new
im_old = im_new;
%store image as rgb
out{j} = label2rgb(im_new,cmap);
end

  9 Comments

Show 6 older comments
Image Analyst
Image Analyst on 12 Sep 2020
QuestionsAccount : did you realize that the screenshot you showed actually clipped off the part of the stuff we need to see? We were expecting you to copy and paste the stuff in the command window, not give a screenshot. I cannot scroll your screenshot up to see what was in your command window. I knew my code would not prevent the error of course. But I was expecting it to show that the two images were of different sizes. This forehead-slapping revelation would then lead you to find the reason why, and fix it.
Also, replace the first chunk of your code with this more robust version:
folder = 'C:\users\Pic' % Wherever...
files = dir(fullfile(folder, '*.png'));
rect = ([360 130 700 520]);
for k = 1:numel(files)
fullFileName = fullfile(files(k).folder, files(k).name);
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
fprintf('%s is %d rows by %d columns by %d color channels.\n', files(k).name, rows, columns, numberOfColorChannels);
% Make gray scale if it's not already.
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
if islogical(grayImage)
grayImage = uint8(255 .* grayImage);
end
BW{k} = imcrop(imbinarize(grayImage),rect);
end
Try this:
v = S.PixelIdxList{k}
whos v
and you'll see that v = S.PixelIdxList{k} is a vector, not an image. Doing
overlapping = im_old(S.PixelIdxList{k});
makes overlapping a vector not an image. This will be of varying size depending on how many pixels are in the PixelIdxList.
QuestionsAccount
QuestionsAccount on 26 Sep 2020
images are of the same size but objects are changing size in each frame. (objects size is not same in my case).
attach is the actually images and in screenshot i showed the desired output (i draw different color of bounding boxes on each silheoute and i want to fix the silheoute with that same color of bouding box in full video. like in attach screenshot i draw blue BB on 1st silheoute and i want that in 2nd frame the same color of bounding box (blue) will draw on that same 1st silheoute and no other silheoute with that color of BB and if 1st silheoute disapear the blue BB will also disapear with that silheoute and same will happed with the other silheoutes as well. In other words i want to fix every silheoute with some color of bouding box in full video.
Image Analyst
Image Analyst on 26 Sep 2020
Come on, make it easy for us to help you. You forgot to attach angle_all.m with the changes I suggested. I'll check back later for it.
By the way, this code I suggested works just fine:
folder = pwd
files = dir(fullfile(folder, 'frame_*.jpg'));
rect = ([360 130 700 520]);
for k = 1:numel(files)
fullFileName = fullfile(files(k).folder, files(k).name);
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
fprintf('%s is %d rows by %d columns by %d color channels.\n', files(k).name, rows, columns, numberOfColorChannels);
% Make gray scale if it's not already.
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
if islogical(grayImage)
grayImage = uint8(255 .* grayImage);
end
BW{k} = imcrop(imbinarize(grayImage),rect);
end
And I can't write a complete turnkey tracking app for you that keeps unique colors like you want. I've described many times in many posts how difficult it is, with all the ambiguities etc. Like
  1. objects occluding each other or merging together or splitting apart
  2. objects leaving the field of view, perhaps reappearing later
  3. objects entering the field of view
  4. objects changing shape from frame to frame
  5. objects changing size, color, or some other unique identifying attribute from frame to frame
One thing you'd have to do is make a feature vector with all the attributes describing all the things that are needed to distinguish one blob from another like size, shape, color, speed, etc. and then try to match up feature vectors for the blobs from one frame to the next to decide which blob was present in the prior frame, and if it was, which one it was. This is a lot of work that I just can't donate to you. It would take months or probably years. I'm sure there are companies out there that have teams of people who have spent years on tracking. Mathworks is one but their tracking stuff is just part of the low level stuff you'd need. There is still a whole bunch of stuff you'd need to add beyond what they did, and I'm sure there are companies that have done all that. You just need to find them and buy their software. That will be MUCH, MUCH easier and faster than writing it from scratch on your own.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 23 Aug 2020
The Computer Vision Toolbox has lots of tracking capabilities:
It could save you a lot of time if you bought that toolbox. Do you already have it? If not, buy it.

  0 Comments

Sign in to comment.


Image Analyst
Image Analyst on 23 Aug 2020

  6 Comments

Show 3 older comments
Rik
Rik on 24 Aug 2020
No. This is tracking. You can claim it isn't, and your end goal might not be actually to track something, but that is what is required to achieve what you describe.
Why don't you try it instead of ignoring his advice? Image Analyst has probably forgotten more about image analysis with Matlab than I have learned. You're getting free help from an expert, you would do well not to ignore it.
Image Analyst
Image Analyst on 24 Aug 2020
You'd have to make a list of features of all the blobs. Then I'd be tempted to try K-Nearest Neighbors, knnsearch() to match blobs from one feature set to the prior feature set. Sorry, I don't have a demo for this already. But it's simple so you can try it yourself easily I'd think. You need the Statistics and Machine Learning Toolbox to use that function. However it will match each blob in set 2 with a blob in set 1 regardless if it's correct or not. I mean the blob in set 1 might have left the scene and the blob in set 2 may have just entered the scene but it will say that some blob in set 1 matches the blob in set 2. In that case, perhaps (but maybe not) some blob in set 1 may have two blobs that match it in set 2. Or it may have none. So you'll need to inspect the number of matches and if there are zero, or two or more, you're going to have to make some decisions about what to do. But if all goes well and you have the same blobs in both sets (none left and none entered), and the features didn't change much from set 1 to set 2, then each blob will have exactly one matching blob in the other set.
Image Analyst
Image Analyst on 25 Aug 2020
You may want to watch this webinar tomorrow:
Join us for the fourth of seven bi-weekly free seminars focusing on the best paper research presented at EI 2020. Talks will be followed by live discussion: seminars will be recorded.*
Best Paper Imaging and Multimedia Analytics in a Web and Mobile World 2020 Conference
Bryan Blakeslee & Andreas Savakis (Rochester Institute of Technology)
Change detection in image pairs has traditionally been a binary process, reporting either "Change" or "No Change." In this paper, we present LambdaNet, a novel deep architecture for performing pixel-level directional change detection based on a four-class classification scheme. LambdaNet successfully incorporates the notion of "directional change" and identifies differences between two images as "Additive Change" when a new object appears, "Subtractive Change" when an object is removed, "Exchange" when different objects are present in the same location, and "No Change".
About the Speaker
Bryan Blakeslee is a recent graduate of the Rochester Institute of Technology’s computer engineering program. His areas of interest are deep learning and embedded systems.
Join us on Wednesday, 26 August 2020
10:00 - 10:45 EDT / 15:00 - 15:45 BST / 16:00 - 16:45 CET

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by