Filter löschen
Filter löschen

Identifying an object and its centroid in an image and then cropping the original image based on this centroid

6 Ansichten (letzte 30 Tage)
Hello,
I need to detect a particular objects out of many objects in an image. Then I need to find its centroid and then crop the image around that particular tracked object using its centroid as the centre of that rectangular crop.
For example, let's say I need to idenfity the marked watermelon piece (as in the attached picture) and then crop the image around this piece of watermelon.
Any help in this regard will be greatly appreciated. Thank you very much!

Akzeptierte Antwort

yanqi liu
yanqi liu am 30 Sep. 2021
sir,please check the follow code to get some information
clc; clear all; close all;
img = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/753699/gas-x-summer-fruits-cause-bloating-main.jpg');
% colorspace
jmg = rgb2ycbcr(img);
jm = mat2gray(jmg(:,:,2));
jm = imcomplement(jm);
% thresh
bw = im2bw(jm, graythresh(jm));
% filter noise
bw = imopen(bw, strel('disk', 5));
bw = imfill(bw, 'holes');
% label every target
[L,num] = bwlabel(bw);
stats = regionprops(L);
figure; imshow(img, []);
for i = 1 : num
% get rect
recti = stats(i).BoundingBox;
% get cen
ceni = stats(i).Centroid;
% crop image
imi = imcrop(img, round(recti));
ims{i} = imi;
% rect and cen
hold on; rectangle('Position', recti, 'EdgeColor', 'c', 'LineWidth', 2);
plot(ceni(1), ceni(2), 'yp', 'MarkerFaceColor', 'y', 'MarkerSize', 16);
end
% make grid
sz = round(sqrt(length(ims)));
if sz*sz < length(ims)
sz = [sz+1 sz];
else
sz = [sz sz];
end
figure;
montage(ims, 'Size', sz, 'BackgroundColor', 'w', 'BorderSize', [3 3])
  4 Kommentare
Sam
Sam am 7 Okt. 2021
Hi,
Thanks for your comment!
I am working on vehicle detection by extracting frames from overhead drone videos. Here, I am trying to track a particular vehicle and crop the bigger image around that tracked vehicle.
For example, attached image 1 is the full size extracted image from the video, and I want to identifiy all the vehicles in this image (if possible). Then dectecting the white car (highlighted in image 2) and cropping the image around it.
It will be very helpful if you can give my some advice in this regard.
Dayangku Nur Faizah Pengiran Mohamad
Hello Sam, I have tried your code to detect mine. But the codes could not find any object in my image. Could you please help me? Thanks in advance.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by