how to detect center of an object in an image and then crop the original image? using original image and green outline region
Hello.
I need to detect an object (suspicious area in an image). Then I need to find its centroid and then crop it the image into 256×256 pixels using image centroid as the centre of bounding box.
img = imread(‘1_245_original.jpg’);
% colorspace
jmg = rgb2gray(img);
jm = mat2gray(jmg(:,:,1));
jm = imcomplement(jm);
% thresh
bw = imbinarize(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’, 20);
end
I have tried out this code, but the cropping area was not in 256×256 pixels. and I want it to save at folder after detect the bounding box. Where can I put that code? Thanks.Hello.
I need to detect an object (suspicious area in an image). Then I need to find its centroid and then crop it the image into 256×256 pixels using image centroid as the centre of bounding box.
img = imread(‘1_245_original.jpg’);
% colorspace
jmg = rgb2gray(img);
jm = mat2gray(jmg(:,:,1));
jm = imcomplement(jm);
% thresh
bw = imbinarize(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’, 20);
end
I have tried out this code, but the cropping area was not in 256×256 pixels. and I want it to save at folder after detect the bounding box. Where can I put that code? Thanks. Hello.
I need to detect an object (suspicious area in an image). Then I need to find its centroid and then crop it the image into 256×256 pixels using image centroid as the centre of bounding box.
img = imread(‘1_245_original.jpg’);
% colorspace
jmg = rgb2gray(img);
jm = mat2gray(jmg(:,:,1));
jm = imcomplement(jm);
% thresh
bw = imbinarize(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’, 20);
end
I have tried out this code, but the cropping area was not in 256×256 pixels. and I want it to save at folder after detect the bounding box. Where can I put that code? Thanks. centroid, crop, automatic, duplicate post MATLAB Answers — New Questions