How to crop square of size 100 X 100 around the centroid of an image?

10 次查看(过去 30 天)
I have a binary image with centroid and I want to crop 100 X 100 image patch from the centroid of the image. I tried using imrect command and imcrop command. But it does not give automatic extraction of that patch instead I need to drag and place the square. Please help with MATLAB code for this.
  3 个评论
Manjiree Waikar
Manjiree Waikar 2017-9-9
编辑:Manjiree Waikar 2017-9-9
if true
% code
h = imrect(gca, [100 100 100 100]); %creates draggable rectangle on the object specified by gca
position = wait(h); %blocks the MATLAB command line and waits until the timer stops running.
position = getPosition(h);
croppedImage = imcrop(binaryImage, position);
figure;
subplot(1,2,1)
imshow(binaryImage)
title('Region of Interest');
end
I don't know how to extract a patch with reference to centroid.
Manjiree Waikar
Manjiree Waikar 2017-9-9
Sir I made some changes in the code but I am not getting desirable position. I have attached the input image for the above and my code too.
if true
% code
clear all; %*bold*
close all;
clc;
fontSize = 13;
rgbImage = imread(imgetfile);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
grayImage = rgb2gray(rgbImage);
% Display the image.
subplot(2, 2, 2);
imshow(grayImage);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
media = round(mean(mean(grayImage)));
% img_bin = imbinarize(grayImage,media);
% afterOpening = imopen(img_bin,se);
b = grayImage<media;
b = imfill(b, 'holes');
% Label the image
[rows, columns, numberOfColorBands] = size(grayImage);
circleCenterX = 120;
circleCenterY = 120; % square area 0f 500*500
circleRadius = 110; % big circle radius
circleImage = false(rows, columns);
[x, y] = meshgrid(1:columns, 1:rows);
circleImage((x - circleCenterX).^2 + (y - circleCenterY).^2 <= circleRadius.^2) = true;
b = and(circleImage,b);
labeledImage = bwlabel(b);
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
matrix = zeros(4,length(measurements));
vector = zeros(1,length(measurements));
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
matrix(:,k) = thisBB(:);
vector(k) = thisBB(2);
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','g','LineWidth',2 )
end
vector = sort(vector);
% Let's extract the biggest blob - that will be the hand.
allAreas = [measurements.Area];
[sortedAreas, sortingIndexes] = sort(allAreas, 'descend');
handIndex1 = sortingIndexes(1);
% Use ismember() to extract the hand from the labeled image.
handImage1 = ismember(labeledImage, handIndex1);
% Now binarize
handImage1 = handImage1 > 0;
se = strel('disk',5);
handImage1 = imerode(handImage1,se);
% Display the binary image.
subplot(2, 2, 3);
imshow(handImage1);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
subplot(2,2,4);
imshow(handImage1);
title('binary image with border');
boundaries=bwboundaries(handImage1);
x = boundaries{1}(:, 2);
y = boundaries{1}(:, 1);
hold on;
plot(x, y, 'black', 'LineWidth', 2);
newImage = bwlabel(handImage1);
measurements = regionprops(newImage, 'Centroid', 'BoundingBox');
xCentroid = measurements.Centroid(1);
yCentroid = measurements.Centroid(2);
figure;
imshow(newImage);
title('Binary Image with Centroid Marked');
hold on;
plot(xCentroid, yCentroid, 'r*', 'MarkerSize', 10, 'LineWidth', 2);
h = imrect(gca, [100 100 100 100]); %creates draggable rectangle on the object specified by gca
% position = wait(h); %blocks the MATLAB command line and waits until the timer stops running.
% position = getPosition(h);
croppedImage = imcrop(grayImage, [xCentroid-50 yCentroid-50 xCentroid yCentroid]);
figure;
imshow(croppedImage);
end

请先登录,再进行评论。

采纳的回答

Jose Marques
Jose Marques 2017-9-9
Change x_cent and y_cent for your centroids; change the name of images variables
x_cent = 120;
y_cent = 150;
size_of_cropped_img = 100;
centroide = [x_cent y_cent];
I = imread('circuit.tif');
imshow(I);
%I2 = imcrop(I,rect) crops the image I. rect is a four-element position vector of the
%form [xmin ymin width height] that specifies the size and position of the crop rectangle.
%imcrop returns the cropped image, I2.
xmin = x_cent-size_of_cropped_img/2;
ymin = y_cent-size_of_cropped_img/2;
I2 = imcrop(I,[xmin ymin size_of_cropped_img size_of_cropped_img]);
figure();
imshow(I2)
  6 个评论

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by